def test_admin_script_list(rf, admin_user): with override_settings(WSHOP_ENABLE_MULTIPLE_SHOPS=True): shop1 = factories.get_shop(identifier="shop-1") shop2 = factories.get_shop(identifier="shop-2") shop1.staff_members.add(admin_user) shop2.staff_members.add(admin_user) script_shop1 = Script.objects.create(shop=shop1, event_identifier="order_received", name="SHOP 1", enabled=True) script_shop2 = Script.objects.create(shop=shop2, event_identifier="order_received", name="SHOP 2", enabled=True) view = ScriptEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) set_shop(request, shop2) with pytest.raises(Http404): response = view(request, pk=script_shop1.id) response = view(request, pk=script_shop2.id) assert response.status_code == 200
def _mbv_command(shop, user, payload, method="post"): request = RequestFactory().generic(method, "/") request.user = user request.session = {} set_shop(request, shop) if method == "post": request._body = json.dumps(payload).encode("UTF-8") else: request.GET = payload mbv = MediaBrowserView.as_view() return json.loads(mbv(request).content.decode("UTF-8"))
def _check_that_staff_can_see_folder(rf, shop, user, folder, expected_files_count): request = apply_request_middleware( rf.get("/", { "action": "folder", "id": folder.id })) request.user = user request.session = {} set_shop(request, shop) view_func = MediaBrowserView.as_view() response = view_func(request) assert isinstance(response, JsonResponse) content = json.loads(response.content.decode("utf-8")) assert len(content["folder"]["files"]) == expected_files_count
def _mbv_upload(shop, user, **extra_data): content = ("42" * 42).encode("UTF-8") imuf = InMemoryUploadedFile(BytesIO(content), "file", "424242.txt", "text/plain", len(content), "UTF-8") request = RequestFactory().post( "/", dict({ "action": "upload", "file": imuf }, **extra_data)) request.user = user request.session = {} set_shop(request, shop) view = MediaBrowserView.as_view() response = view(request) return json.loads(response.content.decode("UTF-8"))
def get(self, request, *args, **kwargs): shop = Shop.objects.filter(pk=kwargs.get("pk")).first() set_shop(request, shop) messages.info(request, (_("Shop {} is now active!")).format(shop.name)) return HttpResponseRedirect(request.META.get("HTTP_REFERER", reverse("wshop_admin:home")))
def test_manufacturer_admin_multishop_shop(rf, staff_user, admin_user, superuser): with override_settings(WSHOP_ENABLE_MULTIPLE_SHOPS=True): shop1 = factories.get_shop(identifier="shop1") shop2 = factories.get_shop(identifier="shop2") shop1.staff_members.add(staff_user) shop2.staff_members.add(staff_user) assert Manufacturer.objects.count() == 0 user = admin_user if superuser else staff_user request = apply_request_middleware(rf.post( "/", data=dict(name="Manuf shop2")), user=user, shop=shop2) set_shop(request, shop2) view_func = ManufacturerEditView.as_view() response = view_func(request) assert response.status_code == 302 if superuser: assert Manufacturer.objects.first().shops.count() == 0 else: assert Manufacturer.objects.first().shops.first() == shop2 for view_class in (ManufacturerEditView, ManufacturerListView): view_instance = view_class() view_instance.request = request assert view_instance.get_queryset().count() == 1 if superuser: assert view_instance.get_queryset().first().shops.count() == 0 else: assert view_instance.get_queryset().first().shops.count() == 1 assert view_instance.get_queryset().first().shops.first( ) == shop2 request = apply_request_middleware(rf.post( "/", data=dict(name="Manuf shop1")), user=user, shop=shop1) set_shop(request, shop1) view_func = ManufacturerEditView.as_view() response = view_func(request) assert response.status_code == 302 if superuser: assert Manufacturer.objects.last().shops.count() == 0 else: assert Manufacturer.objects.last().shops.first() == shop1 for view_class in (ManufacturerEditView, ManufacturerListView): view_instance = view_class() view_instance.request = request assert view_instance.get_queryset().count() == (2 if superuser else 1) if superuser: assert view_instance.get_queryset().first().shops.count() == 0 assert view_instance.get_queryset().last().shops.count() == 0 else: assert view_instance.get_queryset().first().shops.count() == 1 assert view_instance.get_queryset().first().shops.first( ) == shop1