2012-08-31 20:11:15 +02:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils.timezone import utc
|
|
|
|
from django.db.models import Q
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-10-10 22:53:24 +02:00
|
|
|
from zephyr.models import Message, UserProfile, Stream, Recipient, Subscription, \
|
2012-10-22 19:23:11 +02:00
|
|
|
filter_by_subscriptions, Realm, do_send_message, Client
|
2012-10-16 21:42:40 +02:00
|
|
|
from zephyr.views import json_get_updates
|
2012-09-06 23:55:04 +02:00
|
|
|
from zephyr.decorator import TornadoAsyncException
|
2012-10-12 17:49:22 +02:00
|
|
|
from zephyr.lib.initial_password import initial_password
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-08-31 20:11:15 +02:00
|
|
|
import datetime
|
2012-09-06 20:53:13 +02:00
|
|
|
import simplejson
|
2012-09-04 22:31:56 +02:00
|
|
|
import subprocess
|
|
|
|
subprocess.call("zephyr/tests/generate-fixtures");
|
2012-09-27 19:58:42 +02:00
|
|
|
from django.conf import settings
|
2012-10-02 17:47:18 +02:00
|
|
|
import re
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-03 21:26:40 +02:00
|
|
|
settings.MESSAGE_LOG = "/tmp/test-message-log"
|
2012-10-02 17:47:18 +02:00
|
|
|
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
|
2012-10-12 21:27:19 +02:00
|
|
|
settings.HAVE_TORNADO_SERVER = False
|
2012-10-02 17:47:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def find_key_by_email(address):
|
|
|
|
from django.core.mail import outbox
|
|
|
|
key_regex = re.compile("accounts/do_confirm/([a-f0-9]{40})>")
|
|
|
|
for message in reversed(outbox):
|
|
|
|
if address in message.to:
|
|
|
|
return key_regex.search(message.body).groups()[0]
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
class AuthedTestCase(TestCase):
|
2012-10-12 17:49:22 +02:00
|
|
|
def login(self, email, password=None):
|
|
|
|
if password is None:
|
|
|
|
password = initial_password(email)
|
2012-08-31 20:11:15 +02:00
|
|
|
return self.client.post('/accounts/login/',
|
2012-10-12 17:49:22 +02:00
|
|
|
{'username':email, 'password':password})
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def register(self, username, password):
|
2012-10-02 17:47:18 +02:00
|
|
|
self.client.post('/accounts/home/',
|
|
|
|
{'email': username + '@humbughq.com'})
|
2012-08-31 20:11:15 +02:00
|
|
|
return self.client.post('/accounts/register/',
|
2012-09-11 23:26:04 +02:00
|
|
|
{'full_name':username, 'short_name':username,
|
2012-10-02 17:47:18 +02:00
|
|
|
'key': find_key_by_email(username + '@humbughq.com'),
|
2012-09-11 23:26:04 +02:00
|
|
|
'username':username, 'password':password,
|
|
|
|
'domain':'humbughq.com'})
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-22 20:15:25 +02:00
|
|
|
def get_user_profile(self, email):
|
2012-09-06 20:43:41 +02:00
|
|
|
"""
|
2012-09-21 00:26:59 +02:00
|
|
|
Given an email address, return the UserProfile object for the
|
|
|
|
User that has that email.
|
2012-09-06 20:43:41 +02:00
|
|
|
"""
|
|
|
|
# Usernames are unique, even across Realms.
|
2012-10-11 20:05:53 +02:00
|
|
|
return UserProfile.objects.get(user__email=email)
|
2012-09-06 20:43:41 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def send_message(self, sender_name, recipient_name, message_type):
|
2012-10-22 20:15:25 +02:00
|
|
|
sender = self.get_user_profile(sender_name)
|
2012-10-03 21:16:34 +02:00
|
|
|
if message_type == Recipient.PERSONAL:
|
2012-10-22 20:15:25 +02:00
|
|
|
recipient = self.get_user_profile(recipient_name)
|
2012-08-31 20:11:15 +02:00
|
|
|
else:
|
2012-10-10 22:53:24 +02:00
|
|
|
recipient = Stream.objects.get(name=recipient_name, realm=sender.realm)
|
2012-10-03 21:16:34 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=recipient.id, type=message_type)
|
2012-08-31 20:11:15 +02:00
|
|
|
pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
|
2012-10-22 19:23:11 +02:00
|
|
|
(sending_client, _) = Client.objects.get_or_create(name="test suite")
|
2012-10-19 21:37:37 +02:00
|
|
|
do_send_message(Message(sender=sender, recipient=recipient, subject="test",
|
|
|
|
pub_date=pub_date, sending_client=sending_client))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-10 23:13:04 +02:00
|
|
|
def users_subscribed_to_stream(self, stream_name, realm_domain):
|
2012-09-05 22:30:50 +02:00
|
|
|
realm = Realm.objects.get(domain=realm_domain)
|
2012-10-10 23:13:04 +02:00
|
|
|
stream = Stream.objects.get(name=stream_name, realm=realm)
|
2012-10-10 22:57:21 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
2012-09-05 21:55:40 +02:00
|
|
|
subscriptions = Subscription.objects.filter(recipient=recipient)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-22 20:15:25 +02:00
|
|
|
return [subscription.user_profile.user for subscription in subscriptions]
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def message_stream(self, user):
|
2012-10-03 21:05:48 +02:00
|
|
|
return filter_by_subscriptions(Message.objects.all(), user)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-06 21:34:38 +02:00
|
|
|
def assert_json_success(self, result):
|
|
|
|
"""
|
|
|
|
Successful POSTs return a 200 and JSON of the form {"result": "success",
|
|
|
|
"msg": ""}.
|
|
|
|
"""
|
|
|
|
self.assertEquals(result.status_code, 200)
|
|
|
|
json = simplejson.loads(result.content)
|
|
|
|
self.assertEquals(json.get("result"), "success")
|
|
|
|
# We have a msg key for consistency with errors, but it typically has an
|
|
|
|
# empty value.
|
|
|
|
self.assertTrue("msg" in json)
|
|
|
|
|
|
|
|
def assert_json_error(self, result, msg):
|
|
|
|
"""
|
|
|
|
Invalid POSTs return a 400 and JSON of the form {"result": "error",
|
|
|
|
"msg": "reason"}.
|
|
|
|
"""
|
|
|
|
self.assertEquals(result.status_code, 400)
|
|
|
|
json = simplejson.loads(result.content)
|
|
|
|
self.assertEquals(json.get("result"), "error")
|
|
|
|
self.assertEquals(json.get("msg"), msg)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
class PublicURLTest(TestCase):
|
|
|
|
"""
|
|
|
|
Account creation URLs are accessible even when not logged in. Authenticated
|
|
|
|
URLs redirect to a page.
|
|
|
|
"""
|
|
|
|
def fetch(self, urls, expected_status):
|
|
|
|
for url in urls:
|
|
|
|
response = self.client.get(url)
|
|
|
|
self.assertEqual(response.status_code, expected_status,
|
|
|
|
msg="Expected %d, received %d for %s" % (
|
|
|
|
expected_status, response.status_code, url))
|
|
|
|
|
|
|
|
def test_public_urls(self):
|
|
|
|
"""
|
|
|
|
Pages that should return a 200 when not logged in.
|
|
|
|
"""
|
2012-10-16 23:24:58 +02:00
|
|
|
# FIXME: We should also test the Tornado URLs -- this codepath
|
|
|
|
# can't do so because this Django test mechanism doesn't go
|
|
|
|
# through Tornado.
|
2012-10-02 17:47:18 +02:00
|
|
|
urls = {200: ["/accounts/home/", "/accounts/login/", "/accounts/logout/"],
|
2012-10-16 22:10:48 +02:00
|
|
|
302: ["/"],
|
|
|
|
400: ["/accounts/register/",
|
|
|
|
"/api/v1/get_public_streams",
|
|
|
|
"/api/v1/get_subscriptions",
|
|
|
|
"/api/v1/subscribe",
|
|
|
|
"/api/v1/send_message",
|
2012-10-17 23:12:44 +02:00
|
|
|
"/api/v1/fetch_api_key",
|
|
|
|
"/json/fetch_api_key",
|
2012-10-16 22:10:48 +02:00
|
|
|
"/json/send_message/",
|
|
|
|
"/json/update_pointer",
|
|
|
|
"/json/settings/change/",
|
|
|
|
"/json/subscriptions/list",
|
|
|
|
"/json/subscriptions/remove",
|
2012-10-17 20:05:17 +02:00
|
|
|
"/json/subscriptions/exists",
|
2012-10-16 22:10:48 +02:00
|
|
|
"/json/subscriptions/add"],
|
2012-08-31 20:11:15 +02:00
|
|
|
}
|
|
|
|
for status_code, url_set in urls.iteritems():
|
|
|
|
self.fetch(url_set, status_code)
|
|
|
|
|
|
|
|
|
|
|
|
class LoginTest(AuthedTestCase):
|
|
|
|
"""
|
|
|
|
Logging in, registration, and logging out.
|
|
|
|
"""
|
2012-10-03 21:16:34 +02:00
|
|
|
fixtures = ['messages.json']
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def test_login(self):
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-09-21 00:26:59 +02:00
|
|
|
user = User.objects.get(email='hamlet@humbughq.com')
|
2012-09-10 19:43:11 +02:00
|
|
|
self.assertEqual(self.client.session['_auth_user_id'], user.id)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def test_login_bad_password(self):
|
2012-09-21 16:10:36 +02:00
|
|
|
self.login("hamlet@humbughq.com", "wrongpassword")
|
2012-08-31 20:11:15 +02:00
|
|
|
self.assertIsNone(self.client.session.get('_auth_user_id', None))
|
|
|
|
|
|
|
|
def test_register(self):
|
|
|
|
self.register("test", "test")
|
2012-09-21 00:26:59 +02:00
|
|
|
user = User.objects.get(email='test@humbughq.com')
|
2012-09-10 19:43:11 +02:00
|
|
|
self.assertEqual(self.client.session['_auth_user_id'], user.id)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def test_logout(self):
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-08-31 20:11:15 +02:00
|
|
|
self.client.post('/accounts/logout/')
|
|
|
|
self.assertIsNone(self.client.session.get('_auth_user_id', None))
|
|
|
|
|
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
class PersonalMessagesTest(AuthedTestCase):
|
|
|
|
fixtures = ['messages.json']
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def test_auto_subbed_to_personals(self):
|
|
|
|
"""
|
|
|
|
Newly created users are auto-subbed to the ability to receive
|
|
|
|
personals.
|
|
|
|
"""
|
|
|
|
self.register("test", "test")
|
2012-09-21 00:26:59 +02:00
|
|
|
user = User.objects.get(email='test@humbughq.com')
|
2012-10-03 21:16:34 +02:00
|
|
|
old_messages = self.message_stream(user)
|
|
|
|
self.send_message("test@humbughq.com", "test@humbughq.com", Recipient.PERSONAL)
|
|
|
|
new_messages = self.message_stream(user)
|
|
|
|
self.assertEqual(len(new_messages) - len(old_messages), 1)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-10 19:43:11 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=user.id, type=Recipient.PERSONAL)
|
2012-10-03 21:16:34 +02:00
|
|
|
self.assertEqual(new_messages[-1].recipient, recipient)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def test_personal_to_self(self):
|
|
|
|
"""
|
|
|
|
If you send a personal to yourself, only you see it.
|
|
|
|
"""
|
|
|
|
old_users = list(User.objects.all())
|
|
|
|
self.register("test1", "test1")
|
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
old_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for user in old_users:
|
2012-10-03 21:16:34 +02:00
|
|
|
old_messages.append(len(self.message_stream(user)))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
self.send_message("test1@humbughq.com", "test1@humbughq.com", Recipient.PERSONAL)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
new_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for user in old_users:
|
2012-10-03 21:16:34 +02:00
|
|
|
new_messages.append(len(self.message_stream(user)))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
self.assertEqual(old_messages, new_messages)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
user = User.objects.get(email="test1@humbughq.com")
|
2012-09-10 19:43:11 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=user.id, type=Recipient.PERSONAL)
|
2012-10-03 21:16:34 +02:00
|
|
|
self.assertEqual(self.message_stream(user)[-1].recipient, recipient)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def test_personal(self):
|
|
|
|
"""
|
|
|
|
If you send a personal, only you and the recipient see it.
|
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
old_sender = User.objects.filter(email="hamlet@humbughq.com")
|
2012-10-03 21:16:34 +02:00
|
|
|
old_sender_messages = len(self.message_stream(old_sender))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
old_recipient = User.objects.filter(email="othello@humbughq.com")
|
2012-10-03 21:16:34 +02:00
|
|
|
old_recipient_messages = len(self.message_stream(old_recipient))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
other_users = User.objects.filter(~Q(email="hamlet@humbughq.com") & ~Q(email="othello@humbughq.com"))
|
2012-10-03 21:16:34 +02:00
|
|
|
old_other_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for user in other_users:
|
2012-10-03 21:16:34 +02:00
|
|
|
old_other_messages.append(len(self.message_stream(user)))
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
self.send_message("hamlet@humbughq.com", "othello@humbughq.com", Recipient.PERSONAL)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
# Users outside the conversation don't get the message.
|
|
|
|
new_other_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for user in other_users:
|
2012-10-03 21:16:34 +02:00
|
|
|
new_other_messages.append(len(self.message_stream(user)))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
self.assertEqual(old_other_messages, new_other_messages)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
# The personal message is in the streams of both the sender and receiver.
|
|
|
|
self.assertEqual(len(self.message_stream(old_sender)),
|
|
|
|
old_sender_messages + 1)
|
|
|
|
self.assertEqual(len(self.message_stream(old_recipient)),
|
|
|
|
old_recipient_messages + 1)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
sender = User.objects.get(email="hamlet@humbughq.com")
|
|
|
|
receiver = User.objects.get(email="othello@humbughq.com")
|
2012-09-10 19:43:11 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=receiver.id, type=Recipient.PERSONAL)
|
2012-10-03 21:16:34 +02:00
|
|
|
self.assertEqual(self.message_stream(sender)[-1].recipient, recipient)
|
|
|
|
self.assertEqual(self.message_stream(receiver)[-1].recipient, recipient)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
def test_personal_to_nonexistent_person(self):
|
2012-08-28 18:44:51 +02:00
|
|
|
"""
|
|
|
|
"""
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-10 23:13:04 +02:00
|
|
|
class StreamMessagesTest(AuthedTestCase):
|
2012-10-03 21:16:34 +02:00
|
|
|
fixtures = ['messages.json']
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-10 23:13:04 +02:00
|
|
|
def test_message_to_stream(self):
|
2012-08-31 20:11:15 +02:00
|
|
|
"""
|
2012-10-10 23:13:04 +02:00
|
|
|
If you send a message to a stream, everyone subscribed to the stream
|
2012-10-03 21:16:34 +02:00
|
|
|
receives the messages.
|
2012-08-31 20:11:15 +02:00
|
|
|
"""
|
2012-10-10 23:13:04 +02:00
|
|
|
subscribers = self.users_subscribed_to_stream("Scotland", "humbughq.com")
|
2012-10-03 21:16:34 +02:00
|
|
|
old_subscriber_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for subscriber in subscribers:
|
2012-10-03 21:16:34 +02:00
|
|
|
old_subscriber_messages.append(len(self.message_stream(subscriber)))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
|
|
|
non_subscribers = [user for user in User.objects.all() if user not in subscribers]
|
2012-10-03 21:16:34 +02:00
|
|
|
old_non_subscriber_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for non_subscriber in non_subscribers:
|
2012-10-03 21:16:34 +02:00
|
|
|
old_non_subscriber_messages.append(len(self.message_stream(non_subscriber)))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
a_subscriber_email = subscribers[0].email
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login(a_subscriber_email)
|
2012-10-10 22:57:21 +02:00
|
|
|
self.send_message(a_subscriber_email, "Scotland", Recipient.STREAM)
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
new_subscriber_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for subscriber in subscribers:
|
2012-10-03 21:16:34 +02:00
|
|
|
new_subscriber_messages.append(len(self.message_stream(subscriber)))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
new_non_subscriber_messages = []
|
2012-08-31 20:11:15 +02:00
|
|
|
for non_subscriber in non_subscribers:
|
2012-10-03 21:16:34 +02:00
|
|
|
new_non_subscriber_messages.append(len(self.message_stream(non_subscriber)))
|
2012-08-31 20:11:15 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
self.assertEqual(old_non_subscriber_messages, new_non_subscriber_messages)
|
|
|
|
self.assertEqual(new_subscriber_messages, [elt + 1 for elt in old_subscriber_messages])
|
2012-09-04 22:31:56 +02:00
|
|
|
|
2012-09-06 20:53:13 +02:00
|
|
|
class PointerTest(AuthedTestCase):
|
2012-10-03 21:16:34 +02:00
|
|
|
fixtures = ['messages.json']
|
2012-09-06 20:53:13 +02:00
|
|
|
|
|
|
|
def test_update_pointer(self):
|
|
|
|
"""
|
|
|
|
Posting a pointer to /update (in the form {"pointer": pointer}) changes
|
|
|
|
the pointer we store for your UserProfile.
|
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, -1)
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/update_pointer", {"pointer": 1})
|
2012-09-06 21:34:38 +02:00
|
|
|
self.assert_json_success(result)
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, 1)
|
2012-09-06 20:53:13 +02:00
|
|
|
|
|
|
|
def test_missing_pointer(self):
|
|
|
|
"""
|
2012-10-16 21:42:40 +02:00
|
|
|
Posting json to /json/update_pointer which does not contain a pointer key/value pair
|
2012-09-06 20:53:13 +02:00
|
|
|
returns a 400 and error message.
|
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, -1)
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/update_pointer", {"foo": 1})
|
2012-09-06 21:34:38 +02:00
|
|
|
self.assert_json_error(result, "Missing pointer")
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, -1)
|
2012-09-06 20:53:13 +02:00
|
|
|
|
|
|
|
def test_invalid_pointer(self):
|
|
|
|
"""
|
2012-10-16 21:42:40 +02:00
|
|
|
Posting json to /json/update_pointer with an invalid pointer returns a 400 and error
|
2012-09-06 20:53:13 +02:00
|
|
|
message.
|
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, -1)
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/update_pointer", {"pointer": "foo"})
|
2012-09-06 21:34:38 +02:00
|
|
|
self.assert_json_error(result, "Invalid pointer: must be an integer")
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, -1)
|
2012-09-06 20:53:13 +02:00
|
|
|
|
|
|
|
def test_pointer_out_of_range(self):
|
|
|
|
"""
|
2012-10-16 21:42:40 +02:00
|
|
|
Posting json to /json/update_pointer with an out of range (< 0) pointer returns a 400
|
2012-09-06 20:53:13 +02:00
|
|
|
and error message.
|
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, -1)
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/update_pointer", {"pointer": -2})
|
2012-09-06 21:34:38 +02:00
|
|
|
self.assert_json_error(result, "Invalid pointer value")
|
2012-10-22 20:15:25 +02:00
|
|
|
self.assertEquals(self.get_user_profile("hamlet@humbughq.com").pointer, -1)
|
2012-09-06 21:46:03 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
class MessagePOSTTest(AuthedTestCase):
|
|
|
|
fixtures = ['messages.json']
|
2012-09-06 21:46:03 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def test_message_to_self(self):
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-10 23:13:04 +02:00
|
|
|
Sending a message to a stream to which you are subscribed is
|
2012-10-03 21:16:34 +02:00
|
|
|
successful.
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/send_message/", {"type": "stream",
|
|
|
|
"stream": "Verona",
|
2012-10-22 19:23:11 +02:00
|
|
|
"client": "test suite",
|
2012-10-16 21:42:40 +02:00
|
|
|
"content": "Test message",
|
|
|
|
"subject": "Test subject"})
|
2012-09-06 21:46:03 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2012-10-10 23:13:04 +02:00
|
|
|
def test_message_to_nonexistent_stream(self):
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-10 23:13:04 +02:00
|
|
|
Sending a message to a nonexistent stream creates the stream and
|
2012-10-03 21:16:34 +02:00
|
|
|
is successful.
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-10 23:13:04 +02:00
|
|
|
self.assertFalse(Stream.objects.filter(name="nonexistent_stream"))
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/send_message/", {"type": "stream",
|
|
|
|
"stream": "nonexistent_stream",
|
2012-10-22 19:23:11 +02:00
|
|
|
"client": "test suite",
|
2012-10-16 21:42:40 +02:00
|
|
|
"content": "Test message",
|
|
|
|
"subject": "Test subject"})
|
2012-09-06 22:01:08 +02:00
|
|
|
self.assert_json_success(result)
|
2012-10-10 23:13:04 +02:00
|
|
|
self.assertTrue(Stream.objects.filter(name="nonexistent_stream"))
|
2012-09-06 21:46:03 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def test_personal_message(self):
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-03 21:16:34 +02:00
|
|
|
Sending a personal message to a valid username is successful.
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/send_message/", {"type": "personal",
|
|
|
|
"content": "Test message",
|
2012-10-22 19:23:11 +02:00
|
|
|
"client": "test suite",
|
2012-10-16 21:42:40 +02:00
|
|
|
"recipient": "othello@humbughq.com"})
|
2012-09-06 21:46:03 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def test_personal_message_to_nonexistent_user(self):
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-03 21:16:34 +02:00
|
|
|
Sending a personal message to an invalid email returns error JSON.
|
2012-09-06 21:46:03 +02:00
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/send_message/", {"type": "personal",
|
|
|
|
"content": "Test message",
|
2012-10-22 19:23:11 +02:00
|
|
|
"client": "test suite",
|
2012-10-16 21:42:40 +02:00
|
|
|
"recipient": "nonexistent"})
|
2012-10-12 20:29:23 +02:00
|
|
|
self.assert_json_error(result, "Invalid email 'nonexistent'")
|
2012-09-06 21:53:26 +02:00
|
|
|
|
|
|
|
def test_invalid_type(self):
|
|
|
|
"""
|
2012-10-03 21:16:34 +02:00
|
|
|
Sending a message of unknown type returns error JSON.
|
2012-09-06 21:53:26 +02:00
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-10-16 21:42:40 +02:00
|
|
|
result = self.client.post("/json/send_message/", {"type": "invalid type",
|
|
|
|
"content": "Test message",
|
2012-10-22 19:23:11 +02:00
|
|
|
"client": "test suite",
|
2012-10-16 21:42:40 +02:00
|
|
|
"recipient": "othello@humbughq.com"})
|
2012-10-03 00:10:55 +02:00
|
|
|
self.assert_json_error(result, "Invalid message type")
|
2012-09-06 23:55:04 +02:00
|
|
|
|
|
|
|
class DummyHandler(object):
|
|
|
|
def __init__(self, callback):
|
|
|
|
self.callback = callback
|
|
|
|
|
|
|
|
def async_callback(self, _):
|
|
|
|
return self.callback
|
|
|
|
|
2012-10-01 22:57:01 +02:00
|
|
|
def finish(self, _):
|
|
|
|
return
|
|
|
|
|
2012-09-06 23:55:04 +02:00
|
|
|
class POSTRequestMock(object):
|
|
|
|
method = "POST"
|
|
|
|
|
|
|
|
def __init__(self, post_data, user, assert_callback):
|
|
|
|
self.POST = post_data
|
|
|
|
self.user = user
|
|
|
|
self._tornado_handler = DummyHandler(assert_callback)
|
|
|
|
|
2012-09-27 21:44:54 +02:00
|
|
|
class GetUpdatesTest(AuthedTestCase):
|
2012-10-03 21:16:34 +02:00
|
|
|
fixtures = ['messages.json']
|
2012-09-06 23:55:04 +02:00
|
|
|
|
2012-10-16 21:42:40 +02:00
|
|
|
def test_json_get_updates(self):
|
2012-09-06 23:55:04 +02:00
|
|
|
"""
|
2012-10-16 21:42:40 +02:00
|
|
|
json_get_updates returns messages with IDs greater than the
|
2012-09-06 23:55:04 +02:00
|
|
|
last_received ID.
|
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-09-21 00:26:59 +02:00
|
|
|
user = User.objects.get(email="hamlet@humbughq.com")
|
2012-09-06 23:55:04 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def callback(messages):
|
|
|
|
correct_messages = filter_by_subscriptions(Message.objects.all(), user)
|
|
|
|
for message in messages:
|
|
|
|
self.assertTrue(message in correct_messages)
|
|
|
|
self.assertTrue(message.id > 1)
|
2012-09-06 23:55:04 +02:00
|
|
|
|
2012-10-01 22:57:01 +02:00
|
|
|
request = POSTRequestMock({"last": str(1), "first": str(1)}, user, callback)
|
2012-10-16 21:42:40 +02:00
|
|
|
# json_get_updates returns None, which raises an exception in the
|
2012-09-06 23:55:04 +02:00
|
|
|
# @asynchronous decorator, which raises a TornadoAsyncException. So this
|
|
|
|
# is expected, but should probably change.
|
2012-10-16 21:42:40 +02:00
|
|
|
self.assertRaises(TornadoAsyncException, json_get_updates, request)
|
2012-09-06 23:55:04 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def test_beyond_last_message(self):
|
2012-09-06 23:55:04 +02:00
|
|
|
"""
|
2012-10-03 21:16:34 +02:00
|
|
|
If your last_received message is greater than the greatest Message ID, you
|
|
|
|
don't get any new messages.
|
2012-09-06 23:55:04 +02:00
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-09-21 00:26:59 +02:00
|
|
|
user = User.objects.get(email="hamlet@humbughq.com")
|
2012-10-03 21:16:34 +02:00
|
|
|
last_received = max(message.id for message in Message.objects.all()) + 100
|
|
|
|
messages = []
|
2012-09-06 23:55:04 +02:00
|
|
|
|
|
|
|
def callback(data):
|
|
|
|
# We can't make asserts in this nested function, so save the data
|
|
|
|
# and assert in the parent.
|
2012-10-04 21:59:15 +02:00
|
|
|
#
|
|
|
|
# TODO: Find out how to make this blocking so assertEquals below
|
|
|
|
# runs after us.
|
|
|
|
messages.extend(data)
|
2012-09-06 23:55:04 +02:00
|
|
|
|
2012-10-01 22:57:01 +02:00
|
|
|
request = POSTRequestMock({"last": str(last_received), "first": "1"}, user, callback)
|
2012-10-16 21:42:40 +02:00
|
|
|
self.assertRaises(TornadoAsyncException, json_get_updates, request)
|
2012-10-03 21:16:34 +02:00
|
|
|
self.assertEquals(len(messages), 0)
|
2012-09-06 23:55:04 +02:00
|
|
|
|
|
|
|
def test_missing_last_received(self):
|
|
|
|
"""
|
2012-10-16 21:42:40 +02:00
|
|
|
Calling json_get_updates without a last_received key/value pair
|
2012-09-06 23:55:04 +02:00
|
|
|
returns a 400 and error message.
|
|
|
|
"""
|
2012-10-12 17:49:22 +02:00
|
|
|
self.login("hamlet@humbughq.com")
|
2012-09-21 00:26:59 +02:00
|
|
|
user = User.objects.get(email="hamlet@humbughq.com")
|
2012-09-06 23:55:04 +02:00
|
|
|
|
2012-10-03 21:16:34 +02:00
|
|
|
def callback(messages):
|
|
|
|
correct_messages = filter_by_subscriptions(Message.objects.all(), user)
|
|
|
|
for message in messages:
|
|
|
|
self.assertTrue(message in correct_messages)
|
|
|
|
self.assertTrue(message.id > 1)
|
2012-09-06 23:55:04 +02:00
|
|
|
|
|
|
|
request = POSTRequestMock({}, user, callback)
|
2012-10-16 21:42:40 +02:00
|
|
|
self.assert_json_error(json_get_updates(request), "Missing message range")
|