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_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_email_context(self):
        """ Test getting the context used for emails.

        It should return a dictionary of the context used in the emails.
        """
        notification = create_thread_notification()

        expected = {}

        self.assertEqual(expected, notification._email_context())
    def test_load_email_templates(self):
        """ Test loading email templates for notifications.

        The method should find plaintext and html formats for the email.
        """
        notification = create_thread_notification()

        plain, html = notification.load_templates()

        self.assertIsNotNone(plain)
        self.assertIsNotNone(html)
    def test_delete(self):
        """ Test unfollowing a thread.

        If the 'follow' variable is false, the ThreadNotification
        instance for the current thread and user should be deleted.
        """
        self.login()

        thread = create_thread()
        create_thread_notification(
            user=self.user, thread=thread)

        data = {}

        success_url = thread_detail_url(thread=thread)

        url = reverse('simple-forums:follow-thread', kwargs={'pk': thread.pk})
        response = self.client.post(url, data)

        self.assertRedirects(response, success_url)
        self.assertEqual(0, models.ThreadNotification.objects.count())
    def test_duplicate_request(self):
        """ Test trying to create a duplicate notification instance.

        If a user already has notifications set up for a thread and they
        try to create another notification instance, nothing should
        happen.
        """
        self.login()

        thread = create_thread()
        create_thread_notification(
            user=self.user, thread=thread)

        data = {'follow': 'on'}

        success_url = thread_detail_url(thread=thread)

        url = reverse('simple-forums:follow-thread', kwargs={'pk': thread.pk})
        response = self.client.post(url, data)

        self.assertRedirects(response, success_url)
        self.assertEqual(1, models.ThreadNotification.objects.count())