Пример #1
0
def test_company_contact_detail_multishop(rf):
    with override_settings(SHUUP_MANAGE_CONTACTS_PER_SHOP=True,
                           SHUUP_ENABLE_MULTIPLE_SHOPS=True):
        staff_user = create_random_user(is_staff=True)

        shop1 = get_shop(identifier="shop-1", enabled=True)
        shop2 = get_shop(identifier="shop-2", enabled=True)

        shop1.staff_members.add(staff_user)
        shop2.staff_members.add(staff_user)

        company = create_random_company()
        # only available in shop1
        company.add_to_shop(shop1)

        view = ContactDetailView.as_view()

        # company not found for this shop
        request = apply_request_middleware(rf.get("/"),
                                           user=staff_user,
                                           shop=shop2)
        with pytest.raises(Http404):
            response = view(request, pk=company.id)

        request = apply_request_middleware(rf.get("/"),
                                           user=staff_user,
                                           shop=shop1)
        response = view(request, pk=company.id)
        assert response.status_code == 200
Пример #2
0
def test_contact_detail_multishop(rf):
    with override_settings(SHUUP_MANAGE_CONTACTS_PER_SHOP=True,
                           SHUUP_ENABLE_MULTIPLE_SHOPS=True):
        staff_user = create_random_user(is_staff=True)

        shop1 = get_shop(identifier="shop-1", enabled=True)
        shop2 = get_shop(identifier="shop-2", enabled=True)

        shop1.staff_members.add(staff_user)
        shop2.staff_members.add(staff_user)

        contact = create_random_person(locale="en_US",
                                       minimum_name_comp_len=5,
                                       shop=shop2)

        view = ContactDetailView.as_view()

        # contact not found for this shop
        request = apply_request_middleware(rf.get("/"),
                                           user=staff_user,
                                           shop=shop1)
        with pytest.raises(Http404):
            response = view(request, pk=contact.id)

        request = apply_request_middleware(rf.get("/"),
                                           user=staff_user,
                                           shop=shop2)
        response = view(request, pk=contact.id)
        assert response.status_code == 200
