def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact='A hat', defaults={'name': 'A hat'}) self.profile1.hats.add(self.hat) 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 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 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 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_get_last_answer(self): topic = PrivateTopicFactory(author=self.user2) PrivatePostFactory(privatetopic=topic, author=self.user2, position_in_topic=1) self.assertEqual(self.post2, self.topic1.get_last_answer()) self.assertNotEqual(self.post1, self.topic1.get_last_answer()) self.assertIsNone(topic.get_last_answer())
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_get_last_answer(self): topic = PrivateTopicFactory(author=self.profile2.user) PrivatePostFactory( privatetopic=topic, author=self.profile2.user, position_in_topic=1) self.assertEqual(self.post2, self.topic1.get_last_answer()) self.assertNotEqual(self.post1, self.topic1.get_last_answer()) self.assertIsNone(topic.get_last_answer())
def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread 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_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 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_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())
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 setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact='A hat', defaults={'name': 'A hat'}) self.profile1.hats.add(self.hat) 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 create_multiple_private_topics_for_member( self, user, number_of_users=settings.REST_FRAMEWORK['PAGINATE_BY']): return [ PrivateTopicFactory(author=user) for private_topic in xrange(0, number_of_users) ]
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_fail_cite_post_not_in_current_topic(self): another_topic = PrivateTopicFactory(author=self.profile2.user) another_post = PrivatePostFactory(privatetopic=another_topic, author=self.profile2.user, position_in_topic=1) response = self.client.get( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug()]) + "?cite={}".format(another_post.pk) ) self.assertEqual(403, response.status_code)
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_detail_private_post_with_wrong_identifiers(self): """ Tries to get details of a private post in a wrong private topic. """ another_private_topic = PrivateTopicFactory(author=self.profile.user) response = self.client.get( reverse('api-mp-message-detail', args=[another_private_topic.id, self.private_post.id])) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
class FunctionTest(TestCase): def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread 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_never_privateread(self): self.assertTrue(is_privatetopic_unread(self.topic1, self.profile1.user)) mark_read(self.topic1, self.profile1.user) self.assertFalse(is_privatetopic_unread(self.topic1, self.profile1.user)) def test_mark_read(self): self.assertTrue(self.topic1.is_unread(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, self.profile1.user) self.assertFalse(self.topic1.is_unread(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertTrue(self.topic1.is_unread(self.profile1.user))
class FunctionTest(TestCase): def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread 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_never_privateread(self): self.assertTrue(never_privateread(self.topic1, self.profile1.user)) mark_read(self.topic1, self.profile1.user) self.assertFalse(never_privateread(self.topic1, self.profile1.user)) def test_mark_read(self): self.assertTrue(self.topic1.never_read(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, self.profile1.user) self.assertFalse(self.topic1.never_read(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertTrue(self.topic1.never_read(self.profile1.user))
class FunctionTest(TestCase): def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread 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_never_privateread(self): self.assertTrue(is_privatetopic_unread(self.topic1, self.profile1.user)) mark_read(self.topic1, self.profile1.user) self.assertFalse( is_privatetopic_unread(self.topic1, self.profile1.user)) @patch("zds.mp.signals.topic_read") def test_mark_read(self, topic_read): self.assertTrue(self.topic1.is_unread(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, self.profile1.user) self.assertFalse(self.topic1.is_unread(self.profile1.user)) self.assertEqual(topic_read.send.call_count, 1) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertTrue(self.topic1.is_unread(self.profile1.user))
def test_success_delete_topic_no_participants(self): topic = PrivateTopicFactory(author=self.profile1.user) self.client.force_login(self.profile1.user) self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count()) response = self.client.post(reverse("mp-list-delete"), {"items": [topic.pk]}) self.assertEqual(302, response.status_code) self.assertEqual(0, PrivateTopic.objects.filter(pk=topic.pk).count())
def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.user1 = ProfileFactory().user self.user2 = ProfileFactory().user self.outsider = ProfileFactory().user # Create the bot accound and add it to the bot group self.bot = ProfileFactory().user bot_group = Group(name=settings.ZDS_APP["member"]["bot_group"]) bot_group.save() self.bot.groups.add(bot_group) self.topic1 = PrivateTopicFactory(author=self.user1) self.topic1.participants.add(self.user2) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.user1, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.user2, position_in_topic=2)
def setUp(self): self.profile = ProfileFactory() self.client = APIClient() client_oauth2 = create_oauth2_client(self.profile.user) authenticate_client(self.client, client_oauth2, self.profile.user.username, 'hostel77') self.private_topic = PrivateTopicFactory(author=self.profile.user) get_cache(extensions_api_settings.DEFAULT_USE_CACHE).clear()
def test_fail_delete_topic_not_belong_to_user(self): topic = PrivateTopicFactory(author=self.profile1.user) self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count()) self.client.force_login(self.profile2.user) self.client.post(reverse("mp-list-delete"), {"items": [topic.pk]}) self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count())
def test_success_delete_topic_no_participants(self): topic = PrivateTopicFactory(author=self.profile1.user) login_check = self.client.login(username=self.profile1.user.username, password="******") self.assertTrue(login_check) self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count()) response = self.client.post(reverse("mp-list-delete"), {"items": [topic.pk]}) self.assertEqual(302, response.status_code) self.assertEqual(0, PrivateTopic.objects.filter(pk=topic.pk).count())
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 setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact="A hat", defaults={"name": "A hat"}) self.profile1.hats.add(self.hat) 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_fail_delete_topic_not_belong_to_user(self): topic = PrivateTopicFactory(author=self.profile1.user) self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count()) login_check = self.client.login(username=self.profile2.user.username, password="******") self.assertTrue(login_check) self.client.post(reverse("mp-list-delete"), {"items": [topic.pk]}) self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count())
def test_create_post_not_in_participants(self): """ Creates a post in a topic with no authorized permission (the user is not in the allowed participants) """ other_profile = ProfileFactory() another_topic = PrivateTopicFactory(author=other_profile.user) data = {'text': 'Welcome to this private post!'} response = self.client.post( reverse('api-mp-message-list', args=[another_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_fail_topic_no_permission(self): topic = PrivateTopicFactory(author=self.profile1.user) login_check = self.client.login( username=self.profile2.user.username, password='******' ) self.assertTrue(login_check) response = self.client.get(reverse('private-posts-list', args=[topic.pk, topic.slug]), follow=True) self.assertEqual(403, response.status_code)
def test_unicode_subtitle_answer(self): """To test unicode subtitle.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, subtitle="Subtitle with accent àéè") unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory(privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse("private-posts-new", args=[unicode_topic.pk, unicode_topic.slug]), {"text": "answer", "last_post": unicode_post.pk}, follow=True, ) self.assertEqual(response.status_code, 200)
def test_topic_get_page_too_far(self): """ get a page that is too far yet""" login_check = self.client.login(username=self.profile1.user.username, password="******") self.assertTrue(login_check) # create many subjects (at least two pages) for i in range(1, settings.ZDS_APP["forum"]["topics_per_page"] + 5): topic = PrivateTopicFactory(author=self.profile1.user) topic.participants.add(self.profile2.user) PrivatePostFactory(privatetopic=topic, author=self.profile1.user, position_in_topic=1) response = self.client.get(reverse("mp-list") + "?page=42") self.assertEqual(response.status_code, 404)
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_list_of_private_posts_with_x_data_format_markdown(self): """ Gets list of private posts with a Markdown value for X-Data-Format header. """ private_topic = PrivateTopicFactory(author=self.profile.user) self.create_multiple_private_posts_for_member(self.profile.user, private_topic, 1) response = self.client.get( reverse('api-mp-message-list', args=[private_topic.id]), **{'HTTP_X_DATA_FORMAT': 'Markdown'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIsNotNone(response.data.get('results')[0].get('text')) self.assertIsNone(response.data.get('results')[0].get('text_html'))
def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread 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)
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 AnswerViewTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact='A hat', defaults={'name': 'A hat'}) self.profile1.hats.add(self.hat) 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('private-posts-new', args=[1, 'private-topic']), follow=True) self.assertRedirects( response, reverse('member-login') + '?next=' + reverse('private-posts-new', args=[1, 'private-topic'])) def test_fail_answer_not_send_topic_pk(self): response = self.client.post(reverse('private-posts-new', args=[999, 'private-topic'])) self.assertEqual(404, response.status_code) def test_fail_answer_topic_no_exist(self): response = self.client.post(reverse('private-posts-new', args=[156, 'private-topic'])) self.assertEqual(404, response.status_code) def test_fail_cite_post_no_exist(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]) + '&cite=4864') self.assertEqual(404, response.status_code) def test_fail_cite_post_not_in_current_topic(self): another_topic = PrivateTopicFactory(author=self.profile2.user) another_post = PrivatePostFactory( privatetopic=another_topic, author=self.profile2.user, position_in_topic=1) response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite={}'.format(another_post.pk)) self.assertEqual(403, response.status_code) def test_fail_cite_weird_pk(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite=abcd') self.assertEqual(404, response.status_code) def test_success_cite_post(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite={}'.format(self.post2.pk)) self.assertEqual(200, response.status_code) def test_success_preview_answer(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'answer', 'preview': '', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(200, response.status_code) def test_success_answer(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'Bonjour Luc', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(200, response.status_code) self.assertEqual(3, PrivatePost.objects.all().count()) self.assertContains(response, 'Bonjour Luc') def test_answer_with_hat(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'Luc !?', 'last_post': self.topic1.last_message.pk, 'with_hat': self.hat.pk, }, follow=False ) self.assertEqual(302, response.status_code) self.assertEqual(PrivatePost.objects.latest('pubdate').hat, self.hat) def test_fail_answer_with_no_right(self): self.client.logout() self.assertTrue( self.client.login( username=self.profile3.user.username, password='******' ) ) response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'answer', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(403, response.status_code) self.assertEqual(2, PrivatePost.objects.all().count()) def test_unicode_title_answer(self): """To test unicode title.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, title='Title with accent àéè') unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory( privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse('private-posts-new', args=[unicode_topic.pk, unicode_topic.slug]), { 'text': 'answer', 'last_post': unicode_post.pk }, follow=True ) self.assertEqual(response.status_code, 200) def test_unicode_subtitle_answer(self): """To test unicode subtitle.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, subtitle='Subtitle with accent àéè') unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory( privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse('private-posts-new', args=[unicode_topic.pk, unicode_topic.slug]), { 'text': 'answer', 'last_post': unicode_post.pk }, follow=True ) self.assertEqual(response.status_code, 200)
class PrivateTopicTest(TestCase): def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread 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_absolute_url(self): url = reverse('private-posts-list', args=[self.topic1.pk, self.topic1.slug()]) self.assertEqual(self.topic1.get_absolute_url(), url) def test_post_count(self): self.assertEqual(2, self.topic1.get_post_count()) def test_get_last_answer(self): topic = PrivateTopicFactory(author=self.profile2.user) PrivatePostFactory( privatetopic=topic, author=self.profile2.user, position_in_topic=1) self.assertEqual(self.post2, self.topic1.get_last_answer()) self.assertNotEqual(self.post1, self.topic1.get_last_answer()) self.assertIsNone(topic.get_last_answer()) def test_first_post(self): topic = PrivateTopicFactory(author=self.profile2.user) self.assertEqual(self.post1, self.topic1.first_post()) self.assertIsNone(topic.first_post()) def test_last_read_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual( self.post1, self.topic1.last_read_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, user=self.profile1.user) self.assertEqual( self.post2, self.topic1.last_read_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertEqual( self.post2, self.topic1.last_read_post(self.profile1.user)) def test_first_unread_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual( self.post1, self.topic1.first_unread_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread mark_read(self.topic1, self.profile1.user) post3 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertEqual( post3, self.topic1.first_unread_post(self.profile1.user)) def test_alone(self): topic2 = PrivateTopicFactory(author=self.profile1.user) self.assertFalse(self.topic1.alone()) self.assertTrue(topic2.alone()) def test_never_read(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertTrue(self.topic1.is_unread(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, self.profile1.user) self.assertFalse(self.topic1.is_unread(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertTrue(self.topic1.is_unread(self.profile1.user)) def test_topic_never_read_get_last_read(self): """ Trying to read last message of a never read Private Topic Should return the first message of the Topic """ tester = ProfileFactory() self.topic1.participants.add(tester.user) self.assertEqual(self.topic1.last_read_post(user=tester.user), self.post1)
def test_alone(self): topic2 = PrivateTopicFactory(author=self.profile1.user) self.assertFalse(self.topic1.alone()) self.assertTrue(topic2.alone())
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 )
def test_first_post(self): topic = PrivateTopicFactory(author=self.profile2.user) self.assertEqual(self.post1, self.topic1.first_post()) self.assertIsNone(topic.first_post())