Beispiel #1
0
    def test_encode_decode(self) -> None:
        realm = get_realm('zulip')
        stream_name = 'dev. help'
        stream = ensure_stream(realm, stream_name)
        email_address = encode_email_address(stream)
        self.assertTrue(email_address.startswith('dev%0046%0032help'))
        self.assertTrue(email_address.endswith('@testserver'))
        tup = decode_email_address(email_address)
        assert tup is not None
        (decoded_stream_name, token) = tup
        self.assertEqual(decoded_stream_name, stream_name)
        self.assertEqual(token, stream.email_token)

        email_address = email_address.replace('+', '.')
        tup = decode_email_address(email_address)
        assert tup is not None
        (decoded_stream_name, token) = tup
        self.assertEqual(decoded_stream_name, stream_name)
        self.assertEqual(token, stream.email_token)

        email_address = email_address.replace('@testserver', '@zulip.org')
        self.assertEqual(decode_email_address(email_address), None)

        with self.settings(EMAIL_GATEWAY_EXTRA_PATTERN_HACK='@zulip.org'):
            tup = decode_email_address(email_address)
            assert tup is not None
            (decoded_stream_name, token) = tup
            self.assertEqual(decoded_stream_name, stream_name)
            self.assertEqual(token, stream.email_token)

        self.assertEqual(decode_email_address('bogus'), None)
Beispiel #2
0
    def test_encode_decode(self) -> None:
        realm = get_realm('zulip')
        stream_name = 'dev. help'
        stream = ensure_stream(realm, stream_name)
        email_address = encode_email_address(stream)
        self.assertTrue(email_address.startswith('dev%0046%0032help'))
        self.assertTrue(email_address.endswith('@testserver'))
        tup = decode_email_address(email_address)
        assert tup is not None
        (decoded_stream_name, token) = tup
        self.assertEqual(decoded_stream_name, stream_name)
        self.assertEqual(token, stream.email_token)

        email_address = email_address.replace('+', '.')
        tup = decode_email_address(email_address)
        assert tup is not None
        (decoded_stream_name, token) = tup
        self.assertEqual(decoded_stream_name, stream_name)
        self.assertEqual(token, stream.email_token)

        email_address = email_address.replace('@testserver', '@zulip.org')
        self.assertEqual(decode_email_address(email_address), None)

        with self.settings(EMAIL_GATEWAY_EXTRA_PATTERN_HACK='@zulip.org'):
            tup = decode_email_address(email_address)
            assert tup is not None
            (decoded_stream_name, token) = tup
            self.assertEqual(decoded_stream_name, stream_name)
            self.assertEqual(token, stream.email_token)

        self.assertEqual(decode_email_address('bogus'), None)
Beispiel #3
0
    def test_encode_decode(self) -> None:
        realm = get_realm('zulip')
        stream_name = 'dev. help'
        stream = ensure_stream(realm, stream_name)
        email_address = encode_email_address(stream)
        self.assertTrue(email_address.startswith('dev-help'))
        self.assertTrue(email_address.endswith('@testserver'))
        tup = decode_email_address(email_address)
        assert tup is not None
        token, show_sender = tup
        self.assertFalse(show_sender)
        self.assertEqual(token, stream.email_token)

        parts = email_address.split('@')
        parts[0] += "+show-sender"
        email_address_show = '@'.join(parts)
        tup = decode_email_address(email_address_show)
        assert tup is not None
        token, show_sender = tup
        self.assertTrue(show_sender)
        self.assertEqual(token, stream.email_token)

        email_address_dots = email_address.replace('+', '.')
        tup = decode_email_address(email_address_dots)
        assert tup is not None
        token, show_sender = tup
        self.assertFalse(show_sender)
        self.assertEqual(token, stream.email_token)

        email_address_dots_show = email_address_show.replace('+', '.')
        tup = decode_email_address(email_address_dots_show)
        assert tup is not None
        token, show_sender = tup
        self.assertTrue(show_sender)
        self.assertEqual(token, stream.email_token)

        email_address = email_address.replace('@testserver', '@zulip.org')
        email_address_show = email_address_show.replace(
            '@testserver', '@zulip.org')
        self.assertEqual(decode_email_address(email_address), None)
        self.assertEqual(decode_email_address(email_address_show), None)

        with self.settings(EMAIL_GATEWAY_EXTRA_PATTERN_HACK='@zulip.org'):
            tup = decode_email_address(email_address)
            assert tup is not None
            token, show_sender = tup
            self.assertFalse(show_sender)
            self.assertEqual(token, stream.email_token)

            tup = decode_email_address(email_address_show)
            assert tup is not None
            token, show_sender = tup
            self.assertTrue(show_sender)
            self.assertEqual(token, stream.email_token)

        self.assertEqual(decode_email_address('bogus'), None)
