Skip to content

Commit

Permalink
get_realm: raise DoesNotExist instead of returning None.
Browse files Browse the repository at this point in the history
This makes the implementation of `get_realm` consistent with its
declared return type of `Realm` rather than `Optional[Realm]`.

Fixes zulip#12263.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored and timabbott committed May 7, 2019
1 parent 1f76374 commit 9efda71
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 75 deletions.
17 changes: 10 additions & 7 deletions analytics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ def stats(request: HttpRequest) -> HttpResponse:
@require_server_admin
@has_request_variables
def stats_for_realm(request: HttpRequest, realm_str: str) -> HttpResponse:
realm = get_realm(realm_str)
if realm is None:
try:
realm = get_realm(realm_str)
except Realm.DoesNotExist:
return HttpResponseNotFound("Realm %s does not exist" % (realm_str,))

return render_stats(request, '/realm/%s' % (realm_str,), realm.name or realm.string_id)
Expand All @@ -100,8 +101,9 @@ def stats_for_remote_realm(request: HttpRequest, remote_server_id: str,
@has_request_variables
def get_chart_data_for_realm(request: HttpRequest, user_profile: UserProfile,
realm_str: str, **kwargs: Any) -> HttpResponse:
realm = get_realm(realm_str)
if realm is None:
try:
realm = get_realm(realm_str)
except Realm.DoesNotExist:
raise JsonableError(_("Invalid organization"))

return get_chart_data(request=request, user_profile=user_profile, realm=realm, **kwargs)
Expand Down Expand Up @@ -1088,9 +1090,10 @@ def support(request: HttpRequest) -> HttpResponse:
if parse_result.port:
hostname = "{}:{}".format(hostname, parse_result.port)
subdomain = get_subdomain_from_hostname(hostname)
realm = get_realm(subdomain)
if realm is not None:
realms.add(realm)
try:
realms.add(get_realm(subdomain))
except Realm.DoesNotExist:
pass
except ValidationError:
pass

Expand Down
10 changes: 6 additions & 4 deletions zerver/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

from typing import Any, Dict
from typing import Any, Dict, Optional
from django.http import HttpRequest
from django.conf import settings
from django.urls import reverse
Expand Down Expand Up @@ -35,7 +34,7 @@ def common_context(user: UserProfile) -> Dict[str, Any]:
'user_name': user.full_name,
}

def get_realm_from_request(request: HttpRequest) -> Realm:
def get_realm_from_request(request: HttpRequest) -> Optional[Realm]:
if hasattr(request, "user") and hasattr(request.user, "realm"):
return request.user.realm
if not hasattr(request, "realm"):
Expand All @@ -44,7 +43,10 @@ def get_realm_from_request(request: HttpRequest) -> Realm:
# need to do duplicate queries on the same realm while
# processing a single request.
subdomain = get_subdomain(request)
request.realm = get_realm(subdomain)
try:
request.realm = get_realm(subdomain)
except Realm.DoesNotExist:
request.realm = None
return request.realm

