From 15a1b23aa41d1e65eef07a04a71c9db4cf013a33 Mon Sep 17 00:00:00 2001 From: Geert Kapteijns <ghkapteijns@gmail.com> Date: Sun, 29 Jan 2017 14:30:47 +0100 Subject: [PATCH] Add Comment Factory, write view test for submitting comment on ThesisLink I keep get weird bugs. Maybe merging in development will help. --- comments/factories.py | 14 ++++++++++++++ theses/factories.py | 1 - theses/test_views.py | 20 +++++++++++++++++++- theses/views.py | 4 ++-- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 comments/factories.py diff --git a/comments/factories.py b/comments/factories.py new file mode 100644 index 000000000..aaf4cc969 --- /dev/null +++ b/comments/factories.py @@ -0,0 +1,14 @@ +import factory + +from django.utils import timezone + +from scipost.factories import ContributorFactory +from .models import Comment + + +class CommentFactory(factory.django.DjangoModelFactory): + class Meta: + model = Comment + + comment_text = factory.Faker('text') + date_submitted = timezone.now() diff --git a/theses/factories.py b/theses/factories.py index bc57863b3..dd00356df 100644 --- a/theses/factories.py +++ b/theses/factories.py @@ -28,4 +28,3 @@ class VetThesisLinkFormFactory(FormFactory): model = VetThesisLinkForm action_option = VetThesisLinkForm.ACCEPT - # justification = factory.Faker('lorem') diff --git a/theses/test_views.py b/theses/test_views.py index f87971e80..e0ffeeb35 100644 --- a/theses/test_views.py +++ b/theses/test_views.py @@ -7,22 +7,40 @@ from django.contrib.auth.models import Group from django.urls import reverse, reverse_lazy from django.contrib.messages.storage.fallback import FallbackStorage -from .views import RequestThesisLink, VetThesisLinkRequests from scipost.factories import UserFactory, ContributorFactory +from comments.factories import CommentFactory +from comments.forms import CommentForm +from .views import RequestThesisLink, VetThesisLinkRequests, thesis_detail from .factories import ThesisLinkFactory, VetThesisLinkFormFactory from .models import ThesisLink +from common.helpers import model_form_data class TestThesisDetail(TestCase): fixtures = ['groups', 'permissions'] def test_visits_valid_thesis_detail(self): + """ A visitor does not have to be logged in to view a thesis link. """ thesis_link = ThesisLinkFactory() client = Client() target = reverse('theses:thesis', kwargs={'thesislink_id': thesis_link.id}) response = client.post(target) self.assertEqual(response.status_code, 200) + def test_submitting_comment_creates_comment(self): + """ Valid Comment gets saved """ + contributor = ContributorFactory() + thesislink = ThesisLinkFactory() + valid_comment_data = model_form_data(CommentFactory(), CommentForm) + target = reverse('theses:thesis', kwargs={'thesislink_id': thesislink.id}) + + request = RequestFactory().post(target, valid_comment_data) + request.user = contributor.user + + response = thesis_detail(request, thesislink_id=thesislink.id) + print(response) + self.assertTrue(False) + class TestRequestThesisLink(TestCase): fixtures = ['groups', 'permissions'] diff --git a/theses/views.py b/theses/views.py index 80ecb6952..f5646202c 100644 --- a/theses/views.py +++ b/theses/views.py @@ -215,13 +215,13 @@ def thesis_detail(request, thesislink_id): new_comment.save() author.nr_comments = Comment.objects.filter(author=author).count() author.save() - request.session['thesislink_id'] = thesislink_id + # request.session['thesislink_id'] = thesislink_id context = {'ack_header': 'Thank you for contributing a Comment.', 'ack_message': 'It will soon be vetted by an Editor.', 'followup_message': 'Back to the ', 'followup_link': reverse( 'theses:thesis', - kwargs={'thesislink_id': newcomment.thesislink.id} + kwargs={'thesislink_id': new_comment.thesislink.id} ), 'followup_link_label': ' Thesis Link page you came from' } -- GitLab