예제 #1
0
    def test_init(self):
        """
        SimpleAuthClient.__init__

        :return:
        """

        with self.assertRaises(TypeError):
            SimpleAuthClient()

        with self.assertRaises(TypeError):
            SimpleAuthClient(url_server_auth='http://localhost', mock_param=1)

        client = SimpleAuthClient(url_server_auth='http://localhost')
예제 #2
0
    def test_get_auth_url(self):
        """
        SimpleAuthClient.

        :return:
        """
        client = SimpleAuthClient(url_server_auth='http://localhost')
        identifier = 'mock_identifier'
        current_url = 'http://fake.url'
        url = client.get_auth_url(identifier=identifier,
                                  current_url=current_url)
        self.assertEqual(
            url,
            'http://localhost?identifier=mock_identifier'
            '&redirect_url=http%3A%2F%2Ffake.url')
예제 #3
0
    def test_get_identifier(self, mock_requests, mock_json):
        """
        SimpleAuthClient.get_identifier

        :return:
        """

        # init
        mock_response = {'result': {'identifier': 'mock_uud4'}, 'error': False,
                         'msg': ''}

        mock_json.loads = mock.MagicMock(return_value=mock_response)

        # action
        client = SimpleAuthClient(url_server_auth='http://localhost')
        identifier = client.get_identifier()

        # post-check
        self.assertEqual(identifier, mock_response)
예제 #4
0
    def test_expired_session(self, mock_uuid, mock_time, mock_requests):
        server = SimpleAuthServer()
        client = SimpleAuthClient(url_server_auth='http://localhost')

        requests_redirect = render_requests_redirect(server)

        mock_requests.post = requests_redirect
        mock_uuid.uuid4 = mock.MagicMock(
            side_effect=['mock_uud4_0', 'mock_uud4_1'])
        mock_time.time = lambda: 100
        identifier = 'mock_uud4_1'

        server.session_storage.clear()
        server.session_storage[identifier] = FAKE_RESPONSE_GET_TOKEN['result']
        response = client.get_token(identifier=identifier)
        self.assertEqual(response, FAKE_RESPONSE_GET_TOKEN)

        token = response['result']['token']

        # we have enough time

        mock_uuid.uuid4 = mock.MagicMock(
            side_effect=['mock_uud4_2', 'mock_uud4_3'])
        server.session_storage[identifier] = FAKE_RESPONSE_GET_TOKEN['result']

        response = client.update_token(token=token)
        self.assertEqual(response, FAKE_RESPONSE_GET_TOKEN2)

        # we have no time
        mock_time.time = lambda: 161

        server.session_storage.clear()
        mock_uuid.uuid4 = mock.MagicMock(
            side_effect=['mock_uud4_2', 'mock_uud4_3'])
        server.session_storage[identifier] = FAKE_RESPONSE_GET_TOKEN['result']

        response = client.update_token(token=token)
        self.assertEqual(response, {
            'error': True,
            'msg': 'This identifier has expired',
            'result': None
        })
예제 #5
0
    def test_get_token(self, mock_requests):
        """
        SimpleAuthClient.get_token

        :return:
        """

        # success
        mock_response = FAKE_RESPONSE_GET_TOKEN

        class FakeResponse:
            text = json.dumps(mock_response)

        mock_requests.post = mock.MagicMock(
            return_value=FakeResponse())

        client = SimpleAuthClient(url_server_auth='http://localhost')

        identifier = 'mock_identifier'

        response = client.get_token(identifier=identifier)

        # post-check
        self.assertEqual(response, mock_response)
