예제 #1
0
    def test_save_and_delete_image(self):
        test_image = ImageFactory(gallery=self.gallery)
        image_path = test_image.physical.path
        self.assertTrue(os.path.isfile(image_path))

        test_image.delete()
        self.assertFalse(os.path.isfile(image_path))
예제 #2
0
 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)
예제 #3
0
class ImageTest(TestCase):
    def setUp(self):
        self.gallery = GalleryFactory()
        self.image = ImageFactory(gallery=self.gallery)

    def tearDown(self):
        self.image.delete()
        self.gallery.delete()

    def test_get_absolute_url(self):
        absolute_url = f"{settings.MEDIA_URL}/{self.image.physical}".replace(
            "//", "/")

        self.assertEqual(absolute_url, self.image.get_absolute_url())

    def test_get_extension(self):
        self.assertEqual("jpg", self.image.get_extension())

    def test_save_and_delete_image(self):
        test_image = ImageFactory(gallery=self.gallery)
        image_path = test_image.physical.path
        self.assertTrue(os.path.isfile(image_path))

        test_image.delete()
        self.assertFalse(os.path.isfile(image_path))
예제 #4
0
    def setUp(self):
        self.profile = ProfileFactory()
        self.other = ProfileFactory()
        self.new_participant = ProfileFactory()
        self.client = APIClient()
        client_oauth2 = create_oauth2_client(self.profile.user)
        authenticate_client(self.client, client_oauth2,
                            self.profile.user.username, "hostel77")

        self.gallery = GalleryFactory()
        UserGalleryFactory(user=self.profile.user, gallery=self.gallery)
        self.image = ImageFactory(gallery=self.gallery)

        self.gallery_other = GalleryFactory()
        UserGalleryFactory(user=self.other.user, gallery=self.gallery_other)
        self.image_other = ImageFactory(gallery=self.gallery_other)

        self.gallery_shared = GalleryFactory()
        UserGalleryFactory(user=self.other.user, gallery=self.gallery_shared)
        UserGalleryFactory(user=self.profile.user,
                           gallery=self.gallery_shared,
                           mode=GALLERY_READ)
        self.image_shared = ImageFactory(gallery=self.gallery_shared)

        tuto = PublishableContentFactory(
            type="TUTORIAL",
            author_list=[self.profile.user, self.new_participant.user])
        self.gallery_tuto = tuto.gallery
예제 #5
0
class UserGalleryTest(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_can_write(self):
        self.user_gallery.mode = "W"

        self.assertTrue(self.user_gallery.can_write())
        self.assertFalse(self.user_gallery.can_read())

    def test_can_read(self):
        self.user_gallery.mode = "R"

        self.assertFalse(self.user_gallery.can_write())
        self.assertTrue(self.user_gallery.can_read())

    def test_get_images(self):
        self.assertEqual(2, len(self.user_gallery.get_images()))

        self.assertEqual(self.image1, self.user_gallery.get_images()[0])
        self.assertEqual(self.image2, self.user_gallery.get_images()[1])
예제 #6
0
 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")
예제 #7
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)
예제 #8
0
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))
예제 #9
0
    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()
예제 #10
0
def __push_images_into_gallery(gal, i, j, nb_galleries, nb_images, nb_users):
    for k in range(0, nb_images):
        ImageFactory(gallery=gal)
        sys.stdout.write(
            " User {}/{}  \tGallery {}/{}  \tImage {}/{}  \r".format(
                i + 1, nb_users, j + 1, nb_galleries, k + 1, nb_images
            )
        )
        sys.stdout.flush()
예제 #11
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))
예제 #12
0
    def setUp(self):
        self.profile = ProfileFactory()
        self.other = ProfileFactory()
        self.client = APIClient()
        client_oauth2 = create_oauth2_client(self.profile.user)
        authenticate_client(self.client, client_oauth2,
                            self.profile.user.username, "hostel77")

        self.gallery = GalleryFactory()
        UserGalleryFactory(user=self.profile.user, gallery=self.gallery)
        self.image = ImageFactory(gallery=self.gallery)

        self.gallery_other = GalleryFactory()
        UserGalleryFactory(user=self.other.user, gallery=self.gallery_other)
        self.image_other = ImageFactory(gallery=self.gallery_other)

        self.gallery_shared = GalleryFactory()
        UserGalleryFactory(user=self.other.user, gallery=self.gallery_shared)
        UserGalleryFactory(user=self.profile.user,
                           gallery=self.gallery_shared,
                           mode=GALLERY_READ)
        self.image_shared = ImageFactory(gallery=self.gallery_shared)
