8000 some properly failing tests, set up for standard permissions · alumni/django-rest-framework@b07de86 · GitHub
[go: up one dir, main page]

Skip to content

Commit b07de86

Browse files
committed
some properly failing tests, set up for standard permissions
1 parent 4a9dcfa commit b07de86

File tree

4 files changed

+84
-46
lines changed

4 files changed

+84
-46
lines changed

rest_framework/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class DjangoModelPermissionsOrAnonReadOnly(DjangoModelPermissions):
153153

154154
class DjangoObjectLevelModelPermissions(DjangoModelPermissions):
155155
def __init__(self):
156-
assert guardian, 'Using DjangoObjectLevelModelPermissions, but guardian is not installed'
156+
assert guardian, 'Using DjangoObjectLevelModelPermissions, but django-guardian is not installed'
157157

158158

159159
class TokenHasReadWriteScope(BasePermission):

rest_framework/runtests/settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@
123123
'provider.oauth2',
124124
)
125125

126+
# guardian is optional
127+
try:
128+
import guardian
129+
except ImportError:
130+
pass
131+
else:
132+
ANONYMOUS_USER_ID = -1
133+
INSTALLED_APPS += (
134+
'guardian',
135+
)
136+
126137
STATIC_URL = '/static/'
127138

128139
PASSWORD_HASHERS = (

rest_framework/tests/test_permissions.py

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
from django.db import models
44
from django.test import TestCase
55
from rest_framework import generics, status, permissions, authentication, HTTP_HEADER_ENCODING
6-
from rest_framework.test import APIRequestFactory
76
from rest_framework.compat import guardian
7+
from rest_framework.test import APIRequestFactory
8+
from rest_framework.tests.models import BasicModel
9+
from rest_framework.settings import api_settings
810
import base64
911

1012
factory = APIRequestFactory()
1113

12-
13-
class BasicModel(models.Model):
14-
text = models.CharField(max_length=100)
15-
16-
1714
class RootView(generics.ListCreateAPIView):
1815
model = BasicModel
1916
authentication_classes = [authentication.BasicAuthentication]
@@ -145,45 +142,67 @@ def test_options_updateonly(self):
145142
self.assertEqual(list(response.data['actions'].keys()), ['PUT'])
146143

147144

148-
class OwnerModel(models.Model):
149-
text = models.CharField(max_length=100)
150-
owner = models.ForeignKey(User)
145+
class BasicPermModel(BasicModel):
151146

147+
class Meta:
148+
app_label = 'tests'
149+
permissions = (
150+
('read_basicpermmodel', "Can view basic perm model"),
151+
# add, change, delete built in to django
152+
)
152153

153-
class IsOwnerPermission(permissions.BasePermission):
154-
def has_object_permission(self, request, view, obj):
155-
return request.user == obj.owner
156-
157-
158-
class OwnerInstanceView(generics.RetrieveUpdateDestroyAPIView):
159-
model = OwnerModel
154+
class ObjectPermissionInstanceView(generics.RetrieveUpdateDestroyAPIView):
155+
model = BasicModel
160156
authentication_classes = [authentication.BasicAuthentication]
161-
permission_classes = [IsOwnerPermission]
162-
163-
164-
owner_instance_view = OwnerInstanceView.as_view()
165-
166-
167-
class ObjectPermissionsIntegrationTests(TestCase):
168-
"""
169-
Integration tests for the object level permissions API.
170-
"""
171-
172-
def setUp(self):
173-
User.objects.create_user('not_owner', 'not_owner@example.com', 'password')
174-
user = User.objects.create_user('owner', 'owner@example.com', 'password')
175-
176-
self.not_owner_credentials = basic_auth_header('not_owner', 'password')
177-
self.owner_credentials = basic_auth_header('owner', 'password')
178-
179-
OwnerModel(text='foo', owner=user).save()
180-
181-
def test_owner_has_delete_permissions(self):
182-
request = factory.delete('/1', HTTP_AUTHORIZATION=self.owner_credentials)
183-
response = owner_instance_view(request, pk='1')
184-
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
185-
186-
def test_non_owner_does_not_have_delete_permissions(self):
187-
request = factory.delete('/1', HTTP_AUTHORIZATION=self.not_owner_credentials)
188-
response = owner_instance_view(request, pk='1')
189-
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
157+
permission_classes = [permissions.DjangoObjectLevelModelPermissions]
158+
159+
160+
object_permissions_view = ObjectPermissionInstanceView.as_view()
161+
162+
if guardian:
163+
class ObjectPermissionsIntegrationTests(TestCase):
164+
"""
165+
Integration tests for the object level permissions API.
166+
"""
167+
168+
def setUp(self):
169+
# create users
170+
User.objects.create_user('no_permission', 'no_permission@example.com', 'password')
171+
reader = User.objects.create_user('reader', 'reader@example.com', 'password')
172+
writer = User.objects.create_user('writer', 'writer@example.com', 'password')
173+
full_access = User.objects.create_user('full_access', 'full_access@example.com', 'password')
174+
175+
model = BasicPermModel.objects.create(text='foo')
176+
177+
# assign permissions appropriately
178+
from guardian.shortcuts import assign_perm
179+
180+
read = "read_basicpermmodel"
181+
write = "change_basicpermmodel"
182+
delete = "delete_basicpermmodel"
183+
app_label = 'tests.'
184+
# model level permissions
185+
assign_perm(app_label + delete, full_access, obj=model)
186+
(assign_perm(app_label + write, user, obj=model) for user in (writer, full_access))
187+
(assign_perm(app_label + read, user, obj=model) for user in (reader, writer, full_access))
188+
189+
# object level permissions
190+
assign_perm(delete, full_access, obj=model)
191+
(assign_perm(write, user, obj=model) for user in (writer, full_access))
192+
(assign_perm(read, user, obj=model) for user in (reader, writer, full_access))
193+
194+
self.no_permission_credentials = basic_auth_header('no_permission', 'password')
195+
self.reader_credentials = basic_auth_header('reader', 'password')
196+
self.writer_credentials = basic_auth_header('writer', 'password')
197+
self.full_access_credentials = basic_auth_header('full_access', 'password')
198+
199+
200+
def test_has_delete_permissions(self):
201+
request = factory.delete('/1', HTTP_AUTHORIZATION=self.full_access_credentials)
202+
response = object_permissions_view(request, pk='1')
203+
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
204+
205+
def test_no_delete_permissions(self):
206+
request = factory.delete('/1', HTTP_AUTHORIZATION=self.writer_credentials)
207+
response = object_permissions_view(request, pk='1')
208+
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

tox.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ deps = https://www.djangoproject.com/download/1.6a1/tarball/
2525
django-oauth-plus==2.0
2626
oauth2==1.5.211
2727
django-oauth2-provider==0.2.4
28+
django-guardian==1.1.1
2829

2930
[testenv:py2.6-django1.6]
3031
basepython = python2.6
@@ -34,6 +35,7 @@ deps = https://www.djangoproject.com/download/1.6a1/tarball/
3435
django-oauth-plus==2.0
3536
oauth2==1.5.211
3637
django-oauth2-provider==0.2.4
38+
django-guardian==1.1.1
3739

3840
[testenv:py3.3-django1.5]
3941
basepython = python3.3
@@ -55,6 +57,7 @@ deps = django==1.5
5557
django-oauth-plus==2.0
5658
oauth2==1.5.211
5759
django-oauth2-provider==0.2.3
60+
django-guardian==1.1.1
5861

5962
[testenv:py2.6-django1.5]
6063
basepython = python2.6
@@ -64,6 +67,7 @@ deps = django==1.5
6467
django-oauth-plus==2.0
6568
oauth2==1.5.211
6669
django-oauth2-provider==0.2.3
70+
django-guardian==1.1.1
6771

6872
[testenv:py2.7-django1.4]
6973
basepython = python2.7
@@ -73,6 +77,7 @@ deps = django==1.4.3
7377
django-oauth-plus==2.0
7478
oauth2==1.5.211
7579
django-oauth2-provider==0.2.3
80+
django-guardian==1.1.1
7681

7782
[testenv:py2.6-django1.4]
7883
basepython = python2.6
@@ -82,6 +87,7 @@ deps = django==1.4.3
8287
django-oauth-plus==2.0
8388
oauth2==1.5.211
8489
django-oauth2-provider==0.2.3
90+
django-guardian==1.1.1
8591

8692
[testenv:py2.7-django1.3]
8793
basepython = python2.7
@@ -91,6 +97,7 @@ deps = django==1.3.5
9197
django-oauth-plus==2.0
9298
oauth2==1.5.211
9399
django-oauth2-provider==0.2.3
100+
django-guardian==1.1.1
94101

95102
[testenv:py2.6-django1.3]
96103
basepython = python2.6
@@ -100,3 +107,4 @@ deps = django==1.3.5
100107
django-oauth-plus==2.0
101108
oauth2==1.5.211
102109
django-oauth2-provider==0.2.3
110+
django-guardian==1.1.1

0 commit comments

Comments
 (0)
0