Beispiel #1
0
    def test_email_translation(self) -> None:
        def check_translation(phrase: str, request_type: str, *args: Any, **kwargs: Any) -> None:
            if request_type == "post":
                self.client_post(*args, **kwargs)
            elif request_type == "patch":  # nocoverage: see comment below
                self.client_patch(*args, **kwargs)

            email_message = mail.outbox[0]
            self.assertIn(phrase, email_message.body)

            for i in range(len(mail.outbox)):
                mail.outbox.pop()

        hamlet = self.example_user("hamlet")
        hamlet.default_language = "de"
        hamlet.save()
        realm = hamlet.realm
        realm.default_language = "de"
        realm.save()
        stream = get_realm_stream("Denmark", realm.id)
        self.login_user(hamlet)

        # TODO: Uncomment and replace with translation once we have German translations for the strings
        # in confirm_new_email.txt.
        # Also remove the "nocoverage" from check_translation above.
        # check_translation("Viele Grüße", "patch", "/json/settings", {"email": "*****@*****.**"})
        check_translation("Incrível!", "post", "/accounts/home/", {"email": "*****@*****.**"}, HTTP_ACCEPT_LANGUAGE="pt")
        check_translation("Danke, dass du", "post", '/accounts/find/', {'emails': hamlet.delivery_email})
        check_translation("Hallo", "post", "/json/invites",  {"invitee_emails": "*****@*****.**",
                                                              "stream_ids": orjson.dumps([stream.id]).decode()})

        with self.settings(DEVELOPMENT_LOG_EMAILS=True):
            enqueue_welcome_emails(hamlet)
        check_translation("Viele Grüße", "")
Beispiel #2
0
    def test_email_translation(self) -> None:
        def check_translation(phrase: str, request_type: str, *args: Any, **kwargs: Any) -> None:
            if request_type == "post":
                self.client_post(*args, **kwargs)
            elif request_type == "patch":  # nocoverage: see comment below
                self.client_patch(*args, **kwargs)

            email_message = mail.outbox[0]
            self.assertIn(phrase, email_message.body)

            for i in range(len(mail.outbox)):
                mail.outbox.pop()

        hamlet = self.example_user("hamlet")
        hamlet.default_language = "de"
        hamlet.save()
        realm = hamlet.realm
        realm.default_language = "de"
        realm.save()
        stream = get_realm_stream("Denmark", realm.id)
        self.login(hamlet.email)

        # TODO: Uncomment and replace with translation once we have German translations for the strings
        # in confirm_new_email.txt.
        # Also remove the "nocoverage" from check_translation above.
        # check_translation("Viele Grüße", "patch", "/json/settings", {"email": "*****@*****.**"})
        check_translation("Incrível!", "post", "/accounts/home/", {"email": "*****@*****.**"}, HTTP_ACCEPT_LANGUAGE="pt")
        check_translation("Danke, dass Du", "post", '/accounts/find/', {'emails': hamlet.email})
        check_translation("Hallo", "post", "/json/invites",  {"invitee_emails": "*****@*****.**",
                                                              "stream_ids": ujson.dumps([stream.id])})

        with self.settings(DEVELOPMENT_LOG_EMAILS=True):
            enqueue_welcome_emails(hamlet)
        check_translation("Viele Grüße", "")
Beispiel #3
0
 def get_stream_id(self, name: str, realm: Optional[Realm] = None) -> int:
     if not realm:
         realm = get_realm('zulip')
     try:
         stream = get_realm_stream(name, realm.id)
     except Stream.DoesNotExist:
         return self.INVALID_STREAM_ID
     return stream.id
Beispiel #4
0
def access_stream_by_name(user_profile, stream_name):
    # type: (UserProfile, Text) -> Tuple[Stream, Recipient, Subscription]
    error = _("Invalid stream name '%s'" % (stream_name,))
    try:
        stream = get_realm_stream(stream_name, user_profile.realm_id)
    except Stream.DoesNotExist:
        raise JsonableError(error)

    (recipient, sub) = access_stream_common(user_profile, stream, error)
    return (stream, recipient, sub)
