def test_to_xml(self):
     shop = ShopFactory()
     feed = models.Feed(shop)
     feed_el = feed.to_xml()
     self.assertEqual(list(el.tag for el in feed_el), ["shop"])
     self.assertElementsEquals(feed_el[0], shop.to_xml())
     self.assertEqual(feed_el.tag, "yml_catalog")
     self.assertEqual(feed_el.get("date"), feed._date)
Exemplo n.º 2
0
 def test_post_change_without_confirm_change(self):
     shop = ShopFactory(name="bob")
     data = {"name": "sally"}
     response = self.client.post(f"/admin/market/shop/{shop.id}/change/", data)
     # Redirects to changelist
     self.assertEqual(response.status_code, 302)
     self.assertEqual(response.url, "/admin/market/shop/")
     # Shop has changed
     shop.refresh_from_db()
     self.assertEqual(shop.name, "sally")
    def test_cannot_confirm_for_modelform_with_clean_overridden_if_validation_fails(
        self, ):
        shop = ShopFactory()
        data = {
            "shop": shop.id,
            "currency": "USD",
            "total": "222",
            "date": str(timezone.now().date()),
            "timestamp_0": str(timezone.now().date()),
            "timestamp_1": str(timezone.now().time()),
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_checkout_add"), data)

        # Should not have been added yet
        self.assertEqual(Checkout.objects.count(), 0)

        # Should not have been added yet
        self.assertEqual(Checkout.objects.count(), 0)

        # Should show form with error and not confirmation page
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/checkout/change_form.html",
            "admin/market/change_form.html",
            "admin/change_form.html",
        ]
        self.assertEqual(response.template_name, expected_templates)
        self.assertEqual(Checkout.objects.count(), 0)
        self.assertIn("error", str(response.rendered_content))
        self.assertIn("Invalid Total 222", str(response.rendered_content))
        # Should still ask for confirmation
        self.assertIn(CONFIRM_ADD, response.rendered_content)
 def test_to_dict(self):
     shop = ShopFactory()
     feed = models.Feed(shop)
     feed_dict = feed.to_dict()
     self.assertEqual(sorted(list(feed_dict.keys())), ["date", "shop"])
     self.assertEqual(feed_dict["date"], feed.date)
     self.assertEqual(feed_dict["shop"], feed.shop.to_dict())
    def test_raw_id_fields_should_work(self):
        gm1 = GeneralManager.objects.create(name="gm1")
        shops = [ShopFactory(name=i) for i in range(3)]
        town = Town.objects.create(name="town")
        mall = ShoppingMall.objects.create(name="mall",
                                           general_manager=gm1,
                                           town=town)
        mall.shops.set(shops)

        self.selenium.get(self.live_server_url +
                          f"/admin/market/shoppingmall/{mall.id}/change/")
        self.assertIn(CONFIRM_CHANGE, self.selenium.page_source)

        # Make a change to trigger confirmation page
        name = self.selenium.find_element(By.NAME, "name")
        name.send_keys("New Name")

        # Set general_manager via raw_id_fields
        gm2 = GeneralManager.objects.create(name="gm2")
        general_manager = self.selenium.find_element(By.NAME,
                                                     "general_manager")
        general_manager.clear()
        general_manager.send_keys(str(gm2.id))

        self.selenium.find_element(By.NAME, "_continue").click()

        self.assertIn("Confirm", self.selenium.page_source)
        self.selenium.find_element(By.NAME, "_continue").click()

        mall.refresh_from_db()
        self.assertIn("New Name", mall.name)
        self.assertEqual(gm2, mall.general_manager)
    def test_should_have_hidden_formsets(self):
        # Not having formsets would cause a `ManagementForm tampered with` issue
        gm = GeneralManager.objects.create(name="gm")
        shops = [ShopFactory(name=i) for i in range(3)]
        town = Town.objects.create(name="town")
        mall = ShoppingMall.objects.create(name="mall",
                                           general_manager=gm,
                                           town=town)
        mall.shops.set(shops)

        self.selenium.get(self.live_server_url +
                          f"/admin/market/shoppingmall/{mall.id}/change/")
        self.assertIn(CONFIRM_CHANGE, self.selenium.page_source)

        # Make a change to trigger confirmation page
        name = self.selenium.find_element(By.NAME, "name")
        name.send_keys("This is New Name")

        self.selenium.find_element(By.NAME, "_continue").click()

        self.assertIn("Confirm", self.selenium.page_source)
        hidden_form = self.selenium.find_element(By.ID, "hidden-form")
        hidden_form.find_element(By.NAME, "ShoppingMall_shops-TOTAL_FORMS")

        self.selenium.find_element(By.NAME, "_continue").click()

        mall.refresh_from_db()
        self.assertIn("New Name", mall.name)
Exemplo n.º 7
0
    def test_post_add_with_confirm_add(self):
        item = ItemFactory()
        shop = ShopFactory()
        data = {
            "shop": shop.id,
            "item": item.id,
            "quantity": 5,
            "_confirm_add": True,
            "_continue": True,
        }
        response = self.client.post(reverse("admin:market_inventory_add"), data)
        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/inventory/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        form_data = {"shop": str(shop.id), "item": str(item.id), "quantity": str(5)}
        self._assertSimpleFieldFormHtml(
            rendered_content=response.rendered_content, fields=form_data
        )
        self._assertSubmitHtml(
            rendered_content=response.rendered_content, save_action="_continue"
        )

        # Should not have been added yet
        self.assertEqual(Inventory.objects.count(), 0)
    def test_should_have_saved_inline_changes(self):
        gm = GeneralManager.objects.create(name="gm")
        town = Town.objects.create(name="town")
        mall = ShoppingMall.objects.create(name="mall",
                                           general_manager=gm,
                                           town=town)

        shops = [ShopFactory(name=i) for i in range(3)]

        self.selenium.get(self.live_server_url +
                          f"/admin/market/shoppingmall/{mall.id}/change/")
        self.assertIn(CONFIRM_CHANGE, self.selenium.page_source)

        # Make a change to trigger confirmation page
        name = self.selenium.find_element(By.NAME, "name")
        name.send_keys("New Name")

        # Change shops via inline form
        select_shop = Select(
            self.selenium.find_element(By.NAME, "ShoppingMall_shops-0-shop"))
        select_shop.select_by_value(str(shops[2].id))

        self.selenium.find_element(By.NAME, "_continue").click()

        self.assertIn("Confirm", self.selenium.page_source)

        hidden_form = self.selenium.find_element(By.ID, "hidden-form")
        hidden_form.find_element(By.NAME, "ShoppingMall_shops-TOTAL_FORMS")
        self.selenium.find_element(By.NAME, "_continue").click()

        mall.refresh_from_db()
        self.assertIn("New Name", mall.name)
        self.assertIn(shops[2], mall.shops.all())
    def test_m2m_field_post_change_with_confirm_change_multiple_selected(self):
        shops = [ShopFactory() for i in range(10)]
        shopping_mall = ShoppingMall.objects.create(name="My Mall")
        shopping_mall.shops.set(shops)
        # Currently ShoppingMall configured with confirmation_fields = ['name']
        data = {
            "name": "Not My Mall",
            "shops": [1, 2, 3],
            "id": shopping_mall.id,
            "_confirm_change": True,
            "csrfmiddlewaretoken": "fake token",
            "_save": True,
        }
        response = self.client.post(
            f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data)
        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/shoppingmall/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        # Hasn't changed item yet
        shopping_mall.refresh_from_db()
        self.assertEqual(shopping_mall.name, "My Mall")
    def test_cannot_confirm_for_models_with_validator_on_model_field_if_validator_fails(
        self, ):
        # ItemSale.currency has a validator on it
        shop = ShopFactory()
        item = ItemFactory()
        InventoryFactory(shop=shop, item=item, quantity=10)
        transaction = TransactionFactory(shop=shop)
        data = {
            "transaction": transaction.id,
            "item": item.id,
            "quantity": 1,
            "currency": "FAKE",
            "total": 10.00,
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_itemsale_add"), data)

        # Should show form with error and not confirmation page
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/itemsale/change_form.html",
            "admin/market/change_form.html",
            "admin/change_form.html",
        ]
        self.assertEqual(response.template_name, expected_templates)
        self.assertEqual(ItemSale.objects.count(), 0)
        self.assertIn("error", str(response.rendered_content))
        self.assertIn("Invalid Currency", str(response.rendered_content))
        # Should still ask for confirmation
        self.assertIn(CONFIRM_ADD, response.rendered_content)
Exemplo n.º 11
0
 def test_from_xml(self, p):
     shop = ShopFactory()
     feed = models.Feed(shop)
     feed_el = feed.to_xml()
     p.return_value = shop
     parsed_feed = models.Feed.from_xml(feed_el)
     self.assertEqual(p.call_count, 1)
     self.assertEqual(feed.to_dict(), parsed_feed.to_dict())
    def test_post_add_without_confirm_add_m2m(self):
        shops = [ShopFactory() for i in range(3)]

        data = {"name": "name", "shops": [s.id for s in shops]}
        response = self.client.post(reverse("admin:market_shoppingmall_add"),
                                    data)
        # Redirects to changelist and is added
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/admin/market/shoppingmall/")
        self.assertEqual(ShoppingMall.objects.count(), 1)
        self.assertEqual(ShoppingMall.objects.all().first().shops.count(), 3)
    def test_m2m_field_post_change_with_confirm_change(self):
        shops = [ShopFactory() for i in range(10)]
        shopping_mall = ShoppingMall.objects.create(name="My Mall")
        shopping_mall.shops.set(shops)
        # Currently ShoppingMall configured with confirmation_fields = ['name']
        data = {
            "name": "Not My Mall",
            "shops": [1, 2],
            "id": shopping_mall.id,
            "_confirm_change": True,
            "csrfmiddlewaretoken": "fake token",
            "_continue": True,
        }
        response = self.client.post(
            f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data)
        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/shoppingmall/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        # Should show two lists for the m2m current and modified values
        self.assertEqual(response.rendered_content.count("<ul>"), 2)

        self._assertManyToManyFormHtml(
            rendered_content=response.rendered_content,
            options=shops,
            selected_ids=data["shops"],
        )
        self._assertSubmitHtml(rendered_content=response.rendered_content,
                               save_action="_continue")

        # Hasn't changed item yet
        shopping_mall.refresh_from_db()
        self.assertEqual(shopping_mall.name, "My Mall")

        # Selecting to "Yes, I'm sure" on the confirmation page
        # Would post to the same endpoint
        del data["_confirm_change"]
        response = self.client.post(
            f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data)
        # will show the change page for this shopping_mall
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response.url,
            f"/admin/market/shoppingmall/{shopping_mall.id}/change/")
        # Should not be the confirmation page, we already confirmed change
        self.assertNotEqual(response.templates, expected_templates)
        self.assertEqual(ShoppingMall.objects.count(), 1)
        self.assertEqual(ShoppingMall.objects.all().first().shops.count(), 2)
    def test_cannot_confirm_for_models_with_clean_overridden_if_clean_fails(
            self):
        shop = ShopFactory()
        item = ItemFactory()
        InventoryFactory(shop=shop, item=item, quantity=1)
        transaction = TransactionFactory(shop=shop)
        # Asking to buy more than the shop has in stock
        data = {
            "transaction": transaction.id,
            "item": item.id,
            "quantity": 9,
            "currency": "USD",
            "total": 10.00,
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_itemsale_add"), data)

        # Should not have been added yet
        self.assertEqual(ItemSale.objects.count(), 0)

        # Ensure it shows the form and not the confirmation page
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/itemsale/change_form.html",
            "admin/market/change_form.html",
            "admin/change_form.html",
        ]
        self.assertEqual(response.template_name, expected_templates)
        self.assertTrue("error" in str(response.content))
        self.assertTrue("Shop does not have enough of the item stocked" in str(
            response.content))

        # Should still be asking for confirmation
        self.assertIn(CONFIRM_ADD, response.rendered_content)

        # Fix the issue by buying only what shop has in stock
        data["quantity"] = 1
        # _confirm_add would still be in the POST data
        response = self.client.post(reverse("admin:market_itemsale_add"), data)

        # Should show confirmation page
        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/itemsale/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        self._assertSubmitHtml(rendered_content=response.rendered_content)
Exemplo n.º 15
0
    def test_no_add_permissions(self):
        user = User.objects.create_user(username="******", is_staff=True)
        self.client.force_login(user)
        item = ItemFactory()
        shop = ShopFactory()
        data = {"shop": shop.id, "item": item.id, "quantity": 5, "_confirm_add": True}
        response = self.client.post(reverse("admin:market_inventory_add"), data)
        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 403)
        self.assertTrue(isinstance(response, HttpResponseForbidden))

        # Should not have been added
        self.assertEqual(Inventory.objects.count(), 0)
    def test_post_change_without_confirm_change_m2m_value(self):
        # make the m2m the confirmation_field
        ShoppingMallAdmin.confirmation_fields = ["shops"]
        shops = [ShopFactory() for i in range(3)]
        shopping_mall = ShoppingMall.objects.create(name="name")
        shopping_mall.shops.set(shops)
        assert shopping_mall.shops.count() == 3

        data = {"name": "name", "id": str(shopping_mall.id), "shops": ["1"]}
        response = self.client.post(
            f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data)
        # Redirects to changelist
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/admin/market/shoppingmall/")
        # Shop has changed
        shopping_mall.refresh_from_db()
        self.assertEqual(shopping_mall.shops.count(), 1)
    def test_should_respect_get_inlines(self):
        # New in Django 3.0
        django_version = pkg_resources.get_distribution(
            "Django").parsed_version
        if django_version.major < 3:
            pytest.skip(
                "get_inlines() introducted in Django 3.0, and is not in this version"
            )

        shoppingmall_admin.ShoppingMallAdmin.inlines = []
        shoppingmall_admin.ShoppingMallAdmin.get_inlines = (
            lambda self, request, obj=None: [shoppingmall_admin.ShopInline])

        gm = GeneralManager.objects.create(name="gm")
        town = Town.objects.create(name="town")
        mall = ShoppingMall.objects.create(name="mall",
                                           general_manager=gm,
                                           town=town)

        shops = [ShopFactory(name=i) for i in range(3)]

        self.selenium.get(self.live_server_url +
                          f"/admin/market/shoppingmall/{mall.id}/change/")
        self.assertIn(CONFIRM_CHANGE, self.selenium.page_source)

        # Make a change to trigger confirmation page
        name = self.selenium.find_element(By.NAME, "name")
        name.send_keys("New Name")

        # Change shops via inline form
        select_shop = Select(
            self.selenium.find_element(By.NAME, "ShoppingMall_shops-0-shop"))
        select_shop.select_by_value(str(shops[2].id))

        self.selenium.find_element(By.NAME, "_continue").click()

        self.assertIn("Confirm", self.selenium.page_source)

        hidden_form = self.selenium.find_element(By.ID, "hidden-form")
        hidden_form.find_element(By.NAME, "ShoppingMall_shops-TOTAL_FORMS")
        self.selenium.find_element(By.NAME, "_continue").click()

        mall.refresh_from_db()
        self.assertIn("New Name", mall.name)
        self.assertIn(shops[2], mall.shops.all())
    def test_can_confirm_for_models_with_clean_overridden(self):
        shop = ShopFactory()
        item = ItemFactory()
        InventoryFactory(shop=shop, item=item, quantity=10)
        transaction = TransactionFactory(shop=shop)
        data = {
            "transaction": transaction.id,
            "item": item.id,
            "quantity": 9,
            "currency": "USD",
            "total": 10.00,
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_itemsale_add"), data)

        # Should not have been added yet
        self.assertEqual(ItemSale.objects.count(), 0)

        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/itemsale/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        self._assertSubmitHtml(rendered_content=response.rendered_content)

        # Confirmation page would not have the _confirm_add sent on submit
        del data["_confirm_add"]
        # Selecting to "Yes, I'm sure" on the confirmation page
        # Would post to the same endpoint
        response = self.client.post(reverse("admin:market_itemsale_add"), data)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/admin/market/itemsale/")
        self.assertEqual(ItemSale.objects.count(), 1)

        # Ensure that the date and timestamp saved correctly
        item_sale = ItemSale.objects.first()
        self.assertEqual(item_sale.transaction, transaction)
        self.assertEqual(item_sale.item, item)
        self.assertEqual(item_sale.currency, "USD")
    def test_can_confirm_for_modelform_with_clean_field_and_clean_overridden(
            self):
        shop = ShopFactory()
        data = {
            "shop": shop.id,
            "currency": "USD",
            "total": 10.00,
            "date": str(timezone.now().date()),
            "timestamp_0": str(timezone.now().date()),
            "timestamp_1": str(timezone.now().time()),
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_checkout_add"), data)

        # Should not have been added yet
        self.assertEqual(Checkout.objects.count(), 0)

        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/checkout/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        self._assertSubmitHtml(rendered_content=response.rendered_content)

        # Confirmation page would not have the _confirm_add sent on submit
        del data["_confirm_add"]
        # Selecting to "Yes, I'm sure" on the confirmation page
        # Would post to the same endpoint
        response = self.client.post(reverse("admin:market_checkout_add"), data)
        print(response.content)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/admin/market/checkout/")
        self.assertEqual(Checkout.objects.count(), 1)

        # Ensure that the date and timestamp saved correctly
        checkout = Checkout.objects.first()
        self.assertEqual(checkout.shop, shop)
        self.assertEqual(checkout.total, 10.00)
        self.assertEqual(checkout.currency, "USD")
Exemplo n.º 20
0
    def test_confirm_add_of_datetime_and_field(self):
        shop = ShopFactory()
        expected_date = timezone.now().date()
        expected_timestamp = timezone.now()
        data = {
            "date": str(expected_date),
            "timestamp_0": str(expected_timestamp.date()),
            "timestamp_1": str(expected_timestamp.time()),
            "currency": "USD",
            "shop": shop.id,
            "total": 0,
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_transaction_add"), data)

        # Should not have been added yet
        self.assertEqual(Transaction.objects.count(), 0)

        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/transaction/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        self._assertSubmitHtml(rendered_content=response.rendered_content)

        # Confirmation page would not have the _confirm_add sent on submit
        del data["_confirm_add"]
        # Selecting to "Yes, I'm sure" on the confirmation page
        # Would post to the same endpoint
        response = self.client.post(reverse("admin:market_transaction_add"), data)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/admin/market/transaction/")
        self.assertEqual(Transaction.objects.count(), 1)

        # Ensure that the date and timestamp saved correctly
        transaction = Transaction.objects.first()
        self.assertEqual(transaction.date, expected_date)
        self.assertEqual(transaction.timestamp, expected_timestamp)
    def test_post_add_with_confirm_add_m2m(self):
        ShoppingMallAdmin.confirmation_fields = ["shops"]
        shops = [ShopFactory() for i in range(3)]

        data = {
            "name": "name",
            "shops": [s.id for s in shops],
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_shoppingmall_add"),
                                    data)

        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 200)
        expected_templates = [
            "admin/market/shoppingmall/change_confirmation.html",
            "admin/market/change_confirmation.html",
            "admin/change_confirmation.html",
        ]
        self.assertEqual(response.template_name, expected_templates)

        self._assertManyToManyFormHtml(
            rendered_content=response.rendered_content,
            options=shops,
            selected_ids=data["shops"],
        )
        self._assertSubmitHtml(rendered_content=response.rendered_content)

        # Should not have been added yet
        self.assertEqual(ShoppingMall.objects.count(), 0)

        # Confirmation page would not have the _confirm_add sent on submit
        del data["_confirm_add"]
        # Selecting to "Yes, I'm sure" on the confirmation page
        # Would post to the same endpoint
        response = self.client.post(reverse("admin:market_shoppingmall_add"),
                                    data)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/admin/market/shoppingmall/")
        self.assertEqual(ShoppingMall.objects.count(), 1)
        self.assertEqual(ShoppingMall.objects.all().first().shops.count(), 3)
Exemplo n.º 22
0
    def test_confirmation_fields_set_with_confirm_add(self):
        self.assertEqual(InventoryAdmin.confirmation_fields, ["quantity"])

        item = ItemFactory()
        shop = ShopFactory()

        # Don't set quantity - let it default
        data = {"shop": shop.id, "item": item.id, "_confirm_add": True}
        response = self.client.post(reverse("admin:market_inventory_add"), data)
        # No confirmation needed
        self.assertEqual(response.status_code, 302)

        # Should have been added
        self.assertEqual(Inventory.objects.count(), 1)
        new_inventory = Inventory.objects.all().first()
        self.assertEqual(new_inventory.shop, shop)
        self.assertEqual(new_inventory.item, item)
        self.assertEqual(
            new_inventory.quantity, Inventory._meta.get_field("quantity").default
        )
Exemplo n.º 23
0
    def test_form_without_files_should_not_use_cache(self):
        cache.delete_many(CACHE_KEYS.values())
        shop = ShopFactory()
        # Click "Save And Continue"
        data = {
            "id": shop.id,
            "name": "name",
            "_confirm_change": True,
            "_continue": True,
        }
        response = self.client.post(f"/admin/market/shop/{shop.id}/change/", data=data)

        # Should be shown confirmation page
        self._assertSubmitHtml(
            rendered_content=response.rendered_content, save_action="_continue"
        )

        # Should not have set cache since not multipart form
        for key in CACHE_KEYS.values():
            self.assertIsNone(cache.get(key))
Exemplo n.º 24
0
    def test_handles_to_field_not_allowed(self):
        item = ItemFactory()
        shop = ShopFactory()
        data = {
            "shop": shop.id,
            "item": item.id,
            "quantity": 5,
            "_confirm_add": True,
            TO_FIELD_VAR: "shop",
        }
        response = self.client.post(reverse("admin:market_inventory_add"), data)
        # Ensure not redirected (confirmation page does not redirect)
        self.assertEqual(response.status_code, 400)
        self.assertTrue(isinstance(response, HttpResponseBadRequest))
        self.assertEqual(response.reason_phrase, "Bad Request")
        self.assertEqual(
            response.context.get("exception_value"),
            "The field shop cannot be referenced.",
        )

        # Should not have been added
        self.assertEqual(Inventory.objects.count(), 0)
Exemplo n.º 25
0
    def test_if_confirmation_fields_in_readonly_should_not_trigger_confirmation(
            self):
        gm = GeneralManager.objects.create(name="gm")
        shops = [ShopFactory() for i in range(3)]
        town = Town.objects.create(name="town")
        mall = ShoppingMall.objects.create(name="mall",
                                           general_manager=gm,
                                           town=town)
        mall.shops.set(shops)

        # new values
        gm2 = GeneralManager.objects.create(name="gm2")
        town2 = Town.objects.create(name="town2")

        data = {
            "id": mall.id,
            "name": "name",
            "general_manager": gm2.id,
            "shops": [1],
            "town": town2.id,
            "_confirm_change": True,
            "_continue": True,
        }
        response = self.client.post(
            f"/admin/market/shoppingmall/{mall.id}/change/", data=data)
        # Should not be shown confirmation page
        # SInce we used "Save and Continue", should show change page
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url,
                         f"/admin/market/shoppingmall/{mall.id}/change/")

        # Should have saved the non excluded fields
        mall.refresh_from_db()
        for shop in shops:
            self.assertIn(shop, mall.shops.all())
        self.assertEqual(mall.name, "mall")
        # Should have saved other fields
        self.assertEqual(mall.town, town2)
        self.assertEqual(mall.general_manager, gm2)
Exemplo n.º 26
0
    def test_confirmation_fields_set_with_confirm_change(self):
        self.assertEqual(InventoryAdmin.confirmation_fields, ["quantity"])

        inventory = InventoryFactory()
        another_shop = ShopFactory()
        data = {
            "quantity": inventory.quantity,
            "id": inventory.id,
            "item": inventory.item.id,
            "shop": another_shop.id,
            "_confirm_change": True,
            "csrfmiddlewaretoken": "fake token",
        }
        response = self.client.post(
            f"/admin/market/inventory/{inventory.id}/change/", data
        )

        # Should not have shown confirmation page since shop did not change
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, reverse("admin:market_inventory_changelist"))
        # Should have updated inventory
        inventory.refresh_from_db()
        self.assertEqual(inventory.shop, another_shop)
    def test_relation_change(self):
        gm = GeneralManager.objects.create(name="gm")
        shops = [ShopFactory() for i in range(3)]
        town = Town.objects.create(name="town")
        mall = ShoppingMall.objects.create(name="mall",
                                           general_manager=gm,
                                           town=town)
        mall.shops.set(shops)

        # new values
        gm2 = GeneralManager.objects.create(name="gm2")
        shops2 = [ShopFactory() for i in range(3)]
        town2 = Town.objects.create(name="town2")

        # Load the Change ShoppingMall Page
        ShoppingMallAdmin.confirm_change = True
        response = self.client.get(
            f"/admin/market/shoppingmall/{mall.id}/change/")

        # Should be asked for confirmation
        self.assertTrue(response.context_data.get("confirm_change"))
        self.assertIn("_confirm_change", response.rendered_content)

        # Click "Save"
        data = {
            "id": mall.id,
            "name": "name",
            "shops": [s.id for s in shops2],
            "general_manager": gm2.id,
            "town": town2.id,
            "_confirm_change": True,
            "_continue": True,
        }
        response = self.client.post(
            f"/admin/market/shoppingmall/{mall.id}/change/", data=data)

        # Should be shown confirmation page
        self._assertManyToManyFormHtml(
            rendered_content=response.rendered_content,
            options=shops,
            selected_ids=data["shops"],
        )
        self._assertSubmitHtml(rendered_content=response.rendered_content,
                               save_action="_continue")

        # Should not have cached the unsaved obj
        cached_item = cache.get(CACHE_KEYS["object"])
        self.assertIsNone(cached_item)

        # Should not have saved changes yet
        self.assertEqual(ShoppingMall.objects.count(), 1)
        mall.refresh_from_db()
        self.assertEqual(mall.name, "mall")
        self.assertEqual(mall.general_manager, gm)
        self.assertEqual(mall.town, town)
        for shop in mall.shops.all():
            self.assertIn(shop, shops)

        # Click "Yes, I'm Sure"
        confirmation_received_data = data
        del confirmation_received_data["_confirm_change"]
        confirmation_received_data[CONFIRMATION_RECEIVED] = True

        response = self.client.post(
            f"/admin/market/shoppingmall/{mall.id}/change/",
            data=confirmation_received_data,
        )

        # Should not have redirected to changelist
        self.assertEqual(response.url,
                         f"/admin/market/shoppingmall/{mall.id}/change/")

        # Should have saved obj
        self.assertEqual(ShoppingMall.objects.count(), 1)
        saved_item = ShoppingMall.objects.all().first()
        self.assertEqual(saved_item.name, data["name"])
        self.assertEqual(saved_item.general_manager, gm2)
        self.assertEqual(saved_item.town, town2)

        for shop in saved_item.shops.all():
            self.assertIn(shop, shops2)

        # Should have cleared cache
        for key in CACHE_KEYS.values():
            self.assertIsNone(cache.get(key))
