def test_product_detail(browser, admin_user, live_server, settings): shop = get_default_shop() product = create_product("test_sku", shop, default_price=10) configuration.set(None, "shuup_product_tour_complete", True) initialize_admin_browser_test(browser, live_server, settings) url = reverse("shuup_admin:product.edit", kwargs={"pk": product.pk}) browser.visit("%s%s" % (live_server, url)) assert browser.find_by_id("id_base-sku").value == product.sku # Test product save new_sku = "some-new-sku" browser.find_by_id("id_base-sku").fill(new_sku) browser.execute_script("window.scrollTo(0,0)") click_element(browser, "button[form='product_form']") product.refresh_from_db() assert product.sku == new_sku # Test that toolbar action item is there dropdowns = browser.find_by_css(".btn.dropdown-toggle") for dropdown in dropdowns: if "Actions" in dropdown.text: dropdown.click() click_element(browser, "a[href='#%s']" % product.sku) # Make sure that the tabs is clickable in small devices browser.driver.set_window_size(480, 960) click_element(browser, "#product-images-section", header_height=320) click_element(browser, "#additional-details-section", header_height=320)
def test_register_bad_data(): get_default_shop() configuration.set(None, "api_permission_FrontUserViewSet", PermissionLevel.PUBLIC_WRITE) client = _get_client() # no username or email register_data = { "password": "******" } response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "username and/or email is required" in response.content.decode("utf-8") # invalid email register_data = { "email": "invalidemail", "password": "******" } response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "Enter a valid email address" in response.content.decode("utf-8") # no password register_data = { "email": "*****@*****.**", } response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "password" in json.loads(response.content.decode("utf-8"))
def test_product_create(browser, admin_user, live_server, settings): activate("en") shop = get_default_shop() get_default_product_type() get_default_sales_unit() get_default_tax_class() configuration.set(None, "shuup_product_tour_complete", True) object_created.connect(_add_custom_product_created_message, sender=Product, dispatch_uid="object_created_signal_test") initialize_admin_browser_test(browser, live_server, settings) browser.driver.set_window_size(1920, 1080) url = reverse("shuup_admin:shop_product.new") browser.visit("%s%s" % (live_server, url)) sku = "testsku" name = "Some product name" price_value = 10 short_description = "short but gold" browser.fill("base-sku", sku) browser.fill("base-name__en", name) browser.fill("base-short_description__en", short_description) browser.fill("shop%s-default_price_value" % shop.pk, price_value) configuration.set(None, "shuup_category_tour_complete", True) _add_primary_category(browser, shop) _add_additional_category(browser, shop) click_element(browser, "button[form='product_form']") wait_until_appeared(browser, "div[class='message success']") product = Product.objects.filter(sku=sku).first() assert product.log_entries.filter(identifier=OBJECT_CREATED_LOG_IDENTIFIER).count() == 1 object_created.disconnect(sender=Product, dispatch_uid="object_created_signal_test") shop_product = product.get_shop_instance(shop) assert shop_product.categories.count() == 2
def test_add_contact_with_enabled_integration(default_shop, valid_company, valid_test_configuration): configuration.set(default_shop, MC_ENABLED, True) assert MailchimpContact.objects.count() == 0 update_or_create_contact(valid_company.__class__, valid_company) mailchimp_contact = MailchimpContact.objects.get(email=valid_company.email) assert mailchimp_contact.sent_to_mailchimp is not None
def set_configuration(): config = { "api_permission_ShopViewSet": 3, "api_permission_FrontShopProductViewSet": 3, "api_permission_PersonContactViewSet": 4, "api_permission_FrontUserViewSet": 2, "api_permission_FrontOrderViewSet": 4, "api_permission_AttributeViewSet": 5, "api_permission_TaxClassViewSet": 5, "api_permission_FrontProductViewSet": 3, "api_permission_ProductVariationVariableValueViewSet": 5, "api_permission_SalesUnitViewSet": 5, "api_permission_UserViewSet": 5, "api_permission_ShopReviewViewSet": 4, "api_permission_BasketViewSet": 2, "api_permission_CategoryViewSet": 1, "api_permission_ShipmentViewSet": 5, "api_permission_CgpPriceViewSet": 5, "api_permission_ShopProductViewSet": 3, "api_permission_ContactViewSet": 4, "api_permission_OrderViewSet": 5, "api_permission_ProductViewSet": 5, "api_permission_ProductTypeViewSet": 5, "api_permission_ProductVariationVariableViewSet": 5, "api_permission_SupplierViewSet": 5, "api_permission_ManufacturerViewSet": 5, "api_permission_ProductMediaViewSet": 5, "api_permission_ProductAttributeViewSet": 5, "api_permission_MutableAddressViewSet": 5, "api_permission_ProductPackageViewSet": 5, } for field, value in six.iteritems(config): configuration.set(None, field, value)
def test_global_configurations(): with override_settings( CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'test_global_configurations', } }): cache.init_cache() shop = get_default_shop() configuration.set(None, "key1", {"data": "test1"}) configuration.set(shop, "key2", {"data": "test2"}) # key1 from shop should come from global configuration assert configuration.get(shop, "key1").get("data") == "test1" # key2 shouldn't be in global configurations assert configuration.get(None, "key2") is None # Update global configuration configuration.set(None, "key1", {"data": "test_bump"}) assert configuration.get(shop, "key1").get("data") == "test_bump" # Override shop data for global key1 configuration.set(shop, "key1", "test_data") assert configuration.get(shop, "key1") == "test_data" # Update shop configuration for global key1 configuration.set(shop, "key1", "test_data1") assert configuration.get(shop, "key1") == "test_data1"
def toggle_all_seeing_for_user(user): if not getattr(user, "is_superuser", False): return all_seeing_key = ALL_SEEING_FORMAT % {"user_id": user.pk} is_all_seeing = configuration.get(None, all_seeing_key, False) configuration.set(None, all_seeing_key, not is_all_seeing)
def test_category_deletion(admin_user): admin = get_person_contact(admin_user) category = get_default_category() category.children.create(identifier="foo") shop_product = get_default_shop_product() shop_product.categories.add(category) shop_product.primary_category = category shop_product.save() configuration.set(None, get_all_seeing_key(admin), True) assert category.status == CategoryStatus.VISIBLE assert category.children.count() == 1 with pytest.raises(NotImplementedError): category.delete() category.soft_delete() shop_product.refresh_from_db() shop_product.product.refresh_from_db() assert shop_product.categories.count() == 0 assert shop_product.primary_category is None assert category.status == CategoryStatus.DELETED assert category.children.count() == 0 # the child category still exists assert Category.objects.all_visible(customer=admin).count() == 1 assert Category.objects.all_except_deleted().count() == 1 configuration.set(None, get_all_seeing_key(admin), False)
def test_product_available(admin_user, regular_user, available_until, visible): shop = get_default_shop() product = create_product("test-sku", shop=shop) shop_product = product.get_shop_instance(shop) regular_contact = get_person_contact(regular_user) admin_contact = get_person_contact(admin_user) shop_product.available_until = available_until shop_product.save() assert (product in Product.objects.listed(shop=shop)) == visible assert (product in Product.objects.searchable(shop=shop)) == visible assert (product in Product.objects.listed(shop=shop, customer=admin_contact)) == visible assert (product in Product.objects.searchable(shop=shop, customer=admin_contact)) == visible assert (product in Product.objects.searchable(shop=shop, customer=regular_contact)) == visible configuration.set(None, get_all_seeing_key(admin_contact), True) assert product in Product.objects.listed(shop=shop, customer=admin_contact) assert product in Product.objects.searchable(shop=shop, customer=admin_contact) configuration.set(None, get_all_seeing_key(admin_contact), False)
def test_simple_set_and_get_without_shop(): configuration.set(None, "answer", 42) assert configuration.get(None, "answer") == 42 assert configuration.get(None, "non-existing") is None configuration.set(None, "non-existing", "hello") assert configuration.get(None, "non-existing") == "hello"
def test_existing_email_without_contact_update_no_update(default_shop, valid_test_configuration): assert MailchimpContact.objects.count() == 0 configuration.set(default_shop, MC_ENABLED, True) valid_email = "*****@*****.**" MailchimpContact.objects.create(shop=default_shop, email=valid_email, sent_to_mailchimp=now()) with pytest.raises(PostIsNotAllowed): add_email_to_list(default_shop, valid_email, contact=None)
def test_register_email_api(): get_default_shop() configuration.set(None, "api_permission_FrontUserViewSet", PermissionLevel.PUBLIC_WRITE) client = _get_client() register_data = {"email": "*****@*****.**", "password": "******"} response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_201_CREATED response_json = json.loads(response.content.decode("utf-8")) assert "token" in response_json assert get_user_model().objects.count() == 1 # register with DUPLICATED email register_data = {"email": "*****@*****.**", "password": "******"} response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "User already exists" in response.content.decode("utf-8") assert get_user_model().objects.count() == 1 # register with diff email - now ok register_data = { "password": "******", "email": "*****@*****.**" } response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_201_CREATED response_json = json.loads(response.content.decode("utf-8")) assert "token" in response_json assert get_user_model().objects.count() == 2
def _handle_xtheme_save(self): svc_pk = config.get(self.shop, CONTENT_FOOTER_KEY) svc = SavedViewConfig.objects.filter(pk=svc_pk).first() theme = get_current_theme(self.shop) if not svc and theme: context = {"shop": self.shop} rendered_content = template_loader.render_to_string(content_data.FOOTER_TEMPLATE, context).strip() layout = Layout(theme, "footer-bottom") # adds the footer template layout.begin_row() layout.begin_column({"md": 12}) layout.add_plugin(SnippetsPlugin.identifier, {"in_place": rendered_content}) svc = SavedViewConfig( theme_identifier=theme.identifier, shop=self.shop, view_name=XTHEME_GLOBAL_VIEW_NAME, status=SavedViewConfigStatus.CURRENT_DRAFT ) svc.set_layout_data(layout.placeholder_name, layout) svc.save() svc.publish() config.set(self.shop, CONTENT_FOOTER_KEY, svc.pk)
def test_all_seeing_and_maintenance(rf, admin_user): shop = get_default_shop() admin_contact = get_person_contact(admin_user) do_request_and_asserts(rf, admin_contact, maintenance=False, expect_toolbar=True) assert not admin_contact.is_all_seeing configuration.set(None, get_all_seeing_key(admin_user), True) # refresh cache del admin_contact.is_all_seeing assert admin_contact.is_all_seeing assert admin_contact.user.is_superuser do_request_and_asserts(rf, admin_contact, maintenance=False, expect_all_seeing=True, expect_toolbar=True) configuration.set(None, get_all_seeing_key(admin_contact), False) # Test maintenance mode badge shop.maintenance_mode = True shop.save() do_request_and_asserts(rf, admin_contact, maintenance=True, expect_toolbar=True)
def test_register_bad_data(): get_default_shop() configuration.set(None, "api_permission_FrontUserViewSet", PermissionLevel.PUBLIC_WRITE) client = _get_client() # no username or email register_data = {"password": "******"} response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "username and/or email is required" in response.content.decode( "utf-8") # invalid email register_data = {"email": "invalidemail", "password": "******"} response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "Enter a valid email address" in response.content.decode("utf-8") # no password register_data = { "email": "*****@*****.**", } response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "password" in json.loads(response.content.decode("utf-8"))
def test_global_configurations(): with override_settings(CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'test_global_configurations', } }): cache.init_cache() shop = get_default_shop() configuration.set(None, "key1", {"data": "test1"}) configuration.set(shop, "key2", {"data": "test2"}) # key1 from shop should come from global configuration assert configuration.get(shop, "key1").get("data") == "test1" # key2 shouldn't be in global configurations assert configuration.get(None, "key2") is None # Update global configuration configuration.set(None, "key1", {"data": "test_bump"}) assert configuration.get(shop, "key1").get("data") == "test_bump" # Override shop data for global key1 configuration.set(shop, "key1", "test_data") assert configuration.get(shop, "key1") == "test_data" # Update shop configuration for global key1 configuration.set(shop, "key1", "test_data1") assert configuration.get(shop, "key1") == "test_data1"
def save(self): obj = super(ShopBaseForm, self).save() languages = set(self.cleaned_data.get("languages")) shop_languages = [(code, name) for code, name in settings.LANGUAGES if code in languages] configuration.set(obj, "languages", shop_languages) return obj
def save(self, form): if self.context.storage.get("owner_pass"): self.context.user.set_password(self.context.storage["owner_pass"]) self.context.user.save() # mark as done configuration.set(self.context.shop, "step_setup", True)
def test_product_create(browser, admin_user, live_server, settings): activate("en") shop = get_default_shop() get_default_product_type() get_default_sales_unit() get_default_tax_class() configuration.set(None, "shuup_product_tour_complete", True) object_created.connect(_add_custom_product_created_message, sender=Product, dispatch_uid="object_created_signal_test") initialize_admin_browser_test(browser, live_server, settings) url = reverse("shuup_admin:shop_product.new") browser.visit("%s%s" % (live_server, url)) sku = "testsku" name = "Some product name" price_value = 10 short_description = "short but gold" browser.fill("base-sku", sku) browser.fill("base-name__en", name) browser.fill("base-short_description__en", short_description) browser.fill("shop%s-default_price_value" % shop.pk, price_value) configuration.set(None, "shuup_category_tour_complete", True) _add_primary_category(browser, shop) _add_additional_category(browser, shop) click_element(browser, "button[form='product_form']") wait_until_appeared(browser, "div[class='message success']") product = Product.objects.filter(sku=sku).first() assert product.log_entries.filter(identifier=OBJECT_CREATED_LOG_IDENTIFIER).count() == 1 object_created.disconnect(sender=Product, dispatch_uid="object_created_signal_test") shop_product = product.get_shop_instance(shop) assert shop_product.categories.count() == 2
def test_register_api(): get_default_shop() configuration.set(None, "api_permission_FrontUserViewSet", PermissionLevel.PUBLIC_WRITE) client = _get_client() register_data = { "username": "******", "password": "******" } response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_201_CREATED response_json = json.loads(response.content.decode("utf-8")) assert "token" in response_json assert get_user_model().objects.count() == 1 # Try to register with same username register_data = { "username": "******", "password": "******" } response = client.post("/api/shuup/front/user/", content_type="application/json", data=json.dumps(register_data)) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "User already exists" in response.content.decode("utf-8")
def test_address_phase_anonymous_user(rf, allow_company_registration): shop = get_default_shop() configuration.set(shop, "allow_company_registration", allow_company_registration) request = apply_request_middleware(rf.get("/"), shop=shop) view_func = AddressesOnlyCheckoutView.as_view() resp = view_func(request, phase='addresses') assert bool('company' in resp.context_data['form'].form_defs) == allow_company_registration
def _handle_xtheme_save(self): svc_pk = config.get(self.shop, CONTENT_FOOTER_KEY) svc = SavedViewConfig.objects.filter(pk=svc_pk).first() theme = get_current_theme(self.shop) if not svc and theme: context = {"shop": self.shop} rendered_content = template_loader.render_to_string( content_data.FOOTER_TEMPLATE, context).strip() layout = Layout(theme, "footer-bottom") # adds the footer template layout.begin_row() layout.begin_column({"md": 12}) layout.add_plugin(SnippetsPlugin.identifier, {"in_place": rendered_content}) svc = SavedViewConfig(theme_identifier=theme.identifier, shop=self.shop, view_name=XTHEME_GLOBAL_VIEW_NAME, status=SavedViewConfigStatus.CURRENT_DRAFT) svc.set_layout_data(layout.placeholder_name, layout) svc.save() svc.publish() config.set(self.shop, CONTENT_FOOTER_KEY, svc.pk)
def test_company_edit_form_links_company(regular_user, allow_company_registration): get_default_shop() configuration.set(None, "allow_company_registration", allow_company_registration) person = get_person_contact(regular_user) assert not get_company_contact(regular_user) client = SmartClient() client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) data = default_company_data() data.update(default_address_data("billing")) data.update(default_address_data("shipping")) company_edit_url = reverse("shuup:company_edit") if allow_company_registration: soup = client.soup(company_edit_url) response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert get_company_contact(regular_user) else: response = client.get(company_edit_url) assert response.status_code == 404 response = client.post(company_edit_url, data) assert response.status_code == 404
def test_notify_on_company_created(regular_user, allow_company_registration): if "shuup.front.apps.customer_information" not in settings.INSTALLED_APPS: pytest.skip("shuup.front.apps.customer_information required in installed apps") if "shuup.notify" not in settings.INSTALLED_APPS: pytest.skip("shuup.notify required in installed apps") configuration.set(None, "allow_company_registration", allow_company_registration) step = Step( cond_op=StepConditionOperator.NONE, actions=[ AddNotification( { "message": {"constant": "It Works. {{ customer_email }}"}, "message_identifier": {"constant": "company_created"}, } ) ], next=StepNext.STOP, ) script = Script( event_identifier=CompanyAccountCreated.identifier, name="Test Script", enabled=True, shop=get_default_shop() ) script.set_steps([step]) script.save() assert not Notification.objects.filter(identifier="company_created").exists() assert get_person_contact(regular_user) assert not get_company_contact(regular_user) client = SmartClient() client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) company_edit_url = reverse("shuup:company_edit") if allow_company_registration: client.soup(company_edit_url) data = _default_company_data() data.update(_default_address_data("billing")) data.update(_default_address_data("shipping")) response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert get_company_contact(regular_user) assert Notification.objects.filter(identifier="company_created").count() == 1 notification = Notification.objects.filter(identifier="company_created").first() assert notification assert data["contact-email"] in notification.message # New save should not add new notifications response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert Notification.objects.filter(identifier="company_created").count() == 1 script.delete() else: response = client.get(company_edit_url) assert reverse("shuup:customer_edit") in response.url assert Notification.objects.filter(identifier="company_created").count() == 0
def post(self, request): """ Save admin menu for current user to the database """ menus = json.loads(request.POST.get('menus')) configuration.set(None, 'admin_menu_user_{}'.format(request.user.pk), menus) messages.add_message(request, messages.SUCCESS, _('Menu saved')) return HttpResponseRedirect(reverse_lazy('shuup_admin:menu.arrange'))
def test_simple_set_and_get_with_shop(): shop = get_default_shop() configuration.set(shop, "answer", 42) assert configuration.get(shop, "answer") == 42 assert configuration.get(shop, "non-existing") is None configuration.set(shop, "non-existing", "hello") assert configuration.get(shop, "non-existing") == "hello"
def test_add_contact_without_marketing_permission(default_shop, valid_company, valid_test_configuration): configuration.set(default_shop, MC_ENABLED, True) valid_company.marketing_permission = False valid_company.save() assert MailchimpContact.objects.count() == 0 update_or_create_contact(valid_company.__class__, valid_company) assert MailchimpContact.objects.count() == 0
def set_configuration(self, request, menus): configuration_object = configuration.get( None, CUSTOM_ADMIN_MENU_SUPERUSER_KEY, {} ) or {} configuration_object.update({ get_language(): menus }) configuration.set(None, CUSTOM_ADMIN_MENU_SUPERUSER_KEY, configuration_object)
def toggle_all_seeing(request): return_url = request.META["HTTP_REFERER"] if not request.user.is_superuser: return HttpResponseRedirect(return_url) all_seeing_key = "is_all_seeing:%d" % request.user.pk is_all_seeing = not configuration.get(None, all_seeing_key, False) configuration.set(None, all_seeing_key, is_all_seeing) return HttpResponseRedirect(return_url)
def form_valid(self, form): if self.name not in form.forms: return data = form.forms[self.name].cleaned_data configuration.set(self.object, SHIPPING_METHOD_REQUIRED_CONFIG_KEY, data.get("shipping_method_required", False)) configuration.set(self.object, PAYMENT_METHOD_REQUIRED_CONFIG_KEY, data.get("payment_method_required", False))
def test_product_visibility(rf, admin_user, regular_user): anon_contact = get_person_contact(AnonymousUser()) shop_product = get_default_shop_product() admin_contact = get_person_contact(admin_user) regular_contact = get_person_contact(regular_user) configuration.set(None, get_all_seeing_key(admin_contact), True) with modify( shop_product.product, deleted=True ): # NB: assigning to `product` here works because `get_shop_instance` populates `_product_cache` assert error_exists(shop_product.get_visibility_errors(customer=anon_contact), "product_deleted") assert error_exists(shop_product.get_visibility_errors(customer=admin_contact), "product_deleted") with pytest.raises(ProductNotVisibleProblem): shop_product.raise_if_not_visible(anon_contact) assert not shop_product.is_list_visible() with modify( shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_ALL, visibility=ShopProductVisibility.NOT_VISIBLE ): assert error_exists(shop_product.get_visibility_errors(customer=anon_contact), "product_not_visible") assert error_does_not_exist(shop_product.get_visibility_errors(customer=admin_contact), "product_not_visible") assert not shop_product.is_list_visible() with modify( shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN, visibility=ShopProductVisibility.ALWAYS_VISIBLE, ): assert error_exists( shop_product.get_visibility_errors(customer=anon_contact), "product_not_visible_to_anonymous" ) assert error_does_not_exist( shop_product.get_visibility_errors(customer=admin_contact), "product_not_visible_to_anonymous" ) customer_group = get_default_customer_group() grouped_user = get_user_model().objects.create_user(username=printable_gibberish(20)) grouped_contact = get_person_contact(grouped_user) with modify( shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_GROUPS, visibility=ShopProductVisibility.ALWAYS_VISIBLE, ): shop_product.visibility_groups.add(customer_group) customer_group.members.add(grouped_contact) customer_group.members.remove(get_person_contact(regular_user)) assert error_does_not_exist( shop_product.get_visibility_errors(customer=grouped_contact), "product_not_visible_to_group" ) assert error_does_not_exist( shop_product.get_visibility_errors(customer=admin_contact), "product_not_visible_to_group" ) assert error_exists( shop_product.get_visibility_errors(customer=regular_contact), "product_not_visible_to_group" ) configuration.set(None, get_all_seeing_key(admin_contact), False)
def save(self): """ Store the fields in configuration """ if not self.is_valid(): return # that simple, aham for field, value in six.iteritems(self.cleaned_data): configuration.set(None, field, value)
def test_omniscience(admin_user, regular_user): assert not get_person_contact(admin_user).is_all_seeing configuration.set(None, get_all_seeing_key(admin_user), True) assert get_person_contact(admin_user).is_all_seeing assert not get_person_contact(regular_user).is_all_seeing assert not get_person_contact(None).is_all_seeing assert not get_person_contact(AnonymousUser()).is_all_seeing assert not AnonymousContact().is_all_seeing configuration.set(None, get_all_seeing_key(admin_user), False)
def test_registration_company_multiple_shops(django_user_model, client): if "shuup.front.apps.registration" not in settings.INSTALLED_APPS: pytest.skip("shuup.front.apps.registration required in installed apps") configuration.set(None, "allow_company_registration", True) configuration.set(None, "company_registration_requires_approval", False) shop1 = Shop.objects.create(identifier="shop1", status=ShopStatus.ENABLED, domain="shop1.shuup.com") shop2 = Shop.objects.create(identifier="shop2", status=ShopStatus.ENABLED, domain="shop2.shuup.com") username = "******" % uuid.uuid4().time email = "*****@*****.**" % username with override_settings( SHUUP_REGISTRATION_REQUIRES_ACTIVATION=False, SHUUP_MANAGE_CONTACTS_PER_SHOP=True, SHUUP_ENABLE_MULTIPLE_SHOPS=True ): url = reverse("shuup:registration_register_company") client.post(url, data={ 'company-name': "Test company", 'company-name_ext': "test", 'company-tax_number': "12345", 'company-email': "*****@*****.**", 'company-phone': "123123", 'company-www': "", 'billing-street': "testa tesat", 'billing-street2': "", 'billing-postal_code': "12345", 'billing-city': "test test", 'billing-region': "", 'billing-region_code': "", 'billing-country': "FI", 'contact_person-first_name': "Test", 'contact_person-last_name': "Tester", 'contact_person-email': email, 'contact_person-phone': "123", 'user_account-username': username, 'user_account-password1': "password", 'user_account-password2': "password", }, HTTP_HOST="shop1.shuup.com") user = django_user_model.objects.get(username=username) contact = PersonContact.objects.get(user=user) company = CompanyContact.objects.get(members__in=[contact]) assert contact.in_shop(shop1) assert company.in_shop(shop1) assert contact.in_shop(shop1, only_registration=True) assert company.in_shop(shop1, only_registration=True) assert contact.registered_in(shop1) assert company.registered_in(shop1) assert not contact.in_shop(shop2) assert not company.in_shop(shop2) assert not contact.in_shop(shop2, only_registration=True) assert not company.in_shop(shop2, only_registration=True) assert not contact.registered_in(shop2) assert not company.registered_in(shop2)
def test_with_missing_list_id(default_shop, valid_test_configuration): configuration.cache.clear() configuration.set(default_shop, MC_LIST_ID, None) assert interface_test(default_shop) is False configuration.set(default_shop, MC_LIST_ID, "some-list-id") response = interface_test(default_shop) assert isinstance(response, MailChimpError) # invalid api key
def test_add_person_contact_with_name(default_shop, valid_person, valid_test_configuration): assert MailchimpContact.objects.count() == 0 update_or_create_contact(valid_person.__class__, valid_person) assert MailchimpContact.objects.count() == 0 configuration.set(default_shop, MC_ENABLED, True) update_or_create_contact(valid_person.__class__, valid_person) mailchimp_contact = MailchimpContact.objects.get(email=valid_person.email) assert mailchimp_contact.sent_to_mailchimp is not None
def test_notify_on_company_created(regular_user, allow_company_registration): if "shuup.front.apps.customer_information" not in settings.INSTALLED_APPS: pytest.skip("shuup.front.apps.customer_information required in installed apps") if "shuup.notify" not in settings.INSTALLED_APPS: pytest.skip("shuup.notify required in installed apps") configuration.set(None, "allow_company_registration", allow_company_registration) step = Step( cond_op=StepConditionOperator.NONE, actions=[ AddNotification({ "message": {"constant": "It Works. {{ customer_email }}"}, "message_identifier": {"constant": "company_created"}, }) ], next=StepNext.STOP ) script = Script( event_identifier=CompanyAccountCreated.identifier, name="Test Script", enabled=True, shop=get_default_shop()) script.set_steps([step]) script.save() assert not Notification.objects.filter(identifier="company_created").exists() assert get_person_contact(regular_user) assert not get_company_contact(regular_user) client = SmartClient() client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) company_edit_url = reverse("shuup:company_edit") if allow_company_registration: client.soup(company_edit_url) data = _default_company_data() data.update(_default_address_data("billing")) data.update(_default_address_data("shipping")) response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert get_company_contact(regular_user) assert Notification.objects.filter(identifier="company_created").count() == 1 notification = Notification.objects.filter(identifier="company_created").first() assert notification assert data["contact-email"] in notification.message # New save should not add new notifications response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert Notification.objects.filter(identifier="company_created").count() == 1 script.delete() else: response = client.get(company_edit_url) assert reverse("shuup:customer_edit") in response.url assert Notification.objects.filter(identifier="company_created").count() == 0
def set_shop_available_languages(shop, languages): available_codes = [code for code, name in settings.LANGUAGES] # validate languages for language in languages: if language not in available_codes: msg = _("{language_code} is an invalid language code").format(language_code=language) raise ValueError(msg) configuration.set(shop, FRONT_AVAILABLE_LANGUAGES_CONFIG_KEY, ",".join(languages))
def test_configuration_set_and_get(): cache.clear() shop = get_default_shop() test_conf_data = {"data": "test"} configuration.set(shop, "key", test_conf_data) # Get the configuration via configuration API assert configuration.get(shop, "key") == test_conf_data # Check that configuration is saved to database assert ConfigurationItem.objects.get(shop=shop, key="key").value == test_conf_data
def form_valid(self, form): if djangoenv.has_installed("shuup.simple_cms") or djangoenv.has_installed("shuup.xtheme"): content_form = form["content"] content_form.save() if djangoenv.has_installed("shuup.notify"): behavior_form = form["behaviors"] behavior_form.save() configuration.set(None, "wizard_content_completed", True)
def test_view_saved_columns(rf): visible_fields = sorted(["id", "name"]) configuration.set(None, "view_configuration_product_saved", True) for field in visible_fields: configuration.set(None, "view_configuration_product_%s" % field, True) listview = ProductListView() column_names = [c.id for c in sorted(listview.columns, key=lambda x: x.id)] assert len(listview.columns) == len(visible_fields) assert column_names == visible_fields
def test_view_saved_columns(rf): shop = get_default_shop() visible_fields = sorted(["shopproduct_id", "name", "select"]) configuration.set(None, "view_configuration_shopproduct_saved", True) for field in visible_fields: configuration.set(None, "view_configuration_shopproduct_%s" % field, {"active": True, "ordering": 999}) listview = ProductListView() column_names = [c.id for c in sorted(listview.columns, key=lambda x: x.id)] assert len(listview.columns) == len(visible_fields) assert column_names == visible_fields
def test_configuration_cache(): cache.clear() shop = get_default_shop() configuration.set(None, "key1", "test1") configuration.set(shop, "key2", "test2") # Shop configurations cache should be bumped assert cache.get(configuration._get_cache_key(shop)) is None configuration.get(shop, "key1") # Now shop configurations and key2 should found from cache assert cache.get(configuration._get_cache_key(shop)).get("key2") == "test2"
def run(self): for schema in self.schemata: self.objects[schema["model"]] = self.process_schema(schema) if not OrderStatus.objects.exists(): print_("Creating order statuses...", end=" ") create_default_order_statuses() print_("done.") for shop in Shop.objects.all(): if not configuration.get(shop, "languages"): configuration.set(shop, "languages", settings.LANGUAGES) print_("Initialization done.")
def form_valid(self, form): if self.name in form.forms: used_form = form[self.name] for key in used_form.fields.keys(): try: ConfigurationItem.objects.get(shop=self.object, key=key).delete() except ConfigurationItem.DoesNotExist: continue for key, value in six.iteritems(used_form.cleaned_data): configuration.set(shop=self.object, key=key, value=value)
def post(self, request, *args, **kwargs): abort = request.POST.get("abort", False) if self.request.POST.get("pane_id") == self.get_final_pane_identifier(): configuration.set(Shop.objects.first(), "setup_wizard_complete", True) if abort: return JsonResponse({"success": "true"}, status=200) form = self.get_form() if form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form)
def set_configuration(): config = { "api_permission_ShopViewSet": 3, "api_permission_TreesAdminShopProductViewSet": 5, "api_permission_TreesShopViewSet": 3, "api_permission_TreesNotificationViewSet": 4, "api_permission_FrontShopProductViewSet": 3, "api_permission_PersonContactViewSet": 4, "api_permission_TreesAdminShopViewSet": 5, "api_permission_TreesShopProductViewSet": 3, "api_permission_FrontUserViewSet": 2, "api_permission_FrontOrderViewSet": 4, "api_permission_SMSPasswordSet": 2, "api_permission_AttributeViewSet": 5, "api_permission_TaxClassViewSet": 5, "api_permission_RequestResetPassword": 5, "api_permission_TreesAdminCouponViewSet": 5, "api_permission_FrontProductViewSet": 3, "api_permission_TreesProductViewSet": 3, "api_permission_ProductVariationVariableValueViewSet": 5, "api_permission_SalesUnitViewSet": 5, "api_permission_UserViewSet": 5, "api_permission_ShopReviewViewSet": 4, "api_permission_BasketViewSet": 2, "api_permission_CategoryViewSet": 1, "api_permission_ShipmentViewSet": 5, "api_permission_TreesAnalyticsViewSet": 2, "api_permission_CgpPriceViewSet": 5, "api_permission_VerificationSender": 2, "api_permission_ShopProductViewSet": 3, "api_permission_TreesAdminOrderViewSet": 5, "api_permission_ContactViewSet": 4, "api_permission_TreesProductFavoritesViewSet": 4, "api_permission_OrderViewSet": 5, "api_permission_ProductViewSet": 5, "api_permission_CustomerVerificationViewSet": 5, "api_permission_ProductTypeViewSet": 5, "api_permission_ProductReviewViewSet": 4, "api_permission_ProductVariationVariableViewSet": 5, "api_permission_VerificationChecker": 4, "api_permission_TreesFrontOrderViewSet": 5, "api_permission_SupplierViewSet": 5, "api_permission_ManufacturerViewSet": 5, "api_permission_ProductMediaViewSet": 5, "api_permission_TreesShopFavoritesViewSet": 4, "api_permission_ProductAttributeViewSet": 5, "api_permission_TreesAdminPatientsViewSet": 5, "api_permission_MutableAddressViewSet": 5, "api_permission_ProductPackageViewSet": 5, "api_permission_TreesAdminNotificationViewSet": 5, "api_permission_TreesFAQViewSet": 1, } for field, value in six.iteritems(config): configuration.set(None, field, value)
def test_regular_user_is_blind(rf, regular_user): shop = get_default_shop() contact = get_person_contact(regular_user) do_request_and_asserts(rf, contact, maintenance=False, expect_all_seeing=False, expect_toolbar=False) # user needs to be superuser to even get a glimpse assert not contact.is_all_seeing configuration.set(None, get_all_seeing_key(contact), True) assert not contact.is_all_seeing # only superusers can be allseeing # Contact might be all-seeing in database but toolbar requires superuser do_request_and_asserts(rf, contact, maintenance=False, expect_all_seeing=False, expect_toolbar=False)
def get_default_shop(): shop = default_by_identifier(Shop) if not shop: shop = Shop.objects.create( name=DEFAULT_NAME, identifier=DEFAULT_IDENTIFIER, status=ShopStatus.ENABLED, public_name=DEFAULT_NAME ) assert str(shop) == DEFAULT_NAME if not configuration.get(shop, "languages"): configuration.set(shop, "languages", settings.LANGUAGES) return shop
def set_configuration(shop=None, category=None, data=None): if category and category.pk: configuration.set(None, _get_category_configuration_key(category), data) elif shop: configuration.set(shop, FACETED_DEFAULT_CONF_KEY, data) cache.bump_version(get_theme_cache_key(shop)) # clear active keys context_cache.bump_cache_for_item(category) if not category: from shuup.core.models import Category for cat_pk in Category.objects.all().values_list("pk", flat=True): context_cache.bump_cache_for_pk(Category, cat_pk)
def test_admin(rf, admin_user): shop = get_default_shop() configuration.set(shop, "setup_wizard_complete", True) # just visit to make sure everything is ok request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 200 categories = [CategoryFactory().pk, CategoryFactory().pk, CategoryFactory().pk] manager.save_categories(shop, categories) request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 200