def test_004_method_mapping_not_found(self): # Get current directory dir_path = os.path.dirname(os.path.realpath(__file__)) # Get one directory up, that is basically joining '..' # to the path and getting absolute path from it one_dir_up = os.path.abspath(os.path.join(dir_path, "..")) # Add the project name to the path to form the client directory client_dir = os.path.join(one_dir_up, "epages_client") # Create paths for correct and incorrect method mapping files correct_mapping = os.path.join(client_dir, "method_mapping.json") incorrect_mapping = os.path.join(client_dir, "method_rapping.json") # Rename method mapping to be a invalid one os.rename(correct_mapping, incorrect_mapping) # Reload client self.client = RestClient(os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) # When the method mapping file is not found, and the called method # is not found either, there raises a TypeError with self.assertRaises(TypeError) as e: self.client.get_shop_info() # Rename the method mapping file to its original name os.rename(incorrect_mapping, correct_mapping) time.sleep(1)
class TestClient(BaseUnitTest): '''A class for testing the RestClient class''' def setUp(self): self.client = RestClient(os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) self.params = {"query": {}, "param1": "", "param2": ""} def test_001_use_undefined_command(self): with self.assertRaises(TypeError) as e: response = self.client.foobar() def test_002_set_and_get_currency(self): self.client.currency = "EUR" self.assertEqual(self.client.currency, "EUR") def test_003_set_and_get_locale(self): self.client.locale = "fi_FI" self.assertEqual(self.client.locale, "fi_FI") def test_004_method_mapping_not_found(self): # Get current directory dir_path = os.path.dirname(os.path.realpath(__file__)) # Get one directory up, that is basically joining '..' # to the path and getting absolute path from it one_dir_up = os.path.abspath(os.path.join(dir_path, "..")) # Add the project name to the path to form the client directory client_dir = os.path.join(one_dir_up, "epages_client") # Create paths for correct and incorrect method mapping files correct_mapping = os.path.join(client_dir, "method_mapping.json") incorrect_mapping = os.path.join(client_dir, "method_rapping.json") # Rename method mapping to be a invalid one os.rename(correct_mapping, incorrect_mapping) # Reload client self.client = RestClient(os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) # When the method mapping file is not found, and the called method # is not found either, there raises a TypeError with self.assertRaises(TypeError) as e: self.client.get_shop_info() # Rename the method mapping file to its original name os.rename(incorrect_mapping, correct_mapping) time.sleep(1)
def setUp(self): self.client = RestClient( os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) self.params = { "query": {}, "param1": "", "param2": "" }
class TestNewsletterMethods(BaseUnitTest): '''A class for testing newsletter related methods on RestClient class''' def setUp(self): self.client = RestClient( os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) self.params = { "query": {}, "param1": "", "param2": "" } def test_001_get_newsletter_campaigns(self): newsletter_campaigns = self.client.get_newsletter_campaigns( self.params) self.assertEqual(isinstance(newsletter_campaigns, dict), True) def test_002_get_newsletter_campaign_subscribers_no_id(self): with self.assertRaises(ValueError) as e: newsletter_subscribers = self.client.get_newsletter_campaign_subscribers( self.params) def test_003_get_newsletter_campaign_subscribers_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: newsletter_subscribers = self.client.get_newsletter_campaign_subscribers( self.params) def test_004_get_newsletter_campaign_subscribers(self): newsletter_campaigns = self.client.get_newsletter_campaigns( self.params) # If there are some newsletters, check if the first one has subscribers if newsletter_campaigns["results"] > 0: campaign_id = newsletter_campaigns["items"][0]["campaignId"] self.params["param1"] = campaign_id newsletter_subscribers = self.client.get_newsletter_campaign_subscribers( self.params) self.assertEqual(isinstance(newsletter_subscribers, dict), True)
class TestCustomerMethods(BaseUnitTest): '''A class for testing customer related methods on RestClient class''' def setUp(self): self.client = RestClient(os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) self.params = {"query": {}, "param1": "", "param2": ""} def test_001_add_customer_no_object(self): with self.assertRaises(RuntimeError) as e: response = self.client.add_customer(self.params) def test_002_add_customer_invalid_object(self): self.params["object"] = None with self.assertRaises(TypeError) as e: response = self.client.add_customer(self.params) def test_003_add_customer_empty_billing_address(self): customer = CustomerCreate() self.params["object"] = customer with self.assertRaises(ValueError) as e: response = self.client.add_customer(self.params) def test_004_add_customer(self): customer = CustomerCreate() customer.billingAddress.firstName = "John" customer.billingAddress.lastName = "Doe" customer.billingAddress.emailAddress = "*****@*****.**" self.params["object"] = customer response = self.client.add_customer(self.params) self.assertEqual(isinstance(response, dict), True) def test_005_get_customers(self): customers = self.client.get_customers(self.params) self.assertEqual(isinstance(customers, dict), True) def test_006_get_customer_no_id(self): with self.assertRaises(ValueError) as e: customer = self.client.get_customer(self.params) def test_007_get_customer_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: customer = self.client.get_customer(self.params) def test_008_get_customer_correct_id(self): customers = self.client.get_customers(self.params) customer_id = customers["items"][1]["customerId"] self.params["param1"] = customer_id customer = self.client.get_customer(self.params) self.assertEqual(isinstance(customer, dict), True) def test_009_update_customer_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.update_product(self.params) def test_010_update_customer_no_object(self): customers = self.client.get_customers(self.params) customer_id = customers["items"][1]["customerId"] self.params["param1"] = customer_id with self.assertRaises(RuntimeError) as e: response = self.client.update_customer(self.params) def test_011_update_customer_invalid_object(self): customers = self.client.get_customers(self.params) customer_id = customers["items"][1]["customerId"] self.params["param1"] = customer_id self.params["object"] = None with self.assertRaises(TypeError) as e: response = self.client.update_customer(self.params) def test_012_update_customer(self): customers = self.client.get_customers(self.params) customer_id = customers["items"][1]["customerId"] self.params["param1"] = customer_id customer = CustomerUpdate() customer.internalNote = "This customer whines a lot." self.params["object"] = customer response = self.client.update_customer(self.params) self.assertEqual(isinstance(response, dict), True)
class TestProductMethods(BaseUnitTest): '''A class for testing product related methods on RestClient class''' # Get the directory where this file is located dir_path = os.path.dirname(os.path.realpath(__file__)) # Set the resources directory where id files and test image are located resources = os.path.join(dir_path, "resources") # If the resources directory doesn't exist, raise an error if not os.path.exists(resources): raise OSError("Resources directory not found.") # Add category.txt to the resources path category_file = os.path.join(resources, "category.txt") # Add product.txt to the resources path product_file = os.path.join(resources, "product.txt") # The paths for the test images images = [ os.path.join(resources, "test.jpg"), os.path.join(resources, "test2.jpg"), os.path.join(resources, "test3.jpg") ] # Loop images and check that every one of them is found for image in images: if not os.path.isfile(image): raise OSError("Image file '" + image + "' not found.") def get_category_ids(self): '''A function to get category ids from a text file.''' categories = [] # Check if the category file is found and get the categories try: fh = open(self.category_file, "r") # Get lines to list lines = fh.read().splitlines() # Loop lines for line in lines: categories.append(line) fh.close() except FileNotFoundError: raise FileNotFoundError("Category file not found.") return categories def get_product_id(self): '''A function to get product id from a text file.''' product_id = "" try: fh = open(self.product_file, "r") product_id = fh.read() fh.close() except FileNotFoundError: raise FileNotFoundError("Product file not found.") return product_id def setUp(self): self.client = RestClient( os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) self.params = { "query": {}, "param1": "", "param2": "" } def test_001_add_category_invalid_object(self): self.params["object"] = None with self.assertRaises(TypeError) as e: response = self.client.add_category(self.params) def test_002_add_category_no_root_category_id(self): category = CategoryCreate() self.params["object"] = category with self.assertRaises(ValueError) as e: response = self.client.add_category(self.params) def test_003_add_category(self): # Get categories categories = self.client.get_categories(self.params) # Get the id of the root category in the shop self.params["param1"] = categories[0]["categoryId"] category = CategoryCreate() self.params["object"] = category response = self.client.add_category(self.params) self.assertEqual(isinstance(response, dict), True) category_id = response["categoryId"] # Try to write the category id to the category file try: fh = open(self.category_file, "w") fh.write(category_id) fh.close() except IOError: print("Category file couldn't be created.") def test_004_add_category_2(self): # Wait for 1 second before creating a new category time.sleep(1) # Get categories categories = self.client.get_categories(self.params) # Get the id of the root category in the shop self.params["param1"] = categories[0]["categoryId"] category = CategoryCreate() self.params["object"] = category response = self.client.add_category(self.params) self.assertEqual(isinstance(response, dict), True) category_id = response["categoryId"] # Try to write the category id to the category file try: fh = open(self.category_file, "a") fh.write("\n" + category_id) fh.close() except IOError: print("Category file couldn't be opened.") def test_005_add_category_3(self): # Wait for 1 second before creating a new category time.sleep(1) # Get categories categories = self.client.get_categories(self.params) # Get the id of the root category in the shop self.params["param1"] = categories[0]["categoryId"] category = CategoryCreate() self.params["object"] = category response = self.client.add_category(self.params) self.assertEqual(isinstance(response, dict), True) category_id = response["categoryId"] # Try to write the category id to the category file try: fh = open(self.category_file, "a") fh.write("\n" + category_id) fh.close() except IOError: print("Category file couldn't be opened.") def test_006_add_product_no_object(self): with self.assertRaises(RuntimeError) as e: response = self.client.add_product(self.params) def test_007_add_product_invalid_object(self): self.params["object"] = None with self.assertRaises(TypeError) as e: response = self.client.add_product(self.params) def test_008_add_product_no_product_number(self): product = ProductCreate() self.params["object"] = product with self.assertRaises(ValueError) as e: response = self.client.add_product(self.params) def test_009_add_product(self): unique_id = str(uuid.uuid4()) product = ProductCreate() product.productNumber = unique_id product.name = unique_id self.params["object"] = product response = self.client.add_product(self.params) self.assertEqual(isinstance(response, dict), True) product_id = response["productId"] # Try to write the product id to the product file try: fh = open(self.product_file, "w") fh.write(product_id) fh.close() except IOError: print("Product file couldn't be created.") def test_010_get_shop_info(self): shop_info = self.client.get_shop_info(self.params) self.assertEqual(isinstance(shop_info, dict), True) def test_011_get_categories(self): categories = self.client.get_categories(self.params) self.assertEqual(isinstance(categories, list), True) def test_012_get_category_no_id(self): with self.assertRaises(ValueError) as e: category = self.client.get_category(self.params) def test_013_get_category_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: category = self.client.get_category(self.params) def test_014_get_category_correct_id(self): # Get all categories categories = self.client.get_categories(self.params) # Set category id to be the id of the first category self.params["param1"] = categories[0]["categoryId"] category = self.client.get_category(self.params) self.assertEqual(isinstance(category, dict), True) def test_015_get_currencies(self): currencies = self.client.get_currencies(self.params) self.assertEqual(isinstance(currencies, dict), True) def test_016_get_locales(self): locales = self.client.get_locales(self.params) self.assertEqual(isinstance(locales, dict), True) def test_017_get_products(self): # Set currency just to test currency setter self.client.currency = "GBP" products = self.client.get_products(self.params) self.assertEqual(isinstance(products, dict), True) def test_018_get_product_no_id(self): with self.assertRaises(ValueError) as e: product = self.client.get_product(self.params) def test_019_get_product_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: product = self.client.get_product(self.params) def test_020_get_product_correct_id(self): product_id = self.get_product_id() self.params["param1"] = product_id product = self.client.get_product(self.params) self.assertEqual(isinstance(product, dict), True) def test_021_get_product_variations_no_id(self): with self.assertRaises(ValueError) as e: variations = self.client.get_product_variations(self.params) def test_022_get_product_variations_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: variations = self.client.get_product_variations(self.params) @skip_test def test_023_get_product_variations_correct_id(self): # Here the product id is hard-coded, because there # isn't a way to get a product with variations self.params["param1"] = "5A4B8CC7-B4F1-236C-5497-0A2810120FFD" variations = self.client.get_product_variations(self.params) self.assertEqual(isinstance(variations, dict), True) def test_024_upload_product_image_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.upload_product_image(self.params) def test_025_upload_product_image_no_data(self): product_id = self.get_product_id() self.params["param1"] = product_id with self.assertRaises(RuntimeError) as e: response = self.client.upload_product_image(self.params) def test_026_upload_product_image_1(self): product_id = self.get_product_id() self.params["param1"] = product_id image = open(self.images[0], 'rb') self.params["data"] = {'image': image} response = self.client.upload_product_image(self.params) image.close() self.assertEqual(isinstance(response, dict), True) def test_027_upload_product_image_2(self): product_id = self.get_product_id() self.params["param1"] = product_id image = open(self.images[1], 'rb') self.params["data"] = {'image': image} response = self.client.upload_product_image(self.params) image.close() self.assertEqual(isinstance(response, dict), True) def test_028_upload_product_image_3(self): product_id = self.get_product_id() self.params["param1"] = product_id image = open(self.images[2], 'rb') self.params["data"] = {'image': image} response = self.client.upload_product_image(self.params) image.close() self.assertEqual(isinstance(response, dict), True) def test_029_get_product_images_no_id(self): with self.assertRaises(ValueError) as e: images = self.client.get_product_images(self.params) def test_030_get_product_images_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: images = self.client.get_product_images(self.params) def test_031_get_product_images_correct_id(self): product_id = self.get_product_id() self.params["param1"] = product_id images = self.client.get_product_images(self.params) self.assertEqual(isinstance(images, dict), True) def test_032_get_product_image_names_no_id(self): with self.assertRaises(ValueError) as e: images = self.client.get_product_image_names(self.params) def test_033_get_product_image_names_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: images = self.client.get_product_image_names(self.params) def test_034_get_product_image_names_correct_id(self): product_id = self.get_product_id() self.params["param1"] = product_id images = self.client.get_product_image_names(self.params) self.assertEqual(isinstance(images, list), True) def test_035_get_product_custom_attributes_no_id(self): with self.assertRaises(ValueError) as e: attributes = self.client.get_product_custom_attributes(self.params) def test_036_get_product_custom_attributes_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: attributes = self.client.get_product_custom_attributes(self.params) @skip_test def test_037_get_product_custom_attributes_correct_id(self): # Get products products = self.client.get_products(self.params) # Set product id to be the id of the first product self.params["param1"] = products["items"][0]["productId"] attributes = self.client.get_product_custom_attributes(self.params) self.assertEqual(isinstance(attributes, dict), True) def test_038_get_product_lowest_price_no_id(self): with self.assertRaises(ValueError) as e: lowest_price = self.client.get_product_lowest_price(self.params) def test_039_get_product_lowest_price_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: lowest_price = self.client.get_product_lowest_price(self.params) @skip_test def test_040_get_product_lowest_price_correct_id(self): # Here the product id is hard-coded, because there # isn't a way to get a product with variations self.params["param1"] = "5A4B8CC7-B4F1-236C-5497-0A2810120FFD" lowest_price = self.client.get_product_lowest_price(self.params) self.assertEqual(isinstance(lowest_price, dict), True) def test_041_search_products_no_query(self): with self.assertRaises(ValueError) as e: search_results = self.client.search_products(self.params) def test_042_search_products_empty_query(self): self.params["query"] = {"query": ""} with self.assertRaises(ValueError) as e: search_results = self.client.search_products(self.params) def test_043_search_products(self): self.params["query"] = {"query": "snap"} search_results = self.client.search_products(self.params) self.assertEqual(isinstance(search_results, dict), True) def test_044_get_shipping_methods(self): shipping_methods = self.client.get_shipping_methods(self.params) self.assertEqual(isinstance(shipping_methods, list), True) def test_045_get_shipping_method_no_id(self): with self.assertRaises(ValueError) as e: shipping_method = self.client.get_shipping_method(self.params) def test_046_get_shipping_method_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: shipping_method = self.client.get_shipping_method(self.params) def test_047_get_shipping_method_correct_id(self): # Get all shipping methods shipping_methods = self.client.get_shipping_methods(self.params) # Set shipping method id to be the id of the first shipping method self.params["param1"] = shipping_methods[0]["shippingMethodId"] shipping_method = self.client.get_shipping_method(self.params) self.assertEqual(isinstance(shipping_method, dict), True) def test_048_get_tax_classes(self): tax_classes = self.client.get_tax_classes(self.params) self.assertEqual(isinstance(tax_classes, dict), True) def test_049_get_tax_class_no_id(self): with self.assertRaises(ValueError) as e: tax_class = self.client.get_tax_class(self.params) def test_050_get_tax_class_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: tax_class = self.client.get_tax_class(self.params) def test_051_get_tax_class_correct_id(self): # Get all tax classes tax_classes = self.client.get_tax_classes(self.params) # Set tax class id to be the id of the first tax class self.params["param1"] = tax_classes["items"][0]["taxClassId"] tax_class = self.client.get_tax_class(self.params) self.assertEqual(isinstance(tax_class, dict), True) def test_052_get_tax_model(self): tax_model = self.client.get_tax_model(self.params) self.assertEqual(isinstance(tax_model, dict), True) def test_053_update_category_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.update_category(self.params) def test_054_update_category_no_object(self): categories = self.get_category_ids() self.params["param1"] = categories[0] with self.assertRaises(RuntimeError) as e: response = self.client.update_category(self.params) def test_055_update_category_invalid_object(self): categories = self.get_category_ids() self.params["param1"] = categories[0] self.params["object"] = None with self.assertRaises(TypeError) as e: response = self.client.update_category(self.params) def test_056_update_category_empty_object(self): '''When updating a category, the object must have category id and alias set. The category id must be the same that is in the url for updating category.''' categories = self.get_category_ids() self.params["param1"] = categories[0] self.params["object"] = CategoryUpdate() with self.assertRaises(ValueError) as e: response = self.client.update_category(self.params) def test_057_update_category_object_without_alias(self): categories = self.get_category_ids() self.params["param1"] = categories[0] category = CategoryUpdate() category.categoryId = categories[0] self.params["object"] = category with self.assertRaises(ValueError) as e: response = self.client.update_category(self.params) def test_058_update_category_mismatching_ids(self): '''In this test, both required variables are set, but category id has wrong value.''' categories = self.get_category_ids() # Correct category id is set to the final API path self.params["param1"] = categories[0] category = CategoryUpdate() # False category id is set to the object, and it causes mismatching ids error category.categoryId = str(uuid.uuid4()) category.alias = str(uuid.uuid4()) self.params["object"] = category with self.assertRaises(RuntimeError) as e: response = self.client.update_category(self.params) def test_059_update_category(self): categories = self.get_category_ids() self.params["param1"] = categories[0] category = CategoryUpdate() category.categoryId = categories[0] category.alias = str(uuid.uuid4()) self.params["object"] = category response = self.client.update_category(self.params) self.assertEqual(isinstance(response, dict), True) def test_060_get_subcategory_sequence_no_id(self): with self.assertRaises(ValueError) as e: sequence = self.client.get_subcategory_sequence(self.params) def test_061_get_subcategory_sequence_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: sequence = self.client.get_subcategory_sequence(self.params) def test_062_get_subcategory_sequence_correct_id(self): # Get all categories categories = self.client.get_categories(self.params) # Set category id to be the id of the first category self.params["param1"] = categories[0]["categoryId"] sequence = self.client.get_subcategory_sequence(self.params) self.assertEqual(isinstance(sequence, list), True) def test_063_update_subcategory_sequence_no_id(self): with self.assertRaises(ValueError) as e: sequence = self.client.update_subcategory_sequence(self.params) def test_064_update_subcategory_sequence_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: sequence = self.client.update_subcategory_sequence(self.params) def test_065_update_subcategory_sequence_no_list(self): # Get all categories categories = self.client.get_categories(self.params) # Set category id to be the id of the first category self.params["param1"] = categories[0]["categoryId"] with self.assertRaises(RuntimeError) as e: response = self.client.update_subcategory_sequence(self.params) def test_066_update_subcategory_sequence_false_list(self): # Get all categories categories = self.client.get_categories(self.params) # Set category id to be the id of the first category self.params["param1"] = categories[0]["categoryId"] # Get the current subcategory sequence sequence = self.client.get_subcategory_sequence(self.params) # Add random item to the list to make sure this test fails sequence.append('testing') with self.assertRaises(RuntimeError) as e: response = self.client.update_subcategory_sequence(self.params) def test_067_update_subcategory_sequence_correct_id_and_list(self): # Get all categories categories = self.client.get_categories(self.params) # Set category id to be the id of the first category self.params["param1"] = categories[0]["categoryId"] # Get the category sequence sequence = self.client.get_subcategory_sequence(self.params) # Shuffle the sequence of categories shuffle(sequence) # Get the instance of category sequence update csu = CategorySequenceUpdate() # Loop shuffled categories and add them to the object for category in sequence: csu.add(category) # Add the object to the params self.params["object"] = csu response = self.client.update_subcategory_sequence(self.params) self.assertEqual(isinstance(response, list), True) def test_068_update_product_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.update_product(self.params) def test_069_update_product_no_object(self): product_id = self.get_product_id() self.params["param1"] = product_id with self.assertRaises(RuntimeError) as e: response = self.client.update_product(self.params) def test_070_update_product_invalid_object(self): product_id = self.get_product_id() self.params["param1"] = product_id self.params["object"] = None with self.assertRaises(TypeError) as e: response = self.client.update_product(self.params) def test_071_update_product(self): product_id = self.get_product_id() self.params["param1"] = product_id product = ProductUpdate() product.price = 29.90 product.stocklevel = 5 self.params["object"] = product response = self.client.update_product(self.params) self.assertEqual(isinstance(response, dict), True) def test_072_update_product_image_sequence_no_id(self): with self.assertRaises(ValueError) as e: sequence = self.client.update_product_image_sequence(self.params) def test_073_update_product_image_sequence_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: sequence = self.client.update_product_image_sequence(self.params) def test_074_update_product_image_sequence_no_list(self): product_id = self.get_product_id() self.params["param1"] = product_id with self.assertRaises(RuntimeError) as e: response = self.client.update_product_image_sequence(self.params) def test_075_update_product_image_sequence_false_list(self): product_id = self.get_product_id() self.params["param1"] = product_id # Get the current subcategory sequence sequence = self.client.get_product_image_names(self.params) # Add random item to the list to make sure this test fails sequence.append('testing') with self.assertRaises(RuntimeError) as e: response = self.client.update_product_image_sequence(self.params) def test_076_update_product_image_sequence_correct_id_and_list(self): product_id = self.get_product_id() self.params["param1"] = product_id # Get the image sequence sequence = self.client.get_product_image_names(self.params) # Shuffle the sequence of images shuffle(sequence) # Get the instance of product slideshow sequence update pssu = ProductSlideshowSequenceUpdate() # Loop shuffled images and add them to the object for image in sequence: pssu.add(image) # Add the object to the params self.params["object"] = pssu response = self.client.update_product_image_sequence(self.params) self.assertEqual(isinstance(response, list), True) def test_077_delete_product_image_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.delete_product_image(self.params) def test_078_delete_product_image_false_id(self): self.params["param1"] = str(uuid.uuid4()) self.params["param2"] = os.path.basename(self.images[0]) with self.assertRaises(RuntimeError) as e: response = self.client.delete_product_image(self.params) def test_079_delete_product_image_no_name(self): product_id = self.get_product_id() self.params["param1"] = product_id with self.assertRaises(ValueError) as e: response = self.client.delete_product_image(self.params) def test_080_delete_product_image_false_name(self): product_id = self.get_product_id() self.params["param1"] = product_id self.params["param2"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: response = self.client.delete_product_image(self.params) def test_081_delete_product_image_1(self): product_id = self.get_product_id() self.params["param1"] = product_id self.params["param2"] = os.path.basename(self.images[0]) response = self.client.delete_product_image(self.params) self.assertEqual(isinstance(response, dict), True) def test_082_delete_product_image_2(self): product_id = self.get_product_id() self.params["param1"] = product_id self.params["param2"] = os.path.basename(self.images[1]) response = self.client.delete_product_image(self.params) self.assertEqual(isinstance(response, dict), True) def test_083_delete_product_image_3(self): product_id = self.get_product_id() self.params["param1"] = product_id self.params["param2"] = os.path.basename(self.images[2]) response = self.client.delete_product_image(self.params) self.assertEqual(isinstance(response, dict), True) def test_084_get_updated_products_no_product_property(self): with self.assertRaises(ValueError) as e: response = self.client.get_updated_products(self.params) def test_085_get_updated_products_stocklevel(self): # Get products, whose stocklevel has been lately changed self.params["param1"] = "stocklevel" response = self.client.get_updated_products(self.params) self.assertEqual(isinstance(response, dict), True) def test_086_connect_category_and_product_no_data(self): with self.assertRaises(RuntimeError) as e: response = self.client.connect_category_and_product(self.params) def test_087_connect_category_and_product_false_data(self): self.params["data"] = {'categoryId': str( uuid.uuid4()), 'productId': str(uuid.uuid4())} with self.assertRaises(RuntimeError) as e: response = self.client.connect_category_and_product(self.params) def test_088_connect_category_and_product(self): categories = self.get_category_ids() product_id = self.get_product_id() self.params["data"] = { 'categoryId': categories[0], 'productId': product_id} response = self.client.connect_category_and_product(self.params) self.assertEqual(isinstance(response, dict), True) def test_089_disconnect_product_and_category_no_data(self): with self.assertRaises(ValueError) as e: response = self.client.disconnect_product_and_category(self.params) def test_090_disconnect_product_and_category_false_data(self): self.params["query"]["categoryId"] = str(uuid.uuid4()) self.params["query"]["productId"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: response = self.client.disconnect_product_and_category(self.params) def test_091_disconnect_product_and_category(self): categories = self.get_category_ids() product_id = self.get_product_id() self.params["query"]["categoryId"] = categories[0] self.params["query"]["productId"] = product_id response = self.client.disconnect_product_and_category(self.params) self.assertEqual(isinstance(response, dict), True) def test_092_get_watched_products(self): watched_products = self.client.get_watched_products(self.params) self.assertEqual(isinstance(watched_products, dict), True) def test_093_delete_product_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.delete_product(self.params) def test_094_delete_product_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: response = self.client.delete_product(self.params) def test_095_delete_product_correct_id(self): product_id = self.get_product_id() self.params["param1"] = product_id response = self.client.delete_product(self.params) self.assertEqual(isinstance(response, dict), True) # Remove the product file containing the product id/name os.remove(self.product_file) def test_096_delete_category_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.delete_category(self.params) def test_097_delete_category_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: response = self.client.delete_category(self.params) def test_098_delete_category(self): categories = self.get_category_ids() self.params["param1"] = categories[0] response = self.client.delete_category(self.params) self.assertEqual(isinstance(response, dict), True) def test_099_delete_category(self): # Wait for 1 second before deleting a category time.sleep(1) categories = self.get_category_ids() self.params["param1"] = categories[1] response = self.client.delete_category(self.params) self.assertEqual(isinstance(response, dict), True) def test_100_delete_category(self): # Wait for 1 second before deleting a category time.sleep(1) categories = self.get_category_ids() self.params["param1"] = categories[2] response = self.client.delete_category(self.params) self.assertEqual(isinstance(response, dict), True) # Remove the category file containing the category ids os.remove(self.category_file) def tearDown(self): pass
class TestScriptTagMethods(BaseUnitTest): '''A class for testing script tag related methods on RestClient class''' def setUp(self): self.client = RestClient(os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) self.params = {"query": {}, "param1": "", "param2": ""} def test_001_add_script_tag_no_object(self): with self.assertRaises(RuntimeError) as e: response = self.client.add_script_tag(self.params) def test_002_add_script_tag_invalid_object(self): self.params["object"] = None with self.assertRaises(TypeError) as e: response = self.client.add_script_tag(self.params) def test_003_add_script_tag_no_script_url(self): script_tag = ScriptTagCreate() self.params["object"] = script_tag with self.assertRaises(ValueError) as e: response = self.client.add_script_tag(self.params) def test_004_add_script_tag_false_url(self): script_tag = ScriptTagCreate() with self.assertRaises(ValueError) as e: script_tag.scriptUrl = "foobar" def test_005_add_script_tag(self): script_tag = ScriptTagCreate() script_tag.scriptUrl = "https://code.jquery.com/jquery-3.3.1.min.js" self.params["object"] = script_tag response = self.client.add_script_tag(self.params) self.assertEqual(isinstance(response, dict), True) def test_006_get_script_tags(self): script_tags = self.client.get_script_tags(self.params) self.assertEqual(isinstance(script_tags, dict), True) def test_007_delete_script_tag_no_id(self): with self.assertRaises(ValueError) as e: response = self.client.delete_script_tag(self.params) def test_008_delete_script_tag_false_id(self): self.params["param1"] = str(uuid.uuid4()) with self.assertRaises(RuntimeError) as e: response = self.client.delete_script_tag(self.params) def test_009_delete_script_tag(self): script_tags = self.client.get_script_tags() script_tag_id = script_tags["items"][0]["scriptTagId"] self.params["param1"] = script_tag_id response = self.client.delete_script_tag(self.params) self.assertEqual(isinstance(response, dict), True)
class TestCartsOrdersAndOrdersMethods(BaseUnitTest): '''A class for testing order related methods on RestClient class''' # filename for saving cart credential cart_file = "cart_credential.csv" # filename for saving couponLineItemId coupon_line_file = "cart_coupon_line.txt" # filename for saving lineItemId product_line_file = "cart_product_line_item_id.txt" # filename for saving order id order_id_file = "order_id.txt" # Code for a coupon coupun_code = 'TEST-CODE-ABC123' def add_cart_credential(self, params): '''This function adds cart token and cart id to given params from csv file.''' content = self.get_resource(self.cart_file) ids = content.split(";") params.update({ 'param1': ids[0], 'headers': {'X-ePages-Cart-Token': ids[1]} }) return params def save_cart_credential(self, response): '''Function to save cart token and cart_id to csv-file''' content = "%s;%s" % ( response['X-ePages-Cart-Token'], response['cartId']) self.save_resource(self.cart_file, content) def get_products(self): products = self.client.get_products({ "query": {}, "param1": "", "param2": "" }) return products['items'] def setUp(self): self.client = RestClient( os.environ["EPAGES_API_URL"], os.environ["EPAGES_API_TOKEN"]) self.params = { "query": {}, "param1": "", "param2": "" } def test_0001_create_empty_cart(self): # Create a cart without products # Create a new cart cart = CartCreate() self.params["object"] = cart response = self.client.add_cart(self.params) self.assertEqual(isinstance(response, dict), True) # Save cartId credentials to csv file self.save_cart_credential(response) def test_0002_get_cart(self): # Returns a specific cart from a shop # set credential of cart self.params = self.add_cart_credential(self.params) response = self.client.get_cart(self.params) self.assertEqual(isinstance(response, dict), True) def test_0003_add_product_line(self): # Creates a product line item in a cart. # set credential of cart self.params = self.add_cart_credential(self.params) # Get one product product_id = self.get_products()[0]['productId'] # Crate product order line line = ProductLineItemCreate() line.productId = product_id line.quantity = 1 self.params["object"] = line response = self.client.add_cart_line_item(self.params) self.assertEqual(isinstance(response, dict), True) # We'll need lineItemId for updating and removing a product from the cart self.save_resource( self.product_line_file, response['lineItemContainer']['productLineItems'][0]['lineItemId'].strip()) def test_0004_update_product_line(self): # Update quantity of product line line = ProductLineItemUpdate() line.quantity = 5 # set credential of cart self.params = self.add_cart_credential(self.params) self.params["param2"] = self.get_resource(self.product_line_file) self.params["object"] = line response = self.client.update_cart_line_item(self.params) self.assertEqual(isinstance(response, dict), True) def test_0005_add_coupon_to_cart(self): # Applies a coupon code on a cart # set credential of cart self.params = self.add_cart_credential(self.params) # x-www-form-urlencoded self.params["data"] = {'code': self.coupun_code} response = self.client.add_coupon(self.params) self.assertEqual(isinstance(response, dict), True) self.save_resource( self.coupon_line_file, response['lineItemContainer']['couponLineItem']['couponLineItemId'].strip()) def test_0006_delete_coupon_from_cart(self): # Deletes a coupon from a cart # set credential of cart self.params = self.add_cart_credential(self.params) self.params["param2"] = self.get_resource(self.coupon_line_file) # x-www-form-urlencoded self.params["data"] = {'code': self.coupun_code} response = self.client.delete_coupon(self.params) self.assertEqual(isinstance(response, dict), True) def test_0007_delete_product_line(self): # Removes a product line item from a cart. # set credentials of the cart self.params = self.add_cart_credential(self.params) self.params["param2"] = self.get_resource(self.product_line_file) response = self.client.delete_cart_line_item(self.params) self.assertEqual(isinstance(response, dict), True) def test_0008_add_billing_address(self): # Modifies the billing address for a cart. billing_address = Address() billing_address.firstName = "Äijö" billing_address.lastName = "Äälinen" billing_address.street = "Pellavatehtaankatu 19" billing_address.zipCode = "33210" billing_address.city = "Tampere" billing_address.country = "FI" billing_address.emailAddress = "*****@*****.**" # set credentials of the cart self.params = self.add_cart_credential(self.params) self.params["object"] = billing_address response = self.client.update_billing_address(self.params) self.assertEqual(isinstance(response, dict), True) def test_0009_delete_billing_address(self): # set credentials of the cart self.params = self.add_cart_credential(self.params) response = self.client.delete_billing_address(self.params) self.assertEqual(isinstance(response, dict), True) def test_0010_add_shipping_address(self): # Modifies the shipping address for a cart. shipping_address = Address() shipping_address.firstName = "Лев" shipping_address.lastName = "Толстой" shipping_address.street = "Pellavatehtaankatu 19" shipping_address.zipCode = "33210" shipping_address.city = "Tampere" shipping_address.country = "FI" shipping_address.emailAddress = "*****@*****.**" shipping_address.gender = "MALE" shipping_address.jobTitle = "writer" # set credentials of the cart self.params = self.add_cart_credential(self.params) self.params["object"] = shipping_address response = self.client.update_shipping_address(self.params) self.assertEqual(isinstance(response, dict), True) def test_0011_shipping_shipping_address(self): # set credentials of the cart self.params = self.add_cart_credential(self.params) response = self.client.delete_shipping_address(self.params) self.assertEqual(isinstance(response, dict), True) def test_1001_add_order(self): # create a cart cart = CartCreate() cart.currency = 'EUR' cart.locale = 'fi_fi' cart.taxType = 'NET' products = self.get_products() # Add products to the cart item1 = ProductLineItemCreate() item1.productId = products[0]['productId'] item1.quantity = 2 cart.lineItems.add(item1) item2 = ProductLineItemCreate() item2.productId = products[1]['productId'] item2.quantity = 4 cart.lineItems.add(item2) time.sleep(1) # send to a shop self.params["object"] = cart response = self.client.add_cart(self.params) self.save_cart_credential(response) basic_params = self.add_cart_credential({}) # Add required billing address buyer = Address() buyer.country = "FI" buyer.emailAddress = "*****@*****.**" # params for update billing address params = {'object': buyer} params.update(basic_params) time.sleep(1) response = self.client.update_billing_address(params) self.assertEqual(isinstance(response, dict), True) time.sleep(1) response = self.client.add_order(basic_params) self.assertEqual(isinstance(response, dict), True) self.save_resource(self.order_id_file, response['orderId']) def test_1002_get_orders(self): # Get orders # find all finnish and euro orders self.params['query'] = { 'currency': 'EUR', 'locale': 'fi_FI' } response = self.client.get_orders(self.params) self.assertEqual(isinstance(response, dict), True) def test_1003_get_the_order(self): # Get the order created before # Get order id self.params["param1"] = self.get_resource(self.order_id_file) response = self.client.get_order(self.params) self.assertEqual(isinstance(response, dict), True) def test_1004_update_order(self): # update the order created before order = OrderUpdate() order.billingAddress.emailAddress = "*****@*****.**" order.billingAddress.firstName = "David" order.billingAddress.lastName = "Mattson" order.billingAddress.lastName = "MALE" # Get order id self.params["param1"] = self.get_resource(self.order_id_file) self.params["object"] = order response = self.client.update_order(self.params) self.assertEqual(isinstance(response, dict), True) def test_1005_get_order_documents(self): # Returns finalized invoice and credit note order documents by orderId. # Get order id self.params["param1"] = self.get_resource(self.order_id_file) response = self.client.get_order_documents(self.params) self.assertEqual(isinstance(response, dict), True) def test_2001_try_add_order_without_billing_address(self): # User forget update billing address # create a cart response = self.client.add_cart(self.params) self.save_cart_credential(response) basic_params = self.add_cart_credential({}) # Billing address is missing! with self.assertRaises(RuntimeError) as e: response = self.client.add_order(basic_params) def test_2002_try_add_billing_address_without_params_object(self): # User forget add object to params # set credential of cart self.params = self.add_cart_credential(self.params) billing_address = Address() billing_address.firstName = "Test" billing_address.lastName = "Männen" billing_address.email = "*****@*****.**" billing_address.country = "FI" # billingAddress is missing! with self.assertRaises(RuntimeError) as e: response = self.client.update_shipping_address(self.params) def test_2003_try_add_billing_address_without_country(self): # Try add a billing address without country # set credential of cart self.params = self.add_cart_credential(self.params) billing_address = Address() billing_address.firstName = "Test" billing_address.lastName = "Männen" billing_address.email = "*****@*****.**" self.params['object'] = billing_address # Country not Valid (missing) with self.assertRaises(RuntimeError) as e: response = self.client.update_shipping_address(self.params) def test_2004_try_add_billing_address_without_email(self): # Try add a billing address without email # set credential of cart self.params = self.add_cart_credential(self.params) billing_address = Address() billing_address.firstName = "Test" billing_address.lastName = "Männen" billing_address.country = "GB" self.params['object'] = billing_address # Country not Valid (missing) with self.assertRaises(RuntimeError) as e: response = self.client.update_billing_address(self.params) def test_2005_add_order_twice(self): # try two times create order from a cart # Add correct billing address basic_params = self.add_cart_credential(self.params) billing_address = Address() billing_address.country = "FI" billing_address.emailAddress = "*****@*****.**" basic_params["object"] = billing_address response = self.client.update_billing_address(basic_params) response = self.client.add_order(basic_params) # Reason: Not Found with self.assertRaises(RuntimeError) as e: response = self.client.add_order(basic_params)
from epages_client.dataobjects.category_sequence_update import CategorySequenceUpdate from pprint import pprint import json import uuid import os import requests from random import shuffle # Get api url and token from environment variables api_url = os.environ.get("EPAGES_API_URL") api_token = os.environ.get("EPAGES_API_TOKEN") if api_url is None or api_token is None: quit("Environment variables not set.") client = RestClient(api_url, api_token) #client.commands() #client.currency = "GBP" #client.get_categories() #payload = {'categoryId': '5A498829-7618-ACCC-E387-0A281012346F', 'productId': '5A61A5D5-C7C0-0D9B-F10F-0A2810110CA3'} headers = {} #headers["Accept"] = "application/vnd.epages.v1+json" # Won't work, works without it though # The above won't work although the response is HTTP 204 as in with product delete. With product deletion # the accept header can be the one above # headers["Accept"] = "*/*" # headers["Content-Type"] = "application/x-www-form-urlencoded"