def test_get_accounts_from_source_unexpected_response(self): """Test to get all accounts with unexpected response.""" responses.add(responses.POST, 'http://{}:{}/api/v1/token-auth/'.format( self.koku_host, self.koku_port), json={'token': self.mock_token}, status=200) mock_authentication = "arn:aws:iam::999999999999:role/CostManagement" mock_billing_source = "cost-usage-bucket" mock_customer_name = "Test Customer" mock_provider_type = "AWS" mock_schema_name = "testcustomer" mock_response = { "count": 1, "next": 'null', "previous": 'null', "results": [{ "BAD_AUTHENTICATION": { "provider_resource_name": mock_authentication, "uuid": "7e4ec31b-7ced-4a17-9f7e-f77e9efa8fd6" }, "billing_source": { "bucket": mock_billing_source, "uuid": "75b17096-319a-45ec-92c1-18dbd5e78f94" }, "created_by": { "email": "*****@*****.**", "username": "******", "uuid": "83c8098c-bd2f-4a45-a19d-4d8a43e1c816" }, "customer": { "date_created": "2018-07-20T13:55:46.798105Z", "name": mock_customer_name, "owner": { "email": "*****@*****.**", "username": "******", "uuid": "83c8098c-bd2f-4a45-a19d-4d8a43e1c816" }, "schema_name": mock_schema_name, "uuid": "0e1911ba-ec60-4656-96ea-39b60bd4c5d3" }, "name": "Test Provider", "type": mock_provider_type, "uuid": "6e212746-484a-40cd-bba0-09a19d132d64" }] } responses.add(responses.GET, 'http://{}:{}/api/v1/providers/'.format( self.koku_host, self.koku_port), json=mock_response, status=200) accessor = CURAccountsNetwork() with self.assertRaises(CURAccountsInterfaceError): accessor.get_accounts_from_source()
def test_get_accounts_from_source_network_error(self, mock_host): """Test to get all accounts with network error.""" responses.add(responses.POST, 'http://{}:{}/api/v1/token-auth/'.format( self.koku_host, self.koku_port), json={'token': self.mock_token}, status=200) # Don't mock the providers call accessor = CURAccountsNetwork() with self.assertRaises(CURAccountsInterfaceError): accessor.get_accounts_from_source()
def test_get_accounts_from_source_response_error(self): """Test to get all accounts with response error.""" responses.add(responses.POST, 'http://{}:{}/api/v1/token-auth/'.format( self.koku_host, self.koku_port), json={'token': self.mock_token}, status=200) # Don't mock the providers call accessor = CURAccountsNetwork() with patch.object( requests, 'get', side_effect=requests.exceptions.RequestException('err')): with self.assertRaises(CURAccountsInterfaceError): accessor.get_accounts_from_source()
def test_get_accounts_from_source_unexpected_type(self): """Test to get all accounts with non application/json response.""" responses.add(responses.POST, 'http://{}:{}/api/v1/token-auth/'.format( self.koku_host, self.koku_port), json={'token': self.mock_token}, status=200) responses.add(responses.GET, 'http://{}:{}/api/v1/providers/'.format( self.koku_host, self.koku_port), body='sample text response', headers={'Content-Type': 'text/plain'}, status=200) accessor = CURAccountsNetwork() with self.assertRaises(CURAccountsInterfaceError): accessor.get_accounts_from_source()
def test_get_accounts_from_source(self): """Test to get all accounts.""" responses.add(responses.POST, 'http://{}:{}/api/v1/token-auth/'.format( self.koku_host, self.koku_port), json={'token': self.mock_token}, status=200) mock_authentication = "arn:aws:iam::999999999999:role/CostManagement" mock_billing_source = "cost-usage-bucket" mock_customer_name = "acct10001" mock_provider_type = "AWS" mock_schema_name = "acct10001" mock_response = { "count": 1, "next": 'null', "previous": 'null', "results": [{ "authentication": { "provider_resource_name": mock_authentication, "uuid": "7e4ec31b-7ced-4a17-9f7e-f77e9efa8fd6" }, "billing_source": { "bucket": mock_billing_source, "uuid": "75b17096-319a-45ec-92c1-18dbd5e78f94" }, "created_by": { "email": "*****@*****.**", "username": "******", "uuid": "83c8098c-bd2f-4a45-a19d-4d8a43e1c816" }, "customer": { "date_created": "2018-07-20T13:55:46.798105Z", "name": mock_customer_name, "owner": { "email": "*****@*****.**", "username": "******", "uuid": "83c8098c-bd2f-4a45-a19d-4d8a43e1c816" }, "schema_name": mock_schema_name, "uuid": "0e1911ba-ec60-4656-96ea-39b60bd4c5d3" }, "name": "Test Provider", "type": mock_provider_type, "uuid": "6e212746-484a-40cd-bba0-09a19d132d64" }] } responses.add(responses.GET, 'http://{}:{}/api/v1/providers/'.format( self.koku_host, self.koku_port), json=mock_response, status=200) accessor = CURAccountsNetwork() accounts = accessor.get_accounts_from_source() self.assertEqual(len(accounts), 1) account = accounts.pop() self.assertTrue('authentication' in account.keys()) self.assertTrue('billing_source' in account.keys()) self.assertTrue('customer_name' in account.keys()) self.assertTrue('provider_type' in account.keys()) self.assertTrue('schema_name' in account.keys()) self.assertEqual(account.get('authentication'), mock_authentication) self.assertEqual(account.get('billing_source'), mock_billing_source) self.assertEqual(account.get('customer_name'), mock_customer_name) self.assertEqual(account.get('provider_type'), mock_provider_type) self.assertEqual(account.get('schema_name'), mock_schema_name)
def test_get_accounts_from_source_unexpected_response(self): """Test to get all accounts with unexpected response.""" responses.add( responses.POST, 'http://{}:{}/api/v1/token-auth/'.format(self.koku_host, self.koku_port), json={'token': self.mock_token}, status=200, ) mock_authentication = 'arn:aws:iam::999999999999:role/CostManagement' mock_billing_source = 'cost-usage-bucket' mock_customer_name = 'acct10001' mock_provider_type = 'AWS' mock_schema_name = 'acct10001' mock_response = { 'count': 1, 'next': 'null', 'previous': 'null', 'results': [{ 'BAD_AUTHENTICATION': { 'provider_resource_name': mock_authentication, 'uuid': '7e4ec31b-7ced-4a17-9f7e-f77e9efa8fd6', }, 'billing_source': { 'bucket': mock_billing_source, 'uuid': '75b17096-319a-45ec-92c1-18dbd5e78f94', }, 'created_by': { 'email': '*****@*****.**', 'username': '******', 'uuid': '83c8098c-bd2f-4a45-a19d-4d8a43e1c816', }, 'customer': { 'date_created': '2018-07-20T13:55:46.798105Z', 'name': mock_customer_name, 'owner': { 'email': '*****@*****.**', 'username': '******', 'uuid': '83c8098c-bd2f-4a45-a19d-4d8a43e1c816', }, 'schema_name': mock_schema_name, 'uuid': '0e1911ba-ec60-4656-96ea-39b60bd4c5d3', }, 'name': 'Test Provider', 'type': mock_provider_type, 'uuid': '6e212746-484a-40cd-bba0-09a19d132d64', }], } responses.add( responses.GET, 'http://{}:{}/api/v1/providers/'.format(self.koku_host, self.koku_port), json=mock_response, status=200, ) accessor = CURAccountsNetwork() with self.assertRaises(CURAccountsInterfaceError): accessor.get_accounts_from_source()