예제 #13
0
 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")
예제 #14
0
    def test_get_avatar_url(self):
        # if no url was specified -> gravatar !
        self.assertEqual(
            self.user1.get_avatar_url(),
            "https://secure.gravatar.com/avatar/{}?d=identicon".format(
                md5(self.user1.user.email.lower().encode()).hexdigest()),
        )
        # if an url is specified -> take it !
        user2 = ProfileFactory()
        testurl = "http://test.com/avatar.jpg"
        user2.avatar_url = testurl
        self.assertEqual(user2.get_avatar_url(), testurl)

        # if url is relative, send absolute url
        gallerie_avtar = GalleryFactory()
        image_avatar = ImageFactory(gallery=gallerie_avtar)
        user2.avatar_url = image_avatar.physical.url
        self.assertNotEqual(user2.get_avatar_url(), image_avatar.physical.url)
        self.assertIn("http", user2.get_avatar_url())
예제 #15
0
 def test_success_initial_content(self):
     author = ProfileFactory().user
     author2 = ProfileFactory().user
     tutorial = PublishedContentFactory(author_list=[author, author2])
     gallery = GalleryFactory()
     image = ImageFactory(gallery=gallery)
     tutorial.image = image
     tutorial.save()
     staff = StaffProfileFactory()
     self.client.force_login(staff.user)
     response = self.client.get(
         "{}{}".format(
             reverse("featured-resource-create"), f"?content_type=published_content&content_id={tutorial.pk}"
         )
     )
     initial_dict = response.context["form"].initial
     self.assertEqual(initial_dict["title"], tutorial.title)
     self.assertEqual(initial_dict["authors"], f"{author}, {author2}")
     self.assertEqual(initial_dict["type"], _("Un tutoriel"))
     self.assertEqual(initial_dict["url"], f"http://testserver{tutorial.get_absolute_url_online()}")
     self.assertEqual(initial_dict["image_url"], "http://testserver{}".format(image.physical["featured"].url))
예제 #16
0
 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")
     default_gallery_u1 = GalleryFactory(title="default", slug="default", subtitle="bla")
     UserGalleryFactory(user=self.profile1.user, gallery=default_gallery_u1, is_default=True)
     default_gallery_u2 = GalleryFactory(title="default", slug="default", subtitle="bla")
     UserGalleryFactory(user=self.profile2.user, gallery=default_gallery_u2, is_default=True)