Пример #3
0
def test_create_company_from_customer_dashboard(
    allow_company_registration, company_registration_requires_approval, client, rf, admin_user
):
    if "shuup.front.apps.registration" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.front.apps.registration required in installed apps")

    shop = get_default_shop()
    configuration.set(None, "allow_company_registration", allow_company_registration)
    configuration.set(None, "company_registration_requires_approval", company_registration_requires_approval)

    Script.objects.create(
        event_identifier="company_registration_received",
        name="Send Company Registration Received Email",
        enabled=True,
        shop=shop,
        template="company_registration_received_email",
        _step_data=[
            {
                "conditions": [],
                "next": "stop",
                "cond_op": "all",
                "actions": [
                    {
                        "fallback_language": {"constant": "en"},
                        "template_data": {
                            "en": {"content_type": "html", "subject": "Company Registered", "body": "Waiting approval"},
                        },
                        "recipient": {"variable": "customer_email"},
                        "language": {"variable": "language"},
                        "identifier": "send_email",
                    },
                ],
                "enabled": True,
            }
        ],
    )
    Script.objects.create(
        event_identifier="company_approved_by_admin",
        name="Send Company Activated Email",
        enabled=True,
        shop=shop,
        template="company_activated_email",
        _step_data=[
            {
                "actions": [
                    {
                        "fallback_language": {"constant": "en"},
                        "template_data": {
                            "en": {
                                "content_type": "html",
                                "subject": "Company activated",
                                "body": "Company has been approved. "
                                "Please activate your account by clicking the link: {{activation_url}}",
                            },
                        },
                        "recipient": {"variable": "customer_email"},
                        "language": {"variable": "language"},
                        "identifier": "send_email",
                    }
                ],
                "enabled": True,
                "next": "stop",
                "conditions": [],
                "cond_op": "all",
            }
        ],
    )
    # This view creates CompanyContact object for already registered user
    view_func = CompanyEditView.as_view()

    if not allow_company_registration:
        # If company registration is not allowed,
        # can't create company contacts from customer dashboard
        request = apply_request_middleware(rf.get("/"), user=admin_user)
        response = view_func(request)
        assert reverse("shuup:customer_edit") in response.url
    else:
        request = apply_request_middleware(
            rf.post(
                "/",
                {
                    "contact-name": "Test company",
                    "contact-name_ext": "test",
                    "contact-tax_number": "12345",
                    "contact-email": "*****@*****.**",
                    "contact-phone": "123123",
                    "contact-www": "",
                    "billing-name": "testa tesat",
                    "billing-phone": "testa tesat",
                    "billing-email": email,
                    "billing-street": "testa tesat",
                    "billing-street2": "",
                    "billing-postal_code": "12345",
                    "billing-city": "test test",
                    "billing-region": "",
                    "billing-region_code": "",
                    "billing-country": "FI",
                    "shipping-name": "testa tesat",
                    "shipping-phone": "testa tesat",
                    "shipping-email": email,
                    "shipping-street": "testa tesat",
                    "shipping-street2": "",
                    "shipping-postal_code": "12345",
                    "shipping-city": "test test",
                    "shipping-region": "",
                    "shipping-region_code": "",
                    "shipping-country": "FI",
                },
            ),
            user=admin_user,
        )
        response = view_func(request)
        if company_registration_requires_approval:
            # CompanyContact was created as inactive but PersonContact stays active
            assert CompanyContact.objects.filter(is_active=False).count() == 1
            assert PersonContact.objects.filter(is_active=True).count() == 1
            assert mail.outbox[0].subject == "Company Registered"
            # Activate new CompanyContact from admin
            request = apply_request_middleware(rf.post("/", {"set_is_active": "1"}), user=admin_user)
            view_func = ContactDetailView.as_view()
            response = view_func(request, pk=CompanyContact.objects.first().pk)
            urls = re.findall(
                "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", mail.outbox[1].body
            )
            assert mail.outbox[1].subject == "Company activated"
            assert CompanyContact.objects.filter(is_active=True).count() == 1
        else:
            assert CompanyContact.objects.filter(is_active=True).count() == 1
            assert PersonContact.objects.filter(is_active=True).count() == 1

        contact = PersonContact.objects.first()
        assert contact.in_shop(shop)
        assert contact.in_shop(shop, only_registration=True)  # registered here

        company = CompanyContact.objects.first()
        assert company.in_shop(shop)
        assert company.in_shop(shop, only_registration=True)  # registered here