Beispiel #5
0
def access_stream_by_name(user_profile: UserProfile,
                          stream_name: str,
                          allow_realm_admin: bool=False) -> Tuple[Stream, Recipient, Optional[Subscription]]:
    error = _("Invalid stream name '%s'") % (stream_name,)
    try:
        stream = get_realm_stream(stream_name, user_profile.realm_id)
    except Stream.DoesNotExist:
        raise JsonableError(error)

    (recipient, sub) = access_stream_common(user_profile, stream, error,
                                            allow_realm_admin=allow_realm_admin)
    return (stream, recipient, sub)
Beispiel #6
0
def access_stream_by_name(user_profile: UserProfile,
                          stream_name: str,
                          allow_realm_admin: bool=False) -> Tuple[Stream, Recipient, Optional[Subscription]]:
    error = _("Invalid stream name '%s'" % (stream_name,))
    try:
        stream = get_realm_stream(stream_name, user_profile.realm_id)
    except Stream.DoesNotExist:
        raise JsonableError(error)

    (recipient, sub) = access_stream_common(user_profile, stream, error,
                                            allow_realm_admin=allow_realm_admin)
    return (stream, recipient, sub)
Beispiel #7
0
def generate_all_emails(request: HttpRequest) -> HttpResponse:
    if not settings.TEST_SUITE:  # nocoverage
        # It's really convenient to automatically inline the email CSS
        # here, since that saves a step when testing out changes to
        # the email CSS.  But we don't run this inside the test suite,
        # because by role, the tests shouldn't be doing a provision-like thing.
        subprocess.check_call(["./scripts/setup/inline_email_css.py"])

    # We import the Django test client inside the view function,
    # because it isn't needed in production elsewhere, and not
    # importing it saves ~50ms of unnecessary manage.py startup time.

    from django.test import Client
    client = Client()

    # write fake data for all variables
    registered_email = "*****@*****.**"
    unregistered_email_1 = "*****@*****.**"
    unregistered_email_2 = "*****@*****.**"
    realm = get_realm("zulip")
    other_realm = Realm.objects.exclude(string_id='zulip').first()
    user = get_user_by_delivery_email(registered_email, realm)
    host_kwargs = {'HTTP_HOST': realm.host}

    # Password reset emails
    # active account in realm
    result = client.post('/accounts/password/reset/',
                         {'email': registered_email}, **host_kwargs)
    assert result.status_code == 302
    # deactivated user
    user.is_active = False
    user.save(update_fields=['is_active'])
    result = client.post('/accounts/password/reset/',
                         {'email': registered_email}, **host_kwargs)
    assert result.status_code == 302
    user.is_active = True
    user.save(update_fields=['is_active'])
    # account on different realm
    result = client.post('/accounts/password/reset/',
                         {'email': registered_email},
                         HTTP_HOST=other_realm.host)
    assert result.status_code == 302
    # no account anywhere
    result = client.post('/accounts/password/reset/',
                         {'email': unregistered_email_1}, **host_kwargs)
    assert result.status_code == 302

    # Confirm account email
    result = client.post('/accounts/home/', {'email': unregistered_email_1},
                         **host_kwargs)
    assert result.status_code == 302

    # Find account email
    result = client.post('/accounts/find/', {'emails': registered_email},
                         **host_kwargs)
    assert result.status_code == 302

    # New login email
    logged_in = client.login(dev_auth_username=registered_email, realm=realm)
    assert logged_in

    # New user invite and reminder emails
    stream = get_realm_stream("Denmark", user.realm.id)
    result = client.post(
        "/json/invites", {
            "invitee_emails": unregistered_email_2,
            "stream_ids": ujson.dumps([stream.id])
        }, **host_kwargs)
    assert result.status_code == 200

    # Verification for new email
    result = client.patch(
        '/json/settings',
        urllib.parse.urlencode({'email': '*****@*****.**'}),
        **host_kwargs)
    assert result.status_code == 200

    # Email change successful
    key = Confirmation.objects.filter(
        type=Confirmation.EMAIL_CHANGE).latest('id').confirmation_key
    url = confirmation_url(key, realm.host, Confirmation.EMAIL_CHANGE)
    user_profile = get_user_by_delivery_email(registered_email, realm)
    result = client.get(url)
    assert result.status_code == 200

    # Reset the email value so we can run this again
    do_change_user_delivery_email(user_profile, registered_email)

    # Follow up day1 day2 emails for normal user
    enqueue_welcome_emails(user_profile)

    # Follow up day1 day2 emails for admin user
    enqueue_welcome_emails(get_user_by_delivery_email("*****@*****.**", realm),
                           realm_creation=True)

    # Realm reactivation email
    do_send_realm_reactivation_email(realm)

    return redirect(email_page)
