def setUp(self): settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" self.mas = ProfileFactory() settings.ZDS_APP["member"]["bot_account"] = self.mas.user.username self.anonymous = UserFactory(username=settings.ZDS_APP["member"]["anonymous_account"], password="******") self.external = UserFactory(username=settings.ZDS_APP["member"]["external_account"], password="******") self.category1 = ForumCategoryFactory(position=1) self.forum11 = ForumFactory(category=self.category1, position_in_category=1) self.staff = StaffProfileFactory().user self.bot = Group(name=settings.ZDS_APP["member"]["bot_group"]) self.bot.save()
def test_pubdate_on_notification_updated(self): """ When we update a notification, we should update its pubdate too. """ topic = TopicFactory(forum=self.forum11, author=self.user1) PostFactory(topic=topic, author=self.user1, position=1) topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by( self.user1) self.assertEqual(1, len(topics_followed)) post = PostFactory(topic=topic, author=self.user2, position=2) old_notification = Notification.objects.get( subscription__user=self.user1, object_id=post.pk, is_read=False) old_notification.pubdate = datetime.now() - timedelta(days=1) old_notification.save() self.assertEqual(old_notification.object_id, post.pk) self.assertEqual(old_notification.subscription.object_id, topic.pk) # read it. old_notification.is_read = True old_notification.save() user3 = UserFactory() post2 = PostFactory(topic=topic, author=user3, position=3) new_notification = Notification.objects.get( subscription__user=self.user1, object_id=post2.pk, is_read=False) self.assertEqual(new_notification.object_id, post2.pk) self.assertEqual(new_notification.subscription.object_id, topic.pk) # Check that the pubdate is well updated. self.assertTrue(old_notification.pubdate < new_notification.pubdate)
def setUp(self): self.overridden_zds_app = overridden_zds_app # don't build PDF to speed up the tests overridden_zds_app["content"]["build_pdf_when_published"] = False self.staff = StaffProfileFactory().user settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" self.mas = ProfileFactory().user overridden_zds_app["member"]["bot_account"] = self.mas.username bot = Group(name=overridden_zds_app["member"]["bot_group"]) bot.save() self.external = UserFactory( username=overridden_zds_app["member"]["external_account"], password="******") self.beta_forum = ForumFactory( pk=overridden_zds_app["forum"]["beta_forum_id"], category=ForumCategoryFactory(position=1), position_in_category=1, ) # ensure that the forum, for the beta versions, is created self.licence = LicenceFactory() self.subcategory = SubCategoryFactory() self.tag = TagFactory() self.user_author = ProfileFactory().user self.user_staff = StaffProfileFactory().user self.user_guest = ProfileFactory().user # create an article self.article = PublishableContentFactory(type="ARTICLE") self.article.authors.add(self.user_author) UserGalleryFactory(gallery=self.article.gallery, user=self.user_author, mode="W") self.article.licence = self.licence self.article.subcategory.add(self.subcategory) self.article.tags.add(self.tag) self.article.save() # fill it with one extract self.article_draft = self.article.load_version() self.extract1 = ExtractFactory(container=self.article_draft, db_object=self.article) # then, publish it ! version = self.article_draft.current_version self.published = publish_content(self.article, self.article_draft, is_major_update=True) self.article.sha_public = version self.article.sha_draft = version self.article.public_version = self.published self.article.save() self.articlefeed = LastArticlesFeedRSS()
def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.anonymous_account = UserFactory( username=settings.ZDS_APP["member"]["anonymous_account"]) self.bot_group = Group() self.bot_group.name = settings.ZDS_APP["member"]["bot_group"] self.bot_group.save() self.anonymous_account.groups.add(self.bot_group) self.anonymous_account.save() self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.client.force_login(self.profile1.user)
def setUp(self): self.staff = StaffProfileFactory().user settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" self.mas = ProfileFactory().user self.overridden_zds_app["member"]["bot_account"] = self.mas.username self.licence = LicenceFactory() self.subcategory = SubCategoryFactory() self.user_author = ProfileFactory().user self.user_staff = StaffProfileFactory().user self.user_guest = ProfileFactory().user self.tuto = PublishableContentFactory(type="TUTORIAL") self.tuto.authors.add(self.user_author) UserGalleryFactory(gallery=self.tuto.gallery, user=self.user_author, mode="W") self.tuto.licence = self.licence self.tuto.subcategory.add(self.subcategory) self.tuto.save() self.beta_forum = ForumFactory( pk=self.overridden_zds_app["forum"]["beta_forum_id"], category=ForumCategoryFactory(position=1), position_in_category=1, ) # ensure that the forum, for the beta versions, is created self.tuto_draft = self.tuto.load_version() self.part1 = ContainerFactory(parent=self.tuto_draft, db_object=self.tuto) self.chapter1 = ContainerFactory(parent=self.part1, db_object=self.tuto) self.extract1 = ExtractFactory(container=self.chapter1, db_object=self.tuto) bot = Group(name=self.overridden_zds_app["member"]["bot_group"]) bot.save() self.external = UserFactory( username=self.overridden_zds_app["member"]["external_account"], password="******")
def test_topics_followed_by_a_user(self): """ Check that we correctly retrieve all topics followed by a user. """ user = UserFactory() topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by(user) self.assertEqual(0, len(topics_followed)) first = TopicFactory(forum=self.forum11, author=user) second = TopicFactory(forum=self.forum11, author=user) third = TopicFactory(forum=self.forum11, author=user) # Subscribes to all topics. TopicAnswerSubscription.objects.get_or_create_active(user, second) TopicAnswerSubscription.objects.get_or_create_active(user, first) TopicAnswerSubscription.objects.get_or_create_active(user, third) topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by(user) self.assertEqual(3, len(topics_followed))
class AddParticipantViewTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.anonymous_account = UserFactory( username=settings.ZDS_APP["member"]["anonymous_account"]) self.bot_group = Group() self.bot_group.name = settings.ZDS_APP["member"]["bot_group"] self.bot_group.save() self.anonymous_account.groups.add(self.bot_group) self.anonymous_account.save() self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.client.force_login(self.profile1.user) def test_denies_anonymous(self): self.client.logout() response = self.client.get(reverse("mp-edit-participant", args=[1, "private-topic"]), follow=True) self.assertRedirects( response, reverse("member-login") + "?next=" + reverse("mp-edit-participant", args=[1, "private-topic"])) def test_fail_add_participant_topic_no_exist(self): response = self.client.post(reverse("mp-edit-participant", args=[451, "private-topic"]), follow=True) self.assertEqual(404, response.status_code) def test_test_fail_add_bot_as_participant(self): self.client.logout() self.client.force_login(self.profile1.user) self.client.post( reverse("mp-edit-participant", args=[self.topic1.pk, self.topic1.slug]), {"username": self.anonymous_account.username}, ) # TODO (Arnaud-D): this test actually succeeds because of a Http404. # NotReachableError is not raised. self.assertFalse( self.anonymous_account in self.topic1.participants.all()) def test_fail_add_participant_who_no_exist(self): response = self.client.post(reverse( "mp-edit-participant", args=[self.topic1.pk, self.topic1.slug]), {"username": "******"}, follow=True) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.context["messages"])) def test_fail_add_participant_with_no_right(self): profile3 = ProfileFactory() self.client.logout() self.client.force_login(profile3.user) response = self.client.post( reverse("mp-edit-participant", args=[self.topic1.pk, self.topic1.slug]), {"username": profile3.user.username}, ) self.assertEqual(403, response.status_code) self.assertNotIn( profile3.user, PrivateTopic.objects.get(pk=self.topic1.pk).participants.all()) def test_fail_add_participant_already_in(self): response = self.client.post( reverse("mp-edit-participant", args=[self.topic1.pk, self.topic1.slug]), {"username": self.profile2.user.username}, follow=True, ) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.context["messages"])) def test_success_add_participant(self): profile3 = ProfileFactory() response = self.client.post( reverse("mp-edit-participant", args=[self.topic1.pk, self.topic1.slug]), {"username": profile3.user.username}, follow=True, ) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.context["messages"])) self.assertIn( profile3.user, PrivateTopic.objects.get(pk=self.topic1.pk).participants.all())
class LeaveViewTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.anonymous_account = UserFactory( username=settings.ZDS_APP["member"]["anonymous_account"]) self.bot_group = Group() self.bot_group.name = settings.ZDS_APP["member"]["bot_group"] self.bot_group.save() self.anonymous_account.groups.add(self.bot_group) self.anonymous_account.save() self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.client.force_login(self.profile1.user) def test_denies_anonymous(self): self.client.logout() response = self.client.get(reverse("mp-delete", args=[1, "private-topic"]), follow=True) self.assertRedirects( response, reverse("member-login") + "?next=" + reverse("mp-delete", args=[1, "private-topic"])) def test_fail_leave_topic_no_exist(self): response = self.client.post( reverse("mp-delete", args=[999, "private-topic"])) self.assertEqual(404, response.status_code) def test_success_leave_topic_as_author_no_participants(self): self.topic1.participants.clear() self.topic1.save() response = self.client.post(reverse( "mp-delete", args=[self.topic1.pk, self.topic1.slug]), follow=True) self.assertEqual(200, response.status_code) self.assertEqual( 0, PrivateTopic.objects.filter(pk=self.topic1.pk).all().count()) def test_success_leave_topic_as_author(self): response = self.client.post(reverse( "mp-delete", args=[self.topic1.pk, self.topic1.slug]), follow=True) self.assertEqual(200, response.status_code) self.assertEqual( 1, PrivateTopic.objects.filter(pk=self.topic1.pk).all().count()) self.assertEqual(self.profile2.user, PrivateTopic.objects.get(pk=self.topic1.pk).author) def test_success_leave_topic_as_participant(self): self.client.logout() self.client.force_login(self.profile2.user) response = self.client.post(reverse( "mp-delete", args=[self.topic1.pk, self.topic1.slug]), follow=True) self.assertEqual(200, response.status_code) self.assertNotIn( self.profile2.user, PrivateTopic.objects.get(pk=self.topic1.pk).participants.all()) self.assertNotEqual(self.profile2.user, PrivateTopic.objects.get(pk=self.topic1.pk).author)