|
| 1 | +from io import BytesIO |
| 2 | + |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from django.test import Client, TestCase, tag |
| 5 | +from django.urls import reverse |
| 6 | +from PIL import Image |
| 7 | + |
| 8 | +from project.newsletter.models import Post |
| 9 | + |
| 10 | + |
| 11 | +@tag("lab_test") |
| 12 | +class TestUpdatePost(TestCase): |
| 13 | + def setUp(self): |
| 14 | + self.user = User.objects.create_superuser(username="lab1_2") |
| 15 | + self.client = Client() |
| 16 | + self.client.force_login(self.user) |
| 17 | + |
| 18 | + def test_create_get_broken(self): |
| 19 | + response = self.client.get(reverse("newsletter:create_post")) |
| 20 | + self.assertNotContains(response, ' enctype="multipart/form-data"') |
| 21 | + |
| 22 | + def test_create_post_broken(self): |
| 23 | + img = BytesIO() |
| 24 | + Image.new("RGB", (1, 1), "#FF0000").save(img, format="PNG") |
| 25 | + img.name = "myimage.png" |
| 26 | + img.seek(0) |
| 27 | + data = { |
| 28 | + "title": "Test Create", |
| 29 | + "slug": "test-create", |
| 30 | + "categories": [], |
| 31 | + "content": "content", |
| 32 | + "summary": "summary", |
| 33 | + "is_public": False, |
| 34 | + "is_published": True, |
| 35 | + "open_graph_description": "description", |
| 36 | + "open_graph_image": img, |
| 37 | + } |
| 38 | + response = self.client.post(reverse("newsletter:create_post"), data=data) |
| 39 | + |
| 40 | + self.assertRedirects( |
| 41 | + response, reverse("newsletter:update_post", kwargs={"slug": "test-create"}) |
| 42 | + ) |
| 43 | + post = Post.objects.get(slug="test-create") |
| 44 | + self.assertIsNone(post.open_graph_image._file) |
| 45 | + |
| 46 | + def test_update_get_broken(self): |
| 47 | + post = Post.objects.create( |
| 48 | + author=self.user, |
| 49 | + title="Test Update", |
| 50 | + slug="test-update", |
| 51 | + content="c", |
| 52 | + ) |
| 53 | + response = self.client.get( |
| 54 | + reverse("newsletter:update_post", kwargs={"slug": post.slug}) |
| 55 | + ) |
| 56 | + self.assertNotContains(response, ' enctype="multipart/form-data"') |
| 57 | + |
| 58 | + def test_update_post_broken(self): |
| 59 | + post = Post.objects.create( |
| 60 | + author=self.user, |
| 61 | + title="Test Update", |
| 62 | + slug="test-update", |
| 63 | + content="c", |
| 64 | + ) |
| 65 | + img = BytesIO() |
| 66 | + Image.new("RGB", (1, 1), "#FF0000").save(img, format="PNG") |
| 67 | + img.name = "myimage.png" |
| 68 | + img.seek(0) |
| 69 | + data = { |
| 70 | + "title": "Test Update", |
| 71 | + "slug": "test-update", |
| 72 | + "categories": [], |
| 73 | + "content": "content", |
| 74 | + "summary": "summary", |
| 75 | + "is_public": False, |
| 76 | + "is_published": True, |
| 77 | + "open_graph_description": "description", |
| 78 | + "open_graph_image": img, |
| 79 | + } |
| 80 | + url = reverse("newsletter:update_post", kwargs={"slug": post.slug}) |
| 81 | + response = self.client.post(url, data=data) |
| 82 | + |
| 83 | + self.assertRedirects(response, url) |
| 84 | + post.refresh_from_db() |
| 85 | + self.assertIsNone(post.open_graph_image._file) |
0 commit comments