Пример #4
0
def test_company_registration(
    django_user_model, client, allow_company_registration, company_registration_requires_approval, rf, admin_user
):
    if "shuup.front.apps.registration" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.front.apps.registration required in installed apps")

    shop = get_default_shop()

    configuration.set(shop, "allow_company_registration", allow_company_registration)
    configuration.set(shop, "company_registration_requires_approval", company_registration_requires_approval)

    url = reverse("shuup:registration_register_company")
    Script.objects.create(
        event_identifier="company_approved_by_admin",
        name="Send Company Activated Email",
        enabled=True,
        shop=shop,
        template="company_activated_email",
        _step_data=[
            {
                "actions": [
                    {
                        "fallback_language": {"constant": "en"},
                        "template_data": {
                            "en": {
                                "content_type": "html",
                                "subject": "Company activated",
                                "body": "Company has been approved. "
                                "Please activate your account by clicking the link: {{activation_url}}",
                            },
                        },
                        "recipient": {"variable": "customer_email"},
                        "language": {"variable": "language"},
                        "identifier": "send_email",
                    }
                ],
                "enabled": True,
                "next": "stop",
                "conditions": [],
                "cond_op": "all",
            }
        ],
    )
    Script.objects.create(
        event_identifier="company_registration_received",
        name="Send Company Registration Received Email",
        enabled=True,
        shop=shop,
        template="company_registration_received_email",
        _step_data=[
            {
                "conditions": [],
                "next": "stop",
                "cond_op": "all",
                "actions": [
                    {
                        "fallback_language": {"constant": "en"},
                        "template_data": {
                            "en": {"content_type": "html", "subject": "Generic welcome message", "body": "Welcome!"},
                        },
                        "recipient": {"variable": "customer_email"},
                        "language": {"variable": "language"},
                        "identifier": "send_email",
                    },
                    {
                        "fallback_language": {"constant": "en"},
                        "template_data": {
                            "en": {
                                "content_type": "plain",
                                "subject": "New company registered",
                                "body": "New company registered",
                            },
                        },
                        "recipient": {"constant": "*****@*****.**"},
                        "language": {"constant": "en"},
                        "identifier": "send_email",
                    },
                ],
                "enabled": True,
            }
        ],
    )
    if not allow_company_registration:
        response = client.get(url)
        assert reverse("shuup:registration_register") in response.url
    else:
        response = 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",
            },
        )
        user = django_user_model.objects.get(username=username)
        contact = PersonContact.objects.get(user=user)
        company = CompanyContact.objects.get(members__in=[contact])

        # one of each got created
        assert django_user_model.objects.count() == 2  # admin_user + registered user
        assert PersonContact.objects.count() == 1
        assert CompanyContact.objects.count() == 1

        if company_registration_requires_approval:
            assert PersonContact.objects.filter(is_active=False).count() == 1
            assert PersonContact.objects.filter(user__is_active=False).count() == 1
            assert CompanyContact.objects.filter(is_active=False).count() == 1
            assert mail.outbox[0].subject == "Generic welcome message"
            assert mail.outbox[1].subject == "New company registered"
            # Activating Company for the first time from admin triggers company_approved_by_admin event
            request = apply_request_middleware(rf.post("/", {"set_is_active": "1"}), user=admin_user)
            view_func = ContactDetailView.as_view()
            response = view_func(request, pk=company.pk)
            urls = re.findall(
                "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", mail.outbox[2].body
            )
            assert mail.outbox[2].subject == "Company activated"
            assert user.registrationprofile.activation_key in urls[0]
            # User receives link to activate his own account
            response = client.get(urls[0], follow=True)
            user.refresh_from_db()
            assert user.is_active is True
        else:
            assert PersonContact.objects.filter(is_active=True).count() == 1

            # Since we don't support company registration without the activation
            # the user here is still False. Then it is up to merchant to customize
            # the activation notification so that the activation email is sent to
            # the newly created user. This is tested in the previous unit test.
            assert PersonContact.objects.filter(user__is_active=False).count() == 1
            assert CompanyContact.objects.filter(is_active=True).count() == 1
            assert mail.outbox[0].subject == "Generic welcome message"

        contact = PersonContact.objects.first()
        assert contact.in_shop(shop)
        assert contact.in_shop(shop, only_registration=True)  # registered here

        company = CompanyContact.objects.first()
        assert company.in_shop(shop)
        assert company.in_shop(shop, only_registration=True)  # registered here
