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_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_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 reverse("shuup:customer_edit") in response.url response = client.post(company_edit_url, data) assert reverse("shuup:customer_edit") in response.url
def test_notify_on_company_created(regular_user): 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") 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) script.set_steps([step]) script.save() assert not Notification.objects.filter( identifier="company_created").exists() get_default_shop() 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") 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()
def test_get_company_contact(regular_user): person_contact = get_person_contact(regular_user) assert person_contact != AnonymousContact() assert not get_company_contact(regular_user) company_contact = create_random_company() company_contact.members.add(person_contact) assert get_company_contact(regular_user) == company_contact
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_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 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_forcing_to_person_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 force_person_contact_for_user(admin_user) assert get_company_contact(admin_user) is None force_person_contact_for_user(admin_user, False) assert company_contact == get_company_contact(admin_user)
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()) form_group.add_form_def("billing", AddressForm, kwargs={ "instance": _get_default_address_for_contact( company, "default_billing_address", person) }) form_group.add_form_def("shipping", AddressForm, 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 get_form(self, form_class=None): 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 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_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_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 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_force_contact_views(rf): shop = factories.get_default_shop() user = factories.create_random_user(is_staff=True) shop.staff_members.add(user) person_contact = get_person_contact(user) request = apply_request_middleware(rf.get("/"), user=user) assert request.customer == person_contact # Force contact to anonymous contact _call_force_view(request, force_anonymous_contact) # Re-process middlewares so we check the force contact request = apply_request_middleware(rf.get("/"), user=user) assert request.customer.is_anonymous assert get_person_contact(user).is_anonymous assert_all_good_with_random_user() # Force contact to person contact _call_force_view(request, force_person_contact) request = apply_request_middleware(rf.get("/"), user=user) assert request.customer == person_contact assert get_person_contact(user) == person_contact assert_all_good_with_random_user() # Force contact to company contact. This also ensures # company contact for staff user if does not exists. _call_force_view(request, force_company_contact) request = apply_request_middleware(rf.get("/"), user=user) assert get_company_contact(user) == request.customer assert person_contact in request.customer.members.all() assert request.person == person_contact assert_all_good_with_random_user() # Finally force back to person contact. Now without # forcing the request would have company contact # since company contact for shop staff was created # while forcing company. _call_force_view(request, force_person_contact) request = apply_request_middleware(rf.get("/"), user=user) assert request.customer == person_contact assert get_person_contact(user) == person_contact assert get_company_contact(user) is None assert_all_good_with_random_user()
def test_company_edit_form_links_company(regular_user, rf): 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)
def _set_customer(self, request): if not request.person or should_force_person_contact(request.user): company = None else: company = get_company_contact(request.user) request.customer = (company or request.person) request.is_company_member = bool(company) request.customer_groups = (company or request.person).groups.all()
def initialize_test(regular_user, person=True): client = SmartClient() get_default_shop() if person: contact = get_person_contact(regular_user) else: contact = get_company_contact(regular_user) client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) return client, contact
def get_form(self, form_class): contact = get_company_contact(self.request.user) form_group = FormGroup(**self.get_form_kwargs()) form_group.add_form_def( "billing", AddressForm, kwargs={"instance": contact.default_billing_address if contact else None} ) form_group.add_form_def( "shipping", AddressForm, kwargs={"instance": contact.default_shipping_address if contact else None} ) form_group.add_form_def("contact", CompanyContactForm, kwargs={"instance": contact}) return form_group
def process_request(self, with_basket=True, shop=None): """ Add context to request that's expected by basket """ request = self.request._request user = self.request.user request.shop = shop or self.get_basket_shop() request.person = get_person_contact(user) company = get_company_contact(user) request.customer = (company or request.person) if with_basket: request.basket = self.get_object()
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 process_request(self, with_basket=True): """ Add context to request that's expected by basket """ request = self.request._request user = self.request.user request.shop = self.get_basket_shop() request.person = get_person_contact(user) company = get_company_contact(user) request.customer = (company or request.person) if with_basket: request.basket = self.get_object()
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_company_still_linked_if_customer_contact_edited(regular_user): get_default_shop() person = get_person_contact(regular_user) assert not get_company_contact(regular_user) company = CompanyContact() company.save() company.members.add(person) assert get_company_contact(regular_user) client = SmartClient() client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) customer_edit_url = reverse("shuup:customer_edit") soup = client.soup(customer_edit_url) data = default_customer_data() data.update(default_address_data("billing")) data.update(default_address_data("shipping")) response, soup = client.response_and_soup(customer_edit_url, data, "post") assert response.status_code == 302 assert get_company_contact(regular_user)
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 __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 form_valid(self, form): contact = form["contact"].save() user = contact.user billing_address = form["billing"].save() shipping_address = form["shipping"].save() if billing_address.pk != contact.default_billing_address_id: # Identity changed due to immutability contact.default_billing_address = billing_address if shipping_address.pk != contact.default_shipping_address_id: # Identity changed due to immutability contact.default_shipping_address = shipping_address if not bool(get_company_contact(self.request.user)): # Only update user details for non-company members user.email = contact.email user.first_name = contact.first_name user.last_name = contact.last_name user.save() contact.save() messages.success(self.request, _("Account information saved successfully.")) return redirect("shuup:customer_edit")
def get_form(self, form_class): contact = get_company_contact(self.request.user) form_group = FormGroup(**self.get_form_kwargs()) form_group.add_form_def( "billing", AddressForm, kwargs={ "instance": contact.default_billing_address if contact else None }) form_group.add_form_def( "shipping", AddressForm, kwargs={ "instance": contact.default_shipping_address if contact else None }) form_group.add_form_def("contact", CompanyContactForm, kwargs={"instance": contact}) return form_group
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()) form_group.add_form_def( "billing", AddressForm, kwargs={ "instance": _get_default_address_for_contact(company, "default_billing_address", person) } ) form_group.add_form_def( "shipping", AddressForm, 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 save(self): contact = self.forms["contact"].save() user = contact.user if "billing" in self.forms: billing_address = self.forms["billing"].save() if billing_address.pk != contact.default_billing_address_id: # Identity changed due to immutability contact.default_billing_address = billing_address if "shipping" in self.forms: shipping_address = self.forms["shipping"].save() if shipping_address.pk != contact.default_shipping_address_id: # Identity changed due to immutability contact.default_shipping_address = shipping_address if not bool(get_company_contact(self.request.user)): # Only update user details for non-company members user.email = contact.email user.first_name = contact.first_name user.last_name = contact.last_name user.save() contact.save() return contact
def register(self, form): user = super(CompanyRegistrationView, self).register(form) if settings.SHUUP_ENABLE_MULTIPLE_SHOPS and settings.SHUUP_MANAGE_CONTACTS_PER_SHOP: company = get_company_contact(user) company.shops.add(self.request.shop)
def dispatch(self, request, *args, **kwargs): if not (bool(get_company_contact(self.request.user)) or allow_company_registration(self.request.shop)): return redirect("shuup:customer_edit") return super(CompanyEditView, self).dispatch(request, *args, **kwargs)
def _set_customer(self, request): company = get_company_contact(request.user) request.customer = (company or request.person) request.is_company_member = bool(company) request.customer_groups = (company or request.person).groups.all()
def register(self, form): user = super(CompanyRegistrationView, self).register(form) if settings.SHUUP_ENABLE_MULTIPLE_SHOPS and settings.SHUUP_MANAGE_CONTACTS_PER_SHOP: company = get_company_contact(user) company.add_to_shop(self.request.shop)