Beispiel #8
0
def generate_all_emails(request: HttpRequest) -> HttpResponse:
    if not settings.TEST_SUITE:  # nocoverage
        # It's really convenient to automatically inline the email CSS
        # here, since that saves a step when testing out changes to
        # the email CSS.  But we don't run this inside the test suite,
        # because by role, the tests shouldn't be doing a provision-like thing.
        subprocess.check_call(["./tools/inline-email-css"])

    # We import the Django test client inside the view function,
    # because it isn't needed in production elsewhere, and not
    # importing it saves ~50ms of unnecessary manage.py startup time.

    from django.test import Client
    client = Client()

    # write fake data for all variables
    registered_email = "*****@*****.**"
    unregistered_email_1 = "*****@*****.**"
    unregistered_email_2 = "*****@*****.**"
    realm = get_realm("zulip")
    other_realm = Realm.objects.exclude(string_id='zulip').first()
    user = get_user_by_delivery_email(registered_email, realm)
    host_kwargs = {'HTTP_HOST': realm.host}

    # Password reset emails
    # active account in realm
    result = client.post('/accounts/password/reset/', {'email': registered_email}, **host_kwargs)
    assert result.status_code == 302
    # deactivated user
    user.is_active = False
    user.save(update_fields=['is_active'])
    result = client.post('/accounts/password/reset/', {'email': registered_email}, **host_kwargs)
    assert result.status_code == 302
    user.is_active = True
    user.save(update_fields=['is_active'])
    # account on different realm
    result = client.post('/accounts/password/reset/', {'email': registered_email},
                         HTTP_HOST=other_realm.host)
    assert result.status_code == 302
    # no account anywhere
    result = client.post('/accounts/password/reset/', {'email': unregistered_email_1}, **host_kwargs)
    assert result.status_code == 302

    # Confirm account email
    result = client.post('/accounts/home/', {'email': unregistered_email_1}, **host_kwargs)
    assert result.status_code == 302

    # Find account email
    result = client.post('/accounts/find/', {'emails': registered_email}, **host_kwargs)
    assert result.status_code == 302

    # New login email
    logged_in = client.login(dev_auth_username=registered_email, realm=realm)
    assert logged_in

    # New user invite and reminder emails
    stream = get_realm_stream("Denmark", get_realm("zulip"))
    result = client.post("/json/invites",
                         {"invitee_emails": unregistered_email_2,
                          "stream_ids": ujson.dumps([stream.id])},
                         **host_kwargs)
    assert result.status_code == 200

    # Verification for new email
    result = client.patch('/json/settings',
                          urllib.parse.urlencode({'email': '*****@*****.**'}),
                          **host_kwargs)
    assert result.status_code == 200

    # Email change successful
    key = Confirmation.objects.filter(type=Confirmation.EMAIL_CHANGE).latest('id').confirmation_key
    url = confirmation_url(key, realm.host, Confirmation.EMAIL_CHANGE)
    user_profile = get_user_by_delivery_email(registered_email, realm)
    result = client.get(url)
    assert result.status_code == 200

    # Reset the email value so we can run this again
    do_change_user_delivery_email(user_profile, registered_email)

    # Follow up day1 day2 emails for normal user
    enqueue_welcome_emails(user_profile)

    # Follow up day1 day2 emails for admin user
    enqueue_welcome_emails(get_user_by_delivery_email("*****@*****.**", realm), realm_creation=True)

    # Realm reactivation email
    do_send_realm_reactivation_email(realm)

    return redirect(email_page)