"""Tests for login dialog."""
import json
from unittest import mock, TestCase
from anaconda_navigator.api.team_edition_api import TeamEditionAPI

TEST_CHANNELS = {
    'total_count': 1,
    'items': [
        {'id': '1df63-4c1-44-af0f-027308381',
         'name': 'testing_channel',
         'description': '',
         'privacy': 'public',
         'artifact_count': 2,
         'download_count': 1,
         'subchannel_count': 0,
         'group_count': 1,
         'mirror_count': 0,
         'filters': {},
         'owners': ['test_user'],
         'created': '2021-02-17T16:05:34.521373+00:00',
         'updated': None}
    ]
}


class TestTeamEditionApi(TestCase):

    def setUp(self):
        self.token = {
            'token': 'iOiJSUzI1NiIsInR5cCIgOiAiSldUIiIiA6ICI2eTRpaVhkWXl3eC0wQUlWeVAyTlA5OWd6bWhtRUVOVmZ2M2',
            'refresh_token': 'eyiIsInR5cIiwia2lkIiA6ICJhOTMxNTYxYi05NDY4LTRhNTAtOTBlZS1iNDAxY',
            'jwt_token': 'eOiJSUzI1NiIsInR5cCIgOiAiSldUIiA6ICI2eTRpaVhkWXl3eC0wQUlWeVAyTlA5OWd6bWhtRUVOV',
            'jwt_token_refresh': 'hbGciOJIUzI1NiIsInR5cCIlkIiA6ICJhOTMxNTYxYi05NDY4LTRhNTAtOTBlZS1iN'
        }
        ate_api_mock = TeamEditionAPI('https://ate.testing.base.mock', mock.Mock())
        ate_api_mock.load_token = mock.Mock(return_value=json.dumps(self.token))
        self.ate_api = ate_api_mock

    @mock.patch('anaconda_navigator.api.team_edition_api.requests')
    def test_get_channels(self, mock_requests):
        mock_response = mock.Mock()
        mock_response.json.return_value = TEST_CHANNELS
        mock_requests.get.return_value = mock_response

        with self.subTest('Bearer token test'):
            self.assertEqual(self.ate_api.get_channels(), TEST_CHANNELS['items'])
            headers = {'Authorization': "Bearer {}".format(self.token['token'])}
            mock_requests.get.called_with(self.ate_api._urls['channels'], headers=headers)

        with self.subTest('Bearer token test'):
            self.assertEqual(self.ate_api.get_channels(), TEST_CHANNELS['items'])
            headers = {'X-Auth': None}
            mock_requests.get.called_with(self.ate_api._urls['channels'], headers=headers)