Beispiel #4
0
    def test_encode_decode_nonlatin_alphabet_stream_name(self) -> None:
        realm = get_realm('zulip')
        stream_name = 'Тестовы some ascii letters'
        stream = ensure_stream(realm, stream_name)
        email_address = encode_email_address(stream)

        msg_string = get_email_gateway_message_string_from_address(
            email_address)
        assert msg_string is not None
        parts = msg_string.split('+')
        # Stream name should be completely stripped to '', so msg_string
        # should only have the email_token in it.
        self.assertEqual(len(parts), 1)

        # Correctly decode the resulting address that doesn't have the stream name:
        tup = decode_email_address(email_address)
        assert tup is not None
        token, show_sender = tup
        self.assertFalse(show_sender)
        self.assertEqual(token, stream.email_token)

        asciiable_stream_name = "ąężć"
        stream = ensure_stream(realm, asciiable_stream_name)
        email_address = encode_email_address(stream)
        self.assertTrue(email_address.startswith("aezc+"))
Beispiel #5
0
def extract_and_validate(email: str) -> Tuple[Stream, bool]:
    temp = decode_email_address(email)
    if temp is None:
        raise ZulipEmailForwardError("Malformed email recipient " + email)
    stream_name, token, show_sender = temp

    if not valid_stream(stream_name, token):
        raise ZulipEmailForwardError("Bad stream token from email recipient " + email)

    return Stream.objects.get(email_token=token), show_sender
Beispiel #6
0
def extract_and_validate(email: str) -> Stream:
    temp = decode_email_address(email)
    if temp is None:
        raise ZulipEmailForwardError("Malformed email recipient " + email)
    stream_name, token = temp

    if not valid_stream(stream_name, token):
        raise ZulipEmailForwardError("Bad stream token from email recipient " + email)

    return Stream.objects.get(email_token=token)
Beispiel #7
0
    def test_decode_ignores_stream_name(self) -> None:
        stream = get_stream("Denmark", get_realm("zulip"))
        stream_to_address = encode_email_address(stream)
        stream_to_address = stream_to_address.replace("denmark", "Some_name")

        # get the email_token:
        tup = decode_email_address(stream_to_address)
        assert tup is not None
        token = tup[0]
        self.assertEqual(token, stream.email_token)
Beispiel #8
0
def extract_and_validate(email):
    try:
        stream_name, token = decode_email_address(email)
    except (TypeError, ValueError):
        raise ZulipEmailForwardError("Malformed email recipient " + email)

    if not valid_stream(stream_name, token):
        raise ZulipEmailForwardError("Bad stream token from email recipient " + email)

    return Stream.objects.get(email_token=token)
def extract_and_validate(email):
    try:
        stream_name, token = decode_email_address(email)
    except (TypeError, ValueError):
        raise ZulipEmailForwardError("Malformed email recipient " + email)

    if not valid_stream(stream_name, token):
        raise ZulipEmailForwardError("Bad stream token from email recipient " + email)

    return Stream.objects.get(email_token=token)
Beispiel #10
0
def extract_and_validate(email):
    # type: (Text) -> Stream
    temp = decode_email_address(email)
    if temp is None:
        raise ZulipEmailForwardError("Malformed email recipient " + email)
    stream_name, token = temp

    if not valid_stream(stream_name, token):
        raise ZulipEmailForwardError("Bad stream token from email recipient " + email)

    return Stream.objects.get(email_token=token)
Beispiel #11
0
def extract_and_validate(email: str) -> Tuple[Stream, bool]:
    temp = decode_email_address(email)
    if temp is None:
        raise ZulipEmailForwardError("Malformed email recipient " + email)
    token, show_sender = temp

    try:
        stream = Stream.objects.get(email_token=token)
    except Stream.DoesNotExist:
        raise ZulipEmailForwardError("Bad stream token from email recipient " +
                                     email)

    return stream, show_sender
Beispiel #12
0
    def test_error_to_stream_with_wrong_address(self) -> None:
        stream = get_stream("Denmark", get_realm("zulip"))
        stream_to_address = encode_email_address(stream)
        # get the email_token:
        tup = decode_email_address(stream_to_address)
        assert tup is not None
        token = tup[0]
        stream_to_address = stream_to_address.replace(token, "Wrong_token")

        result = self.send_offline_message(stream_to_address,
                                           self.example_email('hamlet'))
        self.assert_json_error(
            result, "5.1.1 Bad destination mailbox address: "
            "Please use the address specified in your Streams page.")