def test_unseen_mail_notifications(self): """Test whether the notifications are correctly *not* sent for messages which are not visible to the user""" mock_name = 'oioioi.questions.management.commands.mailnotifyd.visible_messages' with mock.patch(mock_name, return_value=Message.objects.none()): message = Message.objects.get(pk=4) mailnotify(message) self.assertEqual(len(mail.outbox), 0)
def test_unseen_mail_notifications(self): """Test whether the notifications are correctly *not* sent for messages which are not visible to the user""" mock_name = \ 'oioioi.questions.management.commands.mailnotifyd.visible_messages' with mock.patch(mock_name, return_value=Message.objects.none()): message = Message.objects.get(pk=4) mailnotify(message) self.assertEqual(len(mail.outbox), 0)
def test_mailnotify(self): def assertMessageId(id, body): self.assertIn("/questions/{}/".format(id), body) # Notify about a private message message = Message.objects.get(pk=3) mailnotify(message) self.assertEqual(len(mail.outbox), 1) m = mail.outbox[0] self.assertIn("[OIOIOI][Test contest]", m.subject) self.assertEqual("*****@*****.**", m.to[0]) self.assertIn("A new message has just appeared", m.body) self.assertIn("private-answer-body", m.body) # Private answer, the user has access to the top reference, so they # should receive it in the e-mail assertMessageId(message.top_reference.id, m.body) # Do not notify about a question question = Message.objects.get(pk=2) mailnotify(question) self.assertEqual(len(mail.outbox), 1) # Do not notify again about the same question with self.assertRaises(AssertionError): mailnotify(message) # Notify two users about a public message pubmsg = Message.objects.get(pk=4) mailnotify(pubmsg) self.assertEqual(len(mail.outbox), 3) m = mail.outbox[1] mm = mail.outbox[2] self.assertIn("[OIOIOI][Test contest]", m.subject) self.assertEqual("*****@*****.**", m.to[0]) self.assertEqual("*****@*****.**", mm.to[0]) self.assertIn("A new message has just appeared", m.body) self.assertIn("public-answer-body", m.body) self.assertIn("A new message has just appeared", mm.body) self.assertIn("public-answer-body", mm.body) self.assertEqual(m.subject, mm.subject) # the author should receive the link to the top_reference # and the original question assertMessageId(pubmsg.top_reference.id, m.body) self.assertIn("question-body", m.body) # the non-author should receive the link to the answer # and should not receive the original question assertMessageId(pubmsg.id, mm.body) self.assertNotIn("question-body", mm.body)
def test_mailnotify(self): def assertMessageId(id, body): self.assertIn("/questions/{}/".format(id), body) # Notify about a private message message = Message.objects.get(pk=3) mailnotify(message) self.assertEquals(len(mail.outbox), 1) m = mail.outbox[0] self.assertIn("[OIOIOI][Test contest]", m.subject) self.assertEquals("*****@*****.**", m.to[0]) self.assertIn("A new message has just appeared", m.body) self.assertIn("private-answer-body", m.body) # Private answer, the user has access to the top reference, so they # should receive it in the e-mail assertMessageId(message.top_reference.id, m.body) # Do not notify about a question question = Message.objects.get(pk=2) mailnotify(question) self.assertEquals(len(mail.outbox), 1) # Do not notify again about the same question with self.assertRaises(AssertionError): mailnotify(message) # Notify two users about a public message pubmsg = Message.objects.get(pk=4) mailnotify(pubmsg) self.assertEquals(len(mail.outbox), 3) m = mail.outbox[1] mm = mail.outbox[2] self.assertIn("[OIOIOI][Test contest]", m.subject) self.assertEquals("*****@*****.**", m.to[0]) self.assertEquals("*****@*****.**", mm.to[0]) self.assertIn("A new message has just appeared", m.body) self.assertIn("public-answer-body", m.body) self.assertEquals(m.subject, mm.subject) # the bodies should just differ by one character - the message id # the link self.assertEquals(len(m.body), len(mm.body)) num_different = \ sum(m.body[i] != mm.body[i] for i in range(len(m.body))) self.assertEquals(num_different, 1) # the author should receive the link to the top_reference assertMessageId(pubmsg.top_reference.id, m.body) # the non-author should receive the link to the answer assertMessageId(pubmsg.id, mm.body)
def test_unescaped(self): message = Message.objects.get(pk=10) mailnotify(message) m = mail.outbox[0] self.assertIn(">", m.subject) self.assertIn("& <> !! #$*&$!#", m.body)