def test_create_company_from_customer_dashboard(allow_company_registration, company_registration_requires_approval,
                                                client, rf, admin_user):
    if "shuup.front.apps.registration" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.front.apps.registration required in installed apps")

    get_default_shop()
    configuration.set(None, "allow_company_registration", allow_company_registration)
    configuration.set(None, "company_registration_requires_approval", company_registration_requires_approval)

    Script.objects.create(
        event_identifier="company_registration_received",
        name="Send Company Registration Received Email",
        enabled=True,
        template="company_registration_received_email",
        _step_data=[
            {
                'conditions': [],
                'next': 'stop',
                'cond_op': 'all',
                'actions': [
                    {
                        'fallback_language': {'constant': 'en'},
                        'template_data': {
                             'en': {
                                 'content_type': 'html',
                                 'subject': 'Company Registered',
                                 'body': 'Waiting approval'
                             },
                        },
                        'recipient': {'variable': 'customer_email'},
                        'language': {'variable': 'language'},
                        'identifier': 'send_email'
                    },
                ],
                'enabled': True}],
    )
    Script.objects.create(
        event_identifier="company_approved_by_admin",
        name="Send Company Activated Email",
        enabled=True,
        template="company_activated_email",
        _step_data=[
            {'actions': [
                {
                    'fallback_language': {'constant': 'en'},
                    'template_data': {
                        'en': {
                            'content_type': 'html',
                            'subject': 'Company activated',
                            'body': 'Company has been approved. '
                                    'Please activate your account by clicking the link: {{activation_url}}'
                        },
                    },
                    'recipient': {'variable': 'customer_email'},
                    'language': {'variable': 'language'},
                    'identifier': 'send_email'}
            ],
                'enabled': True,
                'next': 'stop',
                'conditions': [],
                'cond_op': 'all'
            }
        ]
    )
    # This view creates CompanyContact object for already registered user
    view_func = CompanyEditView.as_view()

    if not allow_company_registration:
        # If company registration is not allowed,
        # can't create company contacts from customer dashboard
        request = apply_request_middleware(rf.get("/"), user=admin_user)
        response = view_func(request)
        assert response.status_code == 404
    else:
        request = apply_request_middleware(
            rf.post("/", {
                'contact-name': "Test company",
                'contact-name_ext': "test",
                'contact-tax_number': "12345",
                'contact-email': "*****@*****.**",
                'contact-phone': "123123",
                'contact-www': "",
                'billing-name': "testa tesat",
                'billing-phone': "testa tesat",
                'billing-email': email,
                'billing-street': "testa tesat",
                'billing-street2': "",
                'billing-postal_code': "12345",
                'billing-city': "test test",
                'billing-region': "",
                'billing-region_code': "",
                'billing-country': "FI",
                'shipping-name': "testa tesat",
                'shipping-phone': "testa tesat",
                'shipping-email': email,
                'shipping-street': "testa tesat",
                'shipping-street2': "",
                'shipping-postal_code': "12345",
                'shipping-city': "test test",
                'shipping-region': "",
                'shipping-region_code': "",
                'shipping-country': "FI",
            }), user=admin_user)
        response = view_func(request)
        if company_registration_requires_approval:
            # CompanyContact was created as inactive but PersonContact stays active
            assert CompanyContact.objects.filter(is_active=False).count() == 1
            assert PersonContact.objects.filter(is_active=True).count() == 1
            assert mail.outbox[0].subject == "Company Registered"
            # Activate new CompanyContact from admin
            request = apply_request_middleware(rf.post("/", {"set_is_active": "1"}), user=admin_user)
            view_func = ContactDetailView.as_view()
            response = view_func(request, pk=CompanyContact.objects.first().pk)
            urls = re.findall(
                'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
                mail.outbox[1].body)
            assert mail.outbox[1].subject == "Company activated"
            assert CompanyContact.objects.filter(is_active=True).count() == 1
        else:
            assert CompanyContact.objects.filter(is_active=True).count() == 1
            assert PersonContact.objects.filter(is_active=True).count() == 1
