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)
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)
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())
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)
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)
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)
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)
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')
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')
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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'] )
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.')
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)
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)
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)
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'])
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)
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)
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)
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'])
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())