def test_get_azure_credentials(self): """Test to get Azure credentials from authentication service.""" resource_id = 2 authentication_id = 3 authentication = str(uuid4()) username = str(uuid4()) tenent_id = str(uuid4()) subscription_id = str(uuid4()) applications_reponse = { "id": resource_id, "source_id": self.source_id, "extra": {"resource_group": "RG1", "storage_account": "mysa1", "subscription_id": subscription_id}, } authentications_response = { "id": authentication_id, "source_id": self.source_id, "authtype": "tenant_id_client_id_client_secret", "username": username, "extra": {"azure": {"tenant_id": tenent_id}}, } client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER, source_id=self.source_id) with requests_mock.mock() as m: m.get( f"{MOCK_URL}/api/v1.0/{ENDPOINT_APPLICATIONS}?source_id={self.source_id}", status_code=200, json={"data": [applications_reponse]}, ) m.get( f"{MOCK_URL}/api/v1.0/{ENDPOINT_AUTHENTICATIONS}?source_id={self.source_id}", status_code=200, json={"data": [authentications_response]}, ) m.get( ( f"{MOCK_URL}/internal/v1.0/{ENDPOINT_AUTHENTICATIONS}/" f"{authentication_id}?expose_encrypted_attribute[]=password" ), status_code=200, json={"password": authentication}, ) response = client._get_azure_credentials() self.assertDictEqual( response, { "client_id": username, "client_secret": authentication, "subscription_id": subscription_id, "tenant_id": tenent_id, }, )
def test_get_azure_credentials_errors(self): """Test to get Azure credentials errors.""" resource_id = 2 authentication_id = 3 authentication = str(uuid4()) username = str(uuid4()) tenent_id = str(uuid4()) subscription_id = str(uuid4()) applications_reponse_table = [ {"valid": False, "json": None}, {"valid": False, "json": {}}, { "valid": False, "json": { # missing sub_id "id": resource_id, "source_id": self.source_id, "extra": {"resource_group": "RG1", "storage_account": "mysa1"}, }, }, { "valid": True, "json": { # valid response "id": resource_id, "source_id": self.source_id, "extra": {"resource_group": "RG1", "storage_account": "mysa1", "subscription_id": subscription_id}, }, }, ] authentications_response_table = [ {"valid": False, "json": None}, {"valid": False, "json": {}}, { "valid": False, "json": { # missing username "id": authentication_id, "source_id": self.source_id, "authtype": "tenant_id_client_id_client_secret", "extra": {"azure": {"tenant_id": tenent_id}}, }, }, { "valid": False, "json": { # missing extra "id": authentication_id, "source_id": self.source_id, "authtype": "tenant_id_client_id_client_secret", "username": username, }, }, { "valid": True, "json": { # valid response "id": authentication_id, "source_id": self.source_id, "authtype": "tenant_id_client_id_client_secret", "username": username, "extra": {"azure": {"tenant_id": tenent_id}}, }, }, ] internal_response_table = [ { "valid": False, "json": {} }, # missing password { "valid": True, "json": { "password": authentication } }, ] auth_type = AUTH_TYPES.get(Provider.PROVIDER_AZURE) client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER, source_id=self.source_id) for app, auth, internal in product(applications_reponse_table, authentications_response_table, internal_response_table): with self.subTest(test=(app, auth, internal)): with requests_mock.mock() as m: m.get( f"{MOCK_URL}/api/v1.0/{ENDPOINT_APPLICATIONS}?" f"filter[source_id]={self.source_id}&filter[application_type_id]={COST_MGMT_APP_TYPE_ID}", status_code=200, json={"data": [app.get("json")]}, ) m.get( f"{MOCK_URL}/api/v1.0/{ENDPOINT_AUTHENTICATIONS}?" f"filter[source_id]={self.source_id}&filter[authtype]={auth_type}", status_code=200, json={"data": [auth.get("json")]}, ) m.get( (f"{MOCK_URL}/internal/v1.0/{ENDPOINT_AUTHENTICATIONS}/" f"{authentication_id}?expose_encrypted_attribute[]=password" ), status_code=200, json=internal.get("json"), ) if all([ app.get("valid"), auth.get("valid"), internal.get("valid") ]): self.assertIsNotNone( client._get_azure_credentials( COST_MGMT_APP_TYPE_ID)) else: with self.assertRaises(SourcesHTTPClientError): client._get_azure_credentials( COST_MGMT_APP_TYPE_ID)