diff --git a/.gitignore b/.gitignore index 0eeb06ba57fd256d5cd7eab1ba083b5be4f0cc83..63c279548e44fe2f5fc42adada3d549cd9f72055 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ __pycache__ *~ +.DS_STORE + # Package managers /venv* /node_modules/ diff --git a/README.md b/README.md index da09e066c26eea7bc0ad388464ef197014d84910..0a4608961069118a9a1404f4966efe48fdb0e225 100644 --- a/README.md +++ b/README.md @@ -158,8 +158,16 @@ To build the documentation, run: After this, generated documentation should be available in `docs/_build/html`. ## Writing tests -It is recommended, when writing tests, to use the `ContributorFactory` located in `scipost.factories`. This will automatically generate a related user with Registered Contributor membership. You may probably need to use the fixture list `["permissions", "groups"]` in your tests make sure the permissions groups are working properly. -It is recommended, when writing tests for new models, to make use of ModelFactories instead of fixtures to prevent issues with altering fields in the model later on. +It is recommended, when writing tests, to use the `ContributorFactory` located in `scipost.factories`. This will +automatically generate a related user with Registered Contributor membership. Using the `Contributor` model in tests +requires loading the permissions and groups. Previously, this was done by including `fixtures = ["permissions", +"groups"]` at the top of the `TestCase`, but since these fixtures behave unpredictable and are a nuisance to keep up to +date with the actual groups and permissions, it is much better to call `add_groups_and_permissions`, located in +`common.helpers.test`, in a function named `setUp`, which runs before each test. `add_groups_and_permissions` wraps the +management command of the same name. + +It is recommended, when writing tests for new models, to make use of `ModelFactory` instead of fixtures +for the same reason. A basic example of a test might look like: ```shell @@ -167,12 +175,12 @@ from django.contrib.auth.models import Group from django.test import TestCase from scipost.factories import ContributorFactory +from common.helpers.test import add_groups_and_permissions class VetCommentaryRequestsTest(TestCase): - fixtures = ['groups', 'permissions'] - def setUp(self): + add_groups_and_permissions() self.contributor = ContributorFactory(user__password='test123') # The default password is `adm1n` def test_example_test(self): diff --git a/commentaries/test_forms.py b/commentaries/test_forms.py index cec31d3071f2944cc61a8c0cebb785adb186f27f..8733064ae2267d07111ed7c9fe0c1662cc85fb3f 100644 --- a/commentaries/test_forms.py +++ b/commentaries/test_forms.py @@ -6,12 +6,12 @@ from scipost.factories import UserFactory from .factories import VettedCommentaryFactory, UnvettedCommentaryFactory from .forms import RequestCommentaryForm, VetCommentaryForm from .models import Commentary +from common.helpers.test import add_groups_and_permissions class TestVetCommentaryForm(TestCase): - fixtures = ['permissions', 'groups'] - def setUp(self): + add_groups_and_permissions() self.commentary = UnvettedCommentaryFactory.create() self.user = UserFactory() self.form_data = { @@ -71,9 +71,8 @@ class TestVetCommentaryForm(TestCase): class TestRequestCommentaryForm(TestCase): - fixtures = ['permissions', 'groups'] - def setUp(self): + add_groups_and_permissions() factory_instance = VettedCommentaryFactory.build() self.user = UserFactory() self.valid_form_data = model_form_data(factory_instance, RequestCommentaryForm) diff --git a/commentaries/test_views.py b/commentaries/test_views.py index 50d6850fd46c2b7166370a078492a227e04dcda5..0451056d9fe81bd1f747c9a73b704ed851b6214f 100644 --- a/commentaries/test_views.py +++ b/commentaries/test_views.py @@ -7,11 +7,13 @@ from scipost.factories import ContributorFactory from .factories import UnvettedCommentaryFactory, VettedCommentaryFactory, UnpublishedVettedCommentaryFactory from .forms import CommentarySearchForm from .models import Commentary +from common.helpers.test import add_groups_and_permissions class RequestCommentaryTest(TestCase): """Test cases for `request_commentary` view method""" - fixtures = ['permissions', 'groups', 'contributors'] + def setUp(self): + add_groups_and_permissions() def setUp(self): self.view_url = reverse('commentaries:request_commentary') @@ -38,9 +40,9 @@ class RequestCommentaryTest(TestCase): class VetCommentaryRequestsTest(TestCase): """Test cases for `vet_commentary_requests` view method""" - fixtures = ['groups', 'permissions'] def setUp(self): + add_groups_and_permissions() self.view_url = reverse('commentaries:vet_commentary_requests') self.login_url = reverse('scipost:login') self.password = 'test123' @@ -90,9 +92,9 @@ class VetCommentaryRequestsTest(TestCase): class BrowseCommentariesTest(TestCase): """Test cases for `browse` view.""" - fixtures = ['groups', 'permissions'] def setUp(self): + add_groups_and_permissions() VettedCommentaryFactory(discipline='physics') self.view_url = reverse('commentaries:browse', kwargs={ 'discipline': 'physics', @@ -111,9 +113,8 @@ class BrowseCommentariesTest(TestCase): class CommentaryDetailTest(TestCase): - fixtures = ['permissions', 'groups'] - def setUp(self): + add_groups_and_permissions() self.client = Client() self.commentary = UnpublishedVettedCommentaryFactory() self.target = reverse( diff --git a/comments/test_views.py b/comments/test_views.py index e1824139a5c2dd7def2f41ae2f6c6fd742914e41..d80fea2bd5e779336dc0d3ce31a6e77f9ae23a91 100644 --- a/comments/test_views.py +++ b/comments/test_views.py @@ -13,12 +13,13 @@ from .factories import CommentFactory from .forms import CommentForm from .models import Comment from .views import new_comment - from common.helpers import model_form_data +from common.helpers.test import add_groups_and_permissions class TestNewComment(TestCase): - fixtures = ['groups', 'permissions'] + def setUp(self): + add_groups_and_permissions() def install_messages_middleware(self, request): # I don't know what the following three lines do, but they help make a RequestFactory diff --git a/common/helpers/__init__.py b/common/helpers/__init__.py index 5bab1621fd150b0eaa7904b18cbf5e41cc82f628..78f99d9b8e50de1c0ce2fc15ae82ddf0eb23ef85 100644 --- a/common/helpers/__init__.py +++ b/common/helpers/__init__.py @@ -29,7 +29,6 @@ def model_form_data(model, form_class, form_kwargs={}): form_fields = list(form_class(**form_kwargs).fields.keys()) return filter_keys(model_data, form_fields) - def random_arxiv_identifier_with_version_number(): return random_arxiv_identifier_without_version_number() + "v0" diff --git a/common/helpers/test.py b/common/helpers/test.py new file mode 100644 index 0000000000000000000000000000000000000000..2c0d7c96d9724179fc73d98a7a14709ba2d35fbb --- /dev/null +++ b/common/helpers/test.py @@ -0,0 +1,4 @@ +import scipost.management.commands.add_groups_and_permissions + +def add_groups_and_permissions(): + scipost.management.commands.add_groups_and_permissions.Command().handle(verbose=False) diff --git a/scipost/management/commands/add_groups_and_permissions.py b/scipost/management/commands/add_groups_and_permissions.py index 9d8d1f39b96bd9d0d7ecee776198c73c01a6fb58..6ee4e935c17da5f72672d5d405f465f4374c4faa 100644 --- a/scipost/management/commands/add_groups_and_permissions.py +++ b/scipost/management/commands/add_groups_and_permissions.py @@ -9,7 +9,7 @@ from scipost.models import Contributor class Command(BaseCommand): help = 'Defines groups and permissions' - def handle(self, *args, **options): + def handle(self, *args, verbose=True, **options): """Append all user Groups and setup a Contributor roles to user.""" # Create Groups @@ -222,4 +222,6 @@ class Command(BaseCommand): can_view_docs_scipost, ) - self.stdout.write(self.style.SUCCESS('Successfully created groups and permissions.')) + + if verbose: + self.stdout.write(self.style.SUCCESS('Successfully created groups and permissions.')) diff --git a/scipost/test_views.py b/scipost/test_views.py index 5b764927f0baa855389a30b22dafd751cfaa6a2a..22291f792c19630af0cc6b5192667fb3b4c4d8b4 100644 --- a/scipost/test_views.py +++ b/scipost/test_views.py @@ -8,8 +8,8 @@ from commentaries.forms import CommentarySearchForm from commentaries.models import Commentary from .factories import ContributorFactory,\ - EditorialCollegeFellowFactory, EditorialCollegeFactory -from .models import EditorialCollege, EditorialCollegeFellow + EditorialCollegeFellowshipFactory, EditorialCollegeFactory +from .models import EditorialCollege, EditorialCollegeFellowship class RequestCommentaryTest(TestCase): @@ -137,7 +137,7 @@ class AboutViewTest(TestCase): # Create College with 10 members self.college = EditorialCollegeFactory() - EditorialCollegeFellowFactory.create_batch(10) + EditorialCollegeFellowshipFactory.create_batch(10) def test_status_code_200_including_members(self): response = self.client.get(self.target) @@ -151,4 +151,4 @@ class AboutViewTest(TestCase): # Members exist in college self.assertTrue(college.member.count() >= 10) last_member = college.member.last() - self.assertTrue(isinstance(last_member, EditorialCollegeFellow)) + self.assertTrue(isinstance(last_member, EditorialCollegeFellowship)) diff --git a/theses/test_forms.py b/theses/test_forms.py index dd649cedb0fecb2ead03e333ea6e280e3f658df9..e43421af0883d236ceaf90809572d91d06a1e792 100644 --- a/theses/test_forms.py +++ b/theses/test_forms.py @@ -6,12 +6,12 @@ from scipost.factories import ContributorFactory from .factories import ThesisLinkFactory, VetThesisLinkFormFactory from .forms import RequestThesisLinkForm, VetThesisLinkForm from common.helpers import model_form_data +from common.helpers.test import add_groups_and_permissions class TestRequestThesisLink(TestCase): - fixtures = ['permissions', 'groups'] - def setUp(self): + add_groups_and_permissions() self.contributor = ContributorFactory() self.user = self.contributor.user self.request = RequestFactory() diff --git a/theses/test_models.py b/theses/test_models.py index 9cda6172906f7feeb4a8789f014fb4a09f5f7bf9..c878df0efa98a652ee2799dc19b40a82511e354e 100644 --- a/theses/test_models.py +++ b/theses/test_models.py @@ -5,10 +5,12 @@ from django.core.exceptions import ValidationError from .models import ThesisLink from .factories import ThesisLinkFactory +from common.helpers.test import add_groups_and_permissions class ThesisLinkTestCase(TestCase): - fixtures = ['permissions', 'groups'] + def setUp(self): + add_groups_and_permissions() def test_domain_cannot_be_blank(self): thesis_link = ThesisLinkFactory() diff --git a/theses/test_views.py b/theses/test_views.py index db8819fabf80d7b405bb0a95f00bef5060696700..91e37fc3c6b157d72e479765f8dc7d3c5ba6344e 100644 --- a/theses/test_views.py +++ b/theses/test_views.py @@ -17,10 +17,11 @@ from .factories import ThesisLinkFactory, VettedThesisLinkFactory, VetThesisLink from .models import ThesisLink from .forms import VetThesisLinkForm from common.helpers import model_form_data - +from common.helpers.test import add_groups_and_permissions class TestThesisDetail(TestCase): - fixtures = ['groups', 'permissions'] + def setUp(self): + add_groups_and_permissions() def test_visits_valid_thesis_detail(self): """ A visitor does not have to be logged in to view a thesis link. """ @@ -32,9 +33,8 @@ class TestThesisDetail(TestCase): class TestRequestThesisLink(TestCase): - fixtures = ['groups', 'permissions'] - def setUp(self): + add_groups_and_permissions() self.client = Client() self.target = reverse('theses:request_thesislink') @@ -51,9 +51,8 @@ class TestRequestThesisLink(TestCase): class TestVetThesisLinkRequests(TestCase): - fixtures = ['groups', 'permissions'] - def setUp(self): + add_groups_and_permissions() self.client = Client() self.thesislink = ThesisLinkFactory() self.target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id}) @@ -158,9 +157,8 @@ class TestVetThesisLinkRequests(TestCase): class TestTheses(TestCase): - fixtures = ['groups', 'permissions'] - def setUp(self): + add_groups_and_permissions() self.client = Client() self.target = reverse('theses:theses')