예제 #6
0
    def test_is_valid_token(self, mock_time):
        """
        SimpleAuthClient.is_valid_token

        :return:
        """

        client = SimpleAuthClient(url_server_auth='http://localhost')

        token = {
            'access_token': 'mock_uud4_0',
            'expired_access_token': 130,
            'expired_update_token': 160,
            'update_token': 'mock_uud4_1'
        }

        mock_time.time = lambda: 100
        self.assertEqual(
            client.is_valid_token(token=token),
            True
        )

        mock_time.time = lambda: 130
        self.assertEqual(
            client.is_valid_token(token=token),
            False
        )

        mock_time.time = lambda: 131
        self.assertEqual(
            client.is_valid_token(token=token),
            False
        )

        self.assertEqual(
            client.is_valid_token(token=None),
            False
        )

        self.assertEqual(
            client.is_valid_token(token={}),
            False
        )
예제 #7
0
    def test_is_valid_identifier(self, mock_requests):
        """
        SimpleAuthClient.is_valid_identifier

        :return:
        """

        # mocks
        mock_response = {'error': False, 'msg': '',
                         'result': {'identifier': 'fake_identifier'}}

        class FakeResponse:
            def __init__(self, data):
                self.text = json.dumps(data)

        mock_requests.post = mock.MagicMock(
            return_value=FakeResponse(mock_response))

        # init
        client = SimpleAuthClient(url_server_auth='http://localhost')
        result = client.is_valid_identifier(identifier='fake_identifier')

        # post-check
        self.assertEqual(result, True)

        # Error

        # mocks
        mock_response = {'error': True, 'msg': '', 'result': None}
        mock_requests.post = mock.MagicMock(
            return_value=FakeResponse(mock_response))

        # init
        client = SimpleAuthClient(url_server_auth='http://localhost')
        result = client.is_valid_identifier(identifier='fake_identifier')

        # check
        self.assertEqual(result, False)
예제 #8
0
    def test_multi_client(self, mock_uuid, mock_time, mock_requests):
        server = SimpleAuthServer()
        client1 = SimpleAuthClient(url_server_auth='http://localhost')
        client2 = SimpleAuthClient(url_server_auth='http://localhost')

        mock_requests.post = render_requests_redirect(server)
        mock_uuid.uuid4 = mock.MagicMock(
            side_effect=['mock_uud4_%s' % i for i in range(1000)])

        # mocks
        mock_time.time = lambda: 100

        # get identifiers from server
        identifier1 = client1.get_identifier()['result']['identifier']
        self.assertEqual('mock_uud4_0', identifier1, "Wrong identifier")

        identifier2 = client2.get_identifier()['result']['identifier']
        self.assertEqual('mock_uud4_2', identifier2, "Wrong identifier")

        expected_session_storage = {
            'mock_uud4_0': {
                'timestamp': 100,
                'timestamp_expired': 145,
                'main_token': 'mock_uud4_1',
                'action': 'identifier'
            },
            'mock_uud4_2': {
                'timestamp': 100,
                'timestamp_expired': 145,
                'main_token': 'mock_uud4_3',
                'action': 'identifier'
            }
        }

        self.assertEqual(expected_session_storage, server.session_storage,
                         "Session storage has got wrong data")

        # authenticate the user on the server
        # set up user to identifier1
        server.add_user_data(identifier1, 1)
        expected_session_storage[identifier1]['user'] = {
            'id': 1,
            'name': 'User name',
            'level': 5,
            'access': [1, 2, 3]
        }

        self.assertEqual(expected_session_storage, server.session_storage,
                         "Session storage has got wrong data")

        response = server.get_token(identifier1)['result']

        expected_response = {
            'timestamp': 100,
            'timestamp_expired': 130,
            'user': {
                'id': 1,
                'name': 'User name',
                'level': 5,
                'access': [1, 2, 3]
            },
            'token': {
                'access_token': 'mock_uud4_4',
                'update_token': 'mock_uud4_5',
                'expired_access_token': 130,
                'expired_update_token': 160
            }
        }

        self.assertEqual(
            expected_response, response,
            "The request server.get_token has returned a wrong response")

        key1 = 'mock_uud4_1'
        # authenticate the user on the server
        # set up user to identifier1
        response = server.merge_main_tokens(key1=key1, key2=identifier2)

        self.assertEqual(
            {
                'error': False,
                'msg': '',
                'result': None
            }, response,
            "The request server.get_token has returned a wrong response")

        expected_session_storage = {
            'mock_uud4_2': {
                'timestamp': 100,
                'timestamp_expired': 145,
                'main_token': 'mock_uud4_1',
                'action': 'identifier'
            },
            'mock_uud4_4': {
                'timestamp': 100,
                'timestamp_expired': 130,
                'main_token': 'mock_uud4_1',
                'action': 'access',
                'user': {
                    'id': 1,
                    'name': 'User name',
                    'level': 5,
                    'access': [1, 2, 3]
                },
                'token': {
                    'access_token': 'mock_uud4_4',
                    'update_token': 'mock_uud4_5',
                    'expired_access_token': 130,
                    'expired_update_token': 160
                }
            },
            'mock_uud4_5': {
                'timestamp': 100,
                'timestamp_expired': 160,
                'main_token': 'mock_uud4_1',
                'action': 'update',
                'user': {
                    'id': 1,
                    'name': 'User name',
                    'level': 5,
                    'access': [1, 2, 3]
                },
                'token': {
                    'access_token': 'mock_uud4_4',
                    'update_token': 'mock_uud4_5',
                    'expired_access_token': 130,
                    'expired_update_token': 160
                }
            },
            'mock_uud4_1': {
                'timestamp': 100,
                'main_token': 'mock_uud4_1',
                'timestamp_expired': 160,
                'action': 'main'
            }
        }

        self.assertEqual(expected_session_storage, server.session_storage,
                         "Session storage has got wrong data")

        return
