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
4c8bd404
Commit
4c8bd404
authored
Feb 10, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for DjangoModelPermissions.
parent
fd57978c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
0 deletions
+89
-0
rest_framework/tests/permissions.py
+89
-0
No files found.
rest_framework/tests/permissions.py
0 → 100644
View file @
4c8bd404
from
__future__
import
unicode_literals
from
django.contrib.auth.models
import
User
,
Permission
from
django.db
import
models
from
django.test
import
TestCase
from
rest_framework
import
generics
,
status
,
permissions
,
authentication
,
HTTP_HEADER_ENCODING
from
rest_framework.tests.utils
import
RequestFactory
import
base64
import
json
factory
=
RequestFactory
()
class
BasicModel
(
models
.
Model
):
text
=
models
.
CharField
(
max_length
=
100
)
class
RootView
(
generics
.
ListCreateAPIView
):
model
=
BasicModel
authentication_classes
=
[
authentication
.
BasicAuthentication
]
permission_classes
=
[
permissions
.
DjangoModelPermissions
]
class
InstanceView
(
generics
.
RetrieveUpdateDestroyAPIView
):
model
=
BasicModel
authentication_classes
=
[
authentication
.
BasicAuthentication
]
permission_classes
=
[
permissions
.
DjangoModelPermissions
]
root_view
=
RootView
.
as_view
()
instance_view
=
InstanceView
.
as_view
()
def
basic_auth_header
(
username
,
password
):
credentials
=
(
'
%
s:
%
s'
%
(
username
,
password
))
base64_credentials
=
base64
.
b64encode
(
credentials
.
encode
(
HTTP_HEADER_ENCODING
))
.
decode
(
HTTP_HEADER_ENCODING
)
return
'Basic
%
s'
%
base64_credentials
class
ModelPermissionsIntegrationTests
(
TestCase
):
def
setUp
(
self
):
User
.
objects
.
create_user
(
'disallowed'
,
'disallowed@example.com'
,
'password'
)
user
=
User
.
objects
.
create_user
(
'permitted'
,
'permitted@example.com'
,
'password'
)
user
.
user_permissions
=
[
Permission
.
objects
.
get
(
codename
=
'add_basicmodel'
),
Permission
.
objects
.
get
(
codename
=
'change_basicmodel'
),
Permission
.
objects
.
get
(
codename
=
'delete_basicmodel'
)
]
self
.
permitted_credentials
=
basic_auth_header
(
'permitted'
,
'password'
)
self
.
disallowed_credentials
=
basic_auth_header
(
'disallowed'
,
'password'
)
BasicModel
(
text
=
'foo'
)
.
save
()
def
test_has_create_permissions
(
self
):
request
=
factory
.
post
(
'/'
,
json
.
dumps
({
'text'
:
'foobar'
}),
content_type
=
'application/json'
,
HTTP_AUTHORIZATION
=
self
.
permitted_credentials
)
response
=
root_view
(
request
,
pk
=
1
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
def
test_has_put_permissions
(
self
):
request
=
factory
.
put
(
'/1'
,
json
.
dumps
({
'text'
:
'foobar'
}),
content_type
=
'application/json'
,
HTTP_AUTHORIZATION
=
self
.
permitted_credentials
)
response
=
instance_view
(
request
,
pk
=
'1'
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
def
test_has_delete_permissions
(
self
):
request
=
factory
.
delete
(
'/1'
,
HTTP_AUTHORIZATION
=
self
.
permitted_credentials
)
response
=
instance_view
(
request
,
pk
=
1
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
def
test_does_not_have_create_permissions
(
self
):
request
=
factory
.
post
(
'/'
,
json
.
dumps
({
'text'
:
'foobar'
}),
content_type
=
'application/json'
,
HTTP_AUTHORIZATION
=
self
.
disallowed_credentials
)
response
=
root_view
(
request
,
pk
=
1
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
def
test_does_not_have_put_permissions
(
self
):
request
=
factory
.
put
(
'/1'
,
json
.
dumps
({
'text'
:
'foobar'
}),
content_type
=
'application/json'
,
HTTP_AUTHORIZATION
=
self
.
disallowed_credentials
)
response
=
instance_view
(
request
,
pk
=
'1'
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
def
test_does_not_have_delete_permissions
(
self
):
request
=
factory
.
delete
(
'/1'
,
HTTP_AUTHORIZATION
=
self
.
disallowed_credentials
)
response
=
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