Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
baacdd82
Commit
baacdd82
authored
Feb 10, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add object permissions tests.
parent
870f1048
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
0 deletions
+46
-0
rest_framework/tests/permissions.py
+46
-0
No files found.
rest_framework/tests/permissions.py
View file @
baacdd82
...
@@ -107,3 +107,49 @@ class ModelPermissionsIntegrationTests(TestCase):
...
@@ -107,3 +107,49 @@ class ModelPermissionsIntegrationTests(TestCase):
HTTP_AUTHORIZATION
=
self
.
updateonly_credentials
)
HTTP_AUTHORIZATION
=
self
.
updateonly_credentials
)
response
=
instance_view
(
request
,
pk
=
'2'
)
response
=
instance_view
(
request
,
pk
=
'2'
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
class
OwnerModel
(
models
.
Model
):
text
=
models
.
CharField
(
max_length
=
100
)
owner
=
models
.
ForeignKey
(
User
)
class
IsOwnerPermission
(
permissions
.
BasePermission
):
def
has_permission
(
self
,
request
,
view
,
obj
=
None
):
if
not
obj
:
return
True
return
request
.
user
==
obj
.
owner
class
OwnerInstanceView
(
generics
.
RetrieveUpdateDestroyAPIView
):
model
=
OwnerModel
authentication_classes
=
[
authentication
.
BasicAuthentication
]
permission_classes
=
[
IsOwnerPermission
]
owner_instance_view
=
OwnerInstanceView
.
as_view
()
class
ObjectPermissionsIntegrationTests
(
TestCase
):
"""
Integration tests for the object level permissions API.
"""
def
setUp
(
self
):
User
.
objects
.
create_user
(
'not_owner'
,
'not_owner@example.com'
,
'password'
)
user
=
User
.
objects
.
create_user
(
'owner'
,
'owner@example.com'
,
'password'
)
self
.
not_owner_credentials
=
basic_auth_header
(
'not_owner'
,
'password'
)
self
.
owner_credentials
=
basic_auth_header
(
'owner'
,
'password'
)
OwnerModel
(
text
=
'foo'
,
owner
=
user
)
.
save
()
def
test_owner_has_delete_permissions
(
self
):
request
=
factory
.
delete
(
'/1'
,
HTTP_AUTHORIZATION
=
self
.
owner_credentials
)
response
=
owner_instance_view
(
request
,
pk
=
'1'
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
def
test_non_owner_does_not_have_delete_permissions
(
self
):
request
=
factory
.
delete
(
'/1'
,
HTTP_AUTHORIZATION
=
self
.
not_owner_credentials
)
response
=
owner_instance_view
(
request
,
pk
=
'1'
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment