from django.test import Client from django.urls import reverse from filament.models import ( EmptySpoolStock, InventoryItem, Manufacturer, OrderItem, ProductType, Variant, ) from .base import CatalogTestCase class EmptySpoolTests(CatalogTestCase): def setUp(self): self.client.force_login(self.alice) self.tab = reverse("empty_spools") self.stock = EmptySpoolStock.objects.create(owner=self.alice, quantity=1) maker = Manufacturer.objects.create(name="SUNLU") product = ProductType.objects.create(manufacturer=maker, name="PLA Matte") self.sunlu_refill = Variant.objects.create( product_type=product, color=self.red, spool_type="refill" ) self.refills = InventoryItem.objects.create( owner=self.alice, variant=self.sunlu_refill, quantity=2 ) self.mount_url = reverse("inventory_mount", args=[self.refills.pk]) def test_mount_spends_one_empty_spool_and_consumption_returns_it_with_either_action(self): self.assertContains(self.client.get(reverse("inventory")), "Auf Leerrolle aufziehen") self.assertContains(self.client.get(self.mount_url), "Eine Rolle aufziehen") for action, remaining in (("consume", 2), ("reorder", 1)): with self.subTest(action=action): self.assertRedirects( self.client.post(self.mount_url, {"quantity": remaining}), reverse("inventory") ) self.stock.refresh_from_db() self.assertEqual(self.stock.quantity, 0) mounted = InventoryItem.objects.get( owner=self.alice, variant=self.sunlu_refill, spool_type="spool" ) self.assertTrue(mounted.bambu_spool) self.assertEqual(mounted.quantity, 1) consume = reverse("inventory_consume", args=[mounted.pk]) self.assertContains( self.client.get(consume), "Eine Bambu-Lab-Leerrolle wird zurückgebucht." ) self.assertRedirects( self.client.post(consume, {"quantity": 1, "action": action}), reverse("inventory"), ) self.stock.refresh_from_db() self.assertEqual(self.stock.quantity, 1) self.assertFalse(InventoryItem.objects.filter(pk=mounted.pk).exists()) self.assertEqual( OrderItem.objects.filter(variant=self.sunlu_refill).count(), int(action == "reorder"), ) self.assertFalse(InventoryItem.objects.filter(pk=self.refills.pk).exists()) def test_mount_and_correction_validate_current_quantity_and_stay_private(self): self.assertRedirects( self.client.post(self.tab, {"quantity": 0, "expected_quantity": 1}), self.tab ) self.assertContains(self.client.get(self.mount_url), "Leerrollen erfassen") response = self.client.post(self.mount_url, {"quantity": 2}) self.assertContains(response, "Du hast keine Bambu-Lab-Leerrolle im Bestand.") self.refills.refresh_from_db() self.assertEqual(self.refills.quantity, 2) self.assertRedirects( self.client.post(self.tab, {"quantity": 10, "expected_quantity": 1}), self.tab ) self.stock.refresh_from_db() self.assertEqual(self.stock.quantity, 0) csrf = Client(enforce_csrf_checks=True) csrf.force_login(self.alice) self.assertEqual( csrf.post(self.tab, {"quantity": 1, "expected_quantity": 0}).status_code, 403 ) self.assertEqual(csrf.post(self.mount_url, {"quantity": 2}).status_code, 403) self.client.force_login(self.bob) self.assertEqual(self.client.post(self.mount_url, {"quantity": 2}).status_code, 404) self.assertRedirects( self.client.post( self.tab, {"quantity": 3, "expected_quantity": 0, "owner": self.alice.pk} ), self.tab, ) self.assertEqual(EmptySpoolStock.objects.get(owner=self.bob).quantity, 3) self.stock.refresh_from_db() self.assertEqual(self.stock.quantity, 0) def test_spool_manufacturer_can_be_corrected_and_only_bambu_spools_are_credited(self): add_url = reverse("inventory_add") self.assertRedirects( self.client.post(add_url, self.selection(self.spool) | {"spool_brand": "other"}), reverse("inventory"), ) other = InventoryItem.objects.get(variant=self.spool) self.assertFalse(other.bambu_spool) old_amount = self.stock.quantity consume = reverse("inventory_consume", args=[other.pk]) self.assertRedirects( self.client.post(consume, {"quantity": 1, "action": "consume"}), reverse("inventory") ) self.stock.refresh_from_db() self.assertEqual(self.stock.quantity, old_amount) original = InventoryItem.objects.create(owner=self.alice, variant=self.spool, quantity=1) self.assertTrue(original.bambu_spool) edit = reverse("inventory_edit", args=[original.pk]) self.assertRedirects( self.client.post(edit, {"quantity": 1, "expected_quantity": 1, "bambu_spool": "other"}), reverse("inventory"), ) self.assertFalse(InventoryItem.objects.get(variant=self.spool).bambu_spool) def test_failed_reorder_rolls_back_the_empty_spool_credit(self): mounted = InventoryItem.objects.create(owner=self.alice, variant=self.spool, quantity=1) OrderItem.objects.create(owner=self.alice, variant=self.spool, quantity=9999) url = reverse("inventory_consume", args=[mounted.pk]) self.assertRedirects(self.client.post(url, {"quantity": 1, "action": "reorder"}), url) self.stock.refresh_from_db() self.assertEqual(self.stock.quantity, 1) self.assertTrue(InventoryItem.objects.filter(pk=mounted.pk).exists())