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_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_unique_constrain_in_product_list_name(self):
        test_product_list_name = "product_list_name"
        apicall_data = {
            "product_list_name": test_product_list_name
        }
        apicall.create_product_list(self.client, test_product_list_name)

        # try to create the product again
        response = self.client.post(apiurl.PRODUCT_LIST_API_ENDPOINT, apicall_data, format='json')

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        apicall.result_contains_error(self, STRING_UNIQUE_FIELD_REQUIRED,
                                      "product_list_name",
                                      response.content.decode("utf-8"))

        apicall.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_valid_byname_api_call(self):
        test_product_list_name = "my_get_name_api_call_test"
        apicall.create_product_list(self.client, test_product_list_name)

        # call getbyname
        valid_apicall = {
            "product_list_name": test_product_list_name
        }
        response = self.client.post(apiurl.PRODUCT_LIST_BY_NAME_API_ENDPOINT, valid_apicall)

        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_list_name":"%s".*' % re.escape(test_product_list_name))
        apicall.clean_db(self.client)
    def test_valid_byname_api_call_with_content_type_json(self):
        test_product_list_name = "my_get_name_api_call_test"
        apicall.create_product_list(self.client, test_product_list_name)

        # call byname
        valid_apicall = {
            "product_list_name": test_product_list_name
        }
        response = self.client.post(apiurl.PRODUCT_LIST_BY_NAME_API_ENDPOINT,
                                    json.dumps(valid_apicall),
                                    content_type="application/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"),
                         '.*"id":.*"product_list_name":"%s".*' % re.escape(test_product_list_name))
    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_api_anonymous_product_list_read_access(self):
        # create test product
        test_product_list_name = "product_list_name"
        product_list = apicalls.create_product_list(self.client, test_product_list_name)

        # create new client to force unauthenticated access
        client = APIClient()
        response = client.get(apiurl.PRODUCT_LIST_DETAIL_API_ENDPOINT % product_list['id'], product_list)

        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,.*')