def test_create_new_message(self): """ Test creating a new message. Creating a new message on a thread that has a notification associated with it should send an email notification. """ user = get_test_user() thread = create_thread() notification = create_thread_notification( user=user, thread=thread) message = create_message( user=get_test_user(username="******"), thread=thread) self.assertEqual(1, len(mail.outbox)) result = mail.outbox[0] mail.outbox = [] notification.send_notification(message) expected = mail.outbox[0] self.assertMailEqual(expected, result)
def test_send_notification(self): """ Test sending a notification email. Calling this method should send an email to the user assocated with the notification. """ user = get_test_user(email='*****@*****.**') thread = create_thread() message = create_message(thread=thread) notification = models.ThreadNotification.objects.create( user=user, thread=thread) notification.send_notification(message) expected = 'Thread #%d was updated' % (thread.pk) self.assertEqual(1, len(mail.outbox)) m = mail.outbox[0] self.assertEqual('Thread Updated', m.subject) self.assertEqual(expected, m.body) self.assertEqual('*****@*****.**', m.from_email) self.assertEqual([user.email], m.to)
def test_update_message(self): """ Test updating an existing message. Updating an existing message should not trigger a new notification. """ user = get_test_user() thread = create_thread() message = create_message(thread=thread) create_thread_notification( user=user, thread=thread) message.body = "New body text" message.save() self.assertEqual(0, len(mail.outbox))
def test_self_notification(self): """ Test user replying to watched thread. If a user is receiving notifications for a thread, they should not receive a notification if they were the one who posted a reply. """ user = get_test_user() thread = create_thread() create_thread_notification( user=user, thread=thread) create_message(user=user, thread=thread) self.assertEqual(0, len(mail.outbox))
def test_create(self): """ Test creating a new ThreadNotification. A ThreadNotification instance should be able to be created from a user and a thread. """ thread = create_thread() user = get_test_user() notification = models.ThreadNotification.objects.create( user=user, thread=thread) self.assertEqual(1, models.ThreadNotification.objects.count()) self.assertEqual(user, notification.user) self.assertEqual(thread, notification.thread)
def create_thread_notification(**kwargs): """ Create a ThreadNotification instance for testing. Fills in default values for testing purposes. """ user = kwargs.pop('user', get_test_user()) thread = kwargs.pop('thread', create_thread()) if len(kwargs): raise ValueError("Received unexpected kwargs: %s" % kwargs) notification_kwargs = dict( user=user, thread=thread) return models.ThreadNotification.objects.create(**notification_kwargs)
def test_string_conversion(self): """ Test converting a ThreadNotification instance to a string. Converting a ThreadNotification instance to a string should result in a string of the form: 'Notify <user> of changes to thread #<id> (<thread title>)' """ notification = models.ThreadNotification( user=get_test_user(), thread=create_thread()) expected = 'Notify %s of changes to thread #%d (%s)' % ( notification.user.username, notification.thread.id, notification.thread) self.assertEqual(expected, str(notification))