Skip to content
Snippets Groups Projects
Commit 37d1e7c9 authored by Todd Dembrey's avatar Todd Dembrey
Browse files

Add some basic tests to cover the review pages

parent 70d2bb78
No related branches found
No related tags found
No related merge requests found
import factory
from opentech.apply.funds.tests.factories import ApplicationSubmissionFactory
from opentech.apply.users.tests.factories import StaffFactory
from ..models import Review
from ..views import get_form_for_stage
class ReviewDataFactory(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 ReviewFactory(factory.DjangoModelFactory):
class Meta:
model = Review
submission = factory.SubFactory(ApplicationSubmissionFactory)
author = factory.SubFactory(StaffFactory)
review = factory.Dict({'submission': factory.SelfAttribute('..submission')}, dict_factory=ReviewDataFactory)
from django.test import TestCase, RequestFactory
from django.urls import reverse
from opentech.apply.users.tests.factories import StaffFactory
from .factories import ReviewFactory
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='review'):
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='review'):
return self.client.get(self.url(instance, view_name), secure=True, follow=True)
def post_page(self, instance, data, view_name='review'):
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 StaffReviewsTestCase(BaseTestCase):
user_factory = StaffFactory
url_name = 'funds:submissions:reviews:{}'
def get_kwargs(self, instance):
return {'pk': instance.id, 'submission_pk': instance.submission.id}
def test_can_access_review(self):
submission = ApplicationSubmissionFactory()
review = ReviewFactory(submission=submission, author=self.user)
response = self.get_page(review)
self.assertContains(response, review.submission.title)
self.assertContains(response, self.user.full_name)
def test_cant_access_other_review(self):
submission = ApplicationSubmissionFactory()
review = ReviewFactory(submission=submission)
response = self.get_page(review)
self.assertEqual(response.status_code, 403)
class StaffReviewListingTestCase(BaseTestCase):
user_factory = StaffFactory
url_name = 'funds:submissions:reviews:{}'
def get_kwargs(self, instance):
return {'submission_pk': instance.id}
def test_can_access_review_listing(self):
submission = ApplicationSubmissionFactory()
reviews = ReviewFactory.create_batch(3, submission=submission)
response = self.get_page(submission, 'list')
self.assertContains(response, submission.title)
for review in reviews:
self.assertContains(response, review.author.full_name)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment