SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 2fd40d4a authored by Geert Kapteijns's avatar Geert Kapteijns
Browse files

Add two tests for thesis vetting.

Theses that are accepted are vetted by the correct contributor.
Mails are not yet sent. Thesis is not yet deleted when refused.
parent 18ec0f36
No related branches found
No related tags found
No related merge requests found
......@@ -27,4 +27,5 @@ def model_form_data(model, form_class):
def filter_keys(dictionary, keys_to_keep):
return {key: dictionary[key] for key in keys_to_keep}
# Field is empty if not on model.
return {key: dictionary.get(key, "") for key in keys_to_keep}
......@@ -18,7 +18,7 @@ class ThesisLinkFactory(factory.django.DjangoModelFactory):
author = factory.Faker('name')
supervisor = factory.Faker('name')
institution = factory.Faker('company')
defense_date = factory.Faker('date_time_this_century')
defense_date = factory.Faker('date')
abstract = factory.Faker('text')
domain = 'ET'
......
......@@ -16,7 +16,7 @@ class RequestThesisLinkForm(forms.ModelForm):
}
class VetThesisLinkForm(forms.Form):
class VetThesisLinkForm(RequestThesisLinkForm):
MODIFY = 0
ACCEPT = 1
REFUSE = 2
......@@ -41,16 +41,53 @@ class VetThesisLinkForm(forms.Form):
justification = forms.CharField(widget=forms.Textarea(
attrs={'rows': 5, 'cols': 40}), label='Justification (optional)', required=False)
def vet_request(self, thesis_link_id):
if self.cleaned_data['action_option'] == VetThesisLinkForm.ACCEPT:
def vet_request(self, thesislink, user):
if int(self.cleaned_data['action_option']) == VetThesisLinkForm.ACCEPT:
thesislink.vetted = True
thesislink.vetted_by = Contributor.objects.get(user=request.user)
thesislink.vetted_by = Contributor.objects.get(user=user)
thesislink.save()
elif self.cleaned_data['action_option'] == VetThesisLinkForm.MODIFY:
elif int(self.cleaned_data['action_option']) == VetThesisLinkForm.MODIFY:
print('hai')
elif self.cleaned_data['action_option'] == VetThesisLinkForm.REFUSE:
print('bla')
elif int(self.cleaned_data['action_option']) == VetThesisLinkForm.REFUSE:
print('hoi')
# class VetThesisLinkForm(forms.Form):
# MODIFY = 0
# ACCEPT = 1
# REFUSE = 2
# THESIS_ACTION_CHOICES = (
# (MODIFY, 'modify'),
# (ACCEPT, 'accept'),
# (REFUSE, 'refuse (give reason below)'),
# )
#
# EMPTY_CHOICE = 0
# ALREADY_EXISTS = 1
# LINK_DOES_NOT_WORK = 2
# THESIS_REFUSAL_CHOICES = (
# (EMPTY_CHOICE, '---'),
# (ALREADY_EXISTS, 'a link to this thesis already exists'),
# (LINK_DOES_NOT_WORK, 'the external link to this thesis does not work'),
# )
#
# action_option = forms.ChoiceField(
# widget=forms.RadioSelect, choices=THESIS_ACTION_CHOICES, required=True, label='Action')
# refusal_reason = forms.ChoiceField(choices=THESIS_REFUSAL_CHOICES, required=False)
# justification = forms.CharField(widget=forms.Textarea(
# attrs={'rows': 5, 'cols': 40}), label='Justification (optional)', required=False)
#
# def vet_request(self, thesis_link_id):
# if self.cleaned_data['action_option'] == VetThesisLinkForm.ACCEPT:
#
# thesislink.vetted = True
# thesislink.vetted_by = Contributor.objects.get(user=request.user)
# thesislink.save()
# elif self.cleaned_data['action_option'] == VetThesisLinkForm.MODIFY:
# print('hai')
# elif self.cleaned_data['action_option'] == VetThesisLinkForm.REFUSE:
# print('bla')
class ThesisLinkSearchForm(forms.Form):
......
......@@ -11,9 +11,10 @@ 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, VetThesisLinkRequests, thesis_detail
from .views import RequestThesisLink, VetThesisLink, thesis_detail
from .factories import ThesisLinkFactory, VetThesisLinkFormFactory
from .models import ThesisLink
from .forms import VetThesisLinkForm
from common.helpers import model_form_data
......@@ -71,7 +72,8 @@ class TestVetThesisLinkRequests(TestCase):
def setUp(self):
self.client = Client()
self.target = reverse('theses:vet_thesislink_requests')
self.thesislink = ThesisLinkFactory()
self.target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id})
def test_response_when_not_logged_in(self):
response = self.client.get(self.target)
......@@ -82,30 +84,46 @@ class TestVetThesisLinkRequests(TestCase):
A Contributor needs to be in the Vetting Editors group to be able to
vet submitted thesis links.
'''
# Create ThesisLink to vet.
ThesisLinkFactory()
request = RequestFactory().get(self.target)
user = UserFactory()
request.user = user
self.assertRaises(
PermissionDenied, VetThesisLinkRequests.as_view(), request)
PermissionDenied, VetThesisLink.as_view(), request, pk=self.thesislink.id)
def test_response_vetting_editor(self):
# Create ThesisLink to vet.
ThesisLinkFactory()
request = RequestFactory().get(self.target)
user = UserFactory()
user.groups.add(Group.objects.get(name="Vetting Editors"))
request.user = user
response = VetThesisLinkRequests.as_view()(request)
response = VetThesisLink.as_view()(request, pk=self.thesislink.id)
self.assertEqual(response.status_code, 200)
def test_thesislink_is_vetted_by_correct_contributor(self):
thesis_link = ThesisLinkFactory()
contributor = ContributorFactory()
contributor.user.groups.add(Group.objects.get(name="Vetting Editors"))
post_data = VetThesisLinkFormFactory().data
target = reverse('theses:vet_thesislink_request', kwargs={'thesislink_id': thesis_link.id})
post_data = model_form_data(ThesisLinkFactory(), VetThesisLinkForm)
post_data["action_option"] = VetThesisLinkForm.ACCEPT
target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id})
request = RequestFactory().post(target, post_data)
request.user = contributor.user
# I don't know what the following three lines do, but they help make a RequestFactory
# work with the messages middleware
setattr(request, 'session', 'session')
messages = FallbackStorage(request)
setattr(request, '_messages', messages)
response = VetThesisLink.as_view()(request, pk=self.thesislink.id)
self.thesislink.refresh_from_db()
self.assertEqual(self.thesislink.vetted_by, contributor)
def test_thesislink_that_is_refused_is_not_vetted(self):
contributor = ContributorFactory()
contributor.user.groups.add(Group.objects.get(name="Vetting Editors"))
post_data = model_form_data(ThesisLinkFactory(), VetThesisLinkForm)
post_data["action_option"] = VetThesisLinkForm.REFUSE
target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id})
request = RequestFactory().post(target, post_data)
request.user = contributor.user
......@@ -116,5 +134,6 @@ class TestVetThesisLinkRequests(TestCase):
messages = FallbackStorage(request)
setattr(request, '_messages', messages)
response = VetThesisLinkRequests.as_view()(request, thesislink_id=thesis_link.id)
self.assertEqual(thesis_link.vetted_by, contributor)
response = VetThesisLink.as_view()(request, pk=self.thesislink.id)
self.thesislink.refresh_from_db()
self.assertEqual(self.thesislink.vetted_by, None)
......@@ -51,45 +51,40 @@ class UnvettedThesisLinks(ListView):
context_object_name = 'thesislinks'
queryset = ThesisLink.objects.filter(vetted=False)
@method_decorator(permission_required(
'scipost.can_vet_thesislink_requests', raise_exception=True), name='dispatch')
class VetThesisLink(UpdateView):
model = ThesisLink
fields = ['type', 'discipline', 'domain', 'subject_area',
'title', 'author', 'supervisor', 'institution',
'defense_date', 'pub_link', 'abstract']
template_name = "theses/vet_thesislink.html"
# @method_decorator(permission_required(
# 'scipost.can_vet_thesislink_requests', raise_exception=True), name='dispatch')
# class VetThesisLink(UpdateView):
# form_class = VetThesisLinkForm
# model = ThesisLink
# fields = ['type', 'discipline', 'domain', 'subject_area',
# 'title', 'author', 'supervisor', 'institution',
# 'defense_date', 'pub_link', 'abstract']
# template_name = "theses/vet_thesislink.html"
# success_url = reverse_lazy('theses:unvetted_thesislinks')
#
# def form_valid(self, form):
@method_decorator(permission_required(
'scipost.can_vet_thesislink_requests', raise_exception=True), name='dispatch')
class VetThesisLink(UpdateView):
model = ThesisLink
form_class = VetThesisLinkForm
template_name = "theses/vet_thesislink.html"
success_url = reverse_lazy('theses:unvetted_thesislinks')
def form_valid(self, form):
# I totally override the form_valid method. I do not call super.
# This is because, by default, an UpdateView saves the object as instance,
# which it builds from the form data. So, the changes (by whom the thesis link was vetted, etc.)
# would be lost.
# Builds model that reflects changes made during update. Does not yet save.
self.object = form.save(commit=False)
# Process vetting actions
form.vet_request(self.object, self.request.user)
# Save again.
self.object.save()
# @method_decorator(permission_required(
# 'scipost.can_vet_thesislink_requests', raise_exception=True), name='dispatch')
# class VetThesisLinkRequests(FormView):
# form_class = VetThesisLinkForm
# template_name = 'theses/vet_thesislink_requests.html'
# success_url = reverse_lazy('theses:vet_thesislink_requests')
#
# def get_context_data(self, **kwargs):
# context = super(VetThesisLinkRequests, self).get_context_data(**kwargs)
# context['thesislink_to_vet'] = ThesisLink.objects.filter(vetted=False).first()
# return context
#
# def form_valid(self, form):
# form.vet_request(self.kwargs['thesislink_id'])
# messages.add_message(self.request, messages.SUCCESS,
# strings.acknowledge_vet_thesis_link)
# return super(VetThesisLinkRequests, self).form_valid(form)
messages.add_message(self.request, messages.SUCCESS,
strings.acknowledge_vet_thesis_link)
return HttpResponseRedirect(self.get_success_url())
@permission_required('scipost.can_vet_thesislink_requests', raise_exception=True)
......
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