Example #1
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
Example #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)
Example #3
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)
Example #4
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')
Example #5
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()
Example #6
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()
     login_check = self.client.login(username=staff.user.username,
                                     password='******')
     self.assertTrue(login_check)
     response = self.client.get('{}{}'.format(
         reverse('featured-resource-create'),
         '?content_type=published_content&content_id={}'.format(
             tutorial.pk)))
     initial_dict = response.context['form'].initial
     self.assertEqual(initial_dict['title'], tutorial.title)
     self.assertEqual(initial_dict['authors'],
                      '{}, {}'.format(author, author2))
     self.assertEqual(initial_dict['type'], _('Un tutoriel'))
     self.assertEqual(
         initial_dict['url'],
         'http://testserver{}'.format(tutorial.get_absolute_url_online()))
     self.assertEqual(
         initial_dict['image_url'],
         'http://testserver{}'.format(image.physical['featured'].url))
Example #7
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())

        login_check = self.client.login(username=self.profile1.user.username,
                                        password='******')
        self.assertTrue(login_check)

        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()
Example #8
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()
     login_check = self.client.login(username=staff.user.username,
                                     password="******")
     self.assertTrue(login_check)
     response = self.client.get("{}{}".format(
         reverse("featured-resource-create"),
         "?content_type=published_content&content_id={}".format(
             tutorial.pk)))
     initial_dict = response.context["form"].initial
     self.assertEqual(initial_dict["title"], tutorial.title)
     self.assertEqual(initial_dict["authors"],
                      "{}, {}".format(author, author2))
     self.assertEqual(initial_dict["type"], _("Un tutoriel"))
     self.assertEqual(
         initial_dict["url"],
         "http://testserver{}".format(tutorial.get_absolute_url_online()))
     self.assertEqual(
         initial_dict["image_url"],
         "http://testserver{}".format(image.physical["featured"].url))
Example #9
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))
Example #10
0
def load_gallery(cli, size, fake):
    """
    Load galleries
    """
    nb_galleries = size * 3
    nb_images = size * 5
    cli.stdout.write(u"Nombres de galéries à créer par utilisateur: {}".format(nb_galleries))
    cli.stdout.write(u"Nombres d'images à créer par gallerie: {}".format(nb_images))
    tps1 = time.time()
    nb_users = User.objects.count()
    if nb_users == 0:
        cli.stdout.write(u"Il n'y a aucun membre actuellement. "
                         u"Vous devez rajouter les membres dans vos fixtures (member)")
    else:
        profiles = list(Profile.objects.all())
        for i in range(0, nb_users):
            for j in range(0, nb_galleries):
                gal = GalleryFactory(title=fake.text(max_nb_chars=80), subtitle=fake.text(max_nb_chars=200))
                UserGalleryFactory(user=profiles[i].user, gallery=gal)
                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()
        tps2 = time.time()
        cli.stdout.write(u"\nFait en {} sec".format(tps2 - tps1))
Example #11
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')
Example #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)
Example #13
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))
Example #14
0
    def test_new_article(self):
        # Create a Gallery
        gallery = GalleryFactory()

        # Attach an image of a gallery
        image_article = ImageFactory(gallery=gallery)
        UserGalleryFactory(user=self.user_author, gallery=gallery)

        # Create a subcategory
        subcat = SubCategoryFactory()

        # Try the preview button
        result = self.client.post(reverse('zds.article.views.new'), {
            'text': 'A wonderful poetry by Victor Hugo',
            'preview': '',
            'title': '',
            'description': '',
            'text': '',
            'image': image_article.pk,
            'subcategory': subcat.pk,
            'licence': self.licence.pk,
            'msg_commit': ''
        },
                                  follow=True)
        self.assertEqual(result.status_code, 200)

        # Create an article
        result = self.client.post(reverse('zds.article.views.new'), {
            'title':
            'Create a new article test',
            'description':
            'Describe the mew article',
            'text':
            'A wonderful poetry by Victor Hugo',
            'image':
            image_article.pk,
            'subcategory':
            subcat.pk,
            'licence':
            self.licence.pk,
            'msg_commit':
            'Celui qui ouvre une porte d\'école, ferme une prison.'
        },
                                  follow=True)
        self.assertEqual(result.status_code, 200)
        self.assertEqual(
            Article.objects.filter(title='Create a new article test').count(),
            1)
Example #15
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/{0}?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())
Example #16
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()
Example #17
0
    def test_toggle(self):
        author = ProfileFactory()
        login_check = self.client.login(username=author.user.username,
                                        password='******')
        self.assertTrue(login_check)

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

        response = self.client.post(reverse('topic-edit') +
                                    '?topic={}'.format(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') +
                                    '?topic={}'.format(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()
        login_check = self.client.login(username=other.user.username,
                                        password='******')
        self.assertTrue(login_check)

        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())
Example #18
0
    def test_filters(self):
        # create topic and content and toggle request
        author = ProfileFactory().user
        category = CategoryFactory(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()
        login_check = self.client.login(username=staff.user.username,
                                        password='******')
        self.assertTrue(login_check)

        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!
Example #19
0
 def setUp(self):
     self.gallery = GalleryFactory()
     self.image = ImageFactory(gallery=self.gallery)