From aa1518dc041099c903d8fc3c018214818b972006 Mon Sep 17 00:00:00 2001 From: Dan Braghis <dan.braghis@torchbox.com> Date: Tue, 19 Jun 2018 13:12:12 +0100 Subject: [PATCH] Add basic tests --- .../apply/determinations/tests/factories.py | 39 +++++++ .../apply/determinations/tests/test_views.py | 105 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 opentech/apply/determinations/tests/factories.py create mode 100644 opentech/apply/determinations/tests/test_views.py diff --git a/opentech/apply/determinations/tests/factories.py b/opentech/apply/determinations/tests/factories.py new file mode 100644 index 000000000..5a3c5f9d4 --- /dev/null +++ b/opentech/apply/determinations/tests/factories.py @@ -0,0 +1,39 @@ +import factory + +from opentech.apply.funds.tests.factories import ApplicationSubmissionFactory + +from ..models import Determination, APPROVED, UNDETERMINED, UNAPPROVED +from ..views import get_form_for_stage + + +class DeterminationDataFactory(factory.DictFactory): + @classmethod + def _build(cls, model_class, *args, **kwargs): + submission = kwargs.pop('submission') + form = get_form_for_stage(submission)(request=None, submission=None) + form_fields = {} + for field_name, field in form.fields.items(): + form_fields[field_name] = 0 + + form_fields.update(**kwargs) + return super()._build(model_class, *args, **form_fields) + + +class DeterminationFactory(factory.DjangoModelFactory): + class Meta: + model = Determination + + class Params: + submitted = factory.Trait(determination=APPROVED, is_draft=False) + approved = factory.Trait(determination=APPROVED) + rejected = factory.Trait(determination=UNAPPROVED) + not_draft = factory.Trait(is_draft=False) + + submission = factory.SubFactory(ApplicationSubmissionFactory) + author = factory.SelfAttribute('submission.lead') + + determination = UNDETERMINED + determination_message = factory.Faker('sentence') + determination_data = factory.Dict({'submission': factory.SelfAttribute('..submission')}, dict_factory=DeterminationDataFactory) + + is_draft = True diff --git a/opentech/apply/determinations/tests/test_views.py b/opentech/apply/determinations/tests/test_views.py new file mode 100644 index 000000000..7c88c1079 --- /dev/null +++ b/opentech/apply/determinations/tests/test_views.py @@ -0,0 +1,105 @@ +from django.test import TestCase, RequestFactory +from django.urls import reverse + +from opentech.apply.users.tests.factories import StaffFactory, UserFactory +from .factories import DeterminationFactory +from opentech.apply.funds.tests.factories import ApplicationSubmissionFactory + + +class BaseTestCase(TestCase): + url_name = '' + user_factory = None + + def setUp(self): + self.factory = RequestFactory() + self.user = self.user_factory() + self.client.force_login(self.user) + + def url(self, instance, view_name='detail'): + full_url_name = self.url_name.format(view_name) + url = reverse(full_url_name, kwargs=self.get_kwargs(instance)) + request = self.factory.get(url, secure=True) + return request.build_absolute_uri() + + def get_page(self, instance, view_name='detail'): + return self.client.get(self.url(instance, view_name), secure=True, follow=True) + + def post_page(self, instance, data, view_name='detail'): + return self.client.post(self.url(instance, view_name), data, secure=True, follow=True) + + def refresh(self, instance): + return instance.__class__.objects.get(id=instance.id) + + +class StaffDeterminationsTestCase(BaseTestCase): + user_factory = StaffFactory + url_name = 'funds:submissions:determinations:{}' + + def get_kwargs(self, instance): + return {'pk': instance.id, 'submission_pk': instance.submission.id} + + def test_can_access_determination(self): + submission = ApplicationSubmissionFactory(status='in_discussion') + determination = DeterminationFactory(submission=submission, author=self.user, not_draft=True) + response = self.get_page(determination) + self.assertContains(response, determination.submission.title) + self.assertContains(response, self.user.full_name) + self.assertContains(response, reverse('funds:submissions:detail', kwargs={'pk': submission.id})) + + +class DeterminationFormTestCase(BaseTestCase): + user_factory = StaffFactory + url_name = 'funds:submissions:determinations:{}' + + def get_kwargs(self, instance): + return {'submission_pk': instance.id} + + def test_can_access_form_if_lead(self): + submission = ApplicationSubmissionFactory(status='in_discussion', lead=self.user) + response = self.get_page(submission, 'form') + self.assertContains(response, submission.title) + self.assertContains(response, reverse('funds:submissions:detail', kwargs={'pk': submission.id})) + + def test_cannot_access_form_if_not_lead(self): + submission = ApplicationSubmissionFactory(status='in_discussion') + response = self.get_page(submission, 'form') + self.assertContains(response, submission.title) + self.assertContains(response, reverse('funds:submissions:detail', kwargs={'pk': submission.id})) + + def test_cant_access_wrong_status(self): + submission = ApplicationSubmissionFactory() + response = self.get_page(submission, 'form') + self.assertEqual(response.status_code, 403) + + def test_cant_resubmit_determination(self): + submission = ApplicationSubmissionFactory(status='in_discussion', lead=self.user) + determination = DeterminationFactory(submission=submission, author=self.user, submitted=True) + response = self.post_page(submission, {'data': 'value', 'determination': determination.determination}, 'form') + self.assertTrue(response.context['has_determination_response']) + self.assertContains(response, 'You have already added a determination for this submission') + + def test_can_edit_draft_determination(self): + submission = ApplicationSubmissionFactory(status='in_discussion', lead=self.user) + determination = DeterminationFactory(submission=submission, author=self.user) + response = self.post_page(submission, {'data': 'value', 'determination': determination.determination}, 'form') + self.assertFalse(response.context['has_determination_response']) + self.assertEqual(response.context['title'], 'Update Determination draft') + + def test_cannot_edit_draft_determination_if_not_lead(self): + submission = ApplicationSubmissionFactory(status='in_discussion') + determination = DeterminationFactory(submission=submission, author=self.user) + response = self.post_page(submission, {'data': 'value', 'determination': determination.determination}, 'form') + self.assertEqual(response.status_code, 403) + + +class UserDeterminationFormTestCase(BaseTestCase): + user_factory = UserFactory + url_name = 'funds:submissions:determinations:{}' + + def get_kwargs(self, instance): + return {'submission_pk': instance.id} + + def test_cant_access_form(self): + submission = ApplicationSubmissionFactory(status='in_discussion') + response = self.get_page(submission, 'form') + self.assertEqual(response.status_code, 403) -- GitLab