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: token = decode_email_address(stream_to_address)[0] self.assertEqual(token, stream.email_token)
def extract_and_validate(email: str) -> Tuple[Stream, Dict[str, bool]]: token, options = decode_email_address(email) try: stream = Stream.objects.get(email_token=token) except Stream.DoesNotExist: raise ZulipEmailForwardError("Bad stream token from email recipient " + email) return stream, options
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: token = decode_email_address(stream_to_address)[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.")
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) 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: token, show_sender = decode_email_address(email_address) 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+"))
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')) token, show_sender = decode_email_address(email_address) self.assertFalse(show_sender) self.assertEqual(token, stream.email_token) parts = email_address.split('@') parts[0] += "+show-sender" email_address_show = '@'.join(parts) token, show_sender = decode_email_address(email_address_show) self.assertTrue(show_sender) self.assertEqual(token, stream.email_token) email_address_dots = email_address.replace('+', '.') token, show_sender = decode_email_address(email_address_dots) self.assertFalse(show_sender) self.assertEqual(token, stream.email_token) email_address_dots_show = email_address_show.replace('+', '.') token, show_sender = decode_email_address(email_address_dots_show) 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') with self.assertRaises(ZulipEmailForwardError): decode_email_address(email_address) with self.assertRaises(ZulipEmailForwardError): decode_email_address(email_address_show) with self.settings(EMAIL_GATEWAY_EXTRA_PATTERN_HACK='@zulip.org'): token, show_sender = decode_email_address(email_address) self.assertFalse(show_sender) self.assertEqual(token, stream.email_token) token, show_sender = decode_email_address(email_address_show) self.assertTrue(show_sender) self.assertEqual(token, stream.email_token) with self.assertRaises(ZulipEmailForwardError): decode_email_address('bogus')