def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
Expand Down
11 changes: 8 additions & 3 deletions zerver/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def check_subdomain_available(subdomain: str, from_management_command: bool=Fals
if len(subdomain) < 3:
raise ValidationError(error_strings['too short'])
if is_reserved_subdomain(subdomain) or \
get_realm(subdomain) is not None:
Realm.objects.filter(string_id=subdomain).exists():
raise ValidationError(error_strings['unavailable'])

class RegistrationForm(forms.Form):
Expand Down Expand Up @@ -279,7 +279,10 @@ def clean(self) -> Dict[str, Any]:

if username is not None and password:
subdomain = get_subdomain(self.request)
realm = get_realm(subdomain)
try:
realm = get_realm(subdomain) # type: Optional[Realm]
except Realm.DoesNotExist:
realm = None
return_data = {} # type: Dict[str, Any]
self.user_cache = authenticate(self.request, username=username, password=password,
realm=realm, return_data=return_data)
Expand Down Expand Up @@ -356,6 +359,8 @@ class RealmRedirectForm(forms.Form):

def clean_subdomain(self) -> str:
subdomain = self.cleaned_data['subdomain']
if get_realm(subdomain) is None:
try:
get_realm(subdomain)
except Realm.DoesNotExist:
raise ValidationError(_("We couldn't find that Zulip organization."))
return subdomain
5 changes: 2 additions & 3 deletions zerver/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
ScheduledEmail, MAX_TOPIC_NAME_LENGTH, \
MAX_MESSAGE_LENGTH, get_client, get_stream, get_personal_recipient, \
get_user_profile_by_id, PreregistrationUser, \
get_realm, bulk_get_recipients, get_stream_recipient, get_stream_recipients, \
bulk_get_recipients, get_stream_recipient, get_stream_recipients, \
email_allowed_for_realm, email_to_username, display_recipient_cache_key, \
get_user_by_delivery_email, get_stream_cache_key, active_non_guest_user_ids, \
UserActivityInterval, active_user_ids, get_active_streams, \
Expand Down Expand Up @@ -3536,8 +3536,7 @@ def do_change_stream_description(stream: Stream, new_description: str) -> None:

def do_create_realm(string_id: str, name: str,
emails_restricted_to_domains: Optional[bool]=None) -> Realm:
existing_realm = get_realm(string_id)
if existing_realm is not None:
if Realm.objects.filter(string_id=string_id).exists():
raise AssertionError("Realm %s already exists!" % (string_id,))

kwargs = {} # type: Dict[str, Any]
Expand Down
4 changes: 2 additions & 2 deletions zerver/lib/subdomains.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
from typing import Optional

from zerver.models import get_realm, Realm, UserProfile
from zerver.models import Realm, UserProfile

def get_subdomain(request: HttpRequest) -> str:

Expand Down Expand Up @@ -51,4 +51,4 @@ def user_matches_subdomain(realm_subdomain: Optional[str], user_profile: UserPro
def is_root_domain_available() -> bool:
if settings.ROOT_DOMAIN_LANDING_PAGE:
return False
return get_realm(Realm.SUBDOMAIN_FOR_ROOT_DOMAIN) is None
return not Realm.objects.filter(string_id=Realm.SUBDOMAIN_FOR_ROOT_DOMAIN).exists()
5 changes: 3 additions & 2 deletions zerver/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@ def process_response(self, request: HttpRequest, response: HttpResponse) -> Http
not request.path.startswith("/json/")):
subdomain = get_subdomain(request)
if subdomain != Realm.SUBDOMAIN_FOR_ROOT_DOMAIN:
realm = get_realm(subdomain)
if (realm is None):
try:
get_realm(subdomain)
except Realm.DoesNotExist:
return render(request, "zerver/invalid_realm.html", status=404)
"""
If request.session was modified, or if the configuration is to save the
Expand Down
13 changes: 4 additions & 9 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ class Meta:
post_save.connect(flush_realm, sender=Realm)

def get_realm(string_id: str) -> Realm:
return Realm.objects.filter(string_id=string_id).first()
return Realm.objects.get(string_id=string_id)

def name_changes_disabled(realm: Optional[Realm]) -> bool:
if realm is None:
Expand Down Expand Up @@ -1949,7 +1949,7 @@ def get_system_bot(email: str) -> UserProfile:

def get_user_by_id_in_realm_including_cross_realm(
uid: int,
realm: Realm
realm: Optional[Realm]
) -> UserProfile:
user_profile = get_user_profile_by_id(uid)
if user_profile.realm == realm:
Expand Down Expand Up @@ -1986,14 +1986,9 @@ def active_non_guest_user_ids(realm_id: int) -> List[int]:
return list(query)

def get_source_profile(email: str, string_id: str) -> Optional[UserProfile]:
realm = get_realm(string_id)

if realm is None:
return None

try:
return get_user_by_delivery_email(email, realm)
except UserProfile.DoesNotExist:
return get_user_by_delivery_email(email, get_realm(string_id))
except (Realm.DoesNotExist, UserProfile.DoesNotExist):
return None

@cache_with_key(bot_dicts_in_realm_cache_key, timeout=3600*24*7)
Expand Down
6 changes: 3 additions & 3 deletions zerver/tests/test_auth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_google_backend(self) -> None:
with mock.patch('apiclient.sample_tools.client.verify_id_token', return_value=payload):
self.verify_backend(backend,
good_kwargs=dict(realm=get_realm("zulip")),
bad_kwargs=dict(realm=get_realm('invalid')))
bad_kwargs=dict(realm=None))

# Verify valid_attestation parameter is set correctly
unverified_payload = dict(email_verified=False)
Expand Down Expand Up @@ -301,7 +301,7 @@ def test_ldap_backend(self) -> None:
self.verify_backend(backend,
bad_kwargs=dict(username=username,
password=password,
realm=get_realm('acme')),
realm=None),
good_kwargs=dict(username=username,
password=password,
realm=get_realm('zulip')))
Expand All @@ -311,7 +311,7 @@ def test_devauth_backend(self) -> None:
good_kwargs=dict(dev_auth_username=self.get_username(),
realm=get_realm("zulip")),
bad_kwargs=dict(dev_auth_username=self.get_username(),
realm=get_realm("invalid")))
realm=None))

@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',))
def test_remote_user_backend(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions zerver/tests/test_custom_profile_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from typing import Union, List, Dict, Any

from zerver.lib.actions import get_realm, try_add_realm_custom_profile_field, \
from zerver.lib.actions import try_add_realm_custom_profile_field, \
do_update_user_custom_profile_data, do_remove_realm_custom_profile_field, \
try_reorder_realm_custom_profile_fields
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.bugdown import convert as bugdown_convert
from zerver.models import CustomProfileField, \
custom_profile_fields_for_realm, CustomProfileFieldValue
custom_profile_fields_for_realm, CustomProfileFieldValue, get_realm
import ujson

class CustomProfileFieldTest(ZulipTestCase):
Expand Down
3 changes: 2 additions & 1 deletion zerver/tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ def test_generate_link_and_create_realm(self) -> None:
self.assert_in_success_response([u"Create a new Zulip organization"], result)

# Enter email
self.assertIsNone(get_realm('test'))
with self.assertRaises(Realm.DoesNotExist):
get_realm('test')
result = self.client_post(generated_link, {'email': email})
self.assertEqual(result.status_code, 302)
self.assertTrue(re.search(r'/accounts/do_confirm/\w+$', result["Location"]))
Expand Down
2 changes: 1 addition & 1 deletion zerver/tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3619,7 +3619,7 @@ def test_bot_pm_error_handling(self) -> None:
)
content = 'whatever'
good_realm = test_bot.realm
wrong_realm = get_realm('mit')
wrong_realm = get_realm("zephyr")
wrong_sender = cordelia

send_rate_limited_pm_notification_to_bot_owner(test_bot, wrong_realm, content)
Expand Down
3 changes: 2 additions & 1 deletion zerver/tests/test_realm.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def test_do_change_realm_subdomain_clears_user_realm_cache(self) -> None:
user = get_user_profile_by_email('[email protected]')
self.assertEqual(user.realm.string_id, "newzulip")
# This doesn't use a cache right now, but may later.
self.assertIsNone(get_realm("zulip"))
with self.assertRaises(Realm.DoesNotExist):
get_realm("zulip")

def test_do_deactivate_realm_clears_scheduled_jobs(self) -> None:
user = self.example_user('hamlet')
Expand Down
4 changes: 2 additions & 2 deletions zerver/tests/test_realm_emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import mock

from zerver.lib.actions import do_create_realm, do_create_user, \
get_realm, check_add_realm_emoji
check_add_realm_emoji
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import get_test_image_file
from zerver.models import Realm, RealmEmoji, UserProfile
from zerver.models import Realm, RealmEmoji, UserProfile, get_realm

class RealmEmojiTest(ZulipTestCase):

Expand Down
4 changes: 2 additions & 2 deletions zerver/tests/test_realm_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import re

from zerver.lib.actions import get_realm, do_add_realm_filter
from zerver.lib.actions import do_add_realm_filter
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmFilter
from zerver.models import RealmFilter, get_realm

class RealmFilterTest(ZulipTestCase):

Expand Down
11 changes: 5 additions & 6 deletions zerver/tests/test_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def test_register(self) -> None:
with queries_captured() as queries:
self.register(self.nonreg_email('test'), "test")
# Ensure the number of queries we make is not O(streams)
self.assert_length(queries, 78)
self.assert_length(queries, 77)
user_profile = self.nonreg_user('test')
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
self.assertFalse(user_profile.enable_stream_desktop_notifications)
Expand Down Expand Up @@ -1674,9 +1674,9 @@ class RealmCreationTest(ZulipTestCase):
def check_able_to_create_realm(self, email: str) -> None:
password = "test"
string_id = "zuliptest"
realm = get_realm(string_id)
# Make sure the realm does not exist
self.assertIsNone(realm)
with self.assertRaises(Realm.DoesNotExist):
get_realm(string_id)

# Create new realm with the email
result = self.client_post('/new/', {'email': email})
Expand All @@ -1697,7 +1697,6 @@ def check_able_to_create_realm(self, email: str) -> None:

# Make sure the realm is created
realm = get_realm(string_id)
self.assertIsNotNone(realm)
self.assertEqual(realm.string_id, string_id)
self.assertEqual(get_user(email, realm).realm, realm)

Expand Down Expand Up @@ -1748,7 +1747,8 @@ def test_create_realm_with_subdomain(self) -> None:
realm_name = "Test"

# Make sure the realm does not exist
self.assertIsNone(get_realm(string_id))
with self.assertRaises(Realm.DoesNotExist):
get_realm(string_id)

# Create new realm with the email
result = self.client_post('/new/', {'email': email})
Expand All @@ -1772,7 +1772,6 @@ def test_create_realm_with_subdomain(self) -> None:

# Make sure the realm is created
realm = get_realm(string_id)
self.assertIsNotNone(realm)
self.assertEqual(realm.string_id, string_id)
self.assertEqual(get_user(email, realm).realm, realm)

Expand Down
4 changes: 2 additions & 2 deletions zerver/tests/test_subs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
Realm, Recipient, Stream, Subscription,
DefaultStream, UserProfile, get_user_profile_by_id, active_non_guest_user_ids,
get_default_stream_groups, flush_per_request_caches, DefaultStreamGroup,
get_client, get_user
get_client, get_realm, get_user
)

from zerver.lib.actions import (
do_add_default_stream, do_change_is_admin, do_set_realm_property,
do_create_realm, do_remove_default_stream, bulk_get_subscriber_user_ids,
gather_subscriptions_helper, bulk_add_subscriptions, bulk_remove_subscriptions,
gather_subscriptions, get_default_streams_for_realm, get_realm, get_stream,
gather_subscriptions, get_default_streams_for_realm, get_stream,
do_get_streams, do_change_is_guest,
create_stream_if_needed, create_streams_if_needed,
ensure_stream,
Expand Down
4 changes: 2 additions & 2 deletions zerver/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ def test_get_user_by_id_in_realm_including_cross_realm(self) -> None:
# Pass in the ID of a cross-realm bot but with a invalid realm,
# note that the realm should be irrelevant here
cross_realm_bot = get_user_by_id_in_realm_including_cross_realm(
bot.id, get_realm('invalid'))
bot.id, None)
self.assertEqual(cross_realm_bot.email, bot.email)
self.assertEqual(cross_realm_bot.id, bot.id)

Expand All @@ -793,7 +793,7 @@ def test_get_user_by_id_in_realm_including_cross_realm(self) -> None:
# cross-realm bot, UserProfile.DoesNotExist is raised
with self.assertRaises(UserProfile.DoesNotExist):
get_user_by_id_in_realm_including_cross_realm(
hamlet.id, get_realm('invalid'))
hamlet.id, None)

class ActivateTest(ZulipTestCase):
def test_basics(self) -> None:
Expand Down
Loading

0 comments on commit 9efda71

Please sign in to comment.