Ejemplo n.º 1
0
    def setUp(self):
        super(TestNeptuneAuth, self).setUp()

        self.session = MagicMock()
        self.neptune_auth = NeptuneAuth(self.session)
        self.request = a_request()

        self.url, self.method, self.body, self.headers = \
            self.request.url, self.request.method, self.request.body, self.request.headers

        self.updated_url, self.updated_headers, self.updated_body = \
            a_request().url, a_request().headers, a_request().body
Ejemplo n.º 2
0
    def setUp(self):
        super(TestNeptuneAuth, self).setUp()

        self.session = MagicMock()
        self.session.token = dict()
        self.neptune_auth = NeptuneAuth(lambda: self.session)
        self.neptune_auth.token_expires_at = time.time() + 60
        self.request = a_request()

        self.url, self.method, self.body, self.headers = \
            self.request.url, self.request.method, self.request.body, self.request.headers

        self.updated_url, self.updated_headers, self.updated_body = \
            a_request().url, a_request().headers, a_request().body
Ejemplo n.º 3
0
    def test_apply_oauth2_session_to_request(self, time_mock, session_mock):
        # given
        api_token = MagicMock()
        backend_client = MagicMock()

        auth_tokens = MagicMock()
        auth_tokens.accessToken = an_access_token()
        auth_tokens.refreshToken = a_refresh_token()
        decoded_access_token = jwt.decode(auth_tokens.accessToken,
                                          SECRET,
                                          options=_decoding_options)

        backend_client.api.exchangeApiToken(
            X_Neptune_Api_Token=api_token).response().result = auth_tokens

        # and
        now = time.time()
        time_mock.time.return_value = now

        # and
        session = MagicMock()
        session_mock.return_value = session
        session.token = dict()

        # and
        neptune_authenticator = NeptuneAuthenticator(api_token, backend_client,
                                                     False, None)
        request = a_request()

        # when
        updated_request = neptune_authenticator.apply(request)

        # then
        expected_token = {
            "access_token": auth_tokens.accessToken,
            "refresh_token": auth_tokens.refreshToken,
            "expires_in": decoded_access_token["exp"] - now,
        }

        expected_auto_refresh_url = "{realm_url}/protocol/openid-connect/token".format(
            realm_url=decoded_access_token["iss"])

        session_mock.assert_called_once_with(
            client_id=decoded_access_token["azp"],
            token=expected_token,
            auto_refresh_url=expected_auto_refresh_url,
            auto_refresh_kwargs={"client_id": decoded_access_token["azp"]},
            token_updater=_no_token_updater,
        )

        # and
        self.assertEqual(session, updated_request.auth.session)
Ejemplo n.º 4
0
    def test_apply_oauth2_session_to_request(self, time_mock, session_mock):
        # given
        auth_tokens = MagicMock()
        auth_tokens.accessToken = an_access_token()
        auth_tokens.refreshToken = a_refresh_token()
        decoded_access_token = jwt.decode(auth_tokens.accessToken, SECRET)

        # and
        now = time.time()
        time_mock.time.return_value = now

        # and
        session = MagicMock()
        session_mock.return_value = session
        session.token = dict()

        # and
        neptune_authenticator = NeptuneAuthenticator(auth_tokens, False, None)
        request = a_request()

        # when
        updated_request = neptune_authenticator.apply(request)

        # then
        expected_token = {
            'access_token': auth_tokens.accessToken,
            'refresh_token': auth_tokens.refreshToken,
            'expires_in': decoded_access_token['exp'] - now
        }

        expected_auto_refresh_url = '{realm_url}/protocol/openid-connect/token'.format(
            realm_url=decoded_access_token['iss'])

        session_mock.assert_called_once_with(
            client_id=decoded_access_token['azp'],
            token=expected_token,
            auto_refresh_url=expected_auto_refresh_url,
            auto_refresh_kwargs={'client_id': decoded_access_token['azp']},
            token_updater=_no_token_updater)

        # and
        self.assertEqual(session, updated_request.auth.session)