def test_view_shared_wishlist_detail(rf, admin_user, regular_user, is_anonymous): shop = get_default_shop() admin_person = get_person_contact(admin_user) regular_person = get_person_contact(regular_user) product = get_default_product() shop_product = product.get_shop_instance(shop) wishlist = Wishlist.objects.create(shop=shop, customer=admin_person, name='foo', privacy=WishlistPrivacy.SHARED) wishlist.products.add(shop_product) view_func = WishlistCustomerDetailView.as_view() if(is_anonymous): request = apply_request_middleware(rf.get("/")) assert request.customer.pk is None response = view_func(request, pk=wishlist.pk) assert response.status_code == 302 assert response.url.endswith(reverse('shuup:index')) else: request = apply_request_middleware(rf.get("/"), user=regular_person.user) response = view_func(request, pk=wishlist.pk) assert response.status_code == 200 assert 'customer_wishlist' in response.context_data assert not response.context_data["is_owner"] assert response.context_data['customer_wishlist'].name == wishlist.name assert response.context_data['customer_wishlist'].products.count() == 1
def test_login_as_user_errors(rf, admin_user, regular_user): get_default_shop() view_func = LoginAsUserView.as_view() request = apply_request_middleware(rf.post("/"), user=regular_user, skip_session=True) # log in as self with pytest.raises(Problem): view_func(request, pk=regular_user.pk) user = UserFactory() get_person_contact(user) # non superuser trying to login as someone else with pytest.raises(PermissionDenied): view_func(request, pk=user.pk) request = apply_request_middleware(rf.post("/"), user=admin_user) user.is_superuser = True user.save() # user is trying to login as another superuser with pytest.raises(PermissionDenied): view_func(request, pk=user.pk) user.is_superuser = False user.is_staff = True user.save() # user is trying to login as a staff user with pytest.raises(PermissionDenied): view_func(request, pk=user.pk) user.is_staff = False user.is_active = False user.save() # user is trying to login as an inactive user with pytest.raises(Problem): view_func(request, pk=user.pk)
def new(self, request, *args, **kwargs): """ Create a brand new basket object """ serializer = NewBasketSerializer(data=request.data) serializer.is_valid(True) data = serializer.validated_data self.process_request(with_basket=False, shop=data.get("shop")) basket_class = cached_load("SHUUP_BASKET_CLASS_SPEC") basket = basket_class(request._request) if "customer" in data: customer = data["customer"] else: customer = get_company_contact(request.user) or get_person_contact(request.user) orderer = data.get("orderer", get_person_contact(request.user)) # set the request basket to perform the basket command self.request.basket = basket self._handle_set_customer( request=self.request._request, basket=basket, customer=customer, orderer=orderer ) stored_basket = basket.save() response_data = { "uuid": "%s-%s" % (request.shop.pk, stored_basket.key) } response_data.update(self.get_serializer(basket).data) return Response(data=response_data, status=status.HTTP_201_CREATED)
def test_product_query(admin_user, regular_user): anon_contact = AnonymousContact() shop_product = get_default_shop_product() shop = shop_product.shop product = shop_product.product regular_contact = get_person_contact(regular_user) admin_contact = get_person_contact(admin_user) with modify(shop_product, save=True, listed=True, visible=True, visibility_limit=ProductVisibility.VISIBLE_TO_ALL ): assert Product.objects.list_visible(shop=shop, customer=anon_contact).filter(pk=product.pk).exists() with modify(shop_product, save=True, listed=False, visible=True, visibility_limit=ProductVisibility.VISIBLE_TO_ALL ): assert not Product.objects.list_visible(shop=shop, customer=anon_contact).filter(pk=product.pk).exists() assert not Product.objects.list_visible(shop=shop, customer=regular_contact).filter(pk=product.pk).exists() assert Product.objects.list_visible(shop=shop, customer=admin_contact).filter(pk=product.pk).exists() with modify(shop_product, save=True, listed=True, visible=True, visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN ): assert not Product.objects.list_visible(shop=shop, customer=anon_contact).filter(pk=product.pk).exists() assert Product.objects.list_visible(shop=shop, customer=regular_contact).filter(pk=product.pk).exists() product.soft_delete() assert not Product.objects.all_except_deleted().filter(pk=product.pk).exists()
def test_forcing_to_person_and_anonymous_contact(rf, admin_user): company_contact = get_company_contact(admin_user) assert company_contact is None shop = factories.get_default_shop() company_contact = get_company_contact_for_shop_staff(shop, admin_user) assert isinstance(company_contact, CompanyContact) assert company_contact == get_company_contact(admin_user) person_contact = get_person_contact(admin_user) assert person_contact is not None assert not person_contact.is_anonymous force_person_contact_for_user(admin_user) assert get_company_contact(admin_user) is None force_anonymous_contact_for_user(admin_user) assert get_person_contact(admin_user).is_anonymous force_person_contact_for_user(admin_user, False) assert get_company_contact(admin_user) is None # Since the person contact is still anonymous assert get_person_contact(admin_user).is_anonymous force_anonymous_contact_for_user(admin_user, False) assert company_contact == get_company_contact(admin_user) assert not get_person_contact(admin_user).is_anonymous
def test_company_contact_for_shop_staff(rf, admin_user): company_contact = get_company_contact(admin_user) assert company_contact is None shop = factories.get_default_shop() # Let's create shop for the shop staff company_contact = get_company_contact_for_shop_staff(shop, admin_user) company_contact = get_company_contact_for_shop_staff(shop, admin_user) assert company_contact is not None # Let's create second staff member to make sure all good with # creating company contact for shop staff. new_staff_user = factories.create_random_user() with pytest.raises(AssertionError): get_company_contact_for_shop_staff(shop, new_staff_user) new_staff_user.is_staff = True new_staff_user.save() with pytest.raises(AssertionError): # Since the new staff is not in shop members. The admin user # passed since he is also superuser. get_company_contact_for_shop_staff(shop, new_staff_user) shop.staff_members.add(new_staff_user) assert company_contact == get_company_contact_for_shop_staff(shop, new_staff_user) # Make sure both user has person contact linked to the company contact company_members = company_contact.members.all() assert get_person_contact(admin_user) in company_members assert get_person_contact(new_staff_user) in company_members
def test_comment_visibility(admin_user): shop = factories.get_default_shop() admin_contact = get_person_contact(admin_user) staff_user = factories.create_random_user("en", is_staff=True) staff_contact = get_person_contact(staff_user) shop.staff_members.add(staff_user) normal_user = factories.create_random_user("en") normal_contact = get_person_contact(normal_user) task_type = TaskType.objects.create(name="Request", shop=shop) task = create_task(shop, admin_contact, task_type, "my task") task.comment(admin_contact, "This is only visibile for super users", TaskCommentVisibility.ADMINS_ONLY) task.comment(staff_contact, "This is only visibile for staff only", TaskCommentVisibility.STAFF_ONLY) task.comment(normal_contact, "This is visibile for everyone", TaskCommentVisibility.PUBLIC) # admin see all comments assert task.comments.for_contact(admin_contact).count() == 3 # staff see all public + staff only assert task.comments.for_contact(staff_contact).count() == 2 # normal contact see all public assert task.comments.for_contact(normal_contact).count() == 1 # anonymous contact see all public assert task.comments.for_contact(AnonymousContact()).count() == 1
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) 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")
def test_login_as_user(rf, admin_user, regular_user): get_default_shop() view_func = LoginAsUserView.as_view() request = apply_request_middleware(rf.post("/"), user=admin_user) get_person_contact(regular_user) response = view_func(request, pk=regular_user.pk) assert response["location"] == reverse("shuup:index") assert get_user(request) == regular_user
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 _set_person(self, request): request.person = get_person_contact(request.user) if not request.person.is_active: messages.add_message(request, messages.INFO, _("Logged out since this account is inactive.")) logout(request) # Usually logout is connected to the `refresh_on_logout` # method via a signal and that already sets request.person # to anonymous, but set it explicitly too, just to be sure request.person = get_person_contact(None)
def test_company_tax_number_limitations(regular_user): get_default_shop() 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) company_edit_url = reverse("shuup:company_edit") soup = 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) # re-save should work properly response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 client.logout() # another company tries to use same tax number new_user_password = "******" new_user_username = "******" user = User.objects.create_user(new_user_username, "*****@*****.**", new_user_password) person = get_person_contact(user=user) assert not get_company_contact(user) client = SmartClient() client.login(username=new_user_username, password=new_user_password) company_edit_url = reverse("shuup:company_edit") soup = 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 == 200 # this time around, nothing was saved. assert not get_company_contact(user) # company contact yet # change tax number data["contact-tax_number"] = "111111" response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 # this time around, nothing was saved. assert get_company_contact(user) # company contact yet # go back to normal and try to get tax number approved data["contact-tax_number"] = "111110" response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 200 # this time around, nothing was saved.
def test_forcing_to_anonymous_contact(rf, admin_user): person_contact = get_person_contact(admin_user) assert person_contact is not None assert not get_person_contact(admin_user).is_anonymous company_contact = get_company_contact(admin_user) assert company_contact is None force_anonymous_contact_for_user(admin_user) assert get_person_contact(admin_user).is_anonymous force_anonymous_contact_for_user(admin_user, False) assert not get_person_contact(admin_user).is_anonymous assert get_person_contact(admin_user).user.id == admin_user.id
def test_create_order(admin_user, settings, target_customer): configure(settings) shop = factories.get_default_shop() basket = factories.get_basket() factories.create_default_order_statuses() shop_product = factories.get_default_shop_product() shop_product.default_price = TaxfulPrice(1, shop.currency) shop_product.save() client = _get_client(admin_user) # add shop product payload = { 'shop_product': shop_product.id } if target_customer == "other": target = factories.create_random_person() payload["customer_id"] = target.pk else: target = get_person_contact(admin_user) response = client.post('/api/shuup/basket/{}-{}/add/'.format(shop.pk, basket.key), payload) assert response.status_code == status.HTTP_200_OK response_data = json.loads(response.content.decode("utf-8")) assert len(response_data["items"]) == 1 response = client.post('/api/shuup/basket/{}-{}/create_order/'.format(shop.pk, basket.key), payload) assert response.status_code == status.HTTP_400_BAD_REQUEST response_data = json.loads(response.content.decode("utf-8")) assert "errors" in response_data factories.get_default_payment_method() factories.get_default_shipping_method() response = client.post('/api/shuup/basket/{}-{}/create_order/'.format(shop.pk, basket.key), payload) assert response.status_code == status.HTTP_201_CREATED response_data = json.loads(response.content.decode("utf-8")) basket.refresh_from_db() assert basket.finished order = Order.objects.get(reference_number=response_data["reference_number"]) assert order.status == OrderStatus.objects.get_default_initial() assert order.payment_status == PaymentStatus.NOT_PAID assert order.shipping_status == ShippingStatus.NOT_SHIPPED assert not order.payment_method assert not order.shipping_method assert float(order.taxful_total_price_value) == 1 assert order.customer == target assert order.orderer == get_person_contact(admin_user) assert order.creator == admin_user assert not order.billing_address assert not order.shipping_address
def test_address_ownership(admin_user): address = get_address() address.save() saved = SavedAddress(address=address) saved.owner = get_person_contact(admin_user) assert saved.get_title(), u"get_title does what it should even if there is no explicit title" saved.title = u"My favorite address" assert saved.get_title() == saved.title, u"get_title does what it should when there is an explicit title" assert six.text_type(saved) == saved.get_title(), u"str() is an alias for .get_title()" saved.full_clean() saved.save() assert SavedAddress.objects.for_owner(get_person_contact(admin_user)).filter(address=address).exists(), \ "contacts can save addresses" assert SavedAddress.objects.for_owner(None).count() == 0, "Ownerless saved addresses aren't a real thing"
def test_delete_other_persons_wishlist(rf, admin_user, regular_user): shop = get_default_shop() admin_person = get_person_contact(admin_user) person = get_person_contact(regular_user) product = get_default_product() shop_product = product.get_shop_instance(shop) wishlist = Wishlist.objects.create(shop=shop, customer=admin_person, name='foo', privacy=WishlistPrivacy.PUBLIC) wishlist.products.add(shop_product) view_func = WishlistDeleteView.as_view() request = apply_request_middleware(rf.post("/"), user=person.user) with pytest.raises(Http404): view_func(request, pk=wishlist.pk) assert Wishlist.objects.filter(pk=wishlist.pk).exists()
def test_view_private_wishlist_detail(rf, admin_user, regular_user): shop = get_default_shop() admin_person = get_person_contact(admin_user) regular_person = get_person_contact(regular_user) product = get_default_product() shop_product = product.get_shop_instance(shop) wishlist = Wishlist.objects.create(shop=shop, customer=admin_person, name='foo', privacy=WishlistPrivacy.PRIVATE) wishlist.products.add(shop_product) view_func = WishlistCustomerDetailView.as_view() request = apply_request_middleware(rf.get("/"), user=regular_person.user) with pytest.raises(Http404): view_func(request, pk=wishlist.pk)
def test_cart_delete(rf, regular_user): cart = _save_cart_with_products(rf, regular_user) request = apply_request_middleware(rf.post("/"), customer=get_person_contact(regular_user), user=regular_user) response = CartDeleteView.as_view()(request, pk=cart.pk) cart.refresh_from_db() assert response.status_code == 200 assert cart.deleted, "cart deleted successfully"
def test_category_group_visibilities(regular_user): regular_contact = get_person_contact(regular_user) silver_group = ContactGroup.objects.create(identifier="silver") diamond_group = ContactGroup.objects.create(identifier="gold") regular_contact.groups.add(silver_group) silvers = Category.objects.create( status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_GROUPS, identifier="silver_groups", name="Silvers", ) silvers.visibility_groups.add(regular_contact.get_default_group(), silver_group) diamonds = Category.objects.create( status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_GROUPS, identifier="silver_and_diamonds_groups", name="Diamonds", ) diamonds.visibility_groups.add(diamond_group) # Multiple groups for contact should not cause duplicate results assert Category.objects.all_visible(customer=regular_contact).count() == 1 regular_contact.groups.add(diamond_group) assert Category.objects.all_visible(customer=regular_contact).count() == 2
def get_form(self, form_class): contact = get_person_contact(self.request.user) form_group = FormGroup(**self.get_form_kwargs()) form_group.add_form_def("billing", AddressForm, kwargs={"instance": contact.default_billing_address}) form_group.add_form_def("shipping", AddressForm, kwargs={"instance": contact.default_shipping_address}) form_group.add_form_def("contact", PersonContactForm, kwargs={"instance": contact}) return form_group
def test_save_cart_errors(rf, regular_user): get_default_shop() request = apply_request_middleware(rf.post("/", { "title": "test" })) response = CartSaveView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert response.status_code == 403, "can't save cart as anonymous user" assert not data["ok"], "can't save cart without title" customer = get_person_contact(regular_user) request = apply_request_middleware(rf.post("/", { "title": "" }), customer=customer, user=regular_user) response = CartSaveView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert response.status_code == 400 assert not data["ok"], "can't save cart without title" request = apply_request_middleware(rf.post("/", { "title": "test" }), customer=customer, user=regular_user) response = CartSaveView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert response.status_code == 400 assert not data["ok"], "can't save an empty cart"
def test_force_views_only_for_staff(rf): shop = factories.get_default_shop() user = factories.create_random_user(is_staff=True) person_contact = get_person_contact(user) # Start forcing. There shouldn't be any changes to # request customer due calling the force functions since # those just do the redirect in case the current is user # is not shop staff. request = apply_request_middleware(rf.get("/"), user=user) assert request.customer == person_contact _call_force_view(request, force_anonymous_contact) request = apply_request_middleware(rf.get("/"), user=user) assert request.customer == person_contact _call_force_view(request, force_person_contact) request = apply_request_middleware(rf.get("/"), user=user) assert request.customer == person_contact _call_force_view(request, force_company_contact) request = apply_request_middleware(rf.get("/"), user=user) assert request.customer == person_contact assert get_company_contact(user) is None
def test_stripe_checkout_phase_with_saved_card_exception(rf, stripe_payment_processor): shop = factories.get_default_shop() user = factories.create_random_user() contact = get_person_contact(user) request = apply_request_middleware(rf.post("/"), shop=shop, customer=contact) def raise_stripe_exc(*args, **kwargs): raise stripe.StripeError("DUMMY") with mock.patch("stripe.Customer.retrieve", new=raise_stripe_exc): service = stripe_payment_processor.create_service( "stripe", shop=request.shop, tax_class=factories.get_default_tax_class(), enabled=True) StripeCustomer.objects.create(contact=contact, customer_token="123") checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() context = checkout_phase.get_context_data() assert context["stripe"] assert not context.get("stripe_customer_data") request.method = "POST" request.POST = {} checkout_phase.get_success_url = lambda: reverse("shuup_admin:home") checkout_phase.post(request) assert not checkout_phase.is_valid()
def test_cart_list(rf, regular_user): _save_cart_with_products(rf, regular_user) request = apply_request_middleware(rf.get("/"), customer=get_person_contact(regular_user), user=regular_user) response = CartListView.as_view()(request) assert response.status_code == 200 assert "carts" in response.context_data assert response.context_data["carts"].count() == 1
def test_stripe_checkout_phase_with_saved_card(rf, stripe_payment_processor): shop = factories.get_default_shop() user = factories.create_random_user() contact = get_person_contact(user) request = apply_request_middleware(rf.post("/"), shop=shop, customer=contact) def return_stripe_mock_data(*args, **kwargs): return StripedData(**CUSTOMER_MOCK_DATA) with mock.patch("stripe.Customer.retrieve", new=return_stripe_mock_data): service = stripe_payment_processor.create_service( "stripe", shop=request.shop, tax_class=factories.get_default_tax_class(), enabled=True) StripeCustomer.objects.create(contact=contact, customer_token="123") checkout_phase = StripeCheckoutPhase(request=request, service=service) assert not checkout_phase.is_valid() context = checkout_phase.get_context_data() assert context["stripe"] request.method = "POST" request.POST = { "stripeCustomer": "1234" } checkout_phase.get_success_url = lambda: reverse("shuup_admin:home") checkout_phase.post(request) assert checkout_phase.is_valid() # We should be valid now assert request.session # And things should've been saved into the session checkout_phase.process() # And this should do things to the basket assert request.basket.payment_data["stripe"]["customer"]
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_unsupplied(admin_user): shop_product = get_default_shop_product() fake_supplier = Supplier.objects.create(identifier="fake") admin_contact = get_person_contact(admin_user) with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_ALL, orderable=True): assert any(ve.code == "invalid_supplier" for ve in shop_product.get_orderability_errors(supplier=fake_supplier, customer=admin_contact, quantity=1))
def send_user_registered_notification(user, request, **kwargs): activation_url = None person_contact = get_person_contact(user) activation_key = user.registrationprofile.activation_key if hasattr(user, 'registrationprofile') else None if activation_key: activation_path = reverse('shuup:registration_activate', args=(activation_key,)) activation_url = request.build_absolute_uri(activation_path) customer = person_contact cls = RegistrationReceived email = user.email if person_contact: company = person_contact.company_memberships.first() if company: customer = company cls = CompanyRegistrationReceived email = user.email or company.email event = cls( customer=customer, customer_email=email, activation_url=activation_url, user_is_active=user.is_active, ) event.run(shop=request.shop)
def seed_source(user, shop): source = BasketishOrderSource(shop) source.status = get_initial_order_status() source.customer = get_person_contact(user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() return source
def test_cart_add_all(rf, regular_user): cart = _save_cart_with_products(rf, regular_user) request = apply_request_middleware(rf.post("/"), customer=get_person_contact(regular_user), user=regular_user) assert not request.basket.product_count, "cart is empty" response = CartAddAllProductsView.as_view()(request, pk=cart.pk) assert response.status_code == 200 assert request.basket.product_count, "products added to cart"
def test_timezone_setting(regular_user): get_default_shop() # Create a shop mw = ShuupFrontMiddleware() request = get_unprocessed_request() request.user = regular_user some_tz = ('US/Hawaii' if settings.TIME_ZONE == 'UTC' else 'UTC') person = get_person_contact(regular_user) person.timezone = some_tz person.save() original_tz = timezone.get_current_timezone_name() assert timezone.get_current_timezone_name() != some_tz mw.process_request(request) assert timezone.get_current_timezone_name() == some_tz timezone.activate(original_tz)
def test_invalid_form(admin_user, rf): shop = get_default_shop() customer = get_person_contact(admin_user) customer.save() shop2 = Shop.objects.create(identifier="test2") product = create_product("test", shop=shop) shop_product = product.get_shop_instance(shop) form = WishlistForm(data={ "name": "foo", "privacy": 0, "shop_product_id": product.id }, shop=shop2, customer=customer, shop_product_id=shop_product.id) form.full_clean() assert not form.is_valid() assert form.errors assert form.is_bound form.is_valid() # shouldn't raise
def get_context_data(self, **kwargs): context = super(StripeSavedPaymentInfoView, self).get_context_data(**kwargs) person_contact = get_person_contact(self.request.user) stripe_processor = get_stripe_processor(self.request) stripe_customer = StripeCustomer.objects.filter(contact=person_contact).first() context["stripe_customer"] = stripe_customer context["customer"] = person_contact context["stripe_processor"] = stripe_processor if stripe_customer: stripe.api_key = stripe_processor.secret_key try: customer = stripe.Customer.retrieve(stripe_customer.customer_token) context["stripe_customer_data"] = customer.to_dict() except stripe.error.StripeError: pass return context
def test_product_query_with_group_visibility(regular_user): default_group = get_default_customer_group() shop_product = get_default_shop_product() shop_product.visibility_limit = 3 shop_product.save() shop = shop_product.shop product = shop_product.product shop_product.visibility_groups.add(default_group) regular_contact = get_person_contact(regular_user) assert not Product.objects.listed( shop=shop, customer=regular_contact).filter(pk=product.pk).exists() regular_contact.groups.add(default_group) assert Product.objects.listed( shop=shop, customer=regular_contact).filter(pk=product.pk).count() == 1 shop_product.visibility_groups.add(regular_contact.get_default_group()) # Multiple visibility groups for shop product shouldn't cause duplicate matches assert Product.objects.listed( shop=shop, customer=regular_contact).filter(pk=product.pk).count() == 1
def test_wishlist_create_with_product(admin_user, rf): activate("en") shop1 = get_shop(identifier="shop1", domain="shop1", name="shop1") shop2 = get_shop(identifier="shop2", domain="shop2", name="shop2") person = get_person_contact(admin_user) product1_s1 = create_product("p1_s1", shop1, get_default_supplier()) product1_s2 = create_product("p1_s2", shop2, get_default_supplier()) product2_s1 = create_product("p2_s1", shop1, get_default_supplier()) product2_s2 = create_product("p2_s2", shop2, get_default_supplier()) shop1_product2 = product2_s1.get_shop_instance(shop1) view_func = WishlistCreateView.as_view() request = rf.post("/", {"name": "foo", "privacy": 0, "shop_product_id": shop1_product2.id}, HTTP_HOST=shop1.domain) assert_wishlist(request, shop1, person, view_func, 1) shop2_product2 = product2_s2.get_shop_instance(shop2) request = rf.post("/", {"name": "foo", "privacy": 0, "shop_product_id": shop2_product2.id}, HTTP_HOST=shop2.domain) assert_wishlist(request, shop2, person, view_func, 1)
def test_get_prices_children(rf, regular_user): shop = get_default_shop() parent = create_product("parent", shop, get_default_supplier()) child = create_product("child-no-shop") child.link_to_parent(parent) request = rf.get("/") request.shop = shop request.customer = get_person_contact(regular_user) parent.refresh_from_db() prices = parent.get_priced_children(request) assert len(prices) == 0 child_with_shop = create_product("child-shop", shop, get_default_supplier(), 10) child_with_shop.link_to_parent(parent) parent.refresh_from_db() prices = parent.get_priced_children(request) assert len(prices) == 1
def post(self, request, *args, **kwargs): stripe_processor = get_stripe_processor(request) person_contact = get_person_contact(request.user) stripe_customer = StripeCustomer.objects.filter(contact=person_contact).first() source_id = request.POST.get("source_id") if stripe_customer and source_id: stripe.api_key = stripe_processor.secret_key try: customer = stripe.Customer.retrieve(stripe_customer.customer_token) customer.sources.retrieve(source_id).delete() except stripe.error.StripeError: LOGGER.exception("Failed to delete Stripe source") messages.error(request, _("Unknown error while removing payment details.")) else: messages.success(request, _("Payment successfully removed.")) return HttpResponseRedirect(reverse("shuup:stripe_saved_payment"))
def confirm_login_allowed(self, user): """ Do not let inactive person contact user to login. """ if not get_person_contact(user).is_active: raise forms.ValidationError( self.error_messages["inactive"], code="inactive", ) if (settings.SHUUP_ENABLE_MULTIPLE_SHOPS and settings.SHUUP_MANAGE_CONTACTS_PER_SHOP): if not user.is_superuser: shop = self.request.shop if shop not in user.contact.shops.all(): raise forms.ValidationError( _("You are not allowed to log in to this shop.")) super(EmailAuthenticationForm, self).confirm_login_allowed(user) login_allowed.send(sender=type(self), request=self.request, user=user)
def save(self, **kwargs): is_new = (not self.instance.pk) old_assigned = None if not is_new: old_assigned = Task.objects.get(id=self.instance.pk).assigned_to if is_new: self.instance.creator = get_person_contact(self.request.user) self.instance.shop = get_shop(self.request) result = super(TaskForm, self).save(**kwargs) if not is_new and old_assigned != self.instance.assigned_to: self.instance.add_log_entry(_( "Changed assigment from {from_contact_name} to {to_contact_name}." ).format(**dict(from_contact_name=old_assigned, to_contact_name=self.instance.assigned_to)), kind=LogEntryKind.EDIT) return result
def delete_address(regular_user): client, contact = initialize_test(regular_user) address = get_address() address.save() sa = SavedAddress.objects.create(owner=contact, address=address) delete_url = reverse("shuup:address_book_delete", kwargs={"pk": sa.pk}) response, soup = client.response_and_soup(delete_url) assert response.status_code == 302 assert "Cannot remove address" not in soup user = User.objects.create_user('john', '*****@*****.**', 'doepassword') contact2 = get_person_contact(user) address2 = get_address() address2.save() sa2 = SavedAddress.objects.create(owner=contact2, address=address2) response, soup = client.response_and_soup(delete_url) assert response.status_code == 302 assert "Cannot remove address" in soup
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_order_and_source(admin_user, product, language, language_fallback): # create original source to tamper with contact = get_person_contact(admin_user) contact.language = language contact.save() assert contact.language == language # contact language is naive source = BasketishOrderSource(get_default_shop()) source.status = get_initial_order_status() source.billing_address = MutableAddress.objects.create(name="Original Billing") source.shipping_address = MutableAddress.objects.create(name="Original Shipping") source.customer = contact source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() source.add_line( type=OrderLineType.PRODUCT, product=product, supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) assert len(source.get_lines()) == 2 source.creator = admin_user assert not source._language # is None because it was not directly assigned assert source.language == language_fallback creator = OrderCreator() order = creator.create_order(source) assert order.language == source.language return order, source
def test_correios_pack_source(rf, admin_user): with patch.object(CorreiosWS, 'get_preco_prazo', return_value=MOCKED_SUCCESS_RESULT): pac_carrier = get_correios_carrier_2() contact = get_person_contact(admin_user) p1 = create_product(sku='p1', supplier=get_default_supplier(), width=400, depth=400, height=400, gross_weight=1250) source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=p1, supplier=get_default_supplier(), quantity=2, base_unit_price=source.create_price(10)) billing_address = get_address() shipping_address = get_address(name="My House", country='BR') shipping_address.postal_code = "89070210" source.billing_address = billing_address source.shipping_address = shipping_address source.customer = contact bc = ShippingMethod.objects.filter(carrier=pac_carrier).first().behavior_components.first() packages = bc._pack_source(source) assert len(packages) == 2 results = bc._get_correios_results(source, packages) assert len(results) == 2 # todos devem ter dado certo assert all(result.erro == 0 for result in results) assert all(result.valor > Decimal(0) for result in results) delivery_time = bc.get_delivery_time(ShippingMethod.objects.filter(carrier=pac_carrier).first(), source) assert delivery_time is not None # como os locais de entrega sao iguais, prazo iguais assert delivery_time.max_duration == delivery_time.min_duration assert delivery_time.max_duration.days > 0
def test_simple_orderability(admin_user): shop = get_default_shop() fake_supplier = Supplier.objects.create(identifier="fake") admin_contact = get_person_contact(admin_user) parent = create_product("SimpleVarParent", shop=shop, supplier=fake_supplier) children = [create_product("SimpleVarChild-%d" % x, shop=shop, supplier=fake_supplier) for x in range(10)] for child in children: child.link_to_parent(parent) sp = child.get_shop_instance(shop) # sp = ShopProduct.objects.create( # shop=shop, product=child, visibility=ShopProductVisibility.ALWAYS_VISIBLE # ) assert child.is_variation_child() assert not sp.is_list_visible() # Variation children are not list visible assert parent.mode == ProductMode.SIMPLE_VARIATION_PARENT assert not list(parent.get_all_available_combinations()) # Simple variations can't have these. shop_product = parent.get_shop_instance(shop) assert error_does_not_exist( shop_product.get_orderability_errors(supplier=fake_supplier, customer=admin_contact, quantity=1), code="no_sellable_children") first_child = children[0] child_sp = first_child.get_shop_instance(shop) child_sp.visibility = ShopProductVisibility.NOT_VISIBLE child_sp.save() assert error_does_not_exist( shop_product.get_orderability_errors(supplier=fake_supplier, customer=admin_contact, quantity=1), code="no_sellable_children") for child in children: child_sp = child.get_shop_instance(shop) child_sp.visibility = ShopProductVisibility.NOT_VISIBLE child_sp.save() assert error_exists( shop_product.get_orderability_errors(supplier=fake_supplier, customer=admin_contact, quantity=1), code="no_sellable_children")
def get_search_results(self, request, query): shop = get_shop(request) if len(query) >= 3: contact = get_person_contact(request.user) tasks = ( Task.objects .for_shop(get_shop(request)) .filter(name__icontains=query) .assigned_to(contact) .exclude(status__in=(TaskStatus.DELETED, TaskStatus.COMPLETED)) ) for task in tasks: yield SearchResult( text=force_text("{task_name} [{task_status}]".format(**dict( task_name=task.name, task_status=task.status ))), url=get_model_url(task, shop=shop), category=_("Tasks") )
def test_customer_company_member(regular_user): get_default_shop() # Create a shop mw = ShuupFrontMiddleware() request = get_unprocessed_request() request.user = regular_user person = get_person_contact(regular_user) company = create_random_company() company.members.add(person) assert get_company_contact(regular_user) == company mw.process_request(request) check_request_attribute_basics(request) assert isinstance(request.person, PersonContact) assert isinstance(request.customer, CompanyContact) company = get_company_contact(request.user) assert company and (company == request.customer)
def test_delete_own_wishlist_product(rf, regular_user): shop = get_default_shop() person = get_person_contact(regular_user) product = get_default_product() shop_product = product.get_shop_instance(shop) wishlist = Wishlist.objects.create(shop=shop, customer=person, name='foo', privacy=WishlistPrivacy.PUBLIC) wishlist.products.add(shop_product) view_func = WishlistProductDeleteView.as_view() request = apply_request_middleware(rf.post("/"), user=person.user) response = view_func(request, pk=wishlist.pk, shop_product_pk=shop_product.pk) assert response.status_code == 302 with pytest.raises(ObjectDoesNotExist): Wishlist.objects.get(pk=wishlist.pk).products.get(pk=shop_product.pk)
def __init__(self, *args, **kwargs): self.request = kwargs.pop("request") super(CompanyInformationFormGroup, self).__init__(*args, **kwargs) user = self.request.user company = get_company_contact(user) person = get_person_contact(user) address_form_class = cached_load("SHUUP_ADDRESS_MODEL_FORM") for form_name in self.address_forms: self.add_form_def(form_name, address_form_class, kwargs={ "instance": _get_default_address_for_contact( company, "default_%s_address" % form_name, person) }) self.add_form_def("contact", CompanyContactForm, kwargs={"instance": company})
def post(self, request, *args, **kwargs): status = int(request.POST.get("status", 0)) obj = self.get_object() redirect_url = reverse_lazy("shuup_admin:task.edit", kwargs=dict(pk=obj.pk)) possible_status = [ TaskStatus.COMPLETED.value, TaskStatus.IN_PROGRESS.value ] if not status or status not in possible_status: messages.error(request, _("Invalid status.")) return HttpResponseRedirect(redirect_url) if status == TaskStatus.COMPLETED.value: obj.set_completed(get_person_contact(request.user)) elif status == TaskStatus.IN_PROGRESS.value: obj.set_in_progress() obj.save() messages.success(request, _("Status changed.")) return HttpResponseRedirect(redirect_url)
def test_view_own_wishlist_detail(rf, regular_user): shop = get_default_shop() regular_person = get_person_contact(regular_user) product = get_default_product() shop_product = product.get_shop_instance(shop) wishlist = Wishlist.objects.create(shop=shop, customer=regular_person, name='foo', privacy=WishlistPrivacy.PUBLIC) wishlist.products.add(shop_product) view_func = WishlistCustomerDetailView.as_view() request = apply_request_middleware(rf.get("/"), user=regular_person.user) response = view_func(request, pk=wishlist.pk) assert response.status_code == 200 assert 'customer_wishlist' in response.context_data assert response.context_data["is_owner"] assert response.context_data['customer_wishlist'].name == wishlist.name assert response.context_data['customer_wishlist'].products.count() == 1
def form_valid(self, form): company = form["contact"].save() user = self.request.user person = get_person_contact(user) company.members.add(person) billing_address = form["billing"].save() shipping_address = form["shipping"].save() if billing_address.pk != company.default_billing_address_id: # Identity changed due to immutability company.default_billing_address = billing_address if shipping_address.pk != company.default_shipping_address_id: # Identity changed due to immutability company.default_shipping_address = shipping_address user.email = company.email user.first_name = company.name user.last_name = "" user.save() company.save() messages.success(self.request, _("Company information saved successfully.")) return redirect("shuup:company_edit")
def confirm_login_allowed(self, user): """ Do not let user with inactive person contact to login. """ if not get_person_contact(user).is_active: raise forms.ValidationError( self.error_messages['inactive'], code='inactive', ) if settings.SHUUP_ENABLE_MULTIPLE_SHOPS and settings.SHUUP_MANAGE_CONTACTS_PER_SHOP: if not user.is_superuser: shop = self.request.shop if shop not in user.contact.shops.all(): raise forms.ValidationError(_("You are not allowed to login to this shop.")) super(EmailAuthenticationForm, self).confirm_login_allowed(user) # create GDPR consent if has_installed("shuup.gdpr"): from shuup.gdpr.utils import create_user_consent_for_all_documents create_user_consent_for_all_documents(self.request.shop, user)
def test_copy_order_to_basket(admin_user, settings): configure(settings) shop = factories.get_default_shop() basket = factories.get_basket() p1 = factories.create_product("test", shop=shop, supplier=factories.get_default_supplier()) order = factories.create_order_with_product( factories.get_default_product(), factories.get_default_supplier(), 2, 10, shop=shop) factories.add_product_to_order(order, factories.get_default_supplier(), p1, 2, 5) order.customer = get_person_contact(admin_user) order.save() client = _get_client(admin_user) payload = {"order": order.pk} response = client.post( '/api/shuup/basket/{}-{}/add_from_order/'.format(shop.pk, basket.key), payload) assert response.status_code == status.HTTP_200_OK response_data = json.loads(response.content.decode("utf-8")) assert len(response_data["items"]) == 2 assert not response_data["validation_errors"] basket.refresh_from_db() assert len(basket.data["lines"]) == 2 # do it again, basket should clear first then read items payload = {"order": order.pk} response = client.post( '/api/shuup/basket/{}-{}/add_from_order/'.format(shop.pk, basket.key), payload) assert response.status_code == status.HTTP_200_OK response_data = json.loads(response.content.decode("utf-8")) assert len(response_data["items"]) == 2 assert not response_data["validation_errors"] basket.refresh_from_db() assert len(basket.data["lines"]) == 2
def add_from_order(self, request, *args, **kwargs): """ Add multiple products to the basket """ self.process_request() errors = [] order_queryset = Order.objects.filter(pk=request.data.get("order")) if self.request.basket.customer: order_queryset = order_queryset.filter( customer_id=self.request.basket.customer.id) else: order_queryset = order_queryset.filter( customer_id=get_person_contact(request.user).id) order = order_queryset.first() if not order: return Response({"error": "invalid order"}, status=status.HTTP_404_NOT_FOUND) for line in order.lines.products(): try: self._add_product(request, add_data={ "product": line.product_id, "shop": order.shop_id, "quantity": line.quantity }) except ValidationError as exc: errors.append({exc.code: exc.message}) except ProductNotOrderableProblem as exc: errors.append({"error": "{}".format(exc)}) except serializers.ValidationError as exc: errors.append({"error": str(exc)}) except Exception as exc: errors.append({"error": str(exc)}) if len(errors) > 0: return Response({"errors": errors}, status.HTTP_400_BAD_REQUEST) else: return Response(self.get_serializer(request.basket).data)
def test_save_cart_errors(rf, regular_user): get_default_shop() request = apply_request_middleware(rf.post("/", {"title": "test"})) response = CartSaveView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert response.status_code == 403, "can't save cart as anonymous user" assert not data["ok"], "can't save cart without title" customer = get_person_contact(regular_user) request = apply_request_middleware(rf.post("/", {"title": ""}), customer=customer) response = CartSaveView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert response.status_code == 400 assert not data["ok"], "can't save cart without title" request = apply_request_middleware(rf.post("/", {"title": "test"}), customer=customer) response = CartSaveView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert response.status_code == 400 assert not data["ok"], "can't save an empty cart"
def get_form(self, form_class): user = self.request.user company = get_company_contact(user) person = get_person_contact(user) form_group = FormGroup(**self.get_form_kwargs()) address_form_class = cached_load("SHUUP_ADDRESS_MODEL_FORM") form_group.add_form_def( "billing", address_form_class, kwargs={ "instance": _get_default_address_for_contact(company, "default_billing_address", person) } ) form_group.add_form_def( "shipping", address_form_class, kwargs={ "instance": _get_default_address_for_contact(company, "default_shipping_address", person) } ) form_group.add_form_def("contact", CompanyContactForm, kwargs={"instance": company}) return form_group
def test_method_phase_basic(rf, admin_user, get_method, data, method_id): activate("en") shop = get_default_shop() method = get_method(shop, price=0, name=data["name"]) method.description = data["description"] method.save() assert method.enabled view = MethodsOnlyCheckoutView.as_view() # To make method visible, basket must be available person = get_person_contact(admin_user) request = apply_request_middleware(rf.get("/")) request.shop = shop request.user = admin_user request.person = person request.customer = person basket = get_basket(request) # add product to bakset supplier = get_default_supplier() product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=50) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=2) assert basket.product_count, "basket has products" basket.save() request = apply_request_middleware(request, user=admin_user, person=person, customer=person, basket=basket) # request = apply_request_middleware(rf.get("/")) response = view(request=request, phase='methods') if hasattr(response, "render"): response.render() assert response.status_code in [200, 302] soup = BeautifulSoup(response.content) method_soup = soup.find("div", {"id": method_id}) assert data["name"] in method_soup.text assert data["description"] in method_soup.text assert soup.find("input", {"id": "%s_%s" % (method_id, method.pk)})
def form_valid(self, form): company = form["contact"].save(commit=False) is_new = not bool(company.pk) company.save() user = self.request.user person = get_person_contact(user) company.members.add(person) billing_address = form["billing"].save() shipping_address = form["shipping"].save() if billing_address.pk != company.default_billing_address_id: # Identity changed due to immutability company.default_billing_address = billing_address if shipping_address.pk != company.default_shipping_address_id: # Identity changed due to immutability company.default_shipping_address = shipping_address user.email = company.email user.first_name = company.name user.last_name = "" user.save() message = _("Company information saved successfully.") # If company registration requires activation, # company will be created as inactive. if is_new and configuration.get( None, "company_registration_requires_approval"): company.is_active = False message = _( "Company information saved successfully. " "Please follow the instructions sent to your email address.") company.save() if is_new: user_registered.send(sender=self.__class__, user=self.request.user, request=self.request) CompanyAccountCreated(contact=company, customer_email=company.email).run() messages.success(self.request, message) return redirect("shuup:company_edit")
def test_person_contact_create_form(rf, admin_user): user = get_user_model().objects.create_user( username=printable_gibberish(), first_name=printable_gibberish(), last_name=printable_gibberish(), ) test_first_name = printable_gibberish() test_last_name = printable_gibberish() request = apply_request_middleware(rf.post("/"), user=admin_user) contact_base_form = PersonContactBaseForm(request=request, data={ "first_name": test_first_name, "last_name": test_last_name, "gender": Gender.UNDISCLOSED.value }, user=user) assert contact_base_form.is_valid(), contact_base_form.errors contact = contact_base_form.save() assert isinstance(contact, PersonContact) assert contact.user == user assert get_person_contact(user) == contact assert contact.name == "%s %s" % (test_first_name, test_last_name)
def test_contact_edit_form(): user = get_user_model().objects.create_user( username=printable_gibberish(), first_name=printable_gibberish(), last_name=printable_gibberish(), ) test_first_name = printable_gibberish() test_last_name = printable_gibberish() contact_base_form = ContactBaseForm(bind_user=user, data={ "first_name": test_first_name, "last_name": test_last_name, "gender": Gender.UNDISCLOSED.value }) assert contact_base_form.bind_user == user assert contact_base_form.contact_class == PersonContact assert contact_base_form.is_valid(), contact_base_form.errors contact = contact_base_form.save() assert isinstance(contact, PersonContact) assert contact.user == user assert get_person_contact(user) == contact assert contact.name == "%s %s" % (test_first_name, test_last_name)