class GalleryTest(TestCase): def setUp(self): self.profile = ProfileFactory() self.gallery = GalleryFactory() self.image1 = ImageFactory(gallery=self.gallery) self.image2 = ImageFactory(gallery=self.gallery) self.user_gallery = UserGalleryFactory(user=self.profile.user, gallery=self.gallery) def tearDown(self): self.image1.delete() self.image2.delete() self.user_gallery.delete() self.gallery.delete() def test_get_absolute_url(self): absolute_url = reverse("gallery-details", args=[self.gallery.pk, self.gallery.slug]) self.assertEqual(absolute_url, self.gallery.get_absolute_url()) def test_get_linked_users(self): self.assertEqual(1, len(self.gallery.get_linked_users())) self.assertEqual(self.user_gallery, self.gallery.get_linked_users()[0]) def test_get_images(self): self.assertEqual(2, len(self.gallery.get_images())) self.assertEqual(self.image1, self.gallery.get_images()[0]) self.assertEqual(self.image2, self.gallery.get_images()[1]) def test_get_last_image(self): self.assertEqual(self.image2, self.gallery.get_last_image()) def test_delete_empty_gallery(self): test_gallery = GalleryFactory() path = test_gallery.get_gallery_path() test_gallery.delete() self.assertFalse(os.path.isdir(path)) def test_delete_gallery_with_image(self): test_gallery = GalleryFactory() test_image = ImageFactory(gallery=test_gallery) path_gallery = test_gallery.get_gallery_path() self.assertTrue(os.path.isdir(path_gallery)) path_image = test_image.physical.path self.assertTrue(os.path.isfile(path_image)) # Destroy the gallery and the image test_gallery.delete() self.assertFalse(os.path.isdir(path_gallery)) self.assertFalse(os.path.isfile(path_image))
class NewImageViewTest(TestCase): def setUp(self): self.gallery = GalleryFactory() self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.user_gallery1 = UserGalleryFactory(user=self.profile1.user, gallery=self.gallery, mode="W") self.user_gallery2 = UserGalleryFactory(user=self.profile2.user, gallery=self.gallery, mode="R") def test_success_new_image_write_permission(self): self.client.force_login(self.profile1.user) self.assertEqual(0, len(self.gallery.get_images())) for filename in ( settings.BASE_DIR / "fixtures" / "logo.png", settings.BASE_DIR / "assets" / "licenses" / "copyright.svg", ): with self.subTest(filename): with open(filename, "rb") as fp: response = self.client.post( reverse("gallery-image-new", args=[self.gallery.pk]), {"title": "Test title", "legend": "Test legend", "physical": fp}, follow=True, ) self.assertEqual(200, response.status_code) self.assertEqual(1, len(self.gallery.get_images())) self.assertEqual(3, len(os.listdir(self.gallery.get_gallery_path()))) # New image and thumbnail self.gallery.get_images()[0].delete() def test_fail_new_image_with_read_permission(self): self.client.force_login(self.profile2.user) self.assertEqual(0, len(self.gallery.get_images())) with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp: response = self.client.post( reverse("gallery-image-new", args=[self.gallery.pk]), {"title": "Test title", "legend": "Test legend", "physical": fp}, follow=True, ) self.assertEqual(403, response.status_code) self.assertEqual(0, len(self.gallery.get_images())) def test_fail_new_image_with_no_permission(self): self.client.force_login(self.profile3.user) self.assertEqual(0, len(self.gallery.get_images())) with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp: response = self.client.post( reverse("gallery-image-new", args=[self.gallery.pk]), {"title": "Test title", "legend": "Test legend", "physical": fp}, follow=True, ) self.assertEqual(403, response.status_code) self.assertEqual(0, len(self.gallery.get_images())) def test_fail_gallery_not_exist(self): self.client.force_login(self.profile1.user) with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp: response = self.client.post( reverse("gallery-image-new", args=[156]), {"title": "Test title", "legend": "Test legend", "physical": fp}, follow=True, ) self.assertEqual(404, response.status_code) def test_import_images_in_gallery(self): self.client.force_login(self.profile1.user) with (settings.BASE_DIR / "fixtures" / "archive-gallery.zip").open("rb") as fp: response = self.client.post( reverse("gallery-image-import", args=[self.gallery.pk]), {"file": fp}, follow=False ) self.assertEqual(302, response.status_code) img = self.gallery.get_images()[0] self.assertEqual(Image.objects.filter(gallery=self.gallery).count(), 1) self.assertEqual("jpg", img.get_extension()) response = self.client.post( reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery.pk}), {"delete": "", "image": img.pk}, follow=True, ) self.assertEqual(200, response.status_code) def test_import_images_in_gallery_no_archive(self): self.client.force_login(self.profile1.user) with (settings.BASE_DIR / "fixtures" / "archive-gallery.zip").open("rb"): response = self.client.post( reverse("gallery-image-import", args=[self.gallery.pk]), { # normally we have # 'file': fp # but here we want to act as if we forgot it }, follow=False, ) self.assertEqual(200, response.status_code) self.assertEqual(Image.objects.filter(gallery=self.gallery).count(), 0) def test_denies_import_images_in_gallery(self): self.client.force_login(self.profile2.user) with (settings.BASE_DIR / "fixtures" / "archive-gallery.zip").open("rb") as fp: response = self.client.post( reverse("gallery-image-import", args=[self.gallery.pk]), {"file": fp}, follow=True ) self.assertEqual(403, response.status_code) self.assertEqual(Image.objects.filter(gallery=self.gallery).count(), 0)