Exemplo n.º 1
0
    def test_request_method_refreshes_auth_token(self, m_post, m_request):
        m_post.side_effect = self._auth_endpoint
        m_request.side_effect = self._users_endpoint

        client = HubstaffClient(app_token=self.app_token,
                                auth_token='EXPIRED!',
                                username='******',
                                password='******')
        client._request('get',
                        '/users',
                        params={
                            'organization_memberships': False,
                            'project_memberships': False,
                            'offset': 0
                        })

        self.assertEqual(m_request.call_count, 2)
        # The first call uses expired token
        call_args, call_kwargs = m_request.call_args_list[0]
        self.assertEqual(call_args, (
            'get',
            'https://api.hubstaff.com/v1/users',
        ))
        self.assertDictEqual(
            call_kwargs, {
                'headers': {
                    'App-Token': self.app_token,
                    'Auth-Token': 'EXPIRED!',
                },
                'params': {
                    'organization_memberships': False,
                    'project_memberships': False,
                    'offset': 0
                },
                'data': None,
                'json': None,
            })
        # The second call uses refreshed token
        call_args, call_kwargs = m_request.call_args_list[1]
        self.assertEqual(call_args, (
            'get',
            'https://api.hubstaff.com/v1/users',
        ))
        self.assertDictEqual(
            call_kwargs, {
                'headers': {
                    'App-Token': self.app_token,
                    'Auth-Token': self.auth_token,
                },
                'params': {
                    'organization_memberships': False,
                    'project_memberships': False,
                    'offset': 0
                },
                'data': None,
                'json': None,
            })
Exemplo n.º 2
0
    def test_request_method_doesnt_call_authenticate_method(
            self, m_authenticate, m_request):
        m_request.return_value.status_code = 200
        m_request.return_value.json.return_value = {'users': []}

        client = HubstaffClient(app_token=self.app_token,
                                auth_token=self.auth_token)

        client._request('get',
                        '/users',
                        params={
                            'organization_memberships': False,
                            'project_memberships': False,
                            'offset': 0
                        })

        m_authenticate.assert_not_called()
Exemplo n.º 3
0
    def test_request_method_raises_unauthorized_error(self, _, m_request):
        m_request.return_value.status_code = 401
        m_request.return_value.json.return_value = {
            'error': 'Permission denied'
        }

        client = HubstaffClient(
            app_token=self.app_token,
            auth_token=self.auth_token,  # valid but probably expired
            username='******',
            password='******')
        with self.assertRaises(HubstaffAuthError) as err_ctx:
            client._request('get',
                            '/users',
                            params={
                                'organization_memberships': False,
                                'project_memberships': False,
                                'offset': 0
                            })

        self.assertEqual(err_ctx.exception.message, 'Permission denied')
Exemplo n.º 4
0
    def test_request_method_raises_too_many_requests(self, m_request):
        m_request.return_value.status_code = 429
        m_request.return_value.json.return_value = {
            'error':
            'Rate limit has been reached. '
            'Please wait before making your next request.'
        }

        client = HubstaffClient(app_token=self.app_token,
                                auth_token=self.auth_token)
        with self.assertRaises(HubstaffError) as err_ctx:
            client._request('get',
                            '/users',
                            params={
                                'organization_memberships': False,
                                'project_memberships': False,
                                'offset': 0
                            })

        self.assertEqual(
            err_ctx.exception.message, 'Rate limit has been reached. '
            'Please wait before making your next request.')
Exemplo n.º 5
0
    def test_request_method_tries_to_to_refresh_the_token(
            self, m_authenticate, m_request):
        m_request.return_value.status_code = 401
        m_request.return_value.json.return_value = {
            'error': 'Permission denied'
        }

        client = HubstaffClient(
            app_token=self.app_token,
            auth_token=self.auth_token,  # valid but probably expired
            username='******',
            password='******')

        with self.assertRaises(HubstaffAuthError):
            client._request('get',
                            '/users',
                            params={
                                'organization_memberships': False,
                                'project_memberships': False,
                                'offset': 0
                            })

        m_authenticate.asert_called_once_with()
Exemplo n.º 6
0
    def test_request_method_calls_api_with_auth_token(self, m_post, m_request):
        m_post.side_effect = self._auth_endpoint
        m_request.return_value.status_code = 200
        m_request.return_value.json.return_value = {'users': []}

        client = HubstaffClient(app_token=self.app_token,
                                username='******',
                                password='******')
        client._request('get',
                        '/users',
                        params={
                            'organization_memberships': False,
                            'project_memberships': False,
                            'offset': 0
                        })

        self.assertEqual(m_request.call_count, 1)
        call_args, call_kwargs = m_request.call_args
        self.assertEqual(call_args, (
            'get',
            'https://api.hubstaff.com/v1/users',
        ))
        self.assertDictEqual(
            call_kwargs, {
                'headers': {
                    'App-Token': self.app_token,
                    'Auth-Token': self.auth_token,
                },
                'params': {
                    'organization_memberships': False,
                    'project_memberships': False,
                    'offset': 0
                },
                'data': None,
                'json': None,
            })
Exemplo n.º 7
0
    def test_request_method_returns_json_data(self, m_request):
        m_request.return_value.status_code = 200
        m_request.return_value.json.return_value = {'users': []}

        client = HubstaffClient(app_token=self.app_token,
                                auth_token=self.auth_token)

        result = client._request('get',
                                 '/users',
                                 params={
                                     'organization_memberships': False,
                                     'project_memberships': False,
                                     'offset': 0
                                 })

        self.assertDictEqual(result, {'users': []})