コード例 #1
0
ファイル: test_auth.py プロジェクト: vutuong/tacker
    def test_schedule_refrash_token_expaire(self):
        self.requests_mock.register_uri(
            'GET',
            self.url,
            headers={'Content-Type': 'application/json'},
            json={
                'access_token': 'test_token',
                'token_type': 'bearer'
            },
            status_code=200)

        grant = auth._ClientCredentialsGrant(client_id=self.user_name,
                                             client_password=self.password,
                                             token_endpoint=self.url)

        with mock.patch("threading.Timer", side_effect=self.MockThread) as m:
            client = auth._OAuth2Session(grant)
            client._OAuth2Session__access_token_info.update({
                'access_token': 'test_token',
                'token_type': 'bearer',
                'expires_in': '1'
            })
            client.schedule_refrash_token()

            history = self.requests_mock.request_history
            self.assertEqual(1, len(history))
            self.assertEqual(1, m.call_count)
コード例 #2
0
ファイル: test_auth.py プロジェクト: vutuong/tacker
    def test_request_client_credentials_auth_error(self):
        self.requests_mock.register_uri(
            'GET',
            self.url,
            json={
                'access_token': 'test_token3',
                'token_type': 'bearer'
            },
            headers={'Content-Type': 'application/json'},
            status_code=200)

        self.requests_mock.register_uri('GET',
                                        "https://nfvo.co.jp",
                                        text="error.",
                                        status_code=401)

        grant = auth._ClientCredentialsGrant(client_id=self.user_name,
                                             client_password=self.password,
                                             token_endpoint=self.url)
        client = auth._OAuth2Session(grant)
        client.apply_access_token_info()

        response = client.get('https://nfvo.co.jp')

        self.assertEqual(401, response.status_code)
        history = self.requests_mock.request_history
        self.assertEqual(3, len(history))
コード例 #3
0
ファイル: test_auth.py プロジェクト: vutuong/tacker
    def test_schedule_refrash_token_non_expaire(self):
        grant = auth._ClientCredentialsGrant(client_id=self.user_name,
                                             client_password=self.password,
                                             token_endpoint=self.url)

        with mock.patch("threading.Timer", side_effect=self.MockThread) as m:
            client = auth._OAuth2Session(grant)
            client._OAuth2Session__access_token_info.update({
                'access_token':
                'test_token',
                'token_type':
                'bearer'
            })
            client.schedule_refrash_token()

            history = self.requests_mock.request_history
            self.assertEqual(0, len(history))
            self.assertEqual(0, m.call_count)
コード例 #4
0
ファイル: test_auth.py プロジェクト: vutuong/tacker
    def test_apply_access_token_info_fail_timeout(self):
        self.requests_mock.register_uri('GET',
                                        self.url,
                                        exc=requests.exceptions.ConnectTimeout)

        grant = auth._ClientCredentialsGrant(client_id=self.user_name,
                                             client_password=self.password,
                                             token_endpoint=self.url)

        with mock.patch("threading.Timer", side_effect=self.MockThread) as m:
            try:
                client = auth._OAuth2Session(grant)
                client.apply_access_token_info()
            except requests.exceptions.RequestException as e:
                self.assertIsNone(e.response)

            history = self.requests_mock.request_history
            self.assertEqual(1, len(history))
            self.assertEqual(0, m.call_count)
コード例 #5
0
ファイル: test_auth.py プロジェクト: vutuong/tacker
    def test_request_client_credentials(self, http_method):
        self.requests_mock.register_uri(
            'GET',
            self.url,
            json={
                'access_token': 'test_token3',
                'token_type': 'bearer'
            },
            headers={'Content-Type': 'application/json'},
            status_code=200)

        grant = auth._ClientCredentialsGrant(client_id=self.user_name,
                                             client_password=self.password,
                                             token_endpoint=self.url)
        client = auth._OAuth2Session(grant)
        client.apply_access_token_info()

        self.requests_mock.register_uri(
            http_method,
            'https://nfvo.co.jp',
            headers={'Content-Type': 'application/json'},
            status_code=200)

        if http_method == 'GET':
            response = client.get('https://nfvo.co.jp',
                                  params={'sample_key': 'sample_value'})
        elif http_method == 'PUT':
            response = client.put('https://nfvo.co.jp',
                                  data={'sample_key': 'sample_value'})
        elif http_method == 'POST':
            response = client.post('https://nfvo.co.jp',
                                   data={'sample_key': 'sample_value'})
        elif http_method == 'DELETE':
            response = client.delete('https://nfvo.co.jp',
                                     params={'sample_key': 'sample_value'})
        elif http_method == 'PATCH':
            response = client.patch('https://nfvo.co.jp',
                                    data={'sample_key': 'sample_value'})

        self.assertEqual(200, response.status_code)
        history = self.requests_mock.request_history
        self.assertEqual(2, len(history))
コード例 #6
0
ファイル: test_auth.py プロジェクト: vutuong/tacker
    def test_apply_access_token_info_fail_error_response(self):
        error_description = """
        Either your username or password is incorrect
        or you are not an active user.
        Please try again or contact your administrator.
        """
        self.requests_mock.register_uri('GET',
                                        self.url,
                                        headers={
                                            'Content-Type':
                                            'application/json;charset=UTF-8',
                                            'Cache-Control':
                                            'no-store',
                                            'Pragma':
                                            'no-store',
                                            'WWW-Authenticate':
                                            'Basic realm="example"'
                                        },
                                        json={
                                            'error': 'invalid_client',
                                            'error_description':
                                            error_description
                                        },
                                        status_code=401)

        grant = auth._ClientCredentialsGrant(client_id=self.user_name,
                                             client_password=self.password,
                                             token_endpoint=self.url)

        with mock.patch("threading.Timer", side_effect=self.MockThread) as m:
            try:
                client = auth._OAuth2Session(grant)
                client.apply_access_token_info()
            except requests.exceptions.RequestException as e:
                self.assertEqual(401, e.response.status_code)

            history = self.requests_mock.request_history
            self.assertEqual(1, len(history))
            self.assertEqual(0, m.call_count)
コード例 #7
0
ファイル: test_auth.py プロジェクト: vutuong/tacker
    def test_apply_access_token_info(self):
        res_mock = {
            'json': {
                'access_token': 'test_token',
                'token_type': 'bearer',
                'expires_in': '1'
            },
            'headers': {
                'Content-Type': 'application/json'
            },
            'status_code': 200
        }
        res_mock2 = {
            'json': {
                'access_token': 'test_token2',
                'token_type': 'bearer'
            },
            'headers': {
                'Content-Type': 'application/json'
            },
            'status_code': 200
        }

        self.requests_mock.register_uri('GET', self.url, [res_mock, res_mock2])

        grant = auth._ClientCredentialsGrant(client_id=self.user_name,
                                             client_password=self.password,
                                             token_endpoint=self.url)

        with mock.patch("threading.Timer", side_effect=self.MockThread) as m:
            client = auth._OAuth2Session(grant)
            client.apply_access_token_info()

            history = self.requests_mock.request_history
            self.assertEqual(2, len(history))
            self.assertEqual(1, m.call_count)