Fix bug where we hard code realm for PushDeviceToken.

This had no test coverage, which is part of the reason it went
undetected, plus many instances probably only have one realm
with realm_id=1.
This commit is contained in:
Steve Howell
2017-09-08 08:55:46 -07:00
committed by showell
parent 730da55bf8
commit b6bb7f2b1e
2 changed files with 32 additions and 1 deletions

View File

@@ -1670,7 +1670,7 @@ class UserPresence(models.Model):
) )
mobile_user_ids = [row['user'] for row in PushDeviceToken.objects.filter( mobile_user_ids = [row['user'] for row in PushDeviceToken.objects.filter(
user__realm_id=1, user__realm_id=realm_id,
user__is_active=True, user__is_active=True,
user__is_bot=False, user__is_bot=False,
).distinct("user").values("user")] ).distinct("user").values("user")]

View File

@@ -19,6 +19,7 @@ from zerver.lib.timestamp import datetime_to_timestamp
from zerver.models import ( from zerver.models import (
email_to_domain, email_to_domain,
Client, Client,
PushDeviceToken,
UserActivity, UserActivity,
UserProfile, UserProfile,
UserPresence, UserPresence,
@@ -92,6 +93,36 @@ class UserPresenceModelTests(ZulipTestCase):
presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id) presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id)
self.assertEqual(len(presence_dct), 0) self.assertEqual(len(presence_dct), 0)
def test_push_tokens(self):
# type: () -> None
UserPresence.objects.all().delete()
user_profile = self.example_user('hamlet')
email = user_profile.email
self.login(email)
result = self.client_post("/json/users/me/presence", {'status': 'active'})
self.assert_json_success(result)
def pushable():
# type: () -> bool
presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id)
self.assertEqual(len(presence_dct), 1)
return presence_dct[email]['website']['pushable']
self.assertFalse(pushable())
user_profile.enable_offline_push_notifications = True
user_profile.save()
self.assertFalse(pushable())
PushDeviceToken.objects.create(
user=user_profile,
kind=PushDeviceToken.APNS
)
self.assertTrue(pushable())
class UserPresenceTests(ZulipTestCase): class UserPresenceTests(ZulipTestCase):
def test_invalid_presence(self): def test_invalid_presence(self):
# type: () -> None # type: () -> None