예제 #17
0
    def test_filters(self):
        # create topic and content and toggle request
        author = ProfileFactory().user
        category = ForumCategoryFactory(position=1)
        forum = ForumFactory(category=category, position_in_category=1)
        topic = TopicFactory(forum=forum, author=author)

        FeaturedRequested.objects.toogle_request(topic, author)

        tutorial = PublishedContentFactory(author_list=[author])
        gallery = GalleryFactory()
        image = ImageFactory(gallery=gallery)
        tutorial.image = image
        tutorial.save()

        FeaturedRequested.objects.toogle_request(tutorial, author)

        # without filter
        staff = StaffProfileFactory()
        self.client.force_login(staff.user)

        response = self.client.get(reverse("featured-resource-requests"))
        self.assertEqual(200, response.status_code)

        self.assertEqual(len(response.context["featured_request_list"]), 2)
        self.assertTrue(any(r.content_object == topic for r in response.context["featured_request_list"]))
        self.assertTrue(any(r.content_object == tutorial for r in response.context["featured_request_list"]))

        # filter topic
        response = self.client.get(reverse("featured-resource-requests") + "?type=topic")
        self.assertEqual(200, response.status_code)

        self.assertEqual(len(response.context["featured_request_list"]), 1)
        self.assertTrue(any(r.content_object == topic for r in response.context["featured_request_list"]))
        self.assertFalse(any(r.content_object == tutorial for r in response.context["featured_request_list"]))

        # filter tuto
        response = self.client.get(reverse("featured-resource-requests") + "?type=content")
        self.assertEqual(200, response.status_code)

        self.assertEqual(len(response.context["featured_request_list"]), 1)
        self.assertFalse(any(r.content_object == topic for r in response.context["featured_request_list"]))
        self.assertTrue(any(r.content_object == tutorial for r in response.context["featured_request_list"]))

        # reject topic
        content_type = ContentType.objects.get_for_model(topic)
        q = FeaturedRequested.objects.get(object_id=topic.pk, content_type__pk=content_type.pk)
        q.rejected = True
        q.save()

        response = self.client.get(reverse("featured-resource-requests") + "?type=topic")
        self.assertEqual(200, response.status_code)

        self.assertEqual(len(response.context["featured_request_list"]), 0)

        # filter ignored
        response = self.client.get(reverse("featured-resource-requests") + "?type=ignored")
        self.assertEqual(200, response.status_code)

        self.assertEqual(len(response.context["featured_request_list"]), 1)
        self.assertTrue(any(r.content_object == topic for r in response.context["featured_request_list"]))

        # put back vote count to 0 for tutorial
        FeaturedRequested.objects.toogle_request(tutorial, author)
        response = self.client.get(reverse("featured-resource-requests") + "?type=content")
        self.assertEqual(200, response.status_code)

        self.assertEqual(len(response.context["featured_request_list"]), 0)  # does not appear with no votes

        # upvote topic
        other = ProfileFactory().user
        FeaturedRequested.objects.toogle_request(topic, other)

        response = self.client.get(reverse("featured-resource-requests") + "?type=topic")
        self.assertEqual(200, response.status_code)

        self.assertEqual(len(response.context["featured_request_list"]), 1)  # it is back!
예제 #18
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())
예제 #19
0
 def setUp(self):
     self.gallery = GalleryFactory()
     self.image = ImageFactory(gallery=self.gallery)
