Esempio n. 1
0
 def test_invalid_data_cannot_be_posted(self):
     login(self, self.user, 'password')
     data = {'message': ''}
     response = self.client.post(self.create_url, data)
     self.assertEqual(response.status_code, 200)
     form = response.context.get('form')
     self.assertTrue(form.errors)
Esempio n. 2
0
 def test_submit_success_for_authenticated_user(self):
     current_count = self.thread.followers.count()
     second_user = self.make_user('testuser2')
     login(self, second_user, 'password')
     response = self.client.post(self.follow_url)
     self.assertEqual(response.status_code, 302)
     self.assertEqual(self.thread.followers.count(), current_count + 1)
Esempio n. 3
0
 def test_follow(self):
     login(self, self.user2.username, 'password')
     response = self.client.post(self.url1)
     self.assertEqual(response.status_code, 302)
     self.assertIn(self.user2, self.user1.followers.all())
     self.assertIn(self.user1, self.user2.following.all())
     self.assertNotIn(self.user1, self.user2.followers.all())
Esempio n. 4
0
 def test_view_should_not_render_hidden_thread_for_regular_user(self):
     login(self, self.user, 'password')
     thread = make_threads(visible=False)
     update_url = reverse('thread_detail',
                          kwargs={'thread_slug': thread.slug})
     response = self.client.get(update_url)
     self.assertEqual(response.status_code, 404)
Esempio n. 5
0
 def test_empty_data_rejection(self):
     login(self, self.user, 'password')
     data = {}
     response = self.client.post(self.update_url, data)
     self.assertEqual(response.status_code, 200)
     form = response.context.get('form')
     self.assertTrue(form.errors)
Esempio n. 6
0
 def test_view_should_not_allow_post_for_hidden_comment(self):
     login(self, self.user, 'password')
     response = self.client.post(self.update_url, self.data)
     self.assertEqual(response.status_code, 404)
     prev_msg = self.comment.message
     self.comment.refresh_from_db()
     self.assertEqual(self.comment.message, prev_msg)
Esempio n. 7
0
 def test_view_should_prevent_moderators_from_posting(self):
     current_count = Comment.objects.count()
     login(self, self.user, 'password')
     make_moderator(self.user, self.category)
     response = self.client.post(self.create_url, self.data)
     self.assertEqual(response.status_code, 403)
     self.assertEqual(Comment.objects.count(), current_count)
Esempio n. 8
0
 def test_valid_data_acceptance(self):
     login(self, self.user, 'password')
     data = {'message': 'hello world changed'}
     response = self.client.post(self.update_url, data)
     self.assertEqual(response.status_code, 302)
     self.comment.refresh_from_db()
     self.assertEqual(self.comment.message, 'hello world changed')
Esempio n. 9
0
 def setUp(self):
     super().setUp()
     self.create_url = reverse(
         'comments:comment_create', kwargs={'thread_slug': self.thread.slug}
     )
     self.commenter = self.make_user('testuser2')
     login(self, self.commenter, 'password')
Esempio n. 10
0
 def test_view_submit_success_for_authenticated_user(self):
     current_count = Comment.objects.count()
     login(self, self.user, 'password')
     data = {'message': 'hello word'}
     response = self.client.post(self.create_url, data)
     self.assertEqual(response.status_code, 302)
     self.assertEqual(Comment.objects.count(), current_count + 1)
Esempio n. 11
0
 def test_view_render_for_authenticated_user(self):
     """
     Logged in can access the comment create form directly
     """
     login(self, self.user, 'password')
     response = self.client.get(self.create_url)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.context['form'], CommentForm)
Esempio n. 12
0
 def test_no_notification_after_like_for_owner(self):
     login(self, self.user, 'password')
     response = self.client.post(self.like_url)
     notif_qs = Notification.objects.filter(
         sender=self.user, receiver=self.user,
         comment=self.comment, notif_type=Notification.COMMENT_LIKED
     )
     self.assertEqual(notif_qs.count(), 0)
Esempio n. 13
0
 def test_view_should_prevent_moderators_from_posting(self):
     make_moderator(self.user, self.category)
     login(self, self.user, 'password')
     response = self.client.post(self.update_url, self.data)
     self.assertEqual(response.status_code, 403)
     prev_msg = self.comment.message
     self.comment.refresh_from_db()
     self.assertEqual(self.comment.message, prev_msg)
Esempio n. 14
0
 def test_with_existing_liker(self):
     """An existing liker is removed from likers count"""
     second_user = self.make_user('testuser2')
     login(self, second_user, 'password')
     self.client.post(self.like_url)
     response = self.client.post(self.like_url)
     self.assertEqual(response.status_code, 302)
     self.assertEqual(self.comment.likers.count(), 0)
Esempio n. 15
0
 def test_authenticated_user_with_no_permission(self):
     """
     Only comment owner can see the comment edit form and update comment
     """
     second_user = self.make_user('testuser2')
     login(self, second_user, 'password')
     response = self.client.get(self.notif_url)
     self.assertEqual(response.status_code, 403)
Esempio n. 16
0
 def test_view_should_not_render_hidden_thread_for_regular_user(self):
     """
     A comment form cannot be displayed for regular users when thread
     is hidden
     """
     login(self, self.user, 'password')
     response = self.client.get(self.create_url)
     self.assertEqual(response.status_code, 404)    
Esempio n. 17
0
 def test_authenticated_user_with_permission(self):
     login(self, self.user, 'password')
     response = self.client.get(self.notif_url)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.context['userprofile'], self.user)
     self.assertEqual(
         response.context['current_profile_page'], 'user_notifs')
     self.assertIsInstance(response.context['notifications'], Page)
