Beispiel #1
0
class EditImageViewTest(TestCase):
    def setUp(self):
        self.gallery = GalleryFactory()
        self.image = ImageFactory(gallery=self.gallery)
        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 tearDown(self):
        self.image.delete()

    def test_fail_member_no_permission_can_edit_image(self):
        self.client.force_login(self.profile3.user)

        with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp:

            self.client.post(
                reverse("gallery-image-edit", args=[self.gallery.pk, self.image.pk]),
                {"title": "modify with no perms", "legend": "test legend", "physical": fp},
                follow=True,
            )

        image_test = Image.objects.get(pk=self.image.pk)
        self.assertNotEqual("modify with no perms", image_test.title)
        image_test.delete()

    def test_success_member_edit_image(self):
        self.client.force_login(self.profile1.user)

        for filename in (
            settings.BASE_DIR / "fixtures" / "logo.png",
            settings.BASE_DIR / "assets" / "licenses" / "copyright.svg",
        ):
            with self.subTest(filename):
                nb_files = len(os.listdir(self.gallery.get_gallery_path()))

                with open(filename, "rb") as fp:
                    response = self.client.post(
                        reverse("gallery-image-edit", args=[self.gallery.pk, self.image.pk]),
                        {"title": "edit title", "legend": "dit legend", "physical": fp},
                        follow=True,
                    )
                self.assertEqual(200, response.status_code)
                # Check that 1 image and 2 thumbnails have been saved in the gallery
                self.assertEqual(nb_files + 3, len(os.listdir(self.gallery.get_gallery_path())))

                self.image.refresh_from_db()
                self.assertEqual("edit title", self.image.title)

    def test_access_permission(self):
        self.client.force_login(self.profile1.user)

        response = self.client.get(reverse("gallery-image-edit", args=[self.gallery.pk, self.image.pk]))

        self.assertEqual(200, response.status_code)
Beispiel #2
0
    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))
Beispiel #3
0
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)
Beispiel #4
0
class ModifyImageTest(TestCase):
    def setUp(self):
        self.profile1 = ProfileFactory()
        self.profile2 = ProfileFactory()
        self.profile3 = ProfileFactory()
        self.gallery1 = GalleryFactory()
        self.gallery2 = GalleryFactory()
        self.image1 = ImageFactory(gallery=self.gallery1)
        self.image2 = ImageFactory(gallery=self.gallery1)
        self.image3 = ImageFactory(gallery=self.gallery2)
        self.user_gallery1 = UserGalleryFactory(user=self.profile1.user, gallery=self.gallery1)
        self.user_gallery2 = UserGalleryFactory(user=self.profile1.user, gallery=self.gallery2)
        self.user_gallery3 = UserGalleryFactory(user=self.profile2.user, gallery=self.gallery1, mode="R")

    def tearDown(self):
        self.image1.delete()
        self.image2.delete()
        self.image3.delete()

    def test_fail_modify_image_with_no_permission(self):
        self.client.force_login(self.profile3.user)

        response = self.client.post(
            reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery1.pk}),
            {
                "gallery": self.gallery1.pk,
            },
            follow=True,
        )
        self.assertTrue(403, response.status_code)

    def test_delete_image_from_other_user(self):
        """if user try to remove images from another user without permission"""
        profile4 = ProfileFactory()
        gallery4 = GalleryFactory()
        image4 = ImageFactory(gallery=gallery4)
        UserGalleryFactory(user=profile4.user, gallery=gallery4)
        self.assertEqual(1, Image.objects.filter(pk=image4.pk).count())

        self.client.force_login(self.profile1.user)

        self.client.post(
            reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery1.pk}),
            {"gallery": self.gallery1.pk, "delete": "", "image": image4.pk},
            follow=True,
        )

        self.assertEqual(1, Image.objects.filter(pk=image4.pk).count())
        image4.delete()

    def test_success_delete_image_write_permission(self):
        self.client.force_login(self.profile1.user)
        nb_files = len(os.listdir(self.gallery1.get_gallery_path()))

        response = self.client.post(
            reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery1.pk}),
            {"gallery": self.gallery1.pk, "delete": "", "image": self.image1.pk},
            follow=True,
        )
        self.assertEqual(200, response.status_code)

        self.assertEqual(0, Image.objects.filter(pk=self.image1.pk).count())

        # picture AND thumbnails should be gone
        self.assertEqual(nb_files, len(os.listdir(self.gallery1.get_gallery_path())))

    def test_success_delete_list_images_write_permission(self):
        self.client.force_login(self.profile1.user)

        response = self.client.post(
            reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery1.pk}),
            {"gallery": self.gallery1.pk, "delete_multi": "", "g_items": [self.image1.pk, self.image2.pk]},
            follow=True,
        )
        self.assertEqual(200, response.status_code)

        self.assertEqual(0, Image.objects.filter(pk=self.image1.pk).count())
        self.assertEqual(0, Image.objects.filter(pk=self.image2.pk).count())

    def test_fail_delete_image_read_permission(self):
        self.client.force_login(self.profile2.user)

        response = self.client.post(
            reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery1.pk}),
            {"gallery": self.gallery1.pk, "delete": "", "image": self.image1.pk},
            follow=True,
        )
        self.assertEqual(403, response.status_code)

        self.assertEqual(1, Image.objects.filter(pk=self.image1.pk).count())
Beispiel #5
0
 def test_delete_empty_gallery(self):
     test_gallery = GalleryFactory()
     path = test_gallery.get_gallery_path()
     test_gallery.delete()
     self.assertFalse(os.path.isdir(path))