class InterventionsTest(TestCase): """ This test uses quite complicated paths to check number of notifications: 1. Create private topics and do stuff with them 2. Log the user 3. Render the home page 4. Check the number of unread private messages on home page source code This because a correct test of this function requires a complete context (or it behave strangely) """ def setUp(self): self.author = ProfileFactory() self.user = ProfileFactory() self.topic = PrivateTopicFactory(author=self.author.user) self.topic.participants.add(self.user.user) self.post = PrivatePostFactory(privatetopic=self.topic, author=self.author.user, position_in_topic=1) def test_interventions_privatetopics(self): self.assertTrue( self.client.login(username=self.author.user.username, password='******')) response = self.client.post(reverse('zds.pages.views.home')) self.assertEqual(200, response.status_code) self.assertContains(response, '<span class="notif-count">1</span>', html=True) self.client.logout() self.assertTrue( self.client.login(username=self.user.user.username, password='******')) response = self.client.post(reverse('zds.pages.views.home')) self.assertEqual(200, response.status_code) self.assertContains(response, '<span class="notif-count">1</span>', html=True) def test_interventions_privatetopics_author_leave(self): # profile1 (author) leave topic move = self.topic.participants.first() self.topic.author = move self.topic.participants.remove(move) self.topic.save() self.assertTrue( self.client.login(username=self.user.user.username, password='******')) response = self.client.post(reverse('zds.pages.views.home')) self.assertEqual(200, response.status_code) self.assertContains(response, '<span class="notif-count">1</span>', html=True)
def test_list_of_private_topics_unread(self): """ Gets list of private topics unread of a member. """ private_topic = PrivateTopicFactory(author=self.another_profile.user) private_topic.participants.add(self.profile.user) private_topic.save() response = self.client.get(reverse('api-mp-list-unread')) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.get('count'), 1) self.assertEqual(len(response.data.get('results')), 1) self.assertIsNone(response.data.get('next')) self.assertIsNone(response.data.get('previous'))
def test_unregister(self): """ To test that unregistering user is working. """ # test not logged user can't unregister. self.client.logout() result = self.client.post(reverse('member-unregister'), follow=False) self.assertEqual(result.status_code, 302) # test logged user can register. user = ProfileFactory() login_check = self.client.login(username=user.user.username, password='******') self.assertEqual(login_check, True) result = self.client.post(reverse('member-unregister'), follow=False) self.assertEqual(result.status_code, 302) self.assertEqual( User.objects.filter(username=user.user.username).count(), 0) # Attach a user at tutorials, articles, topics and private topics. After that, # unregister this user and check that he is well removed in all contents. user = ProfileFactory() user2 = ProfileFactory() alone_gallery = GalleryFactory() UserGalleryFactory(gallery=alone_gallery, user=user.user) shared_gallery = GalleryFactory() UserGalleryFactory(gallery=shared_gallery, user=user.user) UserGalleryFactory(gallery=shared_gallery, user=user2.user) # first case : a published tutorial with only one author published_tutorial_alone = PublishedContentFactory(type='TUTORIAL') published_tutorial_alone.authors.add(user.user) published_tutorial_alone.save() # second case : a published tutorial with two authors published_tutorial_2 = PublishedContentFactory(type='TUTORIAL') published_tutorial_2.authors.add(user.user) published_tutorial_2.authors.add(user2.user) published_tutorial_2.save() # third case : a private tutorial with only one author writing_tutorial_alone = PublishableContentFactory(type='TUTORIAL') writing_tutorial_alone.authors.add(user.user) writing_tutorial_alone.save() writing_tutorial_alone_galler_path = writing_tutorial_alone.gallery.get_gallery_path( ) # fourth case : a private tutorial with at least two authors writing_tutorial_2 = PublishableContentFactory(type='TUTORIAL') writing_tutorial_2.authors.add(user.user) writing_tutorial_2.authors.add(user2.user) writing_tutorial_2.save() self.client.login(username=self.staff.username, password="******") # same thing for articles published_article_alone = PublishedContentFactory(type='ARTICLE') published_article_alone.authors.add(user.user) published_article_alone.save() published_article_2 = PublishedContentFactory(type='ARTICLE') published_article_2.authors.add(user.user) published_article_2.authors.add(user2.user) published_article_2.save() writing_article_alone = PublishableContentFactory(type='ARTICLE') writing_article_alone.authors.add(user.user) writing_article_alone.save() writing_article_2 = PublishableContentFactory(type='ARTICLE') writing_article_2.authors.add(user.user) writing_article_2.authors.add(user2.user) writing_article_2.save() # beta content beta_forum = ForumFactory(category=CategoryFactory()) beta_content = BetaContentFactory(author_list=[user.user], forum=beta_forum) beta_content_2 = BetaContentFactory( author_list=[user.user, user2.user], forum=beta_forum) # about posts and topics authored_topic = TopicFactory(author=user.user, forum=self.forum11) answered_topic = TopicFactory(author=user2.user, forum=self.forum11) PostFactory(topic=answered_topic, author=user.user, position=2) edited_answer = PostFactory(topic=answered_topic, author=user.user, position=3) edited_answer.editor = user.user edited_answer.save() upvoted_answer = PostFactory(topic=answered_topic, author=user2.user, position=4) upvoted_answer.like += 1 upvoted_answer.save() CommentVote.objects.create(user=user.user, comment=upvoted_answer, positive=True) private_topic = PrivateTopicFactory(author=user.user) private_topic.participants.add(user2.user) private_topic.save() PrivatePostFactory(author=user.user, privatetopic=private_topic, position_in_topic=1) # add API key self.assertEqual(Application.objects.count(), 0) self.assertEqual(AccessToken.objects.count(), 0) api_application = Application() api_application.client_id = 'foobar' api_application.user = user.user api_application.client_type = 'confidential' api_application.authorization_grant_type = 'password' api_application.client_secret = '42' api_application.save() token = AccessToken() token.user = user.user token.token = 'r@d0m' token.application = api_application token.expires = datetime.now() token.save() self.assertEqual(Application.objects.count(), 1) self.assertEqual(AccessToken.objects.count(), 1) # login and unregister: login_check = self.client.login(username=user.user.username, password='******') self.assertEqual(login_check, True) result = self.client.post(reverse('member-unregister'), follow=False) self.assertEqual(result.status_code, 302) # check that the bot have taken authorship of tutorial: self.assertEqual(published_tutorial_alone.authors.count(), 1) self.assertEqual(published_tutorial_alone.authors.first().username, settings.ZDS_APP["member"]["external_account"]) self.assertFalse(os.path.exists(writing_tutorial_alone_galler_path)) self.assertEqual(published_tutorial_2.authors.count(), 1) self.assertEqual( published_tutorial_2.authors.filter( username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) # check that published tutorials remain published and accessible self.assertIsNotNone( published_tutorial_2.public_version.get_prod_path()) self.assertTrue( os.path.exists( published_tutorial_2.public_version.get_prod_path())) self.assertIsNotNone( published_tutorial_alone.public_version.get_prod_path()) self.assertTrue( os.path.exists( published_tutorial_alone.public_version.get_prod_path())) self.assertEqual( self.client.get(reverse('tutorial:view', args=[ published_tutorial_alone.pk, published_tutorial_alone.slug ]), follow=False).status_code, 200) self.assertEqual( self.client.get(reverse( 'tutorial:view', args=[published_tutorial_2.pk, published_tutorial_2.slug]), follow=False).status_code, 200) # test that published articles remain accessible self.assertTrue( os.path.exists( published_article_alone.public_version.get_prod_path())) self.assertEqual( self.client.get(reverse('article:view', args=[ published_article_alone.pk, published_article_alone.slug ]), follow=True).status_code, 200) self.assertEqual( self.client.get(reverse( 'article:view', args=[published_article_2.pk, published_article_2.slug]), follow=True).status_code, 200) # check that the tutorial for which the author was alone does not exists anymore self.assertEqual( PublishableContent.objects.filter( pk=writing_tutorial_alone.pk).count(), 0) self.assertFalse(os.path.exists( writing_tutorial_alone.get_repo_path())) # check that bot haven't take the authorship of the tuto with more than one author self.assertEqual(writing_tutorial_2.authors.count(), 1) self.assertEqual( writing_tutorial_2.authors.filter( username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) # authorship for the article for which user was the only author self.assertEqual(published_article_alone.authors.count(), 1) self.assertEqual(published_article_alone.authors.first().username, settings.ZDS_APP["member"]["external_account"]) self.assertEqual(published_article_2.authors.count(), 1) self.assertEqual( PublishableContent.objects.filter( pk=writing_article_alone.pk).count(), 0) self.assertFalse(os.path.exists(writing_article_alone.get_repo_path())) # not bot if another author: self.assertEqual( published_article_2.authors.filter( username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) self.assertEqual(writing_article_2.authors.count(), 1) self.assertEqual( writing_article_2.authors.filter( username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) # topics, gallery and PMs: self.assertEqual( Topic.objects.filter(author__username=user.user.username).count(), 0) self.assertEqual( Post.objects.filter(author__username=user.user.username).count(), 0) self.assertEqual( Post.objects.filter(editor__username=user.user.username).count(), 0) self.assertEqual( PrivatePost.objects.filter( author__username=user.user.username).count(), 0) self.assertEqual( PrivateTopic.objects.filter( author__username=user.user.username).count(), 0) self.assertIsNotNone(Topic.objects.get(pk=authored_topic.pk)) self.assertIsNotNone(PrivateTopic.objects.get(pk=private_topic.pk)) self.assertIsNotNone(Gallery.objects.get(pk=alone_gallery.pk)) self.assertEquals(alone_gallery.get_linked_users().count(), 1) self.assertEquals(shared_gallery.get_linked_users().count(), 1) self.assertEquals( UserGallery.objects.filter(user=user.user).count(), 0) self.assertEquals( CommentVote.objects.filter(user=user.user, positive=True).count(), 0) self.assertEquals( Post.objects.filter(pk=upvoted_answer.id).first().like, 0) # zep 12, published contents and beta self.assertIsNotNone( PublishedContent.objects.filter( content__pk=published_tutorial_alone.pk).first()) self.assertIsNotNone( PublishedContent.objects.filter( content__pk=published_tutorial_2.pk).first()) self.assertTrue( Topic.objects.get(pk=beta_content.beta_topic.pk).is_locked) self.assertFalse( Topic.objects.get(pk=beta_content_2.beta_topic.pk).is_locked) # check API self.assertEqual(Application.objects.count(), 0) self.assertEqual(AccessToken.objects.count(), 0)
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.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) ) 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.assertTrue( self.client.login( username=self.profile2.user.username, password='******' ) ) 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 )
class PrivateTopicEditTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() 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) def test_denies_anonymous(self): self.client.logout() self.topic1.title = 'super title' self.topic1.subtitle = 'super subtitle' self.topic1.save() # get response = self.client.get(reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic']), follow=True) self.assertRedirects( response, reverse('member-login') + '?next=' + reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic'])) # post response = self.client.post(reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic']), { 'title': 'test', 'subtitle': 'subtest' }, follow=True) self.assertRedirects( response, reverse('member-login') + '?next=' + reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic'])) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual('super title', topic.title) self.assertEqual('super subtitle', topic.subtitle) def test_success_edit_topic(self): self.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) ) response = self.client.post(reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic']), { 'title': 'test', 'subtitle': 'subtest' }, follow=True) self.assertEqual(200, response.status_code) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual('test', topic.title) self.assertEqual('subtest', topic.subtitle) def test_fail_user_is_not_author(self): self.topic1.title = 'super title' self.topic1.subtitle = 'super subtitle' self.topic1.save() self.assertTrue( self.client.login( username=self.profile2.user.username, password='******' ) ) response = self.client.get(reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic']), follow=True) self.assertEqual(403, response.status_code) response = self.client.post(reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic']), { 'title': 'test', 'subtitle': 'subtest' }, follow=True) self.assertEqual(403, response.status_code) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual('super title', topic.title) self.assertEqual('super subtitle', topic.subtitle) def test_fail_topic_doesnt_exist(self): self.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) ) response = self.client.get(reverse('mp-edit-topic', args=[91, 'private-topic']), follow=True) self.assertEqual(404, response.status_code) response = self.client.post(reverse('mp-edit-topic', args=[91, 'private-topic']), { 'title': 'test', 'subtitle': 'subtest' }, follow=True) self.assertEqual(404, response.status_code) def test_fail_blank_title(self): self.topic1.title = 'super title' self.topic1.subtitle = 'super subtitle' self.topic1.save() self.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) ) response = self.client.post(reverse('mp-edit-topic', args=[self.topic1.pk, 'private-topic']), { 'title': '', 'subtitle': 'subtest' }, follow=True) self.assertEqual(200, response.status_code) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual('super title', topic.title) self.assertEqual('super subtitle', topic.subtitle)
def test_unregister(self): """Tests that unregistering user is working""" # test not logged user can't unregister self.client.logout() result = self.client.post(reverse('zds.member.views.unregister'), follow=False) self.assertEqual(result.status_code, 302) user = ProfileFactory() login_check = self.client.login(username=user.user.username, password='******') self.assertEqual(login_check, True) result = self.client.post(reverse('zds.member.views.unregister'), follow=False) self.assertEqual(result.status_code, 302) self.assertEqual( User.objects.filter(username=user.user.username).count(), 0) user = ProfileFactory() user2 = ProfileFactory() aloneGallery = GalleryFactory() UserGalleryFactory(gallery=aloneGallery, user=user.user) sharedGallery = GalleryFactory() UserGalleryFactory(gallery=sharedGallery, user=user.user) UserGalleryFactory(gallery=sharedGallery, user=user2.user) # first case : a published tutorial with only one author publishedTutorialAlone = MiniTutorialFactory(light=True) publishedTutorialAlone.authors.add(user.user) publishedTutorialAlone.save() # second case : a published tutorial with two authors publishedTutorial2 = MiniTutorialFactory(light=True) publishedTutorial2.authors.add(user.user) publishedTutorial2.authors.add(user2.user) publishedTutorial2.save() # third case : a private tutorial with only one author writingTutorialAlone = MiniTutorialFactory(light=True) writingTutorialAlone.authors.add(user.user) writingTutorialAlone.save() writingTutorialAloneGallerPath = writingTutorialAlone.gallery.get_gallery_path( ) writingTutorialAlonePath = writingTutorialAlone.get_path() # fourth case : a private tutorial with at least two authors writingTutorial2 = MiniTutorialFactory(light=True) writingTutorial2.authors.add(user.user) writingTutorial2.authors.add(user2.user) writingTutorial2.save() self.client.login(username=self.staff.username, password="******") pub = self.client.post(reverse('zds.tutorial.views.ask_validation'), { 'tutorial': publishedTutorialAlone.pk, 'text': u'Ce tuto est excellent', 'version': publishedTutorialAlone.sha_draft, 'source': 'http://zestedesavoir.com', }, follow=False) self.assertEqual(pub.status_code, 302) # reserve tutorial validation = Validation.objects.get( tutorial__pk=publishedTutorialAlone.pk) pub = self.client.post(reverse('zds.tutorial.views.reservation', args=[validation.pk]), follow=False) self.assertEqual(pub.status_code, 302) # publish tutorial pub = self.client.post(reverse('zds.tutorial.views.valid_tutorial'), { 'tutorial': publishedTutorialAlone.pk, 'text': u'Ce tuto est excellent', 'is_major': True, 'source': 'http://zestedesavoir.com', }, follow=False) pub = self.client.post(reverse('zds.tutorial.views.ask_validation'), { 'tutorial': publishedTutorial2.pk, 'text': u'Ce tuto est excellent', 'version': publishedTutorial2.sha_draft, 'source': 'http://zestedesavoir.com', }, follow=False) self.assertEqual(pub.status_code, 302) # reserve tutorial validation = Validation.objects.get(tutorial__pk=publishedTutorial2.pk) pub = self.client.post(reverse('zds.tutorial.views.reservation', args=[validation.pk]), follow=False) self.assertEqual(pub.status_code, 302) # publish tutorial pub = self.client.post(reverse('zds.tutorial.views.valid_tutorial'), { 'tutorial': publishedTutorial2.pk, 'text': u'Ce tuto est excellent', 'is_major': True, 'source': 'http://zestedesavoir.com', }, follow=False) # same thing for articles publishedArticleAlone = ArticleFactory() publishedArticleAlone.authors.add(user.user) publishedArticleAlone.save() publishedArticle2 = ArticleFactory() publishedArticle2.authors.add(user.user) publishedArticle2.authors.add(user2.user) publishedArticle2.save() writingArticleAlone = ArticleFactory() writingArticleAlone.authors.add(user.user) writingArticleAlone.save() writingArticle2 = ArticleFactory() writingArticle2.authors.add(user.user) writingArticle2.authors.add(user2.user) writingArticle2.save() # ask public article pub = self.client.post(reverse('zds.article.views.modify'), { 'article': publishedArticleAlone.pk, 'comment': u'Valides moi ce bébé', 'pending': 'Demander validation', 'version': publishedArticleAlone.sha_draft, 'is_major': True }, follow=False) self.assertEqual(pub.status_code, 302) login_check = self.client.login(username=self.staff.username, password='******') self.assertEqual(login_check, True) # reserve article validation = ArticleValidation.objects.get( article__pk=publishedArticleAlone.pk) pub = self.client.post(reverse('zds.article.views.reservation', args=[validation.pk]), follow=False) self.assertEqual(pub.status_code, 302) # publish article pub = self.client.post(reverse('zds.article.views.modify'), { 'article': publishedArticleAlone.pk, 'comment-v': u'Cet article est excellent', 'valid-article': 'Demander validation', 'is_major': True }, follow=False) self.assertEqual(pub.status_code, 302) # ask public article pub = self.client.post(reverse('zds.article.views.modify'), { 'article': publishedArticle2.pk, 'comment': u'Valides moi ce bébé', 'pending': 'Demander validation', 'version': publishedArticle2.sha_draft, 'is_major': True }, follow=False) self.assertEqual(pub.status_code, 302) login_check = self.client.login(username=self.staff.username, password='******') self.assertEqual(login_check, True) # reserve article validation = ArticleValidation.objects.get( article__pk=publishedArticle2.pk) pub = self.client.post(reverse('zds.article.views.reservation', args=[validation.pk]), follow=False) self.assertEqual(pub.status_code, 302) # publish article pub = self.client.post(reverse('zds.article.views.modify'), { 'article': publishedArticle2.pk, 'comment-v': u'Cet article est excellent', 'valid-article': 'Demander validation', 'is_major': True }, follow=False) self.assertEqual(pub.status_code, 302) # about posts and topics authoredTopic = TopicFactory(author=user.user, forum=self.forum11) answeredTopic = TopicFactory(author=user2.user, forum=self.forum11) PostFactory(topic=answeredTopic, author=user.user, position=2) editedAnswer = PostFactory(topic=answeredTopic, author=user.user, position=3) editedAnswer.editor = user.user editedAnswer.save() privateTopic = PrivateTopicFactory(author=user.user) privateTopic.participants.add(user2.user) privateTopic.save() PrivatePostFactory(author=user.user, privatetopic=privateTopic, position_in_topic=1) login_check = self.client.login(username=user.user.username, password='******') self.assertEqual(login_check, True) result = self.client.post(reverse('zds.member.views.unregister'), follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(publishedTutorialAlone.authors.count(), 1) self.assertEqual(publishedTutorialAlone.authors.first().username, settings.ZDS_APP["member"]["external_account"]) self.assertFalse(os.path.exists(writingTutorialAloneGallerPath)) self.assertEqual(publishedTutorial2.authors.count(), 1) self.assertEqual( publishedTutorial2.authors.filter( username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) self.assertIsNotNone(publishedTutorial2.get_prod_path()) self.assertTrue(os.path.exists(publishedTutorial2.get_prod_path())) self.assertIsNotNone(publishedTutorialAlone.get_prod_path()) self.assertTrue(os.path.exists(publishedTutorialAlone.get_prod_path())) self.assertEqual( self.client.get(reverse( 'zds.tutorial.views.view_tutorial_online', args=[publishedTutorialAlone.pk, publishedTutorialAlone.slug]), follow=False).status_code, 200) self.assertEqual( self.client.get(reverse( 'zds.tutorial.views.view_tutorial_online', args=[publishedTutorial2.pk, publishedTutorial2.slug]), follow=False).status_code, 200) self.assertTrue(os.path.exists(publishedArticleAlone.get_path())) self.assertEqual( self.client.get(reverse( 'zds.article.views.view_online', args=[publishedArticleAlone.pk, publishedArticleAlone.slug]), follow=True).status_code, 200) self.assertEqual( self.client.get(reverse( 'zds.article.views.view_online', args=[publishedArticle2.pk, publishedArticle2.slug]), follow=True).status_code, 200) self.assertEqual( Tutorial.objects.filter(pk=writingTutorialAlone.pk).count(), 0) self.assertEqual(writingTutorial2.authors.count(), 1) self.assertEqual( writingTutorial2.authors.filter(username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) self.assertEqual(publishedArticleAlone.authors.count(), 1) self.assertEqual(publishedArticleAlone.authors.first().username, settings.ZDS_APP["member"]["external_account"]) self.assertEqual(publishedArticle2.authors.count(), 1) self.assertEqual( publishedArticle2.authors.filter( username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) self.assertEqual( Article.objects.filter(pk=writingArticleAlone.pk).count(), 0) self.assertEqual(writingArticle2.authors.count(), 1) self.assertEqual( writingArticle2.authors.filter(username=settings.ZDS_APP["member"] ["external_account"]).count(), 0) self.assertEqual( Topic.objects.filter(author__username=user.user.username).count(), 0) self.assertEqual( Post.objects.filter(author__username=user.user.username).count(), 0) self.assertEqual( Post.objects.filter(editor__username=user.user.username).count(), 0) self.assertEqual( PrivatePost.objects.filter( author__username=user.user.username).count(), 0) self.assertEqual( PrivateTopic.objects.filter( author__username=user.user.username).count(), 0) self.assertFalse(os.path.exists(writingTutorialAlonePath)) self.assertIsNotNone(Topic.objects.get(pk=authoredTopic.pk)) self.assertIsNotNone(PrivateTopic.objects.get(pk=privateTopic.pk)) self.assertIsNotNone(Gallery.objects.get(pk=aloneGallery.pk)) self.assertEquals(aloneGallery.get_users().count(), 1) self.assertEquals(sharedGallery.get_users().count(), 1) self.assertEquals( UserGallery.objects.filter(user=user.user).count(), 0)
class PrivateTopicEditTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() 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) def test_denies_anonymous(self): self.client.logout() self.topic1.title = "super title" self.topic1.subtitle = "super subtitle" self.topic1.save() # get response = self.client.get(reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), follow=True) self.assertRedirects( response, reverse("member-login") + "?next=" + reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), ) # post response = self.client.post( reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), {"title": "test", "subtitle": "subtest"}, follow=True, ) self.assertRedirects( response, reverse("member-login") + "?next=" + reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), ) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual("super title", topic.title) self.assertEqual("super subtitle", topic.subtitle) def test_success_edit_topic(self): self.assertTrue(self.client.login(username=self.profile1.user.username, password="******")) response = self.client.post( reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), {"title": "test", "subtitle": "subtest"}, follow=True, ) self.assertEqual(200, response.status_code) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual("test", topic.title) self.assertEqual("subtest", topic.subtitle) def test_fail_user_is_not_author(self): self.topic1.title = "super title" self.topic1.subtitle = "super subtitle" self.topic1.save() self.assertTrue(self.client.login(username=self.profile2.user.username, password="******")) response = self.client.get(reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), follow=True) self.assertEqual(403, response.status_code) response = self.client.post( reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), {"title": "test", "subtitle": "subtest"}, follow=True, ) self.assertEqual(403, response.status_code) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual("super title", topic.title) self.assertEqual("super subtitle", topic.subtitle) def test_fail_topic_doesnt_exist(self): self.assertTrue(self.client.login(username=self.profile1.user.username, password="******")) response = self.client.get(reverse("mp-edit-topic", args=[91, "private-topic"]), follow=True) self.assertEqual(404, response.status_code) response = self.client.post( reverse("mp-edit-topic", args=[91, "private-topic"]), {"title": "test", "subtitle": "subtest"}, follow=True ) self.assertEqual(404, response.status_code) def test_fail_blank_title(self): self.topic1.title = "super title" self.topic1.subtitle = "super subtitle" self.topic1.save() self.assertTrue(self.client.login(username=self.profile1.user.username, password="******")) response = self.client.post( reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]), {"title": "", "subtitle": "subtest"}, follow=True, ) self.assertEqual(200, response.status_code) topic = PrivateTopic.objects.get(pk=self.topic1.pk) self.assertEqual("super title", topic.title) self.assertEqual("super subtitle", topic.subtitle)
class PrivateTopicDetailAPITest(APITestCase): def setUp(self): self.profile = ProfileFactory() self.private_topic = PrivateTopicFactory(author=self.profile.user) self.private_post = PrivatePostFactory(author=self.profile.user, privatetopic=self.private_topic, position_in_topic=1) self.client = APIClient() client_oauth2 = create_oauth2_client(self.profile.user) authenticate_client(self.client, client_oauth2, self.profile.user.username, 'hostel77') self.bot_group = Group() self.bot_group.name = ZDS_APP["member"]["bot_group"] self.bot_group.save() get_cache(extensions_api_settings.DEFAULT_USE_CACHE).clear() def test_detail_mp_with_client_unauthenticated(self): """ Gets details about a private topic with an unauthenticated client. """ client_unauthenticated = APIClient() response = client_unauthenticated.get( reverse('api-mp-detail', args=[self.private_topic.id])) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_detail_of_a_member(self): """ Gets all information about a private topic. """ response = self.client.get( reverse('api-mp-detail', args=[self.private_topic.id])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.private_topic.id, response.data.get('id')) self.assertEqual(self.private_topic.title, response.data.get('title')) self.assertEqual(self.private_topic.subtitle, response.data.get('subtitle')) self.assertIsNotNone(response.data.get('pubdate')) self.assertEqual(self.profile.user.id, response.data.get('author')) self.assertEqual(self.private_post.id, response.data.get('last_message')) self.assertEqual([], response.data.get('participants')) def test_detail_of_a_private_topic_not_present(self): """ Gets an error 404 when the private topic isn't present in the database. """ response = self.client.get(reverse('api-mp-detail', args=[42])) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_detail_of_private_topic_not_in_participants(self): """ Gets an error 403 when the member doesn't have permission to display details about the private topic. """ another_profile = ProfileFactory() another_private_topic = PrivateTopicFactory( author=another_profile.user) response = self.client.get( reverse('api-mp-detail', args=[another_private_topic.id])) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_update_mp_details_without_any_change(self): """ Updates a MP but without any changes. """ response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), {}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.private_topic.title, response.data.get('title')) self.assertEqual(self.private_topic.subtitle, response.data.get('subtitle')) self.assertEqual(self.private_topic.participants.count(), len(response.data.get('participants'))) def test_update_mp_title(self): """ Update title of a MP. """ data = {'title': 'Do you love Tacos?'} response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.get('title'), data.get('title')) def test_update_mp_subtitle(self): """ Update subtitle of a MP. """ data = {'subtitle': 'If you don\'t love Tacos, you are weird!'} response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.get('subtitle'), data.get('subtitle')) def test_update_mp_participants(self): """ Update participants of a MP. """ another_profile = ProfileFactory() data = {'participants': another_profile.user.id} response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.data.get('participants')[0], data.get('participants')) def test_update_mp_with_client_unauthenticated(self): """ Updates a private topic with an unauthenticated client must fail. """ client_unauthenticated = APIClient() response = client_unauthenticated.put( reverse('api-mp-detail', args=[self.private_topic.id]), {}) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_update_a_private_topic_not_present(self): """ Gets an error 404 when the private topic isn't present in the database. """ response = self.client.put(reverse('api-mp-detail', args=[42]), {}) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_update_private_topic_not_in_participants(self): """ Gets an error 403 when the member doesn't have permission to display details about the private topic. """ another_profile = ProfileFactory() another_private_topic = PrivateTopicFactory( author=another_profile.user) response = self.client.put( reverse('api-mp-detail', args=[another_private_topic.id]), {}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_update_private_topic_with_unreachable_user(self): """ Tries to update a private topic with an unreachable user. """ anonymous_user = UserFactory( username=ZDS_APP["member"]["anonymous_account"]) anonymous_user.groups.add(self.bot_group) anonymous_user.save() data = { 'participants': anonymous_user.id, } response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertNotEqual( response.data.get('participants')[0], data.get('participants')) def test_update_private_topic_with_user_not_author(self): """ Gets an error 403 when we try to update a private topic when we aren't the author. """ another_profile = ProfileFactory() self.private_topic.participants.add(another_profile.user) self.client = APIClient() client_oauth2 = create_oauth2_client(another_profile.user) authenticate_client(self.client, client_oauth2, another_profile.user.username, 'hostel77') data = { 'title': 'Good title', } response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_add_participant_with_an_user_not_author_of_private_topic(self): """ Gets an error 403 when we try to update participants of a private topic without to be the author but in participants. """ another_profile = ProfileFactory() third_profile = ProfileFactory() self.private_topic.participants.add(another_profile.user) self.client = APIClient() client_oauth2 = create_oauth2_client(another_profile.user) authenticate_client(self.client, client_oauth2, another_profile.user.username, 'hostel77') data = { 'participants': third_profile.user.id, } response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_delete_mp_with_client_unauthenticated(self): """ Leaves a private topic with an unauthenticated client must fail. """ client_unauthenticated = APIClient() response = client_unauthenticated.delete( reverse('api-mp-detail', args=[self.private_topic.id]), {}) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_fail_leave_topic_no_exist(self): """ Gets an error 404 when the private topic isn't present in the database. """ response = self.client.delete(reverse('api-mp-detail', args=[42]), {}) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_delete_private_topic_not_in_participants(self): """ Gets an error 403 when the member doesn't have permission to display details about the private topic. """ another_profile = ProfileFactory() another_private_topic = PrivateTopicFactory( author=another_profile.user) response = self.client.delete( reverse('api-mp-detail', args=[another_private_topic.id]), {}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_success_leave_topic_as_author_no_participants(self): """ Leaves a private topic when we are the last participant. """ self.private_topic.participants.clear() self.private_topic.save() response = self.client.delete( reverse('api-mp-detail', args=[self.private_topic.id])) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual( 0, PrivateTopic.objects.filter(pk=self.private_topic.id).count()) def test_success_leave_topic_as_author(self): """ Leaves a private topic when we are the author and check than a participant become the new author. """ another_profile = ProfileFactory() self.private_topic.participants.add(another_profile.user) response = self.client.delete( reverse('api-mp-detail', args=[self.private_topic.id])) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual( 1, PrivateTopic.objects.filter(pk=self.private_topic.id).count()) self.assertEqual( another_profile.user, PrivateTopic.objects.get(pk=self.private_topic.id).author) def test_success_leave_topic_as_participant(self): """ Leaves a private topic when we are just in participants. """ another_profile = ProfileFactory() another_private_topic = PrivateTopicFactory( author=another_profile.user) PrivatePostFactory(author=another_profile.user, privatetopic=another_private_topic, position_in_topic=1) another_private_topic.participants.add(self.profile.user) self.assertEqual( another_profile.user, PrivateTopic.objects.get(pk=another_private_topic.id).author) self.assertIn( self.profile.user, PrivateTopic.objects.get( pk=another_private_topic.id).participants.all()) response = self.client.delete( reverse('api-mp-detail', args=[another_private_topic.id])) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual( another_profile.user, PrivateTopic.objects.get(pk=another_private_topic.id).author) self.assertNotIn( self.profile.user, PrivateTopic.objects.get( pk=another_private_topic.id).participants.all())
class LeaveViewTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() 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.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) ) def test_denies_anonymous(self): self.client.logout() response = self.client.get(reverse('zds.mp.views.leave'), follow=True) self.assertRedirects( response, reverse('zds.member.views.login_view') + '?next=' + urllib.quote(reverse('zds.mp.views.leave'), '')) def test_fail_leave_topic_no_exist(self): response = self.client.post( reverse('zds.mp.views.leave'), { 'leave': '', 'topic_pk': '154' } ) self.assertEqual(404, response.status_code) def test_success_leave_topic_as_author_no_participants(self): self.topic1.participants.remove(self.profile2) self.topic1.save() response = self.client.post( reverse('zds.mp.views.leave'), { 'leave': '', 'topic_pk': self.topic1.pk }, follow=True ) self.assertEqual(200, response.status_code) self.assertEqual( 0, PrivateTopic.objects.all().count() ) def test_success_leave_topic_as_author(self): response = self.client.post( reverse('zds.mp.views.leave'), { 'leave': '', 'topic_pk': self.topic1.pk }, follow=True ) self.assertEqual(200, response.status_code) self.assertEqual( 1, PrivateTopic.objects.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.assertTrue( self.client.login( username=self.profile2.user.username, password='******' ) ) response = self.client.post( reverse('zds.mp.views.leave'), { 'leave': '', 'topic_pk': self.topic1.pk }, 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 )
class InterventionsTest(TestCase): """ This test uses quite complicated paths to check number of notifications: 1. Create private topics and do stuff with them 2. Log the user 3. Render the home page 4. Check the number of unread private messages on home page source code This because a correct test of this function requires a complete context (or it behave strangely) """ def setUp(self): self.author = ProfileFactory() self.user = ProfileFactory() self.topic = PrivateTopicFactory(author=self.author.user) self.topic.participants.add(self.user.user) self.post = PrivatePostFactory(privatetopic=self.topic, author=self.author.user, position_in_topic=1) # humane_delta test periods = ((1, 0), (2, 1), (3, 7), (4, 30), (5, 360)) cont = dict() cont['date_today'] = periods[0][0] cont['date_yesterday'] = periods[1][0] cont['date_last_week'] = periods[2][0] cont['date_last_month'] = periods[3][0] cont['date_last_year'] = periods[4][0] self.context = Context(cont) def test_interventions_privatetopics(self): self.assertTrue( self.client.login(username=self.author.user.username, password='******')) response = self.client.post(reverse('zds.pages.views.home')) self.assertEqual(200, response.status_code) self.assertContains(response, '<span class="notif-count">1</span>', html=True) self.client.logout() self.assertTrue( self.client.login(username=self.user.user.username, password='******')) response = self.client.post(reverse('zds.pages.views.home')) self.assertEqual(200, response.status_code) self.assertContains(response, '<span class="notif-count">1</span>', html=True) def test_interventions_privatetopics_author_leave(self): # profile1 (author) leave topic move = self.topic.participants.first() self.topic.author = move self.topic.participants.remove(move) self.topic.save() self.assertTrue( self.client.login(username=self.user.user.username, password='******')) response = self.client.post(reverse('zds.pages.views.home')) self.assertEqual(200, response.status_code) self.assertContains(response, '<span class="notif-count">1</span>', html=True) def test_interventions_humane_delta(self): tr = Template("{% load interventions %}" "{{ date_today|humane_delta }}").render(self.context) self.assertEqual(u"Aujourd'hui", tr) tr = Template("{% load interventions %}" "{{ date_yesterday|humane_delta }}").render(self.context) self.assertEqual(u"Hier", tr) tr = Template("{% load interventions %}" "{{ date_last_week|humane_delta }}").render(self.context) self.assertEqual(u"Les 7 derniers jours", tr) tr = Template("{% load interventions %}" "{{ date_last_month|humane_delta }}").render( self.context) self.assertEqual(u"Les 30 derniers jours", tr) tr = Template("{% load interventions %}" "{{ date_last_year|humane_delta }}").render(self.context) self.assertEqual(u"Plus ancien", tr)