def test_company_registration(django_user_model, client, allow_company_registration,
                              company_registration_requires_approval, rf, admin_user):
    if "shuup.front.apps.registration" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.front.apps.registration required in installed apps")

    get_default_shop()

    configuration.set(None, "allow_company_registration", allow_company_registration)
    configuration.set(None, "company_registration_requires_approval", company_registration_requires_approval)

    url = reverse("shuup:registration_register_company")
    Script.objects.create(
        event_identifier="company_approved_by_admin",
        name="Send Company Activated Email",
        enabled=True,
        template="company_activated_email",
        _step_data=[
            {'actions': [
                {
                    'fallback_language': {'constant': 'en'},
                    'template_data': {
                        'en': {
                            'content_type': 'html',
                            'subject': 'Company activated',
                            'body': 'Company has been approved. '
                                    'Please activate your account by clicking the link: {{activation_url}}'
                        },
                    },
                    'recipient': {'variable': 'customer_email'},
                    'language': {'variable': 'language'},
                    'identifier': 'send_email'}
            ],
                'enabled': True,
                'next': 'stop',
                'conditions': [],
                'cond_op': 'all'
            }
        ]
    )
    Script.objects.create(
        event_identifier="company_registration_received",
        name="Send Company Registration Received Email",
        enabled=True,
        template="company_registration_received_email",
        _step_data=[
            {
                'conditions': [],
                'next': 'stop',
                'cond_op': 'all',
                'actions': [
                    {
                        'fallback_language': {'constant': 'en'},
                        'template_data': {
                             'en': {
                                 'content_type': 'html',
                                 'subject': 'Generic welcome message',
                                 'body': 'Welcome!'
                             },
                        },
                        'recipient': {'variable': 'customer_email'},
                        'language': {'variable': 'language'},
                        'identifier': 'send_email'
                    },
                    {
                        'fallback_language': {'constant': 'en'},
                        'template_data': {
                            'en': {
                                'content_type': 'plain',
                                'subject': 'New company registered',
                                'body': 'New company registered'
                            },
                        },
                        'recipient': {'constant': '*****@*****.**'},
                        'language': {'constant': 'en'},
                        'identifier': 'send_email'
                    }
                ],
                'enabled': True}],
    )
    if not allow_company_registration:
        response = client.get(url)
        assert response.status_code == 404
    else:
        response = 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",
        })

        user = django_user_model.objects.get(username=username)
        contact = PersonContact.objects.get(user=user)
        company = CompanyContact.objects.get(members__in=[contact])

        # one of each got created
        assert django_user_model.objects.count() == 2  # admin_user + registered user
        assert PersonContact.objects.count() == 1
        assert CompanyContact.objects.count() == 1
        # and they are innactive
        if company_registration_requires_approval:
            assert PersonContact.objects.filter(is_active=False).count() == 1
            assert CompanyContact.objects.filter(is_active=False).count() == 1
            assert mail.outbox[0].subject == "Generic welcome message"
            assert mail.outbox[1].subject == "New company registered"
            # Activating Company for the first time from admin triggers company_approved_by_admin event
            request = apply_request_middleware(rf.post("/", {"set_is_active": "1"}), user=admin_user)
            view_func = ContactDetailView.as_view()
            response = view_func(request, pk=company.pk)
            urls = re.findall(
                'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
                mail.outbox[2].body)
            assert mail.outbox[2].subject == "Company activated"
            assert user.registrationprofile.activation_key in urls[0]
            # User receives link to activate his own account
            response = client.get(urls[0], follow=True)
            user.refresh_from_db()
            assert user.is_active is True
        else:
            assert PersonContact.objects.filter(is_active=True).count() == 1
            assert CompanyContact.objects.filter(is_active=True).count() == 1
Пример #7
0
def test_create_company_from_customer_dashboard(allow_company_registration, company_registration_requires_approval,
                                                client, rf, admin_user):
    if "shuup.front.apps.registration" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.front.apps.registration required in installed apps")

    shop = get_default_shop()
    configuration.set(None, "allow_company_registration", allow_company_registration)
    configuration.set(None, "company_registration_requires_approval", company_registration_requires_approval)

    Script.objects.create(
        event_identifier="company_registration_received",
        name="Send Company Registration Received Email",
        enabled=True,
        shop=shop,
        template="company_registration_received_email",
        _step_data=[
            {
                'conditions': [],
                'next': 'stop',
                'cond_op': 'all',
                'actions': [
                    {
                        'fallback_language': {'constant': 'en'},
                        'template_data': {
                             'en': {
                                 'content_type': 'html',
                                 'subject': 'Company Registered',
                                 'body': 'Waiting approval'
                             },
                        },
                        'recipient': {'variable': 'customer_email'},
                        'language': {'variable': 'language'},
                        'identifier': 'send_email'
                    },
                ],
                'enabled': True}],
    )
    Script.objects.create(
        event_identifier="company_approved_by_admin",
        name="Send Company Activated Email",
        enabled=True,
        shop=shop,
        template="company_activated_email",
        _step_data=[
            {'actions': [
                {
                    'fallback_language': {'constant': 'en'},
                    'template_data': {
                        'en': {
                            'content_type': 'html',
                            'subject': 'Company activated',
                            'body': 'Company has been approved. '
                                    'Please activate your account by clicking the link: {{activation_url}}'
                        },
                    },
                    'recipient': {'variable': 'customer_email'},
                    'language': {'variable': 'language'},
                    'identifier': 'send_email'}
            ],
                'enabled': True,
                'next': 'stop',
                'conditions': [],
                'cond_op': 'all'
            }
        ]
    )
    # This view creates CompanyContact object for already registered user
    view_func = CompanyEditView.as_view()

    if not allow_company_registration:
        # If company registration is not allowed,
        # can't create company contacts from customer dashboard
        request = apply_request_middleware(rf.get("/"), user=admin_user)
        response = view_func(request)
        assert reverse("shuup:customer_edit") in response.url
    else:
        request = apply_request_middleware(
            rf.post("/", {
                'contact-name': "Test company",
                'contact-name_ext': "test",
                'contact-tax_number': "12345",
                'contact-email': "*****@*****.**",
                'contact-phone': "123123",
                'contact-www': "",
                'billing-name': "testa tesat",
                'billing-phone': "testa tesat",
                'billing-email': email,
                'billing-street': "testa tesat",
                'billing-street2': "",
                'billing-postal_code': "12345",
                'billing-city': "test test",
                'billing-region': "",
                'billing-region_code': "",
                'billing-country': "FI",
                'shipping-name': "testa tesat",
                'shipping-phone': "testa tesat",
                'shipping-email': email,
                'shipping-street': "testa tesat",
                'shipping-street2': "",
                'shipping-postal_code': "12345",
                'shipping-city': "test test",
                'shipping-region': "",
                'shipping-region_code': "",
                'shipping-country': "FI",
            }), user=admin_user)
        response = view_func(request)
        if company_registration_requires_approval:
            # CompanyContact was created as inactive but PersonContact stays active
            assert CompanyContact.objects.filter(is_active=False).count() == 1
            assert PersonContact.objects.filter(is_active=True).count() == 1
            assert mail.outbox[0].subject == "Company Registered"
            # Activate new CompanyContact from admin
            request = apply_request_middleware(rf.post("/", {"set_is_active": "1"}), user=admin_user)
            view_func = ContactDetailView.as_view()
            response = view_func(request, pk=CompanyContact.objects.first().pk)
            urls = re.findall(
                'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
                mail.outbox[1].body)
            assert mail.outbox[1].subject == "Company activated"
            assert CompanyContact.objects.filter(is_active=True).count() == 1
        else:
            assert CompanyContact.objects.filter(is_active=True).count() == 1
            assert PersonContact.objects.filter(is_active=True).count() == 1

        contact = PersonContact.objects.first()
        assert contact.in_shop(shop)
        assert contact.in_shop(shop, only_registration=True)  # registered here

        company = CompanyContact.objects.first()
        assert company.in_shop(shop)
        assert company.in_shop(shop, only_registration=True)  # registered here