예제 #20
0
class ImageDetailAPITest(APITestCase):
    def setUp(self):
        self.profile = ProfileFactory()
        self.other = ProfileFactory()
        self.client = APIClient()
        client_oauth2 = create_oauth2_client(self.profile.user)
        authenticate_client(self.client, client_oauth2,
                            self.profile.user.username, "hostel77")

        self.gallery = GalleryFactory()
        UserGalleryFactory(user=self.profile.user, gallery=self.gallery)
        self.image = ImageFactory(gallery=self.gallery)

        self.gallery_other = GalleryFactory()
        UserGalleryFactory(user=self.other.user, gallery=self.gallery_other)
        self.image_other = ImageFactory(gallery=self.gallery_other)

        self.gallery_shared = GalleryFactory()
        UserGalleryFactory(user=self.other.user, gallery=self.gallery_shared)
        UserGalleryFactory(user=self.profile.user,
                           gallery=self.gallery_shared,
                           mode=GALLERY_READ)
        self.image_shared = ImageFactory(gallery=self.gallery_shared)

    def test_get_image(self):
        response = self.client.get(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery.pk,
                        "pk": self.image.pk
                    }))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(response.data.get("id"), self.image.pk)
        self.assertEqual(response.data.get("title"), self.image.title)
        self.assertEqual(response.data.get("legend"), self.image.legend)
        self.assertEqual(response.data.get("slug"), self.image.slug)
        self.assertEqual(response.data.get("thumbnail"),
                         self.image.get_thumbnail_url())
        self.assertEqual(response.data.get("url"),
                         self.image.get_absolute_url())
        self.assertEqual(response.data.get("permissions"), {
            "read": True,
            "write": True
        })

    def test_get_image_read_permissions(self):
        response = self.client.get(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery_shared.pk,
                        "pk": self.image_shared.pk
                    }))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(response.data.get("id"), self.image_shared.pk)
        self.assertEqual(response.data.get("permissions"), {
            "read": True,
            "write": False
        })

    def test_get_image_fail_permissions(self):
        response = self.client.get(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery_other.pk,
                        "pk": self.image_other.pk
                    }))

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_put_modify_image(self):
        title = "un super titre pour une super image"
        legend = "as-tu vu ma légende?"

        response = self.client.put(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery.pk,
                        "pk": self.image.pk
                    }),
            {
                "title":
                title,
                "legend":
                legend,
                "physical":
                (settings.BASE_DIR / "fixtures" / "noir_black.png").open("rb"),
            },
            format="multipart",
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        image = Image.objects.get(pk=self.image.pk)
        self.assertEqual(image.title, title)
        self.assertEqual(image.legend, legend)

    def test_put_fail_modify_image_not_an_image(self):
        title = "un super titre pour une super image"
        legend = "en vrai je peux pas"

        response = self.client.put(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery.pk,
                        "pk": self.image.pk
                    }),
            {
                "title":
                title,
                "legend":
                legend,
                "physical": (settings.BASE_DIR / "assets" / "licenses" /
                             "0.svg").open("rb"),
            },
        )

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        image = Image.objects.get(pk=self.image.pk)
        self.assertNotEqual(image.title, title)
        self.assertNotEqual(image.legend, legend)

    def test_put_fail_modify_image_no_permissions(self):
        title = "un super titre pour une super image"
        legend = "en vrai je peux pas"

        response = self.client.put(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery_other.pk,
                        "pk": self.image_other.pk
                    }),
            {
                "title": title,
                "legend": legend
            },
        )

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_put_fail_modify_image_read_permissions(self):
        title = "un super titre pour une super image"
        legend = "en vrai je peux toujours pas :p"

        response = self.client.put(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery_shared.pk,
                        "pk": self.image_shared.pk
                    }),
            {
                "title": title,
                "legend": legend
            },
        )

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_delete_image(self):
        response = self.client.delete(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery.pk,
                        "pk": self.image.pk
                    }))

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(Image.objects.filter(gallery=self.gallery).count(), 0)

    def test_delete_image_fail_no_permissions(self):
        response = self.client.delete(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery_other.pk,
                        "pk": self.image_other.pk
                    }))

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(
            Image.objects.filter(gallery=self.gallery_other).count(), 1)

    def test_delete_image_fail_read_permissions(self):
        response = self.client.delete(
            reverse("api:gallery:detail-image",
                    kwargs={
                        "pk_gallery": self.gallery_shared.pk,
                        "pk": self.image_shared.pk
                    }))

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(
            Image.objects.filter(gallery=self.gallery_shared).count(), 1)
