def test_input_variations_of_string_product_list(self): mixer.blend("productdb.Product", product_id="Product A") mixer.blend("productdb.Product", product_id="Product B") mixer.blend("productdb.Product", product_id="Product C") data = { "name": "Test Product List", "description": "", "string_product_list": "Product A" } form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with semicolons is allowed data["string_product_list"] = "Product A;Product B;Product C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with line-breaks is allowed data["string_product_list"] = "Product A\nProduct B\nProduct C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with a combination is allowed data["string_product_list"] = "Product A\nProduct B;Product C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # product IDs are checked during validation, therefore an error is thrown for Product D data["string_product_list"] = "Product A\nProduct B;Product D" form = ProductListForm(data=data) assert form.is_valid() is False, "validation should fail, because Product D doesn't exist" assert "string_product_list" in form.errors assert "Product D" in str(form.errors["string_product_list"]), "Should list products that are not found"
def edit_product_list(request, product_list_id=None): pl = get_object_or_404(ProductList, id=product_list_id) if pl.update_user != request.user: messages.add_message( request, level=messages.WARNING, message="You are not allowed to change this Product List. Only the " "original Author is allowed to perform this action.", ) if request.method == "POST": form = ProductListForm(request.POST, instance=pl) if pl.update_user != request.user: messages.add_message( request, level=messages.ERROR, message="You are not allowed to change this Product List. Only the " "original Author is allowed to perform this action.", ) elif form.is_valid(): pl = form.save(commit=False) pl.update_user = request.user pl.save() return redirect(reverse("productdb:list-product_lists")) else: form = ProductListForm(instance=pl) default_back_to = reverse("productdb:detail-product_list", kwargs={"product_list_id": product_list_id}) back_to = request.GET.get("back_to") if request.GET.get("back_to") else default_back_to context = {"product_list": pl, "form": form, "back_to": back_to} return render(request, "productdb/product_list/edit-product_list.html", context=context)
def test_form(self): form = ProductListForm(data={}) assert form.is_valid() is False assert "name" in form.errors assert "description" not in form.errors, "Null/Blank values are allowed" assert "string_product_list" in form.errors data = { "name": "Test Product List", "description": "", "string_product_list": "" } form = ProductListForm(data=data) assert form.is_valid() is False assert "name" not in form.errors, "Should be allowed (can be any string)" assert "description" not in form.errors, "Null/Blank values are allowed" assert "string_product_list" in form.errors, "At least one Product is required" data = { "name": "Test Product List", "description": "", "string_product_list": "Product" } mixer.blend("productdb.Product", product_id="Product") form = ProductListForm(data=data) assert form.is_valid() is True, form.errors
def test_form(self): form = ProductListForm(data={}) v = Vendor.objects.get(id=1) assert form.is_valid() is False assert "name" in form.errors assert "description" not in form.errors, "Null/Blank values are allowed" assert "string_product_list" in form.errors data = { "name": "Test Product List", "description": "", "string_product_list": "", "vendor": "1" } form = ProductListForm(data=data) assert form.is_valid() is False assert "name" not in form.errors, "Should be allowed (can be any string)" assert "description" not in form.errors, "Null/Blank values are allowed" assert "string_product_list" in form.errors, "At least one Product is required" data = { "name": "Test Product List", "description": "", "string_product_list": "Product", "vendor": "1" } models.Product.objects.create(product_id="Product", vendor=v) form = ProductListForm(data=data) assert form.is_valid() is True, form.errors
def add_product_list(request): if request.method == "POST": form = ProductListForm(request.POST) if form.is_valid(): pl = form.save(commit=False) pl.update_user = request.user pl.save() return redirect(reverse("productdb:list-product_lists")) else: form = ProductListForm() context = { "form": form, "back_to": request.GET.get("back_to") if request.GET.get("back_to") else reverse("productdb:list-product_lists"), } return render(request, "productdb/product_list/add-product_list.html", context=context)
def add_product_list(request): if request.method == "POST": form = ProductListForm(request.POST) if form.is_valid(): pl = form.save(commit=False) pl.update_user = request.user pl.save() return redirect(reverse("productdb:list-product_lists")) else: form = ProductListForm() context = { "form": form, "back_to": request.GET.get("back_to") if request.GET.get("back_to") else reverse("productdb:list-product_lists") } return render(request, "productdb/product_list/add-product_list.html", context=context)
def edit_product_list(request, product_list_id=None): pl = get_object_or_404(ProductList, id=product_list_id) if pl.update_user != request.user: messages.add_message( request, level=messages.WARNING, message="You are not allowed to change this Product List. Only the " "original Author is allowed to perform this action." ) if request.method == "POST": form = ProductListForm(request.POST, instance=pl) if pl.update_user != request.user: messages.add_message( request, level=messages.ERROR, message="You are not allowed to change this Product List. Only the " "original Author is allowed to perform this action." ) elif form.is_valid(): pl = form.save(commit=False) pl.update_user = request.user pl.save() return redirect(reverse("productdb:list-product_lists")) else: form = ProductListForm(instance=pl) default_back_to = reverse("productdb:detail-product_list", kwargs={"product_list_id": product_list_id}) back_to = request.GET.get("back_to") if request.GET.get("back_to") else default_back_to context = { "product_list": pl, "form": form, "back_to": back_to } return render(request, "productdb/product_list/edit-product_list.html", context=context)
def test_input_variations_of_string_product_list(self): mixer.blend("productdb.Product", product_id="Product A") mixer.blend("productdb.Product", product_id="Product B") mixer.blend("productdb.Product", product_id="Product C") data = { "name": "Test Product List", "description": "", "string_product_list": "Product A" } form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with semicolons is allowed data["string_product_list"] = "Product A;Product B;Product C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with line-breaks is allowed data["string_product_list"] = "Product A\nProduct B\nProduct C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with a combination is allowed data["string_product_list"] = "Product A\nProduct B;Product C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # product IDs are checked during validation, therefore an error is thrown for Product D data["string_product_list"] = "Product A\nProduct B;Product D" form = ProductListForm(data=data) assert form.is_valid( ) is False, "validation should fail, because Product D doesn't exist" assert "string_product_list" in form.errors assert "Product D" in str(form.errors["string_product_list"] ), "Should list products that are not found"
def test_input_variations_of_string_product_list(self): v = Vendor.objects.get(name__contains="Cisco") models.Product.objects.create(product_id="Product A", vendor=v) models.Product.objects.create(product_id="Product B", vendor=v) models.Product.objects.create(product_id="Product C", vendor=v) data = { "name": "Test Product List", "description": "", "string_product_list": "Product A", "vendor": "1" } form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with semicolons is allowed data["string_product_list"] = "Product A;Product B;Product C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with line-breaks is allowed data["string_product_list"] = "Product A\nProduct B\nProduct C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # format with a combination is allowed data["string_product_list"] = "Product A\nProduct B;Product C" form = ProductListForm(data=data) assert form.is_valid() is True, form.errors # product IDs are checked during validation, therefore an error is thrown for Product D data["string_product_list"] = "Product A\nProduct B;Product D" form = ProductListForm(data=data) assert form.is_valid() is False, "validation should fail, because Product D doesn't exist" expected_error = "The following products are not found in the database for the vendor Cisco Systems: Product D" assert expected_error in str(form.errors["__all__"]), "Should list products that are not found"