|
1 | 1 | from nose.tools import * # noqa: F403 |
2 | 2 | import datetime as datetime |
| 3 | +import pytest |
3 | 4 |
|
| 5 | +from django.test import RequestFactory |
4 | 6 | from django.db.models import Q |
5 | 7 | from django.contrib.auth.models import Group |
6 | 8 | from django.core.exceptions import ValidationError, PermissionDenied |
| 9 | +from django.contrib.admin.sites import AdminSite |
| 10 | +from django.forms.models import model_to_dict |
| 11 | +from django.http import QueryDict |
| 12 | + |
7 | 13 |
|
8 | 14 | from tests.base import AdminTestCase |
9 | 15 |
|
10 | | -from osf_tests.factories import SubjectFactory, UserFactory, RegistrationFactory |
| 16 | +from osf_tests.factories import SubjectFactory, UserFactory, RegistrationFactory, PreprintFactory |
11 | 17 |
|
12 | | -from osf.models import Subject |
| 18 | +from osf.models import Subject, OSFUser, Collection |
13 | 19 | from osf.models.provider import rules_to_subjects |
14 | 20 | from admin.base.utils import get_subject_rules, change_embargo_date |
| 21 | +from osf.admin import OSFUserAdmin |
15 | 22 |
|
16 | 23 |
|
17 | 24 | import logging |
18 | 25 | logger = logging.getLogger(__name__) |
19 | 26 | logging.basicConfig(level=logging.INFO) |
| 27 | +pytestmark = pytest.mark.django_db |
20 | 28 |
|
21 | 29 |
|
22 | 30 | class TestSubjectRules(AdminTestCase): |
@@ -158,3 +166,82 @@ def test_change_embargo_date(self): |
158 | 166 | assert_almost_equal(self.registration.embargo.end_date, self.date_valid2, delta=datetime.timedelta(days=1)) |
159 | 167 |
|
160 | 168 | # Add a test to check privatizing |
| 169 | + |
| 170 | +site = AdminSite() |
| 171 | + |
| 172 | +class TestGroupCollectionsPreprints: |
| 173 | + @pytest.mark.enable_bookmark_creation |
| 174 | + @pytest.fixture() |
| 175 | + def user(self): |
| 176 | + return UserFactory() |
| 177 | + |
| 178 | + @pytest.fixture() |
| 179 | + def admin_url(self, user): |
| 180 | + return '/admin/osf/osfuser/{}/change/'.format(user.id) |
| 181 | + |
| 182 | + @pytest.fixture() |
| 183 | + def preprint(self, user): |
| 184 | + return PreprintFactory(creator=user) |
| 185 | + |
| 186 | + @pytest.fixture() |
| 187 | + def get_request(self, admin_url, user): |
| 188 | + request = RequestFactory().get(admin_url) |
| 189 | + request.user = user |
| 190 | + return request |
| 191 | + |
| 192 | + @pytest.fixture() |
| 193 | + def post_request(self, admin_url, user): |
| 194 | + request = RequestFactory().post(admin_url) |
| 195 | + request.user = user |
| 196 | + return request |
| 197 | + |
| 198 | + @pytest.fixture() |
| 199 | + def osf_user_admin(self): |
| 200 | + return OSFUserAdmin(OSFUser, site) |
| 201 | + |
| 202 | + @pytest.mark.enable_bookmark_creation |
| 203 | + def test_admin_app_formfield_collections(self, preprint, user, get_request, osf_user_admin): |
| 204 | + """ Testing OSFUserAdmin.formfield_many_to_many. |
| 205 | + This should not return any bookmark collections or preprint groups, even if the user is a member. |
| 206 | + """ |
| 207 | + |
| 208 | + formfield = (osf_user_admin.formfield_for_manytomany(OSFUser.groups.field, request=get_request)) |
| 209 | + queryset = formfield.queryset |
| 210 | + |
| 211 | + collections_group = Collection.objects.filter(creator=user, is_bookmark_collection=True)[0].get_group('admin') |
| 212 | + assert(collections_group not in queryset) |
| 213 | + |
| 214 | + assert(preprint.get_group('admin') not in queryset) |
| 215 | + |
| 216 | + @pytest.mark.enable_bookmark_creation |
| 217 | + def test_admin_app_save_related_collections(self, post_request, osf_user_admin, user, preprint): |
| 218 | + """ Testing OSFUserAdmin.save_related |
| 219 | + This should maintain the bookmark collections and preprint groups the user is a member of |
| 220 | + even though they aren't explicitly returned by the form. |
| 221 | + """ |
| 222 | + |
| 223 | + form = osf_user_admin.get_form(request=post_request, obj=user) |
| 224 | + data_dict = model_to_dict(user) |
| 225 | + post_form = form(data_dict, instance=user) |
| 226 | + |
| 227 | + # post_form.errors.keys() generates a list of fields causing JSON Related errors |
| 228 | + # which are preventing the form from being valid (which is required for the form to be saved). |
| 229 | + # By setting the field to '{}', this makes the form valid and resolves JSON errors. |
| 230 | + |
| 231 | + for field in post_form.errors.keys(): |
| 232 | + if field == 'groups': |
| 233 | + data_dict['groups'] = [] |
| 234 | + else: |
| 235 | + data_dict[field] = '{}' |
| 236 | + post_form = form(data_dict, instance=user) |
| 237 | + assert(post_form.is_valid()) |
| 238 | + post_form.save(commit=False) |
| 239 | + qdict = QueryDict('', mutable=True) |
| 240 | + qdict.update(data_dict) |
| 241 | + post_request.POST = qdict |
| 242 | + osf_user_admin.save_related(request=post_request, form=post_form, formsets=[], change=True) |
| 243 | + |
| 244 | + collections_group = Collection.objects.filter(creator=user, is_bookmark_collection=True)[0].get_group('admin') |
| 245 | + assert(collections_group in user.groups.all()) |
| 246 | + |
| 247 | + assert(preprint.get_group('admin') in user.groups.all()) |
0 commit comments