subdomains: Extract zerver.lib.subdomains library.

These never really belonged with the rest of zerver.lib.utils.py, and
having a separate library makes it easier to enforce full test
coverage.
This commit is contained in:
Tim Abbott 2017-10-18 22:21:57 -07:00
parent 7107d19aeb
commit 1ab2ca5986
12 changed files with 47 additions and 40 deletions

View File

@ -95,6 +95,7 @@ not_yet_fully_covered = {
'zerver/lib/sqlalchemy_utils.py', 'zerver/lib/sqlalchemy_utils.py',
'zerver/lib/str_utils.py', 'zerver/lib/str_utils.py',
'zerver/lib/stream_recipient.py', 'zerver/lib/stream_recipient.py',
'zerver/lib/subdomains.py',
'zerver/lib/tex.py', 'zerver/lib/tex.py',
'zerver/lib/timeout.py', 'zerver/lib/timeout.py',
'zerver/lib/unminify.py', 'zerver/lib/unminify.py',

View File

@ -17,7 +17,7 @@ from zproject.backends import (
) )
from zerver.lib.bugdown import convert from zerver.lib.bugdown import convert
from zerver.lib.send_email import FromAddress from zerver.lib.send_email import FromAddress
from zerver.lib.utils import get_subdomain from zerver.lib.subdomains import get_subdomain
from zerver.lib.realm_icon import get_realm_icon_url from zerver.lib.realm_icon import get_realm_icon_url
from version import ZULIP_VERSION from version import ZULIP_VERSION

View File

@ -12,9 +12,9 @@ from django.utils.decorators import available_attrs
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from django.conf import settings from django.conf import settings
from zerver.lib.queue import queue_json_publish from zerver.lib.queue import queue_json_publish
from zerver.lib.subdomains import get_subdomain, check_subdomain
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
from zerver.lib.utils import statsd, get_subdomain, check_subdomain, \ from zerver.lib.utils import statsd, is_remote_server
is_remote_server
from zerver.lib.exceptions import RateLimited, JsonableError, ErrorCode from zerver.lib.exceptions import RateLimited, JsonableError, ErrorCode
from zerver.lib.rate_limiter import incr_ratelimit, is_ratelimited, \ from zerver.lib.rate_limiter import incr_ratelimit, is_ratelimited, \

View File

@ -15,8 +15,8 @@ from zerver.lib.actions import do_change_password, user_email_is_unique, \
from zerver.lib.name_restrictions import is_reserved_subdomain, is_disposable_domain from zerver.lib.name_restrictions import is_reserved_subdomain, is_disposable_domain
from zerver.lib.request import JsonableError from zerver.lib.request import JsonableError
from zerver.lib.send_email import send_email, FromAddress from zerver.lib.send_email import send_email, FromAddress
from zerver.lib.subdomains import get_subdomain, check_subdomain
from zerver.lib.users import check_full_name from zerver.lib.users import check_full_name
from zerver.lib.utils import get_subdomain, check_subdomain
from zerver.models import Realm, get_user_profile_by_email, UserProfile, \ from zerver.models import Realm, get_user_profile_by_email, UserProfile, \
get_realm, email_to_domain, email_allowed_for_realm get_realm, email_to_domain, email_allowed_for_realm
from zproject.backends import password_auth_enabled from zproject.backends import password_auth_enabled

34
zerver/lib/subdomains.py Normal file
View File

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.http import HttpRequest
from typing import Optional, Text
def _extract_subdomain(request):
# type: (HttpRequest) -> Text
domain = request.get_host().lower()
index = domain.find("." + settings.EXTERNAL_HOST)
if index == -1:
return ""
return domain[0:index]
def get_subdomain(request):
# type: (HttpRequest) -> Text
subdomain = _extract_subdomain(request)
if subdomain in settings.ROOT_SUBDOMAIN_ALIASES:
return ""
return subdomain
def is_subdomain_root_or_alias(request):
# type: (HttpRequest) -> bool
subdomain = _extract_subdomain(request)
return not subdomain or subdomain in settings.ROOT_SUBDOMAIN_ALIASES
def check_subdomain(realm_subdomain, user_subdomain):
# type: (Optional[Text], Optional[Text]) -> bool
if realm_subdomain is not None:
if (realm_subdomain == "" and user_subdomain is None):
return True
if realm_subdomain != user_subdomain:
return False
return True

View File

