From c3088ebffbb250362873f7ebc738c816cc63d6e7 Mon Sep 17 00:00:00 2001 From: Geert Kapteijns <ghkapteijns@gmail.com> Date: Sun, 23 Apr 2017 16:15:52 +0200 Subject: [PATCH] RequestCommentary: move validation of doi string to form. Write tests. --- commentaries/forms.py | 11 ++++++++++- commentaries/test_forms.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/commentaries/forms.py b/commentaries/forms.py index 8d95c58d8..b74c0f00a 100644 --- a/commentaries/forms.py +++ b/commentaries/forms.py @@ -2,6 +2,7 @@ import re from django import forms from django.shortcuts import get_object_or_404 +from django.urls import reverse from .models import Commentary @@ -9,9 +10,17 @@ from scipost.models import Contributor class DOIToQueryForm(forms.Form): - doi = forms.CharField(widget=forms.TextInput( + VALID_DOI_REGEXP = r'^(?i)10.\d{4,9}/[-._;()/:A-Z0-9]+$' + doi = forms.RegexField(regex=VALID_DOI_REGEXP, strip=False, widget=forms.TextInput( {'label': 'DOI', 'placeholder': 'ex.: 10.21468/00.000.000000'})) + def clean_doi(self): + input_doi = self.cleaned_data['doi'] + if Commentary.objects.filter(pub_DOI=input_doi).exists(): + error_message = 'There already exists a Commentary Page on this publication.' + raise forms.ValidationError(error_message) + return input_doi + class IdentifierToQueryForm(forms.Form): identifier = forms.CharField(widget=forms.TextInput( diff --git a/commentaries/test_forms.py b/commentaries/test_forms.py index 8733064ae..8566a4c96 100644 --- a/commentaries/test_forms.py +++ b/commentaries/test_forms.py @@ -1,13 +1,39 @@ +import re + from django.test import TestCase from common.helpers import model_form_data from scipost.factories import UserFactory from .factories import VettedCommentaryFactory, UnvettedCommentaryFactory -from .forms import RequestCommentaryForm, VetCommentaryForm +from .forms import RequestCommentaryForm, VetCommentaryForm, DOIToQueryForm from .models import Commentary +from common.helpers import model_form_data from common.helpers.test import add_groups_and_permissions +class TestDOIToQueryForm(TestCase): + def setUp(self): + add_groups_and_permissions() + doi_string = "10.21468/SciPostPhys.2.2.010" + self.valid_form_data = {'doi': doi_string} + + def test_valid_doi_is_valid(self): + form = DOIToQueryForm(self.valid_form_data) + self.assertTrue(form.is_valid()) + + def test_invalid_doi_is_invalid(self): + invalid_data = {'doi': 'blablab'} + form = DOIToQueryForm(invalid_data) + self.assertFalse(form.is_valid()) + + def test_doi_that_already_has_commentary_page_is_invalid(self): + unvetted_commentary = UnvettedCommentaryFactory() + invalid_data = {'doi': unvetted_commentary.pub_DOI} + form = DOIToQueryForm(invalid_data) + self.assertFalse(form.is_valid()) + error_message = form.errors['doi'][0] + self.assertRegexpMatches(error_message, re.compile('already exist')) + class TestVetCommentaryForm(TestCase): def setUp(self): @@ -20,6 +46,7 @@ class TestVetCommentaryForm(TestCase): 'email_response_field': 'Lorem Ipsum' } + def test_valid_accepted_form(self): """Test valid form data and return Commentary""" form = VetCommentaryForm(self.form_data, commentary_id=self.commentary.id, user=self.user) -- GitLab