예제 #9
0
    def test_workflow1(self, mock_uuid, mock_time, mock_requests):
        # init
        server = SimpleAuthServer()
        client = SimpleAuthClient(url_server_auth='http://localhost')

        # mocks
        requests_redirect = render_requests_redirect(server)
        mock_requests.post = requests_redirect
        mock_uuid.uuid4 = lambda: 'mock_uud4'
        mock_time.time = lambda: 100

        # get identifier
        response = client.get_identifier()

        self.assertEqual(response, {
            'result': {
                'identifier': 'mock_uud4'
            },
            'error': False,
            'msg': ''
        })

        identifier = response['result']['identifier']

        # get token
        response = client.get_token(identifier=identifier)

        self.assertEqual(response, {
            'result': None,
            'error': True,
            'msg': 'Have no information about user'
        })

        # added user
        mock_uuid.uuid4 = mock.MagicMock(
            side_effect=['mock_uud4_0', 'mock_uud4_1'])

        response = server.add_user_data(identifier=identifier, user_id=1)

        self.assertEqual(response, {'error': False, 'msg': '', 'result': None})

        # get token
        response = client.get_token(identifier=identifier)

        self.assertEqual(response, FAKE_RESPONSE_GET_TOKEN)

        # check that identifier does not exist
        response = client.get_token(identifier=identifier)
        self.assertEqual(response, {
            'result': None,
            'error': True,
            'msg': 'The identifier is wrong'
        })

        mock_uuid.uuid4 = mock.MagicMock(
            side_effect=['mock_uud4_0', 'mock_uud4_1'])
        response = client.get_token(identifier='mock_uud4_0')
        self.assertEqual(response, FAKE_RESPONSE_GET_TOKEN)
        token = response['result']['token']

        # update token
        self.assertEqual(server.session_storage[token['access_token']],
                         FAKE_RESPONSE_GET_TOKEN['result'])

        mock_uuid.uuid4 = mock.MagicMock(
            side_effect=['mock_uud4_2', 'mock_uud4_3'])
        response = client.update_token(token=token)
        self.assertEqual(response, FAKE_RESPONSE_GET_TOKEN2)