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_custom_page_length(self): for i in range(1, 55 + 1): apicall_data = { "product_list_name": "product_list-%d" % i } post_response = self.client.post(apiurl.PRODUCT_LIST_API_ENDPOINT, apicall_data, format='json') self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) res = self.client.get(apiurl.PRODUCT_LIST_API_ENDPOINT + "?page_size=15") self.assertEquals(res.status_code, status.HTTP_200_OK, res.content.decode("utf-8")) jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres['pagination']['page'], 1) self.assertEquals(jres['pagination']['last_page'], 4) self.assertEquals(jres['pagination']['total_records'], 55) self.assertEquals(jres['pagination']['page_records'], 15) res = self.client.get(apiurl.PRODUCT_LIST_API_ENDPOINT + "?page_size=15&page=2") jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres['pagination']['page'], 2) self.assertEquals(jres['pagination']['last_page'], 4) self.assertEquals(jres['pagination']['total_records'], 55) self.assertEquals(jres['pagination']['page_records'], 15) res = self.client.get(apiurl.PRODUCT_LIST_API_ENDPOINT + "?page_size=15&page=4") jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres['pagination']['page'], 4) self.assertEquals(jres['pagination']['last_page'], 4) self.assertEquals(jres['pagination']['total_records'], 55) self.assertEquals(jres['pagination']['page_records'], 10) apicall.clean_db(self.client)
def test_create_with_valid_list_price(self): """ Valid list price values for POST :return: """ list_prices = [ ["100", '"100.00"'], ["150.00", '"150.00"'], ["1150.00", '"1150.00"'], ["0", '"0.00"'], ["", "null"], ] count = 1 for list_price in list_prices: if list_price[0] == "": # test result of empty call apicall_data = {"product_id": "product_with_%04d" % count} else: apicall_data = {"product_id": "product_with_%04d" % count, "list_price": list_price[0]} response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual( response.status_code, status.HTTP_201_CREATED, "Failed: %s\nwith\n%s" % (list_price[0], response.content.decode("utf-8")), ) verify_regex = '.*"product_id":"%s".*"list_price":%s.*' % (apicall_data["product_id"], list_price[1]) self.assertRegex( response.content.decode("utf-8"), verify_regex, "Failed with\n%s" % response.content.decode("utf-8") ) count += 1 apicalls.clean_db(self.client)
def test_valid_product_names(self): product_names = [ "Testproduct1234500", "Testproduct 1234500", "Testproduct#1234500", "Testproduct+1234500", "Testproduct/1234500", "Testproduct%1234500", "Testproduct-1234500", "Testproduct.1234500", "Tes tpa%rt.12345 00#+/-", ] for product_name in product_names: apicall_data = {"product_id": product_name} self.client.login(username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) post_response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format='json') self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) self.assertRegex( post_response.content.decode("utf-8"), '.*"product_id":"%s".*' % re.escape(apicall_data['product_id'])) self.assertRegex(post_response.content.decode("utf-8"), '.*"vendor":%d.*' % 0) # vendor id field sometimes json decoded as string, sometimes as integer when parsing the post result # not a big deal, but better to get the object again to clarify the output product = json.loads(post_response.content.decode("utf-8")) get_response = self.client.get(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id'], format='json') product = json.loads(get_response.content.decode("utf-8")) # verify URL content second_call = self.client.get(product['url'], format='json') same_product = json.loads(second_call.content.decode("utf-8")) self.assertEqual( product['vendor'], same_product['vendor'], "First call:\n%s\nSecond Call:\n%s\n" % (get_response.content.decode("utf-8"), second_call.content.decode("utf-8"))) self.assertEqual(json.dumps(product, indent=4), json.dumps(same_product, indent=4)) # cleanup response = self.client.delete(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product['id']) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.content.decode("utf-8")) apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_create_with_invalid_lifecycle_dates(self): apicall_data = { "product_id": "test_create_with_invalid_lifecycle_dates", "eox_update_time_stamp": "17.01.23", "end_of_sale_date": "2014-01-1", "end_of_support_date": "2019-16-31", "eol_ext_announcement_date": "013-01-31", "end_of_sw_maintenance_date": "-1-31", "end_of_routine_failure_analysis": "01-31-2015", "end_of_service_contract_renewal": "2019-01-49", "end_of_new_service_attachment_date": "15-21-31", "eol_reference_number": "EOL9449", "eol_reference_url": "http://www.cisco.com/en/US/prod/collateral/switches/ps5718/ps6406/eos-eol-notice-c51-730121.html", } response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") date_error = '["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]."]' self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content.decode("utf-8")) self.assertIn('"eox_update_time_stamp":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_support_date":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"eol_ext_announcement_date":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_sw_maintenance_date":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_routine_failure_analysis":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_service_contract_renewal":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_new_service_attachment_date":%s' % date_error, response.content.decode("utf-8")) 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_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_create_with_valid_lifecycle_dates_and_null_value(self): apicall_data = { "product_id": "test_create_with_valid_lifecycle_dates_and_null_value", "eol_reference_number": "EOL9449", "eol_reference_url": "http://www.cisco.com/en/US/prod/collateral/switches/ps5718/ps6406/eos-eol-notice-c51-730121.html", } 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.assertRegex(response.content.decode("utf-8"), '.*"product_id":"%s".*' % apicall_data["product_id"]) self.assertRegex(response.content.decode("utf-8"), '.*"eox_update_time_stamp":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_support_date":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"eol_ext_announcement_date":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_sw_maintenance_date":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_routine_failure_analysis":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_service_contract_renewal":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_new_service_attachment_date":%s.*' % "null") self.assertRegex( response.content.decode("utf-8"), '.*"eol_reference_number":"%s".*' % apicall_data["eol_reference_number"] ) self.assertRegex( response.content.decode("utf-8"), '.*"eol_reference_url":"%s".*' % apicall_data["eol_reference_url"] ) apicalls.clean_db(self.client)
def test_verify_product_list_name_passed(self): product_list_names = [ "Testproduct123400", "Testproduct 123400", "Testproduct%123400", "Testproduct$123400", "Testproduct#123400", "Testproduct*123400", ] for product_list_name in product_list_names: apicall_data = { "product_list_name": product_list_name } response = self.client.post(apiurl.PRODUCT_LIST_API_ENDPOINT, apicall_data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertRegex(response.content.decode("utf-8"), '.*"product_list_name":"%s".*' % re.escape(apicall_data['product_list_name'])) product_list = json.loads(response.content.decode("utf-8")) # verify URL content second_call = self.client.get(product_list['url']) same_product = json.loads(second_call.content.decode("utf-8")) self.assertDictEqual(product_list, same_product) # cleanup response = self.client.delete(apiurl.PRODUCT_LIST_DETAIL_API_ENDPOINT % product_list['id']) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.content.decode("utf-8")) apicall.clean_db(self.client)
def test_create_with_valid_json_data(self): """ The JSON data are stored as a string, the REST API will only provide a JSON string, the parsing of the data must happen afterwards :return: """ json_data = {"custom_key": "1234567890"} apicall_data = {"product_id": "json_test_valid_data", "json_data": json.dumps(json_data)} 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.assertRegex(response.content.decode("utf-8"), '.*"product_id":"%s".*' % apicall_data["product_id"]) response_json = json.loads(response.content.decode("utf-8")) # to use the JSON data from the API, it must be converted to valid JSON syntax by replacing ' with " try: returned_json = json.loads(response_json["json_data"].replace("'", '"')) if "custom_key" not in returned_json.keys(): self.fail("Expected JSON key not found") self.assertEqual(returned_json["custom_key"], "1234567890") except Exception as ex: print(ex) self.fail("Cannot parse JSON result: %s" % ex) 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_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_product_pagination_defaults(self): for i in range(1, 55 + 1): apicall_data = {"product_id": "product-%d" % i} post_response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) res = self.client.get(apiurl.PRODUCT_API_ENDPOINT) self.assertEquals(res.status_code, status.HTTP_200_OK, res.content.decode("utf-8")) jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres["pagination"]["page"], 1) self.assertEquals(jres["pagination"]["last_page"], 3) self.assertEquals(jres["pagination"]["total_records"], 55) self.assertEquals(jres["pagination"]["page_records"], 25) res = self.client.get(apiurl.PRODUCT_API_ENDPOINT + "?page=2") jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres["pagination"]["page"], 2) self.assertEquals(jres["pagination"]["last_page"], 3) self.assertEquals(jres["pagination"]["total_records"], 55) self.assertEquals(jres["pagination"]["page_records"], 25) res = self.client.get(apiurl.PRODUCT_API_ENDPOINT + "?page=3") jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres["pagination"]["page"], 3) self.assertEquals(jres["pagination"]["last_page"], 3) self.assertEquals(jres["pagination"]["total_records"], 55) self.assertEquals(jres["pagination"]["page_records"], 5) 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} 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_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_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_invalid_create_product_call_with_vendor_id(self): test_product_name = "MyProductName" apicall_data = {"product_id": test_product_name, "vendor": 100000} response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(r'"vendor":["Invalid pk \"100000\" - object does not exist."', response.content.decode("utf-8")) # cleanup 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_invalid_product_call_with_vendor_string(self): test_product_name = "MyProductName" apicall_data = {"product_id": test_product_name, "vendor": "Invalid Vendor value"} response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn( r'"vendor":["Incorrect type. Expected pk value, received str."]', response.content.decode("utf-8") ) # cleanup apicalls.clean_db(self.client)
def test_create_with_invalid_lifecycle_dates(self): apicall_data = { "product_id": "test_create_with_invalid_lifecycle_dates", "eox_update_time_stamp": "17.01.23", "end_of_sale_date": "2014-01-1", "end_of_support_date": "2019-16-31", "eol_ext_announcement_date": "013-01-31", "end_of_sw_maintenance_date": "-1-31", "end_of_routine_failure_analysis": "01-31-2015", "end_of_service_contract_renewal": "2019-01-49", "end_of_new_service_attachment_date": "15-21-31", "eol_reference_number": "EOL9449", "eol_reference_url": "http://www.cisco.com/en/US/prod/collateral/switches/ps5718/ps6406/eos-eol-notice-c51-730121.html", } self.client.login(username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format='json') date_error = '["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]."]' self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content.decode("utf-8")) self.assertIn('"eox_update_time_stamp":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_support_date":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"eol_ext_announcement_date":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_sw_maintenance_date":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_routine_failure_analysis":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_service_contract_renewal":%s' % date_error, response.content.decode("utf-8")) self.assertIn('"end_of_new_service_attachment_date":%s' % date_error, response.content.decode("utf-8")) apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_count_api_endpoint(self): test_product_list_count = 5 for i in range(0, test_product_list_count): apicall.create_product_list(self.client, "product_list-%04d" % i) response = self.client.get(apiurl.PRODUCT_LIST_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_product_list_count) apicall.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_valid_product_names(self): product_names = [ "Testproduct1234500", "Testproduct 1234500", "Testproduct#1234500", "Testproduct+1234500", "Testproduct/1234500", "Testproduct%1234500", "Testproduct-1234500", "Testproduct.1234500", "Tes tpa%rt.12345 00#+/-", ] for product_name in product_names: apicall_data = {"product_id": product_name} post_response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) self.assertRegex( post_response.content.decode("utf-8"), '.*"product_id":"%s".*' % re.escape(apicall_data["product_id"]) ) self.assertRegex(post_response.content.decode("utf-8"), '.*"vendor":%d.*' % 0) # vendor id field sometimes json decoded as string, sometimes as integer when parsing the post result # not a big deal, but better to get the object again to clarify the output product = json.loads(post_response.content.decode("utf-8")) get_response = self.client.get(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product["id"], format="json") product = json.loads(get_response.content.decode("utf-8")) # verify URL content second_call = self.client.get(product["url"], format="json") same_product = json.loads(second_call.content.decode("utf-8")) self.assertEqual( product["vendor"], same_product["vendor"], "First call:\n%s\nSecond Call:\n%s\n" % (get_response.content.decode("utf-8"), second_call.content.decode("utf-8")), ) self.assertEqual(json.dumps(product, indent=4), json.dumps(same_product, indent=4)) # cleanup response = self.client.delete(apiurl.PRODUCT_DETAIL_API_ENDPOINT % product["id"]) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.content.decode("utf-8")) apicalls.clean_db(self.client)
def test_create_with_valid_lifecycle_dates_and_null_value(self): apicall_data = { "product_id": "test_create_with_valid_lifecycle_dates_and_null_value", "eol_reference_number": "EOL9449", "eol_reference_url": "http://www.cisco.com/en/US/prod/collateral/switches/ps5718/ps6406/eos-eol-notice-c51-730121.html", } 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_201_CREATED, response.content.decode("utf-8")) self.assertRegex(response.content.decode("utf-8"), '.*"product_id":"%s".*' % apicall_data['product_id']) self.assertRegex(response.content.decode("utf-8"), '.*"eox_update_time_stamp":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_support_date":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"eol_ext_announcement_date":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_sw_maintenance_date":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_routine_failure_analysis":%s.*' % "null") self.assertRegex(response.content.decode("utf-8"), '.*"end_of_service_contract_renewal":%s.*' % "null") self.assertRegex( response.content.decode("utf-8"), '.*"end_of_new_service_attachment_date":%s.*' % "null") self.assertRegex( response.content.decode("utf-8"), '.*"eol_reference_number":"%s".*' % apicall_data['eol_reference_number']) self.assertRegex( response.content.decode("utf-8"), '.*"eol_reference_url":"%s".*' % apicall_data['eol_reference_url']) apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_create_with_default_vendors(self): # ID values from fixture "default_vendors.yaml" default_vendors = [ {"id": "0", "name": "unassigned"}, {"id": "1", "name": "Cisco Systems"}, {"id": "2", "name": "Juniper Networks"}, ] for i in range(0, len(default_vendors)): apicall_data = {"product_id": "product-%04d" % i, "vendor": default_vendors[i]["id"]} response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format="json") self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertRegex(response.content.decode("utf-8"), '.*"vendor":%s.*' % re.escape(default_vendors[i]["id"])) # cleanup apicalls.clean_db(self.client)
def test_create_with_valid_list_price(self): """ Valid list price values for POST :return: """ list_prices = [ ['100', '"100.00"'], ['150.00', '"150.00"'], ['1150.00', '"1150.00"'], ['0', '"0.00"'], ['', 'null'], ] count = 1 for list_price in list_prices: if list_price[0] == "": # test result of empty call apicall_data = { "product_id": "product_with_%04d" % count, } else: apicall_data = { "product_id": "product_with_%04d" % count, "list_price": list_price[0] } 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_201_CREATED, "Failed: %s\nwith\n%s" % (list_price[0], response.content.decode("utf-8"))) verify_regex = '.*"product_id":"%s".*"list_price":%s.*' % ( apicall_data['product_id'], list_price[1]) self.assertRegex( response.content.decode("utf-8"), verify_regex, "Failed with\n%s" % response.content.decode("utf-8")) count += 1 apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
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_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_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_invalid_create_product_call_with_vendor_id(self): test_product_name = "MyProductName" apicall_data = {"product_id": test_product_name, "vendor": 100000} 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) self.assertIn( r'"vendor":["Invalid pk \"100000\" - object does not exist."', response.content.decode("utf-8")) # cleanup apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_product_custom_page_length(self): for i in range(1, 55 + 1): apicall_data = {"product_id": "product-%d" % i} post_response = self.client.post(apiurl.PRODUCT_API_ENDPOINT, apicall_data, format='json') self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) self.client.login(username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD) res = self.client.get(apiurl.PRODUCT_API_ENDPOINT + "?page_size=15") self.assertEquals(res.status_code, status.HTTP_200_OK, res.content.decode("utf-8")) jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres['pagination']['page'], 1) self.assertEquals(jres['pagination']['last_page'], 4) self.assertEquals(jres['pagination']['total_records'], 55) self.assertEquals(jres['pagination']['page_records'], 15) res = self.client.get(apiurl.PRODUCT_API_ENDPOINT + "?page_size=15&page=2") jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres['pagination']['page'], 2) self.assertEquals(jres['pagination']['last_page'], 4) self.assertEquals(jres['pagination']['total_records'], 55) self.assertEquals(jres['pagination']['page_records'], 15) res = self.client.get(apiurl.PRODUCT_API_ENDPOINT + "?page_size=15&page=4") jres = json.loads(res.content.decode("utf-8")) self.assertEquals(jres['pagination']['page'], 4) self.assertEquals(jres['pagination']['last_page'], 4) self.assertEquals(jres['pagination']['total_records'], 55) self.assertEquals(jres['pagination']['page_records'], 10) apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
def test_verify_product_list_name_passed_with_special_characters(self): apicall_datas = [ {"product_list_name": "Testproduct-123400"}, {"product_list_name": "Testproduct_123400"}, {"product_list_name": "Testproduct%123400"}, {"product_list_name": "Testproduct&123400"}, {"product_list_name": "Testproduct#123400"}, {"product_list_name": "Testproduct\123400"}, ] for apicall_data in apicall_datas: response = self.client.post(apiurl.PRODUCT_LIST_API_ENDPOINT, apicall_data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED, "name: %s\ncontent: %s" % (apicall_data, response.content.decode("utf-8"))) self.assertRegex(response.content.decode("utf-8"), '.*"product_list_name":"%s".*' % apicall_data['product_list_name']) apicall.clean_db(self.client)
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_invalid_product_call_with_vendor_string(self): test_product_name = "MyProductName" apicall_data = { "product_id": test_product_name, "vendor": "Invalid Vendor value" } 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) self.assertIn( r'"vendor":["Incorrect type. Expected pk value, received str."]', response.content.decode("utf-8")) # cleanup apicalls.clean_db(self.client, username=self.ADMIN_USERNAME, password=self.ADMIN_PASSWORD)
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_create_with_default_vendors(self): # ID values from fixture "default_vendors.yaml" default_vendors = [ { 'id': '0', 'name': 'unassigned', }, { 'id': '1', 'name': 'Cisco Systems', }, { 'id': '2', 'name': 'Juniper Networks', }, ] for i in range(0, len(default_vendors)): apicall_data = { "product_id": "product-%04d" % i, "vendor": default_vendors[i]['id'] } 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_201_CREATED) self.assertRegex( response.content.decode("utf-8"), '.*"vendor":%s.*' % re.escape(default_vendors[i]['id'])) # 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)