def test_factory_method_from_service_key_https_is_enforced(self): service_key = { "uaa": { "clientid": "a-client-id", "clientsecret": "a-client-secret", "url": "http://insecure", }, "url": "https://aiservices-dar.cfapps.xxx.hana.ondemand.com/", } with pytest.raises(HTTPSRequired): OnlineCredentialsSource.construct_from_service_key(service_key)
def online_credentials_source_with_mock_session(): mock_timer = create_autospec(time.monotonic) mock_timer.return_value = 100 mock_session = create_autospec(TimeoutRetrySession, instance=True) mock_response = Mock() mock_session.get.return_value = mock_response mock_response.json.side_effect = [ { "access_token": "the-token", "token_type": "bearer", "expires_in": 43199, "scope": "scope1 scope2 scope3", }, { "access_token": "the-token-2", "token_type": "bearer", "expires_in": 43199, "scope": "scope1 scope2 scope3", }, ] source = OnlineCredentialsSource( url="https://test.xyz", clientid="a-client-id", clientsecret="a-client-secret", session=mock_session, timer=mock_timer, ) return source
def test_constructor_enforces_https(self): with pytest.raises(HTTPSRequired): OnlineCredentialsSource( url="http://insecure", clientid="a-client-id", clientsecret="a-client-secret", )
def test_constructor_keyword_args_can_override_session(self): mock_session = create_autospec(TimeoutRetrySession, instance=True) source = OnlineCredentialsSource( url="https://URL", clientid="a-client-id", clientsecret="a-client-secret", session=mock_session, ) assert source.session == mock_session
def test_factory_method_from_service_key(self): # abbreviated service key. Irrelevant keys are # removed. service_key = { "uaa": { "clientid": "a-client-id", "clientsecret": "a-client-secret", "url": "https://URL", }, "url": "https://aiservices-dar.cfapps.xxx.hana.ondemand.com/", } source = OnlineCredentialsSource.construct_from_service_key( service_key) self._assert_properties(source)
def construct_from_credentials( cls: Type[DARClient], dar_url: str, clientid: str, clientsecret: str, uaa_url: str, ) -> DARClient: """ Constructs a DARClient from credentials. The credentials can be obtained from a service key. If a service key is available, see :meth:`construct_from_service_key`. :param dar_url: Service URL :param clientid: Client ID :param clientsecret: Client Secret :param uaa_url: Authentication URL :return: the client instance """ source = OnlineCredentialsSource(uaa_url, clientid, clientsecret) return cls(dar_url, source)
def test_constructor_positional_args_can_override_session(self): mock_session = create_autospec(TimeoutRetrySession, instance=True) source = OnlineCredentialsSource("https://URL", "a-client-id", "a-client-secret", mock_session) assert source.session == mock_session
def test_constructor_keyword_args(self): source = OnlineCredentialsSource(url="https://URL", clientid="a-client-id", clientsecret="a-client-secret") self._assert_properties(source)
def test_constructor_positional_args(self): source = OnlineCredentialsSource("https://URL", "a-client-id", "a-client-secret") self._assert_properties(source)
def test_constructor_has_no_defaults(self): with pytest.raises(TypeError): OnlineCredentialsSource()
def credentials_source(dar_client_id, dar_client_secret, dar_uaa_url): return OnlineCredentialsSource(dar_uaa_url, dar_client_id, dar_client_secret)