diff --git a/commentaries/forms.py b/commentaries/forms.py index 22632aafe7c5a939b6704e2c6fb51af4cbac3e6a..77b60567194416ee5d2a612fc8acf7bec71f8c3a 100644 --- a/commentaries/forms.py +++ b/commentaries/forms.py @@ -71,7 +71,7 @@ class RequestCommentaryForm(forms.ModelForm): def save(self, *args, **kwargs): """Prefill instance before save""" - self.requested_by = Contributor.objects.get(user=self.user) + self.instance.requested_by = Contributor.objects.get(user=self.user) return super(RequestCommentaryForm, self).save(*args, **kwargs) def get_existing_commentary(self): diff --git a/commentaries/test_forms.py b/commentaries/test_forms.py index 4f203579623abbd09d6a8bec9a265b0b35fc6c9b..8a81d2abb6dd2c55da24c687e78ccde109544c71 100644 --- a/commentaries/test_forms.py +++ b/commentaries/test_forms.py @@ -1,11 +1,11 @@ from django.test import TestCase +from common.helpers import model_form_data from scipost.factories import UserFactory -from .models import Commentary from .factories import VettedCommentaryFactory, UnVettedCommentaryFactory from .forms import RequestCommentaryForm, VetCommentaryForm -from common.helpers import model_form_data +from .models import Commentary class TestVetCommentaryForm(TestCase): @@ -90,6 +90,10 @@ class TestRequestCommentaryForm(TestCase): form = RequestCommentaryForm(form_data, user=self.user) self.assertTrue(form.is_valid()) + # Check if the user is properly saved to the new Commentary as `requested_by` + commentary = form.save() + self.assertTrue(commentary.requested_by) + def test_valid_data_is_valid_for_DOI(self): """Test valid form for DOI""" form_data = self.valid_form_data diff --git a/commentaries/test_views.py b/commentaries/test_views.py index 682a28b4b229b6ac21ae42325e0619f65f303f57..9f0500b9a84fab5725b12339d92719a6cc3f2569 100644 --- a/commentaries/test_views.py +++ b/commentaries/test_views.py @@ -1,6 +1,12 @@ from django.core.urlresolvers import reverse +from django.contrib.auth.models import Group from django.test import TestCase +from scipost.factories import ContributorFactory + +from .factories import UnVettedCommentaryFactory, VettedCommentaryFactory +from .models import Commentary + class RequestCommentaryTest(TestCase): """Test cases for `request_commentary` view method""" @@ -27,3 +33,55 @@ class RequestCommentaryTest(TestCase): self.client.login(username="Test", password="testpw") request = self.client.post(self.view_url) self.assertEquals(request.status_code, 200) + + +class VetCommentaryRequestsTest(TestCase): + """Test cases for `vet_commentary_requests` view method""" + fixtures = ['groups', 'permissions'] + + def setUp(self): + self.view_url = reverse('commentaries:vet_commentary_requests') + self.login_url = reverse('scipost:login') + self.password = 'test123' + self.contributor = ContributorFactory(user__password=self.password) + + def set_required_permissions_and_login(self): + '''Set the required permissions to testuser to access vet_commentary_requests.''' + group = Group.objects.get(name="Vetting Editors") + self.contributor.user.groups.add(group) + self.client.login(username=self.contributor.user.username, password=self.password) + + def test_user_permissions(self): + """Test view permission is restricted to Vetting Editors.""" + # Anoymous user + response = self.client.get(self.view_url) + self.assertEquals(response.status_code, 403) + + # Wrong permissions group + self.client.login(username=self.contributor.user.username, password=self.password) + response = self.client.get(self.view_url) + self.assertEquals(response.status_code, 403) + + # Right permissions group + self.set_required_permissions_and_login() + response = self.client.get(self.view_url) + self.assertEquals(response.status_code, 200) + + def test_get_valid_unvetted_commentaries(self): + """Test if valid commentaries are sent back to user, if exists.""" + self.set_required_permissions_and_login() + + # No Commentary exists! + response = self.client.get(self.view_url) + self.assertTrue('commentary_to_vet' in response.context) + self.assertEquals(response.context['commentary_to_vet'], None) + + # Only vetted Commentaries exist! + VettedCommentaryFactory() + response = self.client.get(self.view_url) + self.assertEquals(response.context['commentary_to_vet'], None) + + # Unvetted Commentaries do exist! + UnVettedCommentaryFactory() + response = self.client.get(self.view_url) + self.assertTrue(type(response.context['commentary_to_vet']) is Commentary) diff --git a/scipost/factories.py b/scipost/factories.py index c69b975f60db3f7f1b5952012384a719ecc7c9bb..df335f90e80a06400c418c988d7a21b3a773d388 100644 --- a/scipost/factories.py +++ b/scipost/factories.py @@ -21,10 +21,11 @@ class UserFactory(factory.django.DjangoModelFactory): model = get_user_model() username = factory.Faker('user_name') - password = factory.Faker('password') + password = factory.PostGenerationMethodCall('set_password', 'adm1n') email = factory.Faker('safe_email') first_name = factory.Faker('first_name') last_name = factory.Faker('last_name') + is_active = True # When user object is created, associate new Contributor object to it. contributor = factory.RelatedFactory(ContributorFactory, 'user')