def test_merges_nested_included_objects(self): data = self.alert_response() parsed_json = json.loads(data) rest_client = RESTClientObject() response = Mock(status=200, data=data) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = AlertsApi(api_client) alert = api.show( 1, include='external_account.team.organization,region,signature') self.assertEqual( str(alert.external_account.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'external_accounts')['id']) self.assertEqual( str(alert.external_account.team.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'teams')['id']) self.assertEqual( str(alert.external_account.team.organization.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'organizations')['id']) self.assertEqual( str(alert.region.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'regions')['id']) self.assertEqual( str(alert.signature.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'signatures')['id'])
def test_request_puts_form_params_in_json_api_format(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.organization_response()) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = SubOrganizationsApi(api_client) api.create(1, 'Evident') args, kwargs = rest_client.pool_manager.request.call_args self.assertEqual(args[0], 'POST') if PY3: self.assertRegex(args[1], "/v2/sub_organizations.json_api") else: self.assertRegexpMatches(args[1], "/v2/sub_organizations.json_api") self.assertEqual( kwargs['body'], json.dumps({ 'data': { 'attributes': { "organization_id": 1, 'name': 'Evident' } } }))
def test_call_api_sets_original_params_and_path_on_PaginatedCollection(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.json_list([self.organization_response()], {'page': {'number': 1, 'size': 1}})) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client response, status, headers = api_client.call_api('resource_path', 'PUT', path_params={'path': 'params'}, query_params={'query': 'params'}, header_params={'header': 'params'}, body={'body': 'param'}, post_params={'post': 'params'}, files=None, response_type='PaginatedCollection', auth_settings='auth_settings', callback=None, _return_http_data_only=None, collection_formats=None, _preload_content=True, _request_timeout=None) self.assertEqual(api_client, response._api_client) self.assertEqual('resource_path', response._resource_path) self.assertEqual('PUT', response._method) self.assertEqual([('path', 'params')], response._path_params) self.assertEqual([('query', 'params')], response._query_params) self.assertIn(('header', 'params'), response._header_params.items()) self.assertEqual('param', response._body['body']) self.assertEqual([('post', 'params')], response._post_params) self.assertIsNone(response._files) self.assertEqual('PaginatedCollection', response._response_type) self.assertEqual('auth_settings', response._auth_settings) self.assertIsNone(response._callback) self.assertIsNone(response._return_http_data_only) self.assertIsNone(response._collection_formats) self.assertTrue(response._preload_content) self.assertIsNone(response._request_timeout)
def test_assigns_foreign_keys(self): data = self.json_list([self.alert_response()]) parsed_json = json.loads(data) rest_client = RESTClientObject() response = Mock(status=200, data=data) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = AlertsApi(api_client) alert = api.list_for_report(1).data[0] self.assertEqual( str(alert.external_account_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'external_accounts')['id']) self.assertEqual( str(alert.region_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'regions')['id']) self.assertEqual( str(alert.signature_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'signatures')['id']) # nested objects too self.assertEqual( str(alert.external_account.team_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'external_accounts')['relationships']['team']['data']['id'])
def test_page_size_on_previous_link_is_not_set_if_on_last_page(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 2, 'size': 2}})) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = OrganizationsApi(api_client) orgs = api.list() self.assertNotIn('size', orgs._previous_page_params)
def test_raises_an_ApiException_if_response_is_not_success(self): rest_client = RESTClientObject() response = Mock(status=422, data=self.active_record_error_response()) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = TeamsApi(api_client) with self.assertRaises(ApiException) as cm: api.show(1) self.assertEqual(cm.exception.status, 422) self.assertEqual(cm.exception.reason, "Failed. Response code = 422. Response message = Name can't be blank Name is invalid Description can't be blank")
def test_page_size_is_set_on_each_link(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 2, 'size': 1}})) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = OrganizationsApi(api_client) orgs = api.list() self.assertEqual(orgs._next_page_params['size'], '1') self.assertEqual(orgs._previous_page_params['size'], '1') self.assertEqual(orgs._last_page_params['size'], '1')
def test_page_does_not_call_the_api_and_returns_self_when_already_on_page(self): rest_client = RESTClientObject() response1 = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 2, 'size': 1}})) responses = Mock() responses.side_effect = iter([response1]) rest_client.pool_manager.request = responses api_client = ApiClient() api_client.rest_client = rest_client api = OrganizationsApi(api_client) orgs = api.list() page = orgs.page(2) self.assertEqual(rest_client.pool_manager.request.call_count, 1) self.assertEqual(page.current_page_number, '2')
def test_does_not_error_with_included_nulls(self): data = json.loads(self.json_list([self.alert_response()])) data['included'] += [None] rest_client = RESTClientObject() response = Mock(status=200, data=json.dumps(data)) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = AlertsApi(api_client) alert = api.list_for_report(1).data[0] self.assertIsNotNone(alert.external_account_id) self.assertIsNotNone(alert.region_id) self.assertIsNotNone(alert.signature_id) self.assertIsNotNone(alert.external_account.team_id)
def test_raises_an_ApiException_if_response_is_not_success(self): rest_client = RESTClientObject() response = Mock(status=422, data=self.active_record_error_response()) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = TeamsApi(api_client) with self.assertRaises(ApiException) as cm: api.show(1) self.assertEqual(cm.exception.status, 422) self.assertEqual( cm.exception.reason, "Failed. Response code = 422. Response message = Name can't be blank Name is invalid Description can't be blank" )
def test_request_puts_page_params_in_the_body(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.organization_response()) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = SubOrganizationsApi(api_client) api.list(page={"number": 3}) args, kwargs = rest_client.pool_manager.request.call_args self.assertEqual(args[0], 'PUT') if PY3: self.assertRegex(args[1], "/v2/sub_organizations.json_api") else: self.assertRegexpMatches(args[1], "/v2/sub_organizations.json_api") self.assertEqual(kwargs['body'], json.dumps({"page": {"number": 3}}))
def test_call_api_sets_original_params_and_path_on_PaginatedCollection( self): rest_client = RESTClientObject() response = Mock(status=200, data=self.json_list([self.organization_response()], {'page': { 'number': 1, 'size': 1 }})) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client response, status, headers = api_client.call_api( 'resource_path', 'PUT', path_params={'path': 'params'}, query_params={'query': 'params'}, header_params={'header': 'params'}, body={'body': 'param'}, post_params={'post': 'params'}, files=None, response_type='PaginatedCollection', auth_settings='auth_settings', callback=None, _return_http_data_only=None, collection_formats=None, _preload_content=True, _request_timeout=None) self.assertEqual(api_client, response._api_client) self.assertEqual('resource_path', response._resource_path) self.assertEqual('PUT', response._method) self.assertEqual([('path', 'params')], response._path_params) self.assertEqual([('query', 'params')], response._query_params) self.assertIn(('header', 'params'), response._header_params.items()) self.assertEqual('param', response._body['body']) self.assertEqual([('post', 'params')], response._post_params) self.assertIsNone(response._files) self.assertEqual('PaginatedCollection', response._response_type) self.assertEqual('auth_settings', response._auth_settings) self.assertIsNone(response._callback) self.assertIsNone(response._return_http_data_only) self.assertIsNone(response._collection_formats) self.assertTrue(response._preload_content) self.assertIsNone(response._request_timeout)
def test_next_last_and_previous_pages_are_set_when_not_on_last_page(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 2, 'size': 1}})) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = OrganizationsApi(api_client) orgs = api.list() self.assertEqual(orgs.current_page_number, '2') self.assertEqual(orgs.next_page_number, '3') self.assertEqual(orgs.previous_page_number, '1') self.assertEqual(orgs.last_page_number, '3') self.assertTrue(orgs.has_next_page) self.assertTrue(orgs.has_previous_page) self.assertFalse(orgs.is_last_page)
def test_page_does_not_error_if_no_initial_params_are_supplied(self): rest_client = RESTClientObject() response1 = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 2, 'size': 1}})) response2 = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 3, 'size': 1}})) responses = Mock() responses.side_effect = iter([response1, response2]) rest_client.pool_manager.request = responses api_client = ApiClient() api_client.rest_client = rest_client api = OrganizationsApi(api_client) orgs = api.list() page = orgs.page(3) self.assertEqual(rest_client.pool_manager.request.call_count, 2) self.assertEqual(page.current_page_number, '3') self.assertEqual(orgs.current_page_number, '2')
def test_error_raised_if_page_number_is_greater_than_last_page_number(self): rest_client = RESTClientObject() response1 = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 2, 'size': 1}})) responses = Mock() responses.side_effect = iter([response1]) rest_client.pool_manager.request = responses api_client = ApiClient() api_client.rest_client = rest_client api = OrganizationsApi(api_client) orgs = api.list() if PY3: with self.assertRaisesRegex(ValueError, 'Page number cannot be greater than the last page number.'): page = orgs.page(4) else: with self.assertRaisesRegexp(ValueError, 'Page number cannot be greater than the last page number.'): page = orgs.page(4)
def test_merges_nested_included_objects(self): data = self.alert_response() parsed_json = json.loads(data) rest_client = RESTClientObject() response = Mock(status=200, data=data) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = AlertsApi(api_client) alert = api.show(1, include='external_account.team.sub_organization,region,signature') self.assertEqual(str(alert.external_account.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'external_accounts')['id']) self.assertEqual(str(alert.external_account.team.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'teams')['id']) self.assertEqual(str(alert.external_account.team.sub_organization.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'sub_organizations')['id']) self.assertEqual(str(alert.region.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'regions')['id']) self.assertEqual(str(alert.signature.id), next(obj for obj in parsed_json['included'] if obj['type'] == 'signatures')['id'])
def test_assigns_foreign_keys(self): data = self.json_list([self.alert_response()]) parsed_json = json.loads(data) rest_client = RESTClientObject() response = Mock(status=200, data=data) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = AlertsApi(api_client) alert = api.list_for_report(1).data[0] self.assertEqual(str(alert.external_account_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'external_accounts')['id']) self.assertEqual(str(alert.region_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'regions')['id']) self.assertEqual(str(alert.signature_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'signatures')['id']) # nested objects too self.assertEqual(str(alert.external_account.team_id), next(obj for obj in parsed_json['included'] if obj['type'] == 'external_accounts')['relationships']['team']['data']['id'])
def test_request_puts_form_params_in_json_api_format(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.organization_response()) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = SubOrganizationsApi(api_client) api.create('Evident', 1) args, kwargs = rest_client.pool_manager.request.call_args self.assertEqual(args[0], 'POST') if PY3: self.assertRegex(args[1], "/v2/sub_organizations.json_api") else: self.assertRegexpMatches(args[1], "/v2/sub_organizations.json_api") body = json.loads(kwargs['body']) self.assertEqual(body['data']['attributes']['organization_id'], 1) self.assertEqual(body['data']['attributes']['name'], 'Evident')
def test_request_sets_authorization_headers_for_GET(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.organization_response()) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = SubOrganizationsApi(api_client) api.show(1) args, kwargs = rest_client.pool_manager.request.call_args self.assertEqual(args[0], 'GET') if PY3: self.assertRegex(args[1], "/v2/sub_organizations/1.json_api") else: self.assertRegexpMatches(args[1], "/v2/sub_organizations/1.json_api") self.assertIsNotNone(kwargs['headers']['Authorization']) self.assertIsNotNone(kwargs['headers']['Date']) self.assertIsNotNone(kwargs['headers']['Content-MD5']) self.assertEqual(kwargs['headers']['Content-Type'], 'application/vnd.api+json') self.assertEqual(kwargs['headers']['Accept'], 'application/vnd.api+json')
def test_page_calls_the_api_with_original_url_and_params_and_the_page_number_param(self): rest_client = RESTClientObject() response1 = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 2, 'size': 1}})) response2 = Mock(status=200, data=self.json_list([self.organization_response(), self.organization_response(), self.organization_response()], {'page': {'number': 3, 'size': 1}})) responses = Mock() responses.side_effect = iter([response1, response2]) rest_client.pool_manager.request = responses api_client = ApiClient() api_client.rest_client = rest_client api = OrganizationsApi(api_client) orgs = api.list(filter={'name_eq': 'name'}) args1, kwargs1 = rest_client.pool_manager.request.call_args self.assertEqual(args1[0], 'PUT') if PY3: self.assertRegex(args1[1], "/v2/organizations.json_api") else: self.assertRegexpMatches(args1[1], "/v2/organizations.json_api") self.assertEqual(kwargs1['body'], json.dumps({"filter": {"name_eq": "name"}})) self.assertNotIn('page', json.loads(kwargs1['body'])) page = orgs.page(3) self.assertEqual(rest_client.pool_manager.request.call_count, 2) self.assertEqual(page.current_page_number, '3') self.assertEqual(orgs.current_page_number, '2') args2, kwargs2 = rest_client.pool_manager.request.call_args self.assertEqual(args2[0], args1[0]) if PY3: self.assertRegex(args2[1], args1[1]) else: self.assertRegexpMatches(args2[1], args1[1]) expected_body = {"filter": {"name_eq": "name"}, "page": {"number": '3', "size": '1'}} body = json.loads(kwargs2['body']) self.assertEqual(body['filter'], expected_body['filter']) self.assertEqual(body['page']['number'], expected_body['page']['number']) self.assertEqual(body['page']['size'], expected_body['page']['size']) self.assertEqual(kwargs2['preload_content'], kwargs1['preload_content']) self.assertEqual(kwargs2['timeout'], kwargs1['timeout'])
def test_request_sets_authorization_headers_for_non_GET(self): rest_client = RESTClientObject() response = Mock(status=200, data=self.organization_response()) rest_client.pool_manager.request = Mock(return_value=response) api_client = ApiClient() api_client.rest_client = rest_client api = SubOrganizationsApi(api_client) api.create(1, 'Evident') args, kwargs = rest_client.pool_manager.request.call_args self.assertEqual(args[0], 'POST') if PY3: self.assertRegex(args[1], "/v2/sub_organizations.json_api") else: self.assertRegexpMatches(args[1], "/v2/sub_organizations.json_api") self.assertIsNotNone(kwargs['headers']['Authorization']) self.assertIsNotNone(kwargs['headers']['Date']) self.assertIsNotNone(kwargs['headers']['Content-MD5']) self.assertEqual(kwargs['headers']['Content-Type'], 'application/vnd.api+json') self.assertEqual(kwargs['headers']['Accept'], 'application/vnd.api+json')