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_vendor_default_assignment_to_unassigned(self): test_product_name = "test_vendor_default_assignment_to_unassigned" 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_201_CREATED, response.content.decode("utf-8")) self.assertEquals( "0", str(json.loads(response.content.decode("utf-8"))['vendor']), response.content.decode("utf-8")) # cleanup apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_valid_namedproducts_api_endpoint(self): # create test data first_test_product_name = "first_product_name" second_test_product_name = "second_product_name" test_product_list_name = "product_list_name-01" test_product_list = apicall.create_product_list(self.client, test_product_list_name) first_product = apicall.create_product(self.client, first_test_product_name) second_product = apicall.create_product(self.client, second_test_product_name) product_ids = [ second_product['id'], first_product['id'], ] test_product_list['products'] = product_ids apicall.update_product_list(self.client, test_product_list) # test named products call response = self.client.get(apiurl.PRODUCT_LIST_DETAIL_NAMED_PRODUCTS_API_ENDPOINT % test_product_list['id']) self.assertEqual(response.status_code, status.HTTP_200_OK, response.content.decode("utf-8")) self.assertIn('"products":["%s","%s"]' % (first_test_product_name, second_test_product_name), response.content.decode("utf-8")) apicall.clean_db(self.client)
def test_valid_association_of_a_product_to_multiple_product_list(self): test_product_list_name = "product_list-0001" second_test_product_list_name = "product_list-0002" test_product_id = "product-0001" # create test products products = [ "product-0001", "product-0002", "product-0003", "product-0004", "product-0005", ] for product_id in products: apicalls.create_product(self.client, product_id) # create test product lists product_list_names = [ test_product_list_name, second_test_product_list_name, "product_list-0003", ] for product_list_name in product_list_names: apicalls.create_product_list(self.client, product_list_name) # assign products products = [ "product-0003", test_product_id, "product-0002", ] product_ids = [] for product_id in products: product = apicalls.get_product_by_name(self.client, product_id) product_ids.append(product['id']) # lookup id product_list = apicalls.get_product_list_by_name(self.client, test_product_list_name) second_product_list = apicalls.get_product_list_by_name(self.client, second_test_product_list_name) # associate products to product_lists product_list["products"] = product_ids second_product_list['products'] = product_ids apicalls.update_product_list(self.client, product_list) apicalls.update_product_list(self.client, second_product_list) # verify, that the product is associated with both product_lists product = apicalls.get_product_by_name(self.client, test_product_id) expected_set = { second_test_product_list_name, test_product_list_name, } inters_set = set(product['lists']).intersection(expected_set) self.assertSetEqual(inters_set, expected_set) apicalls.clean_db(self.client)
def test_count_api_endpoint(self): test_data_count = 5 for i in range(0, test_data_count): apicalls.create_product(self.client, "test_count-product_%04d" % i) # response = self.client.get(apiurl.PRODUCT_COUNT_API_ENDPOINT) response.content.decode("utf-8") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.content.decode("utf-8"), '{"count":%s}' % test_data_count) apicalls.clean_db(self.client)
def test_vendor_default_assignment_to_unassigned(self): test_product_name = "test_vendor_default_assignment_to_unassigned" 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_201_CREATED, response.content.decode("utf-8")) self.assertEquals( "0", str(json.loads(response.content.decode("utf-8"))["vendor"]), response.content.decode("utf-8") ) # cleanup apicalls.clean_db(self.client)
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_valid_byname_api_call(self): test_product_name = "my_get_name_api_call_test" apicalls.create_product(self.client, test_product_name) # call byname api endpoint valid_apicall = {"product_id": test_product_name} response = self.client.post(apiurl.PRODUCT_BY_NAME_API_ENDPOINT, valid_apicall) # verify results self.assertEqual(response.status_code, status.HTTP_200_OK, "Failed call: %s" % response.content.decode("utf-8")) self.assertRegex( response.content.decode("utf-8"), '.*"id":.*"product_id":"%s".*' % re.escape(valid_apicall["product_id"]) ) apicalls.clean_db(self.client)
def test_create_product_default_string_in_description_field(self): product_name = "description_field_test" product = apicalls.create_product(self.client, product_name) self.assertEqual(product["description"], "not set") apicalls.clean_db(self.client)
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_create_product_default_string_in_currency_field(self): product_name = "currency_field_test" product = apicalls.create_product(self.client, product_name) self.assertEqual(product["currency"], "USD") apicalls.clean_db(self.client)
def test_valid_association_of_a_product_to_single_product_list(self): test_product_list_name = "product_list-0001" # create test products products = [ "product-0001", "product-0002", "product-0003", "product-0004", "product-0005", ] for product_id in products: apicalls.create_product(self.client, product_id) # create test product lists product_list_names = [ test_product_list_name, "product_list-0002", "product_list-0003", ] for product_list_name in product_list_names: apicalls.create_product_list(self.client, product_list_name) # assign products products = [ "product-0003", "product-0001", "product-0002", ] product_ids = [] for product_id in products: product = apicalls.get_product_by_name(self.client, product_id) product_ids.append(product['id']) # lookup id product_list = apicalls.get_product_list_by_name(self.client, test_product_list_name) # associate products to product_list product_list["products"] = product_ids response_json = apicalls.update_product_list(self.client, product_list) # verify results self.assertEqual(sorted(response_json['products']), sorted(product_ids)) apicalls.clean_db(self.client)
def test_count_api_endpoint(self): test_data_count = 5 for i in range(0, test_data_count): apicalls.create_product(self.client, "test_count-product_%04d" % i, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) self.client.login(username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) response = self.client.get(apiurl.PRODUCT_COUNT_API_ENDPOINT) response.content.decode("utf-8") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.content.decode("utf-8"), '{"count":%s}' % test_data_count) apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_create_product_custom_string_in_currency_field(self): product_name = "currency_field_test" product = apicalls.create_product(self.client, product_name) product["currency"] = "EUR" product = apicalls.update_product(self.client, product_dict=product) self.assertEqual(product["currency"], "EUR") apicalls.clean_db(self.client)
def test_create_product_default_string_in_currency_field(self): product_name = "currency_field_test" product = apicalls.create_product(self.client, product_name, self.ADMIN_USERNAME, self.ADMIN_PASSWORD) self.assertEqual(product['currency'], "USD") apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_create_product_default_string_in_description_field(self): product_name = "description_field_test" product = apicalls.create_product(self.client, product_name, self.ADMIN_USERNAME, self.ADMIN_PASSWORD) self.assertEqual(product['description'], "not set") apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_api_anonymous_product_read_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() response = client.get(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], product) self.assertEqual(response.status_code, status.HTTP_200_OK, "Failed call: %s" % response.content.decode("utf-8")) self.assertRegex(response.content.decode("utf-8"), r'.*"id":1,.*')
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_anonymous_product_read_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() response = client.get(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id']) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED, "Failed call: %s" % response.content.decode("utf-8")) self.assertIn( '"detail":"Authentication credentials were not provided."', 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_api_authenticated_product_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() client.login(username=self.API_USERNAME, password=self.API_PASSWORD) # perform get operation get using a read-only account response = client.get(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], format='json') self.assertEqual(response.status_code, status.HTTP_200_OK, "Failed call: %s" % response.content.decode("utf-8")) self.assertRegex(response.content.decode("utf-8"), r'.*"id":%s,.*' % product['id'])
def test_assign_product_to_product_list(self): test_product_name = "product_in_all_lists" product_lists_to_associated = ["product_list-0001", "product_list-0002", "product_list-0003"] # create test data product = apicalls.create_product(self.client, test_product_name) product["lists"] = product_lists_to_associated # associate product lists to product (which does not work, therefore I don't care if the product_lists # are existing or not) response = self.client.put(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product["id"], product, format="json") # Lists are read-only in product API endpoint, but no error is thrown # Data is not changes self.assertEqual(response.status_code, status.HTTP_200_OK, "FAIL: %s" % response.content.decode("utf-8")) # verify, that the new list does not contain any list associations self.assertRegex(response.content.decode("utf-8"), '.*"lists":\[\].*') apicalls.clean_db(self.client)
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)