예제 #21
0
class ModifyGalleryViewTest(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")
        default_gallery_u1 = GalleryFactory(title="default", slug="default", subtitle="bla")
        UserGalleryFactory(user=self.profile1.user, gallery=default_gallery_u1, is_default=True)
        default_gallery_u2 = GalleryFactory(title="default", slug="default", subtitle="bla")
        UserGalleryFactory(user=self.profile2.user, gallery=default_gallery_u2, is_default=True)

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

    def test_fail_delete_multi_read_permission(self):
        """when user wants to delete a list of galleries just with a read permission"""
        self.client.force_login(self.profile2.user)

        self.assertEqual(4, Gallery.objects.all().count())
        self.assertEqual(5, UserGallery.objects.all().count())
        self.assertEqual(3, Image.objects.all().count())

        response = self.client.post(
            reverse("galleries-delete"), {"delete_multi": "", "g_items": [self.gallery1.pk]}, follow=True
        )
        self.assertEqual(200, response.status_code)

        self.assertEqual(4, Gallery.objects.all().count())
        self.assertEqual(5, UserGallery.objects.all().count())
        self.assertEqual(3, Image.objects.all().count())

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

        self.assertEqual(4, Gallery.objects.all().count())
        self.assertEqual(5, UserGallery.objects.all().count())
        self.assertEqual(3, Image.objects.all().count())

        response = self.client.post(
            reverse("galleries-delete"),
            {"delete_multi": "", "g_items": [self.gallery1.pk, self.gallery2.pk]},
            follow=True,
        )
        self.assertEqual(200, response.status_code)
        self.assertEqual(2, Gallery.objects.all().count())
        self.assertEqual(2, UserGallery.objects.all().count())
        self.assertEqual(0, Image.objects.all().count())

    def test_fail_delete_read_permission(self):
        """when user wants to delete a gallery just with a read permission"""
        self.client.force_login(self.profile2.user)

        self.assertEqual(4, Gallery.objects.all().count())
        self.assertEqual(5, UserGallery.objects.all().count())
        self.assertEqual(3, Image.objects.all().count())

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

        self.assertEqual(4, Gallery.objects.all().count())
        self.assertEqual(5, UserGallery.objects.all().count())
        self.assertEqual(3, Image.objects.all().count())

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

        self.assertEqual(4, Gallery.objects.all().count())
        self.assertEqual(5, UserGallery.objects.all().count())
        self.assertEqual(3, Image.objects.all().count())

        response = self.client.post(
            reverse("galleries-delete"), {"delete": "", "gallery": self.gallery1.pk}, follow=True
        )
        self.assertEqual(200, response.status_code)
        self.assertEqual(0, Gallery.objects.filter(pk=self.gallery1.pk).count())
        self.assertEqual(0, UserGallery.objects.filter(gallery=self.gallery1).count())
        self.assertEqual(0, Image.objects.filter(gallery=self.gallery1).count())

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

        # gallery nonexistent
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": 99999}),
            {
                "action": "add",
            },
        )
        self.assertEqual(404, response.status_code)

        # try to add a user with write permission
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "add",
                "user": self.profile2.user.username,
                "mode": "W",
            },
        )
        self.assertEqual(403, response.status_code)

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

        # Same permission : read
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "add",
                "user": self.profile2.user.username,
                "mode": "R",
            },
            follow=True,
        )
        self.assertEqual(200, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile2.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("R", permissions[0].mode)

        # try to add write permission to a user
        # who has already an read permission
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "add",
                "user": self.profile2.user.username,
                "mode": "W",
            },
            follow=True,
        )
        self.assertEqual(200, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile2.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("R", permissions[0].mode)

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

        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "add",
                "user": self.profile3.user.username,
                "mode": "R",
            },
            follow=True,
        )
        self.assertEqual(200, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile3.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("R", permissions[0].mode)

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

        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "add",
                "user": self.profile3.user.username,
                "mode": "W",
            },
            follow=True,
        )
        self.assertEqual(200, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile3.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("W", permissions[0].mode)

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

        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "add",
                "user": self.profile3.user.username,
                "mode": "W",
            },
            follow=True,
        )
        self.assertEqual(200, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile3.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("W", permissions[0].mode)

        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "edit",
                "user": self.profile3.user.username,
                "mode": "R",
            },
            follow=True,
        )
        self.assertEqual(200, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile3.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("R", permissions[0].mode)

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

        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "edit",
                "user": self.profile1.user.username,
                "mode": "R",
            },
            follow=True,
        )
        self.assertEqual(403, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile1.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("W", permissions[0].mode)

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

        user_galleries = UserGallery.objects.filter(gallery=self.gallery1)
        self.assertEqual(user_galleries.count(), 2)

        # kick the other
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "leave",
                "user": self.profile2.user.username,
            },
            follow=True,
        )

        self.assertEqual(200, response.status_code)
        self.assertEqual(user_galleries.count(), 1)
        self.assertEqual(user_galleries.last().user, self.profile1.user)

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

        user_gallery = UserGallery.objects.filter(gallery=self.gallery1, user=self.profile1.user)
        self.assertEqual(user_gallery.count(), 1)

        # try to quit
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "leave",
                "user": self.profile1.user.username,
            },
            follow=True,
        )

        self.assertEqual(200, response.status_code)
        self.assertEqual(user_gallery.count(), 1)  # not gone

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

        user_galleries = UserGallery.objects.filter(gallery=self.gallery1)
        self.assertEqual(user_galleries.count(), 2)

        # leave
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "leave",
                "user": self.profile2.user.username,
            },
            follow=True,
        )

        self.assertEqual(200, response.status_code)
        self.assertEqual(user_galleries.count(), 1)
        self.assertEqual(user_galleries.last().user, self.profile1.user)

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

        user_galleries = UserGallery.objects.filter(gallery=self.gallery1)
        self.assertEqual(user_galleries.count(), 2)

        # set W
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "edit",
                "user": self.profile2.user.username,
                "mode": "W",
            },
            follow=True,
        )
        self.assertEqual(403, response.status_code)
        permissions = UserGallery.objects.filter(user=self.profile2.user, gallery=self.gallery1)
        self.assertEqual(1, len(permissions))
        self.assertEqual("R", permissions[0].mode)

        # kick other
        response = self.client.post(
            reverse("gallery-members", kwargs={"pk": self.gallery1.pk}),
            {
                "action": "leave",
                "user": self.profile1.user.username,
            },
            follow=True,
        )

        self.assertEqual(403, response.status_code)
        self.assertEqual(user_galleries.count(), 2)
