def test_create_with_invalid_negative_value_in_list_price(self): """ Test failure on negative values in list prices in POST statement :return: """ list_prices = ["-100.00", "-100", "-1"] for list_price in list_prices: apicall_data = { "product_id": "invalid_element", "list_price": list_price } self.client.login(username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format='json') self.assertEqual( response.status_code, status.HTTP_400_BAD_REQUEST, "Failed: %s\n with\n%s" % (list_price, response.content.decode("utf-8"))) apicalls.result_contains_error( self, STRING_LIST_PRICE_GREATER_OR_EQUAL_ZERO, "list_price", response.content.decode("utf-8"))
def test_unique_constrain_in_product_name(self): test_product_name = "MyProductName" apicall_data = {"product_id": test_product_name} self.client.login(username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) apicalls.create_product(self.client, "MyProductName", username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) # try to create the product again response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) apicalls.result_contains_error(self, STRING_UNIQUE_FIELD_REQUIRED, "product_id", response.content.decode("utf-8")) # cleanup apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_create_with_invalid_list_price(self): """ Test invalid list prices in POST statement :return: """ list_prices = [ "1oo", "1OO", "100,00", "12ff", ] for list_price in list_prices: apicall_data = { "product_id": "invalid_element", "list_price": list_price } self.client.login(username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format='json') self.assertEqual( response.status_code, status.HTTP_400_BAD_REQUEST, "Failed: %s\n with\n%s" % (list_price, response.content.decode("utf-8"))) apicalls.result_contains_error( self, STRING_LIST_PRICE_VERIFICATION_FAILED, "list_price", response.content.decode("utf-8"))
def test_update_with_null_value_list_price_string(self): product_name = "list_price" product = apicalls.create_product(self.client, product_name) self.assertEqual(product["list_price"], None) # remove all None values from the result product = self.clean_null_values_from_dict(product) # null value in list price will lead to an 400 error product["list_price"] = None response = self.client.put(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product["id"], product, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK, response.content.decode("utf-8")) # null value in list price data without format = json will lead to a 400 with a validation error product["list_price"] = None response = self.client.put(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product["id"], product) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content.decode("utf-8")) apicalls.result_contains_error( self, STRING_LIST_PRICE_VERIFICATION_FAILED, "list_price", response.content.decode("utf-8") ) apicalls.clean_db(self.client)
def test_invalid_byname_api_call_with_wrong_apicall_data_format(self): # call byname invalid_apicall = {"pa_name": "not_existing_product_number"} response = self.client.post(apiurl.PRODUCT_BY_NAME_API_ENDPOINT, invalid_apicall) self.assertEqual( response.status_code, status.HTTP_400_BAD_REQUEST, "Failed call: %s" % response.content.decode("utf-8") ) apicalls.result_contains_error( self, "invalid parameter given, 'product_id' required", "error", response.content.decode("utf-8") )
def test_invalid_byname_api_call(self): # call byname invalid_apicall = { "product_list_name": "not_existing_product_number" } response = self.client.post(apiurl.PRODUCT_LIST_BY_NAME_API_ENDPOINT, invalid_apicall) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, "Failed call: %s" % response.content.decode("utf-8")) apicall.result_contains_error(self, STRING_PRODUCT_LIST_NOT_FOUND_MESSAGE % invalid_apicall['product_list_name'], "product_list_name", response.content.decode("utf-8"))
def test_unique_constrain_in_product_name(self): test_product_name = "MyProductName" apicall_data = {"product_id": test_product_name} apicalls.create_product(self.client, "MyProductName") # try to create the product again response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) apicalls.result_contains_error( self, STRING_UNIQUE_FIELD_REQUIRED, "product_id", response.content.decode("utf-8") ) # cleanup apicalls.clean_db(self.client)
def test_create_with_invalid_json_structured_data(self): corrupt_json_strings = [ "{'custom_key''1234567890'}", # structural issue "{'custom_key':'1234567890'}", # single quotes are no valid JSON format ] for corrupt_json_string in corrupt_json_strings: apicall_data = {"product_id": "json_test_invalid_data", "json_data": corrupt_json_string} response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) apicalls.result_contains_error( self, STRING_JSON_VERIFICATION_FAILED, "json_data", response.content.decode("utf-8") )
def test_api_unauthenticated_write_access(self): # create test product test_product_id = "product_id" product = apicalls.create_product(self.client, test_product_id) # create new client to force unauthenticated access client = APIClient() product['description'] = "I will change it :)" response = client.put(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], product) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED, "Failed call: %s" % response.content.decode("utf-8")) apicalls.result_contains_error(self, "Authentication credentials were not provided.", "detail", response.content.decode("utf-8"))
def test_api_unauthenticated_write_access(self): # create test product test_product_id = "product_id" product = apicalls.create_product(self.client, test_product_id, self.ADMIN_USERNAME, self.ADMIN_PASSWORD) # create new client to force unauthenticated access client = APIClient() product['description'] = "I will change it :)" response = client.put( apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], product) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED, "Failed call: %s" % response.content.decode("utf-8")) apicalls.result_contains_error( self, "Authentication credentials were not provided.", "detail", response.content.decode("utf-8"))
def test_create_product_with_invalid_string_in_currency_field(self): product_name = "currency_field_test" product = apicalls.create_product(self.client, product_name) product["currency"] = "INV" # try to create the product again response = self.client.put(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product["id"], product, format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) apicalls.result_contains_error( self, STRING_PRODUCT_INVALID_CURRENCY_VALUE % product["currency"], "currency", response.content.decode("utf-8"), ) # cleanup apicalls.clean_db(self.client)
def test_create_with_invalid_negative_value_in_list_price(self): """ Test failure on negative values in list prices in POST statement :return: """ list_prices = ["-100.00", "-100", "-1"] for list_price in list_prices: apicall_data = {"product_id": "invalid_element", "list_price": list_price} response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual( response.status_code, status.HTTP_400_BAD_REQUEST, "Failed: %s\n with\n%s" % (list_price, response.content.decode("utf-8")), ) apicalls.result_contains_error( self, STRING_LIST_PRICE_GREATER_OR_EQUAL_ZERO, "list_price", response.content.decode("utf-8") )
def test_create_with_invalid_list_price(self): """ Test invalid list prices in POST statement :return: """ list_prices = ["1oo", "1OO", "100,00", "12ff"] for list_price in list_prices: apicall_data = {"product_id": "invalid_element", "list_price": list_price} response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual( response.status_code, status.HTTP_400_BAD_REQUEST, "Failed: %s\n with\n%s" % (list_price, response.content.decode("utf-8")), ) apicalls.result_contains_error( self, STRING_LIST_PRICE_VERIFICATION_FAILED, "list_price", response.content.decode("utf-8") )
def test_create_product_with_invalid_string_in_currency_field(self): product_name = "currency_field_test" product = apicalls.create_product(self.client, product_name, self.ADMIN_USERNAME, self.ADMIN_PASSWORD) product['currency'] = "INV" # try to create the product again response = self.client.put(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], product, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) apicalls.result_contains_error( self, STRING_PRODUCT_INVALID_CURRENCY_VALUE % product['currency'], "currency", response.content.decode("utf-8")) # cleanup apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_update_with_null_value_list_price_string(self): product_name = "list_price" product = apicalls.create_product(self.client, product_name, self.ADMIN_USERNAME, self.ADMIN_PASSWORD) self.assertEqual(product['list_price'], None) # remove all None values from the result product = self.clean_null_values_from_dict(product) # null value in list price will lead to an 400 error product['list_price'] = None response = self.client.put(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], product, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK, response.content.decode("utf-8")) # null value in list price data without format = json will lead to a 400 with a validation error product['list_price'] = None response = self.client.put( apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], product) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content.decode("utf-8")) apicalls.result_contains_error(self, STRING_LIST_PRICE_VERIFICATION_FAILED, "list_price", response.content.decode("utf-8")) apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)