def setUp(self): MailingList.objects.create(name="*****@*****.**", subject_prefix="[example] ", archive_policy=ArchivePolicy.private.value) msg = EmailMessage() msg["From"] = "*****@*****.**" msg["Message-ID"] = "<msgid>" msg["Subject"] = "Dummy message" msg.set_payload("Dummy message") msg["Message-ID-Hash"] = self.msgid = add_to_list( "*****@*****.**", msg) self.mailman_client.get_list.side_effect = \ lambda name: FakeMMList(name) User.objects.create_user('superuser', '*****@*****.**', 'testPass', is_superuser=True) self.mm_subbed_user = self._create_user('subbeduser', '*****@*****.**') self.mm_subbed_user.subscriptions = [ FakeMMMember("list.example.com", '*****@*****.**'), ] self.mm_unsubbed_user = self._create_user('unsubbeduser', '*****@*****.**') self.mm_unsubbed_user.subscriptions = [] def mm_get_user(email): if email == '*****@*****.**': return self.mm_subbed_user else: return self.mm_unsubbed_user self.mailman_client.get_user.side_effect = mm_get_user
def setUp(self): self.user = User.objects.create_user('testuser', '*****@*****.**', 'testPass') MailingList.objects.create(name="*****@*****.**", subject_prefix="[example] ", archive_policy=ArchivePolicy.private.value) msg = Message() msg["From"] = "*****@*****.**" msg["Message-ID"] = "<msgid>" msg["Subject"] = "Dummy message" msg.set_payload("Dummy message") msg["Message-ID-Hash"] = self.msgid = add_to_list( "*****@*****.**", msg) # Set the mailman_client after the message has been added to the list, # otherwise MailingList.update_from_mailman() will overwrite the list # properties. self.mailman_client.get_list.side_effect = \ lambda name: FakeMMList(name) self.mm_user = Mock() self.mm_user.user_id = "dummy" self.mailman_client.get_user.side_effect = lambda name: self.mm_user self.mm_user.subscriptions = [ FakeMMMember("list.example.com", self.user.email), ] self.mm_user.addresses = ['*****@*****.**']
def test_get_subscriptions(self): mlist = MailingList.objects.create(name="*****@*****.**") self.mm_user.subscriptions = [ FakeMMMember("test.example.com", self.user.email), ] self.client.login(username='******', password='******') response = self.client.get(reverse("hk_user_subscriptions")) self.assertEqual(response.context["subscriptions"], [{ 'first_post': None, 'posts_count': 0, 'likes': 0, 'dislikes': 0, 'likestatus': 'neutral', 'mlist': mlist, 'list_name': "*****@*****.**", 'all_posts_url': "%[email protected]" % reverse("hk_user_posts", args=[self.mm_user.user_id]), }]) self.assertContains(response, "*****@*****.**", status_code=200)
def test_search_private_lists(self): # Create 1 public and 2 private lists mlist_public = MailingList.objects.create(name="*****@*****.**") mlist_private = MailingList.objects.create( name="*****@*****.**", archive_policy=ArchivePolicy.private.value) mlist_private_sub = MailingList.objects.create( name="*****@*****.**", archive_policy=ArchivePolicy.private.value) # (make sure the mailman client will not reset the archive_policy) mailman_lists = { "*****@*****.**": FakeMMList("*****@*****.**"), "*****@*****.**": FakeMMList("*****@*****.**"), "*****@*****.**": FakeMMList("*****@*****.**"), } mailman_lists["*****@*****.**"].settings["archive_policy"] = \ "private" mailman_lists["*****@*****.**"].settings["archive_policy"] = \ "private" self.mailman_client.get_list.side_effect = \ lambda name: mailman_lists[name] # Subscribe the user to one of the private lists self.mm_user.subscriptions = [ FakeMMMember("private-sub.example.com", self.user.email), ] # Populate the lists with messages self._send_message(mlist_public) self._send_message(mlist_private) self._send_message(mlist_private_sub) # There must be a result from the public and the subscribed list only self.client.login(username='******', password='******') response = self.client.get(reverse("hk_search"), {"q": "keyword"}) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["emails"].paginator.count, 2) self.assertContains(response, "Dummy message", count=2)
def test_get_sender_is_string(self): # The get_sender function must always return a string from mailmanclient.restobjects.address import Address self.mm_user.addresses = [ Address(None, None, dict(email="*****@*****.**")), ] self.mm_user.subscriptions = [ FakeMMMember(self.mlist.list_id, self.mm_user.addresses[0]), ] addr = posting.get_sender(self.request, self.mlist) self.assertTrue(isinstance(addr, str))
def test_list_not_archived(self): self.mm_user.subscriptions = [ FakeMMMember("test.example.com", self.user.email), ] self.client.login(username='******', password='******') response = self.client.get(reverse("hk_user_subscriptions")) self.assertContains(response, "*****@*****.**", status_code=200) self.assertEqual(response.context["subscriptions"], [{ 'first_post': None, 'posts_count': 0, 'likes': 0, 'dislikes': 0, 'likestatus': 'neutral', 'mlist': None, 'all_posts_url': None, 'list_name': "*****@*****.**", }])
def test_search_private_list(self): mlist = MailingList.objects.create( name="*****@*****.**", archive_policy=ArchivePolicy.private.value ) mm_mlist = FakeMMList("*****@*****.**") mm_mlist.settings["archive_policy"] = "private" self.mailman_client.get_list.side_effect = lambda name: mm_mlist self.mm_user.subscriptions = [ FakeMMMember("private.example.com", self.user.email), ] self._send_message(mlist) response = self.client.get( reverse("hk_search"), {"q": "dummy", "mlist": "*****@*****.**"}) self.assertEqual(response.status_code, 403) self.client.login(username='******', password='******') response = self.client.get( reverse("hk_search"), {"q": "dummy", "mlist": "*****@*****.**"}) self.assertEqual(response.status_code, 200) self.assertContains(response, "Dummy message")
def test_sender_subscribed(self): self.mm_user.subscriptions = [ FakeMMMember(self.mlist.list_id, "*****@*****.**"), ] self.assertEqual(posting.get_sender(self.request, self.mlist), "*****@*****.**")
def test_get_subscriptions(self): fake_member = FakeMMMember("list.example.com", "*****@*****.**") self.mm_user.subscriptions = [fake_member] self.assertEqual(mailman.get_subscriptions(self.user), {"list.example.com": "*****@*****.**"})