예제 #22
0
    def test_toggle(self):
        author = ProfileFactory()
        self.client.force_login(author.user)

        # create topic and toggle request
        category = ForumCategoryFactory(position=1)
        forum = ForumFactory(category=category, position_in_category=1)
        topic = TopicFactory(forum=forum, author=author.user)

        response = self.client.post(
            reverse("topic-edit") + f"?topic={topic.pk}",
            {"request_featured": 1},
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(200, response.status_code)

        self.assertEqual(FeaturedRequested.objects.count(), 1)
        r = FeaturedRequested.objects.last()
        self.assertEqual(r.content_object, topic)
        self.assertIn(author.user, r.users_voted.all())

        # lock topic: cannot vote anymore
        topic.is_locked = True
        topic.save()

        response = self.client.post(
            reverse("topic-edit") + f"?topic={topic.pk}",
            {"request_featured": 1},
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(403, response.status_code)
        self.assertEqual(FeaturedRequested.objects.count(), 1)

        # create tutorial and toggle request
        tutorial = PublishedContentFactory(author_list=[author.user])
        gallery = GalleryFactory()
        image = ImageFactory(gallery=gallery)
        tutorial.image = image
        tutorial.save()

        response = self.client.post(
            reverse("content:request-featured", kwargs={"pk": tutorial.pk}),
            {"request_featured": 1},
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(200, response.status_code)

        self.assertEqual(FeaturedRequested.objects.count(), 2)
        r = FeaturedRequested.objects.last()
        self.assertEqual(r.content_object, tutorial)
        self.assertIn(author.user, r.users_voted.all())

        # create opinion: cannot toggle request!
        opinion = PublishedContentFactory(type="OPINION", author_list=[author.user])
        gallery = GalleryFactory()
        image = ImageFactory(gallery=gallery)
        opinion.image = image
        opinion.save()

        response = self.client.post(
            reverse("content:request-featured", kwargs={"pk": opinion.pk}),
            {"request_featured": 1},
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(403, response.status_code)
        self.assertEqual(FeaturedRequested.objects.count(), 2)

        # set tutorial as obsolete: cannot toggle
        tutorial.is_obsolete = True
        tutorial.save()

        response = self.client.post(
            reverse("content:request-featured", kwargs={"pk": tutorial.pk}),
            {"request_featured": 1},
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(403, response.status_code)

        r = FeaturedRequested.objects.get(pk=r.pk)
        self.assertEqual(r.content_object, tutorial)
        self.assertIn(author.user, r.users_voted.all())

        # reject tutorial proposition
        tutorial.is_obsolete = False  # can vote again
        tutorial.save()

        r = FeaturedRequested.objects.get(pk=r.pk)
        r.rejected = True
        r.save()

        # upvote with other user
        other = ProfileFactory()
        self.client.force_login(other.user)

        response = self.client.post(
            reverse("content:request-featured", kwargs={"pk": tutorial.pk}),
            {"request_featured": 1},
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(200, response.status_code)

        r = FeaturedRequested.objects.get(pk=r.pk)
        self.assertIn(other.user, r.users_voted.all())
        self.assertFalse(r.rejected)  # not rejected anymore

        # reject for good, cannot vote anymore!
        r.rejected_for_good = True
        r.save()

        response = self.client.post(
            reverse("content:request-featured", kwargs={"pk": tutorial.pk}),
            {"request_featured": 1},
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(403, response.status_code)

        r = FeaturedRequested.objects.get(pk=r.pk)
        self.assertIn(other.user, r.users_voted.all())