Esempio n. 18
0
 def test_render_for_authenticated_user_with_permission(self):
     login(self, self.user.username, 'password')
     response = self.client.get(self.user_edit_url)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.context['userprofile'], self.user)
     self.assertEqual(
         response.context['current_profile_page'], 'settings')
     self.assertIsInstance(response.context['form'], UserProfileForm)
Esempio n. 19
0
 def test_view_should_render_hidden_thread_for_moderator(self):
     """
     A comment form cannot be displayed for regular users when thread
     is hidden
     """
     make_moderator(self.user, self.category)
     login(self, self.user, 'password')
     response = self.client.get(self.update_url)
     self.assertEqual(response.status_code, 200)
Esempio n. 20
0
 def test_render_for_authenticated_user(self):
     """Logged in users can see the reply form"""
     login(self, self.user, 'password')
     response = self.client.get(self.reply_url)
     self.assertEqual(response.status_code, 200)
     self.assertIsInstance(response.context['form'], CommentForm)
     self.assertEqual(
         self.bbcode_message, response.context['form'].initial['message']
     )
Esempio n. 21
0
 def test_with_empty_data(self):
     login(self, self.user, 'password')
     data = {}
     response = self.client.post(self.upload_url,
                                 data,
                                 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
     self.assertEqual(response.status_code, 200)
     self.assertFalse(response.json()['is_valid'])
     self.assertEqual(response.json()['message'], 'This field is required.')
Esempio n. 22
0
    def test_view_should_not_render_hidden_comment_for_regular_user(self):
        comment = make_comment(self.user, self.thread)
        hidden_comment = make_comment(self.user, self.thread, visible=False)
        response = self.client.get(f"{self.detail_url}")
        self.assertEqual(len(response.context['comments']), 1)

        login(self, self.user, 'password')
        response = self.client.get(f"{self.detail_url}")
        self.assertEqual(len(response.context['comments']), 1)
Esempio n. 23
0
 def test_post_with_authenticated_user_with_ajax(self):
     current_count = Attachment.objects.count()
     login(self, self.user, 'password')
     data = {'image': self.test_image}
     response = self.client.post(self.upload_url,
                                 data,
                                 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(Attachment.objects.count(), current_count + 1)
Esempio n. 24
0
 def test_404_for_production(self):
     current_count = Attachment.objects.count()
     login(self, self.user, 'password')
     data = {'image': self.test_image}
     with self.settings(DEBUG=False):
         response = self.client.post(self.upload_url,
                                     data,
                                     HTTP_X_REQUESTED_WITH='XMLHttpRequest')
     self.assertEqual(response.status_code, 404)
     self.assertEqual(Attachment.objects.count(), current_count)
Esempio n. 25
0
 def test_large_image(self):
     login(self, self.user, 'password')
     self.valid_data.update({'image': self.test_avatar_large})
     data = self.valid_data
     with self.settings(DEBUG=True):
         response = self.client.post(self.user_edit_url, data)
     self.assertEqual(response.status_code, 200)
     message = 'File too large. Size should not exceed 500 KB.'
     form = response.context.get('form')
     self.assertIn(message, form.errors['image'])
Esempio n. 26
0
 def test_invalid_data_rejection(self):
     login(self, self.user, 'password')
     data = {
         'category': 'Choose category',
         'title': '',
         'message': '',
     }
     response = self.client.post(self.update_url, data)
     self.assertEqual(response.status_code, 200)
     form = response.context.get('form')
     self.assertTrue(form.errors)
Esempio n. 27
0
 def test_submit_success_for_authenticated_user(self):
     current_count = Comment.objects.count()
     second_user = self.make_user('testuser2')
     login(self, second_user, 'password')
     reply_message = 'reply to hello world'
     message = self.bbcode_message + reply_message
     data = {'message': message}
     response = self.client.post(self.reply_url, data)
     self.assertEqual(response.status_code, 302)
     self.assertEqual(Comment.objects.count(), current_count + 1)
     self.assertEqual(Comment.objects.last().parent, self.comment)
Esempio n. 28
0
    def test_view_render_for_authenticated_user(self):
        login(self, self.user, 'password')

        response = self.client.get(self.create_url)
        self.assertEqual(response.status_code, 200)

        response2 = self.client.get(self.create_url2)
        self.assertEqual(response2.status_code, 200)

        self.assertEqual(response.context['form'], ThreadForm)
        self.assertIsInstance(response2.context['form'], ThreadForm)
Esempio n. 29
0
 def test_valid_data_acceptance(self):
     login(self, self.user, 'password')
     with self.settings(DEBUG=True):
         response = self.client.post(self.user_edit_url, self.valid_data)
     self.assertEqual(response.status_code, 302)
     self.user.refresh_from_db()
     self.assertIsNotNone(self.user.avatar_url)
     self.assertEqual(self.user.gender, self.valid_data['gender'])
     self.assertEqual(self.user.signature, self.valid_data['signature'])
     self.assertEqual(self.user.location, self.valid_data['location'])
     self.assertEqual(self.user.website, self.valid_data['website'])
Esempio n. 30
0
    def test_follow_each_other(self):
        login(self, self.user2.username, 'password')
        response = self.client.post(self.url1)
        self.assertEqual(response.status_code, 302)
        self.assertIn(self.user2, self.user1.followers.all())
        self.client.logout()

        login(self, self.user1.username, 'password')
        response = self.client.post(self.url2)
        self.assertEqual(response.status_code, 302)
        self.assertIn(self.user1, self.user2.followers.all())