def test_reply_form_unauthenticated(self): """ Test presence of reply form for unauthenticated users. If a user isn't logged in, there should be no reply form. """ thread = create_thread() url = thread_detail_url(thread=thread) response = self.client.get(url) self.assertEqual(200, response.status_code) self.assertTrue('reply_form' not in response.context)
def test_with_message(self): """ Test the view when the given thread has a message. If the thread has a message, it should be displayed following the thread's title. """ thread = create_thread() message = create_message(user=self.user, thread=thread) url = thread_detail_url(thread=thread) response = self.client.get(url) self.assertEqual(200, response.status_code) self.assertContains(response, message.body)
def test_reply_form(self): """ Test presence of a reply form. If a user is logged in, there should be a reply form. """ self.login() thread = create_thread() url = thread_detail_url(thread=thread) response = self.client.get(url) self.assertEqual(200, response.status_code) self.assertFalse(response.context['reply_form'].is_bound)
def test_reply_unauthenticated(self): """ Test replying while unauthenticated. If an unauthenticated user tries to reply to a thread, an error should be shown. """ thread = create_thread() data = { 'body': 'Test body text.', } url = thread_detail_url(thread=thread) response = self.client.post(url, data) self.assertEqual(403, response.status_code) self.assertEqual(0, models.Message.objects.count())
def post(self, request, *args, **kwargs): """ Create a new thread notification """ self.request = request follow = self.request.POST.get('follow', None) pk = kwargs.get('pk') thread = get_object_or_404(forum_models.Thread, pk=pk) if follow: self._follow_thread(request.user, thread) else: self._unfollow_thread(request.user, thread) redirect_url = thread_detail_url(thread=thread) return HttpResponseRedirect(redirect_url)
def test_reply_empty(self): """ Test submitting an empty reply form. If an empty reply form is submitted, the user should be redirected back to the reply form, and an error should be displayed on the form. Regression test for #23 """ self.login() thread = create_thread() url = thread_detail_url(thread=thread) response = self.client.post(url, {}) self.assertEqual(200, response.status_code) self.assertEqual(thread, response.context['thread'])
def post(self, request, *args, **kwargs): """ Create a new reply to the current thread """ if not request.user.is_authenticated(): raise PermissionDenied() self.object = self.get_object() form = forms.ThreadReplyForm(request.POST) if form.is_valid(): form.save(request.user, self.object) return HttpResponseRedirect(thread_detail_url(thread=self.object)) context = self.get_context_data() context['reply_form'] = form return render(request, self.get_template_names(), context)
def test_thread(self): """ Test view when there is a thread. If there is a thread, then the view should show the thread's name. """ thread = create_thread(topic=self.topic) url = thread_list_url(topic=self.topic) response = self.client.get(url) detail_url = thread_detail_url(thread=thread) href_text = 'href="%s"' % detail_url self.assertEqual(200, response.status_code) self.assertQuerysetEqual( response.context['thread_list'], ['<Thread: %s>' % thread.title]) self.assertContains(response, thread.title) self.assertContains(response, href_text)
def test_no_messages(self): """ Test the view when the given thread has no messages. If the thread has no messages, the response should contain a note to the user that there are no messages for the current thread. In practice, this should never occur because when threads are created, there should always be an initial message. """ thread = create_thread() url = thread_detail_url(thread=thread) response = self.client.get(url) no_replies_message = "There are no replies to this thread" self.assertEqual(200, response.status_code) self.assertEqual(thread, response.context['thread']) self.assertContains(response, no_replies_message)
def test_delete(self): """ Test unfollowing a thread. If the 'follow' variable is false, the ThreadNotification instance for the current thread and user should be deleted. """ self.login() thread = create_thread() create_thread_notification( user=self.user, thread=thread) data = {} success_url = thread_detail_url(thread=thread) url = reverse('simple-forums:follow-thread', kwargs={'pk': thread.pk}) response = self.client.post(url, data) self.assertRedirects(response, success_url) self.assertEqual(0, models.ThreadNotification.objects.count())
def test_post_valid_follow(self): """ Test POSTing valid data. If a POST request with valid data is submitted, a new ThreadNotification instance should be created. """ self.login() thread = create_thread() data = {'follow': 'on'} success_url = thread_detail_url(thread=thread) url = reverse('simple-forums:follow-thread', kwargs={'pk': thread.pk}) response = self.client.post(url, data) self.assertRedirects(response, success_url) self.assertEqual(1, models.ThreadNotification.objects.count()) self.assertEqual( self.user, models.ThreadNotification.objects.get().user) self.assertEqual( thread, models.ThreadNotification.objects.get().thread)
def test_reply_errors(self): """ Test submitting an invalid reply form. If an invalid reply is submitted, the reply form should be displayed with errors. """ self.login() thread = create_thread() url = thread_detail_url(thread=thread) response = self.client.post(url, {}) expected_errors = { 'body': ['This field is required.'], } self.assertEqual(200, response.status_code) self.assertTrue(response.context['reply_form'].is_bound) self.assertEqual( expected_errors, response.context['reply_form'].errors)
def test_reply(self): """ Test submitting a valid reply. If a valid form is submitted, a new message should be created on the current thread. """ self.login() thread = create_thread() data = { 'body': 'Test body text.', } url = thread_detail_url(thread=thread) response = self.client.post(url, data) message = thread.message_set.get() self.assertRedirects(response, message.get_absolute_url()) self.assertEqual(1, models.Message.objects.count()) self.assertEqual(self.user, message.user) self.assertEqual(data['body'], message.body)
def test_duplicate_request(self): """ Test trying to create a duplicate notification instance. If a user already has notifications set up for a thread and they try to create another notification instance, nothing should happen. """ self.login() thread = create_thread() create_thread_notification( user=self.user, thread=thread) data = {'follow': 'on'} success_url = thread_detail_url(thread=thread) url = reverse('simple-forums:follow-thread', kwargs={'pk': thread.pk}) response = self.client.post(url, data) self.assertRedirects(response, success_url) self.assertEqual(1, models.ThreadNotification.objects.count())
def test_valid_form(self): """ Test authenticated user submitting a valid form. If an authenticated user submits a valid form, then a new thread should be created. """ self.login() topic = create_topic() data = { 'topic': '%d' % topic.pk, 'title': 'Test Thread Title', 'body': 'Test thread body', } response = self.client.post(self.URL, data) thread = models.Thread.objects.get() message = thread.message_set.get() self.assertRedirects(response, thread_detail_url(thread=thread)) self.assertEqual(data['title'], thread.title) self.assertEqual(data['body'], message.body)
def form_valid(self, form): """ Save form if it is valid """ thread = form.save(self.request.user) return HttpResponseRedirect(thread_detail_url(thread=thread))