def test_delete_entity(self): request_headers = { 'Accept': self.ACCEPT_HEADER, } response_json = {'key': 'value'} with requests_mock.Mocker() as request_mock: request_mock.register_uri('DELETE', "http://entity_url/entity_id", request_headers=request_headers, json=response_json, status_code=200) gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID) get_result = gocd_api_request.delete_entity() assert get_result.status_code == 200 assert get_result.json() == response_json
def test_post_entity(self): data = {'data-key': 'data-value'} request_headers = { 'Accept': self.ACCEPT_HEADER, 'Content-Type': 'application/json' } response_json = {'key': 'value'} with requests_mock.Mocker() as request_mock: request_mock.register_uri('POST', "http://entity_url/entity_id", request_headers=request_headers, json=response_json, status_code=200) gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID) get_result = gocd_api_request.post_entity(data) assert get_result.status_code == 200 assert get_result.json() == response_json
def test_is_auth_not_required_should_return_false_when_status_code_of_encrypt_api_is_not_401(self): authorization = str(base64.b64encode("{}:{}".format(self.USERNAME, self.PASSWORD).encode("utf-8")), 'utf-8') request_headers = { 'Accept': 'application/vnd.go.cd.v1+json', 'Content-Type': 'application/json', 'Authorization': "Basic {}".format(authorization) } with requests_mock.Mocker() as mock: mock.register_uri('POST', "{}{}".format(self.DOMAIN, "/go/api/admin/encrypt"), request_headers=request_headers, json={"value": "AES:encrypted-text"}, status_code=200) gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID, self.USERNAME, self.PASSWORD) assert not gocd_api_request.is_auth_not_required()
def test_is_auth_not_required_should_return_true_when_status_code_of_encrypt_api_is_401_with_message(self): authorization = str(base64.b64encode("{}:{}".format(self.USERNAME, self.PASSWORD).encode("utf-8")), 'utf-8') request_headers = { 'Accept': 'application/vnd.go.cd.v1+json', 'Content-Type': 'application/json', 'Authorization': "Basic {}".format(authorization) } with requests_mock.Mocker() as mock: mock.register_uri('POST', "{}{}".format(self.DOMAIN, "/go/api/admin/encrypt"), request_headers=request_headers, json={ "message": "Basic authentication credentials are not required, since security has been disabled on this server." }, status_code=401) gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID, self.USERNAME, self.PASSWORD) assert gocd_api_request.is_auth_not_required()
def test_is_auth_not_required_should_return_true_when_username_and_password_are_none(self): gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID) assert gocd_api_request.is_auth_not_required()
def test_basic_params_should_return_params_with_auth_key_when_username_and_password_are_none(self, mock): gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID, self.USERNAME, self.PASSWORD) params = gocd_api_request.basic_params() assert 'auth' in params assert params['auth'] == requests.auth.HTTPBasicAuth(self.USERNAME, self.PASSWORD)
def test_basic_params_should_return_params_without_auth_key_when_username_and_password_are_none(self, mock): gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID) params = gocd_api_request.basic_params() assert 'auth' not in params
def test_collection_url(self): gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID) assert gocd_api_request.collection_url() == "{}{}".format(self.DOMAIN, self.TEST_ENTITY_PATH)
def test_entity_url_should_return_url_with_entity_id_when_entity_path_has_trailing_slash(self): gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID) assert gocd_api_request.entity_url() == "{}{}/{}".format(self.DOMAIN, self.TEST_ENTITY_PATH, self.ENTITY_ID)
def test_entity_url_should_return_url_without_entity_id(self): gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN) assert gocd_api_request.entity_url() == "{}{}".format(self.DOMAIN, self.TEST_ENTITY_PATH)