def test_create_success(self, acme_id, response_headers, args, request_params): """The function should return the created ACME ID.""" # Setup the mocked response responses.add(responses.POST, self.api_url, headers=response_headers, match=[responses.json_params_matcher(request_params)], status=201) acme = ACMEAccount(client=self.client) response = acme.create(*args) self.assertEqual(response, {"id": acme_id})
def test_delete_success(self, acme_id): """The function should return True if the deletion succeeded.""" api_url = self.get_acme_account_url(acme_id) # Setup the mocked response responses.add(responses.DELETE, api_url, status=204) acme = ACMEAccount(client=self.client) response = acme.delete(acme_id) self.assertEqual(True, response)
def test_update_success(self, acme_id, new_name): """The function should return True if the update succeeded.""" api_url = self.get_acme_account_url(acme_id) # Setup the mocked response responses.add(responses.PUT, api_url, status=200) acme = ACMEAccount(client=self.client) response = acme.update(acme_id, new_name) self.assertEqual(True, response)
def test_remove_domains_success(self, acme_id, api_url, req_domains, resp): """ The function should return a dictionary containing a list of domains not removed. """ # Setup the mocked response responses.add(responses.DELETE, api_url, json=resp, status=200) acme = ACMEAccount(client=self.client) response = acme.remove_domains(acme_id, req_domains) self.assertEqual(response, resp)
def test_acme_id(self): """The function should return all the data about the specified ACME ID.""" acme_id = 1234 api_url = self.get_acme_account_url(acme_id) valid_response = self.get_acme_account_data(acme_id) # Setup the mocked response responses.add(responses.GET, api_url, json=valid_response, status=200) acme = ACMEAccount(client=self.client) data = acme.get(acme_id) self.assertEqual(len(responses.calls), 1) self.assertEqual(responses.calls[0].request.url, api_url) self.assertEqual(data, valid_response)
def test_cached(self): """The function should return all the data, but should not query the API twice.""" # Setup the mocked response, refrain from matching the query string responses.add(responses.GET, self.api_url, json=self.valid_response, status=200, match_querystring=False) acme = ACMEAccount(client=self.client) acme.all(self.org_id) data = acme.all(self.org_id) # Verify all the query information # There should only be one call the first time "all" is called. # Due to pagination, this is only guaranteed as long as the number of # entries returned is less than the page size self.assertEqual(len(responses.calls), 1) self.match_url_with_qs(responses.calls[0].request.url) self.assertEqual(data, self.valid_response)
def test_need_params(self): """ The function should raise an exception when called without required parameters. """ acme = ACMEAccount(client=self.client) # missing acme_id self.assertRaises(TypeError, acme.delete)
def test_ne_acme_id(self): """The function should raise an HTTPError exception if the specified ACME ID does not exist.""" acme_id = 2345 api_url = self.get_acme_account_url(acme_id) # Setup the mocked response responses.add(responses.GET, api_url, json=self.error_response, status=404) acme = ACMEAccount(client=self.client) self.assertRaises(HTTPError, acme.get, acme_id)
def test_bad_http(self): """The function should raise an HTTPError exception if acme accounts cannot be retrieved from the API.""" # Setup the mocked response responses.add(responses.GET, self.api_url, json=self.error_response, status=404, match_querystring=False) acme = ACMEAccount(client=self.client) self.assertRaises(HTTPError, acme.all, self.org_id) # Verify all the query information self.assertEqual(len(responses.calls), 1) self.match_url_with_qs(responses.calls[0].request.url)
def test_init_param(self): """The URL should change if api_version is passed as a parameter to class initialization.""" # Set a new version version = "v3" api_url = self.get_api_url(api_version=version) # Setup the mocked response responses.add(responses.GET, api_url, json=self.valid_response, status=200, match_querystring=False) acme = ACMEAccount(client=self.client, api_version=version) data = acme.all(self.org_id) # Verify all the query information # There should only be one call the first time "all" is called. # Due to pagination, this is only guaranteed as long as the number of # entries returned is less than the page size self.assertEqual(len(responses.calls), 1) self.match_url_with_qs(responses.calls[0].request.url, api_url=api_url) self.assertEqual(data, self.valid_response)
def test_need_params(self): """ The function should raise an exception when called without required parameters. """ acme = ACMEAccount(client=self.client) # missing name, acme_server, org_id self.assertRaises(TypeError, acme.create) # missing acme_server, org_id self.assertRaises(TypeError, acme.create, "name") # missing org_id self.assertRaises(TypeError, acme.create, "name", "acme_server")
def test_remove_domains_failure_http_error(self, acme_id, api_url, req_domains, _): """ The function should raise an HTTPError exception if the domain removal failed. """ # Setup the mocked response responses.add(responses.DELETE, api_url, status=400) acme = ACMEAccount(client=self.client) self.assertRaises(HTTPError, acme.remove_domains, acme_id, req_domains)
def test_need_params(self): """ The function should raise an exception when called without required parameters or domains argument is not a list """ acme = ACMEAccount(client=self.client) # missing acme_id, domains self.assertRaises(TypeError, acme.remove_domains) # missing domains self.assertRaises(TypeError, acme.remove_domains, 1234) # domains is not iterable self.assertRaises(TypeError, acme.remove_domains, 1234, None)
def test_update_failure_http_error(self, acme_id, new_name): """ The function should raise an HTTPError exception if the update failed. """ api_url = self.get_acme_account_url(acme_id) # Setup the mocked response responses.add(responses.PUT, api_url, status=400) acme = ACMEAccount(client=self.client) self.assertRaises(HTTPError, acme.update, acme_id, new_name)
def test_create_failure_http_error(self, _, __, args, request_params): """ The function should return an error code and description if the ACME Account creation failed. """ # Setup the mocked response responses.add(responses.POST, self.api_url, json=self.error_response, match=[responses.json_params_matcher(request_params)], status=400) acme = ACMEAccount(client=self.client) self.assertRaises(HTTPError, acme.create, *args)
def generic_test(self): """Generic test for .find request parameters/response fields""" api_params[params_to_api["org_id"]] = str(self.org_id) valid_response = [ entry for entry in self.valid_response if all( str(entry[k]).lower().find(str(api_params[k]).lower()) != -1 for k in api_params ) ] # Setup the mocked response responses.add( responses.GET, self.api_url, json=valid_response, status=200, match_querystring=False ) acme = ACMEAccount(client=self.client) data = list(acme.find(self.org_id, **params)) # Verify all the query information # There should only be one call when "find" is called. # Due to pagination, this is only guaranteed as long as the number of # entries returned is less than the page size self.assertEqual(len(responses.calls), 1) self.match_url_with_qs(responses.calls[0].request.url, api_params) self.assertEqual(data, valid_response)
def test_create_failure_http_status_unexpected(self, _, __, args, request_params): """ The function should return an error code and description if the ACME Account creation failed with ACMEAccountCreationResponseError (unexpected HTTP status code). """ # Setup the mocked response responses.add(responses.POST, self.api_url, json=self.error_response, match=[responses.json_params_matcher(request_params)], status=200) # unexpected status acme = ACMEAccount(client=self.client) self.assertRaises(ACMEAccountCreationResponseError, acme.create, *args)
def test_create_failure_acme_id_not_found(self, _, response_headers, args, request_params): """ The function should return an error code and description if the ACME Account creation failed with ACMEAccountCreationResponseError (ACME ID not found in response). """ # Setup the mocked response responses.add(responses.POST, self.api_url, json=self.error_response, headers=response_headers, match=[responses.json_params_matcher(request_params)], status=201) acme = ACMEAccount(client=self.client) self.assertRaises(ACMEAccountCreationResponseError, acme.create, *args)
def test_need_org_id(self): """The function should raise an exception without an org_id parameter.""" acme = ACMEAccount(client=self.client) # We need to wrap this all crazy because it now returns an iterator result = acme.find() # pylint:disable=no-value-for-parameter self.assertRaises(TypeError, result.__next__)
def test_need_acme_id(self): """The function should raise an exception without an acme_id parameter.""" acme = ACMEAccount(client=self.client) self.assertRaises(TypeError, acme.get)