Exemplo n.º 28
0
    def test_when_m2m_field_in_exclude_changes_to_field_should_not_be_saved(
            self):
        gm = GeneralManager.objects.create(name="gm")
        shops = [ShopFactory() for i in range(3)]
        town = Town.objects.create(name="town")
        mall = ShoppingMall.objects.create(name="mall",
                                           general_manager=gm,
                                           town=town)
        mall.shops.set(shops)

        # new values
        gm2 = GeneralManager.objects.create(name="gm2")
        town2 = Town.objects.create(name="town2")

        data = {
            "id": mall.id,
            "name": "name",
            "general_manager": gm2.id,
            "shops": [1],
            "town": town2.id,
            "_confirm_change": True,
            "_continue": True,
        }
        response = self.client.post(
            f"/admin/market/shoppingmall/{mall.id}/change/", data=data)
        # Should be shown confirmation page
        self._assertSubmitHtml(rendered_content=response.rendered_content,
                               save_action="_continue")

        # Should not have cached the unsaved obj
        cached_item = cache.get(CACHE_KEYS["object"])
        self.assertIsNone(cached_item)

        # Should not have saved changes yet
        self.assertEqual(ShoppingMall.objects.count(), 1)
        mall.refresh_from_db()
        self.assertEqual(mall.name, "mall")
        self.assertEqual(mall.general_manager, gm)
        self.assertEqual(mall.town, town)
        for shop in mall.shops.all():
            self.assertIn(shop, shops)

        # Click "Yes, I'm Sure"
        confirmation_received_data = data
        del confirmation_received_data["_confirm_change"]

        response = self.client.post(
            f"/admin/market/shoppingmall/{mall.id}/change/",
            data=confirmation_received_data,
        )

        # Should not have redirected to changelist
        self.assertEqual(response.url,
                         f"/admin/market/shoppingmall/{mall.id}/change/")

        # Should have saved obj
        self.assertEqual(ShoppingMall.objects.count(), 1)
        saved_item = ShoppingMall.objects.all().first()
        # should have updated fields that were in form
        self.assertEqual(saved_item.name, data["name"])
        self.assertEqual(saved_item.town, town2)
        self.assertEqual(saved_item.general_manager, gm2)
        # should have presevered the fields that are not in form (exclude)
        for shop in saved_item.shops.all():
            self.assertIn(shop, shops)

        # Should have cleared cache
        for key in CACHE_KEYS.values():
            self.assertIsNone(cache.get(key))
    def test_relations_add(self):
        gm = GeneralManager.objects.create(name="gm")
        shops = [ShopFactory() for i in range(3)]
        town = Town.objects.create(name="town")

        # Load the Add ShoppingMall Page
        ShoppingMallAdmin.confirm_add = True
        response = self.client.get(reverse("admin:market_shoppingmall_add"))

        # Should be asked for confirmation
        self.assertTrue(response.context_data.get("confirm_add"))
        self.assertIn("_confirm_add", response.rendered_content)

        # Click "Save"
        data = {
            "name": "name",
            "shops": [s.id for s in shops],
            "general_manager": gm.id,
            "town": town.id,
            "_confirm_add": True,
            "_save": True,
        }
        response = self.client.post(reverse("admin:market_shoppingmall_add"),
                                    data=data)

        # Should be shown confirmation page
        self._assertManyToManyFormHtml(
            rendered_content=response.rendered_content,
            options=shops,
            selected_ids=data["shops"],
        )
        self._assertSubmitHtml(rendered_content=response.rendered_content,
                               save_action="_save")

        # Should not have cached the unsaved object
        cached_item = cache.get(CACHE_KEYS["object"])
        self.assertIsNone(cached_item)

        # Should not have saved the object yet
        self.assertEqual(ShoppingMall.objects.count(), 0)

        # Click "Yes, I'm Sure"
        confirmation_received_data = data
        del confirmation_received_data["_confirm_add"]

        response = self.client.post(reverse("admin:market_shoppingmall_add"),
                                    data=confirmation_received_data)

        # Should have redirected to changelist
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/admin/market/shoppingmall/")

        # Should have saved object
        self.assertEqual(ShoppingMall.objects.count(), 1)
        saved_item = ShoppingMall.objects.all().first()
        self.assertEqual(saved_item.name, data["name"])
        self.assertEqual(saved_item.general_manager, gm)
        self.assertEqual(saved_item.town, town)
        for shop in saved_item.shops.all():
            self.assertIn(shop, shops)

        # Should have cleared cache
        for key in CACHE_KEYS.values():
            self.assertIsNone(cache.get(key))