Source code for theses.tests.test_views

__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"


import re

from django.core import mail
from django.core.exceptions import PermissionDenied
from django.test import TestCase, RequestFactory
from django.test.client import Client
from django.contrib.auth.models import Group
from django.urls import reverse, reverse_lazy
from django.contrib.messages.storage.fallback import FallbackStorage

from scipost.factories import UserFactory, ContributorFactory
from comments.factories import CommentFactory
from comments.forms import CommentForm
from comments.models import Comment
from ..views import RequestThesisLink, VetThesisLink, thesis_detail
from ..factories import ThesisLinkFactory, ThesisLinkFactory, VetThesisLinkFormFactory
from ..models import ThesisLink
from ..forms import VetThesisLinkForm
from common.helpers import model_form_data
from common.helpers.test import add_groups_and_permissions

[docs]class TestThesisDetail(TestCase):
[docs] def setUp(self): add_groups_and_permissions()
[docs] 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)
[docs]class TestVetThesisLinkRequests(TestCase):
[docs] def setUp(self): add_groups_and_permissions() ContributorFactory.create_batch(5) self.client = Client() self.thesislink = ThesisLinkFactory() self.target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id})
[docs] def test_response_when_not_logged_in(self): response = self.client.get(self.target) self.assertEqual(response.status_code, 403)
[docs] def test_response_regular_contributor(self): ''' A Contributor needs to be in the Vetting Editors group to be able to vet submitted thesis links. ''' request = RequestFactory().get(self.target) user = UserFactory() request.user = user self.assertRaises( PermissionDenied, VetThesisLink.as_view(), request, pk=self.thesislink.id)
[docs] def test_response_vetting_editor(self): request = RequestFactory().get(self.target) user = UserFactory() user.groups.add(Group.objects.get(name="Vetting Editors")) request.user = user response = VetThesisLink.as_view()(request, pk=self.thesislink.id) self.assertEqual(response.status_code, 200)
[docs]class TestTheses(TestCase):
[docs] def setUp(self): add_groups_and_permissions() self.client = Client() self.target = reverse('theses:theses')
[docs] def test_empty_search_query(self): thesislink = ThesisLinkFactory() response = self.client.get(self.target) search_results = response.context["object_list"] self.assertTrue(thesislink in search_results)
[docs] def test_search_query_on_author(self): thesislink = ThesisLinkFactory() other_thesislink = ThesisLinkFactory() form_data = {'author': thesislink.author} response = self.client.get(self.target, form_data) search_results = response.context['object_list'] self.assertTrue(thesislink in search_results) self.assertTrue(other_thesislink not in search_results) self.assertEqual(len(search_results), 1)