|
12 | 12 | from project.newsletter.test import DataTestCase |
13 | 13 |
|
14 | 14 |
|
| 15 | +class TestLanding(DataTestCase): |
| 16 | + url = reverse("newsletter:landing") |
| 17 | + |
| 18 | + def test_unauthenticated(self): |
| 19 | + response = self.client.get(self.url) |
| 20 | + self.assertTemplateUsed(response, "landing.html") |
| 21 | + self.assertEqual( |
| 22 | + list(response.context["posts"]), [self.data.career_post, self.data.all_post] |
| 23 | + ) |
| 24 | + |
| 25 | + def test_authenticated_no_subscription(self): |
| 26 | + self.client.force_login(User.objects.create_user(username="inline")) |
| 27 | + response = self.client.get(self.url) |
| 28 | + self.assertTemplateUsed(response, "landing.html") |
| 29 | + self.assertEqual( |
| 30 | + list(response.context["posts"]), [self.data.career_post, self.data.all_post] |
| 31 | + ) |
| 32 | + # Verify the categories are rendered |
| 33 | + self.assertContains(response, self.data.career_post.categories.first().title) |
| 34 | + |
| 35 | + def test_authenticated_subscriber(self): |
| 36 | + self.client.force_login(self.data.subscription.user) |
| 37 | + response = self.client.get(self.url) |
| 38 | + self.assertTemplateUsed(response, "landing.html") |
| 39 | + self.assertEqual( |
| 40 | + list(response.context["posts"]), |
| 41 | + [self.data.private_post, self.data.career_post, self.data.all_post], |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class TestListPosts(DataTestCase): |
| 46 | + url = reverse("newsletter:list_posts") |
| 47 | + |
| 48 | + @patch("project.newsletter.views.LIST_POSTS_PAGE_SIZE", 1) |
| 49 | + def test_unauthenticated(self): |
| 50 | + response = self.client.get(self.url) |
| 51 | + self.assertTemplateUsed(response, "posts/list.html") |
| 52 | + self.assertEqual(list(response.context["page"]), [self.data.career_post]) |
| 53 | + |
| 54 | + # Verify the pagination exists. |
| 55 | + self.assertInHTML( |
| 56 | + '<a class="item active" href="?page=1">1</a>', |
| 57 | + response.content.decode("utf-8"), |
| 58 | + ) |
| 59 | + self.assertInHTML( |
| 60 | + '<a class="item" href="?page=2">2</a>', response.content.decode("utf-8") |
| 61 | + ) |
| 62 | + |
| 63 | + @patch("project.newsletter.views.LIST_POSTS_PAGE_SIZE", 1) |
| 64 | + def test_authenticated(self): |
| 65 | + self.client.force_login(self.data.subscription.user) |
| 66 | + response = self.client.get(self.url) |
| 67 | + self.assertTemplateUsed(response, "posts/list.html") |
| 68 | + self.assertEqual(list(response.context["page"]), [self.data.private_post]) |
| 69 | + |
| 70 | + # Verify the categories are rendered |
| 71 | + self.assertContains(response, self.data.private_post.categories.first().title) |
| 72 | + # Verify the pagination exists. |
| 73 | + self.assertInHTML( |
| 74 | + '<a class="item active" href="?page=1">1</a>', |
| 75 | + response.content.decode("utf-8"), |
| 76 | + ) |
| 77 | + self.assertInHTML( |
| 78 | + '<a class="item" href="?page=2">2</a>', response.content.decode("utf-8") |
| 79 | + ) |
| 80 | + |
| 81 | + @patch("project.newsletter.views.LIST_POSTS_PAGE_SIZE", 1) |
| 82 | + def test_pagination(self): |
| 83 | + self.client.force_login(self.data.subscription.user) |
| 84 | + response = self.client.get(self.url + "?page=2") |
| 85 | + self.assertTemplateUsed(response, "posts/list.html") |
| 86 | + self.assertEqual(list(response.context["page"]), [self.data.career_post]) |
| 87 | + # Verify the pagination exists. |
| 88 | + self.assertInHTML( |
| 89 | + '<a class="item" href="?page=1">1</a>', response.content.decode("utf-8") |
| 90 | + ) |
| 91 | + self.assertInHTML( |
| 92 | + '<a class="item active" href="?page=2">2</a>', |
| 93 | + response.content.decode("utf-8"), |
| 94 | + ) |
| 95 | + |
| 96 | + |
15 | 97 | class TestViewPost(DataTestCase): |
16 | 98 | def test_unauthenticated(self): |
17 | 99 | response = self.client.get( |
@@ -227,7 +309,7 @@ def test_toggle(self): |
227 | 309 | ) |
228 | 310 | url = reverse("newsletter:toggle_post_privacy", kwargs={"slug": post.slug}) |
229 | 311 | response = self.client.post(url) |
230 | | - self.assertRedirects(response, "/admin/") |
| 312 | + self.assertRedirects(response, reverse("newsletter:list_posts")) |
231 | 313 | post.refresh_from_db() |
232 | 314 | self.assertFalse(post.is_public) |
233 | 315 |
|
@@ -332,7 +414,10 @@ def test_invalid(self): |
332 | 414 | def test_update(self): |
333 | 415 | self.client.force_login(self.user) |
334 | 416 | data = {"categories": [self.data.career.slug]} |
335 | | - self.client.post(reverse("newsletter:update_subscription"), data=data) |
| 417 | + response = self.client.post( |
| 418 | + reverse("newsletter:update_subscription"), data=data |
| 419 | + ) |
| 420 | + self.assertRedirects(response, reverse("newsletter:list_posts")) |
336 | 421 | self.assertEqual(self.subscription.categories.get(), self.data.career) |
337 | 422 |
|
338 | 423 |
|
|
0 commit comments