Пример #8
0
def test_company_registration(django_user_model, client, allow_company_registration,
                              company_registration_requires_approval, rf, admin_user):
    if "shuup.front.apps.registration" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.front.apps.registration required in installed apps")

    shop = get_default_shop()

    configuration.set(shop, "allow_company_registration", allow_company_registration)
    configuration.set(shop, "company_registration_requires_approval", company_registration_requires_approval)

    url = reverse("shuup:registration_register_company")
    Script.objects.create(
        event_identifier="company_approved_by_admin",
        name="Send Company Activated Email",
        enabled=True,
        shop=shop,
        template="company_activated_email",
        _step_data=[
            {'actions': [
                {
                    'fallback_language': {'constant': 'en'},
                    'template_data': {
                        'en': {
                            'content_type': 'html',
                            'subject': 'Company activated',
                            'body': 'Company has been approved. '
                                    'Please activate your account by clicking the link: {{activation_url}}'
                        },
                    },
                    'recipient': {'variable': 'customer_email'},
                    'language': {'variable': 'language'},
                    'identifier': 'send_email'}
            ],
                'enabled': True,
                'next': 'stop',
                'conditions': [],
                'cond_op': 'all'
            }
        ]
    )
    Script.objects.create(
        event_identifier="company_registration_received",
        name="Send Company Registration Received Email",
        enabled=True,
        shop=shop,
        template="company_registration_received_email",
        _step_data=[
            {
                'conditions': [],
                'next': 'stop',
                'cond_op': 'all',
                'actions': [
                    {
                        'fallback_language': {'constant': 'en'},
                        'template_data': {
                             'en': {
                                 'content_type': 'html',
                                 'subject': 'Generic welcome message',
                                 'body': 'Welcome!'
                             },
                        },
                        'recipient': {'variable': 'customer_email'},
                        'language': {'variable': 'language'},
                        'identifier': 'send_email'
                    },
                    {
                        'fallback_language': {'constant': 'en'},
                        'template_data': {
                            'en': {
                                'content_type': 'plain',
                                'subject': 'New company registered',
                                'body': 'New company registered'
                            },
                        },
                        'recipient': {'constant': '*****@*****.**'},
                        'language': {'constant': 'en'},
                        'identifier': 'send_email'
                    }
                ],
                'enabled': True}],
    )
    if not allow_company_registration:
        response = client.get(url)
        assert reverse("shuup:registration_register") in response.url
    else:
        response = 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",
        })

        user = django_user_model.objects.get(username=username)
        contact = PersonContact.objects.get(user=user)
        company = CompanyContact.objects.get(members__in=[contact])

        # one of each got created
        assert django_user_model.objects.count() == 2  # admin_user + registered user
        assert PersonContact.objects.count() == 1
        assert CompanyContact.objects.count() == 1

        if company_registration_requires_approval:
            assert PersonContact.objects.filter(is_active=False).count() == 1
            assert PersonContact.objects.filter(user__is_active=False).count() == 1
            assert CompanyContact.objects.filter(is_active=False).count() == 1
            assert mail.outbox[0].subject == "Generic welcome message"
            assert mail.outbox[1].subject == "New company registered"
            # Activating Company for the first time from admin triggers company_approved_by_admin event
            request = apply_request_middleware(rf.post("/", {"set_is_active": "1"}), user=admin_user)
            view_func = ContactDetailView.as_view()
            response = view_func(request, pk=company.pk)
            urls = re.findall(
                'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
                mail.outbox[2].body)
            assert mail.outbox[2].subject == "Company activated"
            assert user.registrationprofile.activation_key in urls[0]
            # User receives link to activate his own account
            response = client.get(urls[0], follow=True)
            user.refresh_from_db()
            assert user.is_active is True
        else:
            assert PersonContact.objects.filter(is_active=True).count() == 1

            # Since we don't support company registration without the activation
            # the user here is still False. Then it is up to merchant to customize
            # the activation notification so that the activation email is sent to
            # the newly created user. This is tested in the previous unit test.
            assert PersonContact.objects.filter(user__is_active=False).count() == 1
            assert CompanyContact.objects.filter(is_active=True).count() == 1
            assert mail.outbox[0].subject == "Generic welcome message"

        contact = PersonContact.objects.first()
        assert contact.in_shop(shop)
        assert contact.in_shop(shop, only_registration=True)  # registered here

        company = CompanyContact.objects.first()
        assert company.in_shop(shop)
        assert company.in_shop(shop, only_registration=True)  # registered here