@ -12,7 +12,6 @@ import sys
from time import sleep from time import sleep
from django.conf import settings from django.conf import settings
from django.http import HttpRequest
from six.moves import map, zip_longest from six.moves import map, zip_longest
from zerver.lib.str_utils import force_text from zerver.lib.str_utils import force_text
@ -185,35 +184,6 @@ def query_chunker(queries, id_collector=None, chunk_size=1000, db_chunk_size=Non
yield [row for row_id, i, row in tup_chunk] yield [row for row_id, i, row in tup_chunk]
def _extract_subdomain(request):
# type: (HttpRequest) -> Text
domain = request.get_host().lower()
index = domain.find("." + settings.EXTERNAL_HOST)
if index == -1:
return ""
return domain[0:index]
def get_subdomain(request):
# type: (HttpRequest) -> Text
subdomain = _extract_subdomain(request)
if subdomain in settings.ROOT_SUBDOMAIN_ALIASES:
return ""
return subdomain
def is_subdomain_root_or_alias(request):
# type: (HttpRequest) -> bool
subdomain = _extract_subdomain(request)
return not subdomain or subdomain in settings.ROOT_SUBDOMAIN_ALIASES
def check_subdomain(realm_subdomain, user_subdomain):
# type: (Optional[Text], Optional[Text]) -> bool
if realm_subdomain is not None:
if (realm_subdomain == "" and user_subdomain is None):
return True
if realm_subdomain != user_subdomain:
return False
return True
def split_by(array, group_size, filler): def split_by(array, group_size, filler):
# type: (List[Any], int, Any) -> List[List[Any]] # type: (List[Any], int, Any) -> List[List[Any]]
""" """

View File

@ -8,10 +8,11 @@ from django.utils.translation import ugettext as _
from django.utils.deprecation import MiddlewareMixin from django.utils.deprecation import MiddlewareMixin
from zerver.lib.response import json_error, json_response_from_error from zerver.lib.response import json_error, json_response_from_error
from zerver.lib.subdomains import get_subdomain
from zerver.lib.exceptions import JsonableError, ErrorCode from zerver.lib.exceptions import JsonableError, ErrorCode
from django.db import connection from django.db import connection
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
from zerver.lib.utils import statsd, get_subdomain from zerver.lib.utils import statsd
from zerver.lib.queue import queue_json_publish from zerver.lib.queue import queue_json_publish
from zerver.lib.cache import get_remote_cache_time, get_remote_cache_requests from zerver.lib.cache import get_remote_cache_time, get_remote_cache_requests
from zerver.lib.bugdown import get_bugdown_time, get_bugdown_requests from zerver.lib.bugdown import get_bugdown_time, get_bugdown_requests

View File

@ -26,7 +26,7 @@ from zerver.forms import HomepageForm, OurAuthenticationForm, \
from zerver.lib.mobile_auth_otp import is_valid_otp, otp_encrypt_api_key from zerver.lib.mobile_auth_otp import is_valid_otp, otp_encrypt_api_key
from zerver.lib.request import REQ, has_request_variables, JsonableError from zerver.lib.request import REQ, has_request_variables, JsonableError
from zerver.lib.response import json_success, json_error from zerver.lib.response import json_success, json_error
from zerver.lib.utils import get_subdomain, is_subdomain_root_or_alias from zerver.lib.subdomains import get_subdomain, is_subdomain_root_or_alias
from zerver.lib.validator import validate_login_email from zerver.lib.validator import validate_login_email
from zerver.models import PreregistrationUser, UserProfile, remote_user_to_email, Realm, \ from zerver.models import PreregistrationUser, UserProfile, remote_user_to_email, Realm, \
get_realm get_realm

View File

@ -24,7 +24,8 @@ from zerver.lib.i18n import get_language_list, get_language_name, \
get_language_list_for_templates get_language_list_for_templates
from zerver.lib.push_notifications import num_push_devices_for_user from zerver.lib.push_notifications import num_push_devices_for_user
from zerver.lib.streams import access_stream_by_name from zerver.lib.streams import access_stream_by_name
from zerver.lib.utils import statsd, get_subdomain from zerver.lib.subdomains import get_subdomain
from zerver.lib.utils import statsd
import calendar import calendar
import datetime import datetime

View File

@ -12,7 +12,7 @@ import ujson
from zerver.decorator import has_request_variables, REQ from zerver.decorator import has_request_variables, REQ
from zerver.lib import bugdown from zerver.lib import bugdown
from zerver.lib.integrations import CATEGORIES, INTEGRATIONS, HUBOT_LOZENGES from zerver.lib.integrations import CATEGORIES, INTEGRATIONS, HUBOT_LOZENGES
from zerver.lib.utils import get_subdomain from zerver.lib.subdomains import get_subdomain
from zerver.templatetags.app_filters import render_markdown_path from zerver.templatetags.app_filters import render_markdown_path
def add_api_uri_context(context, request): def add_api_uri_context(context, request):

View File

@ -29,7 +29,7 @@ from zerver.decorator import require_post, has_request_variables, \
from zerver.lib.onboarding import send_initial_pms, setup_initial_streams, \ from zerver.lib.onboarding import send_initial_pms, setup_initial_streams, \
setup_initial_private_stream, send_initial_realm_messages setup_initial_private_stream, send_initial_realm_messages
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.utils import get_subdomain from zerver.lib.subdomains import get_subdomain
from zerver.lib.timezone import get_all_timezones from zerver.lib.timezone import get_all_timezones
from zproject.backends import password_auth_enabled from zproject.backends import password_auth_enabled

View File

@ -25,7 +25,7 @@ from social_core.exceptions import AuthFailed, SocialAuthBaseException
from django.contrib.auth import authenticate from django.contrib.auth import authenticate
from zerver.lib.users import check_full_name from zerver.lib.users import check_full_name
from zerver.lib.request import JsonableError from zerver.lib.request import JsonableError
from zerver.lib.utils import check_subdomain, get_subdomain from zerver.lib.subdomains import check_subdomain, get_subdomain
from social_django.models import DjangoStorage from social_django.models import DjangoStorage
from social_django.strategy import DjangoStrategy from social_django.strategy import DjangoStrategy