Extract a zephyr.py library.

Right now it only has one function, but the function
we removed never really belonged in actions.py, and
now we have better test coverage on actions.py, which
is an important module to get to 100%.
This commit is contained in:
Steve Howell 2018-08-11 14:26:46 +00:00 committed by Tim Abbott
parent 5bc33213c9
commit 413a0174f4
6 changed files with 33 additions and 26 deletions

View File

@ -90,6 +90,7 @@ not_yet_fully_covered = {
'zerver/lib/timeout.py',
'zerver/lib/unminify.py',
'zerver/lib/utils.py',
'zerver/lib/zephyr.py',
# Low priority for coverage
'zerver/lib/ccache.py',
'zerver/lib/generate_test_data.py',

View File

@ -67,6 +67,7 @@ from zerver.lib.users import (
user_ids_to_users
)
from zerver.lib.user_groups import create_user_group, access_user_group_by_id
from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \
RealmDomain, Service, SubMessage, \
Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, \
@ -126,16 +127,14 @@ from zerver.lib.exceptions import JsonableError, ErrorCode, BugdownRenderingExce
from zerver.lib.sessions import delete_user_sessions
from zerver.lib.upload import attachment_url_re, attachment_url_to_path_id, \
claim_attachment, delete_message_image, upload_emoji_image
from zerver.lib.str_utils import NonBinaryStr, force_str
from zerver.lib.str_utils import NonBinaryStr
from zerver.tornado.event_queue import request_event_queue, send_event
from zerver.lib.types import ProfileFieldData
from analytics.models import StreamCount
import DNS
import ujson
import time
import traceback
import re
import datetime
import os
@ -858,26 +857,6 @@ def compute_irc_user_fullname(email: NonBinaryStr) -> NonBinaryStr:
def compute_jabber_user_fullname(email: NonBinaryStr) -> NonBinaryStr:
return email.split("@")[0] + " (XMPP)"
def compute_mit_user_fullname(email: NonBinaryStr) -> NonBinaryStr:
try:
# Input is either e.g. username@mit.edu or user|CROSSREALM.INVALID@mit.edu
match_user = re.match(r'^([a-zA-Z0-9_.-]+)(\|.+)?@mit\.edu$', email.lower())
if match_user and match_user.group(2) is None:
answer = DNS.dnslookup(
"%s.passwd.ns.athena.mit.edu" % (match_user.group(1),),
DNS.Type.TXT)
hesiod_name = force_str(answer[0][0]).split(':')[4].split(',')[0].strip()
if hesiod_name != "":
return hesiod_name
elif match_user:
return match_user.group(1).lower() + "@" + match_user.group(2).upper()[1:]
except DNS.Base.ServerError:
pass
except Exception:
print("Error getting fullname for %s:" % (email,))
traceback.print_exc()
return email.lower()
@cache_with_key(lambda realm, email, f: user_profile_by_email_cache_key(email),
timeout=3600*24*7)
def create_mirror_user_if_needed(realm: Realm, email: str,

25
zerver/lib/zephyr.py Normal file
View File

@ -0,0 +1,25 @@
import re
import traceback
import DNS
from zerver.lib.str_utils import NonBinaryStr, force_str
def compute_mit_user_fullname(email: NonBinaryStr) -> NonBinaryStr:
try:
# Input is either e.g. username@mit.edu or user|CROSSREALM.INVALID@mit.edu
match_user = re.match(r'^([a-zA-Z0-9_.-]+)(\|.+)?@mit\.edu$', email.lower())
if match_user and match_user.group(2) is None:
answer = DNS.dnslookup(
"%s.passwd.ns.athena.mit.edu" % (match_user.group(1),),
DNS.Type.TXT)
hesiod_name = force_str(answer[0][0]).split(':')[4].split(',')[0].strip()
if hesiod_name != "":
return hesiod_name
elif match_user:
return match_user.group(1).lower() + "@" + match_user.group(2).upper()[1:]
except DNS.Base.ServerError:
pass
except Exception:
print("Error getting fullname for %s:" % (email,))
traceback.print_exc()
return email.lower()

View File

@ -11,8 +11,8 @@ from zerver.lib.rate_limiter import (
remove_ratelimit_rule,
RateLimitedUser,
)
from zerver.lib.zephyr import compute_mit_user_fullname
from zerver.lib.actions import compute_mit_user_fullname
from zerver.lib.test_classes import (
ZulipTestCase,
)

View File

@ -15,7 +15,7 @@ from django.utils.html import escape as escape_html
from zerver.lib import bugdown
from zerver.lib.zcommand import process_zcommands
from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \
compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \
compute_irc_user_fullname, compute_jabber_user_fullname, \
create_mirror_user_if_needed, check_send_message, do_update_message, \
extract_recipients, truncate_body, render_incoming_message, do_delete_message, \
do_mark_all_as_read, do_mark_stream_messages_as_read, \
@ -36,6 +36,7 @@ from zerver.lib.topic_mutes import exclude_topic_mutes
from zerver.lib.utils import statsd
from zerver.lib.validator import \
check_list, check_int, check_dict, check_string, check_bool
from zerver.lib.zephyr import compute_mit_user_fullname
from zerver.models import Message, UserProfile, Stream, Subscription, Client,\
Realm, RealmDomain, Recipient, UserMessage, bulk_get_recipients, get_personal_recipient, \
get_stream, email_to_domain, get_realm, get_active_streams, \

View File

@ -20,7 +20,7 @@ from zerver.lib.send_email import send_email, FromAddress
from zerver.lib.events import do_events_register
from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \
do_activate_user, do_create_user, do_create_realm, \
email_not_system_bot, compute_mit_user_fullname, validate_email_for_realm, \
email_not_system_bot, validate_email_for_realm, \
do_set_user_display_setting, lookup_default_stream_groups, bulk_add_subscriptions
from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, \
CreateUserForm, FindMyTeamForm
@ -33,6 +33,7 @@ from zerver.lib.response import json_success
from zerver.lib.subdomains import get_subdomain, is_root_domain_available
from zerver.lib.timezone import get_all_timezones
from zerver.lib.users import get_accounts_for_email
from zerver.lib.zephyr import compute_mit_user_fullname
from zerver.views.auth import create_preregistration_user, \
redirect_and_log_into_subdomain, \
redirect_to_deactivation_notice