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
2230784f
Commit
2230784f
authored
Dec 06, 2016
by
Xavier Ordoquy
Committed by
GitHub
Dec 06, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4745 from auvipy/authtest
converted authentication test asserts to pytest
parents
792b50fe
841a91e9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
35 deletions
+35
-35
tests/test_authentication.py
+35
-35
No files found.
tests/test_authentication.py
View file @
2230784f
...
@@ -106,7 +106,7 @@ class BasicAuthTests(TestCase):
...
@@ -106,7 +106,7 @@ class BasicAuthTests(TestCase):
{
'example'
:
'example'
},
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
HTTP_AUTHORIZATION
=
auth
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
def
test_post_json_passing_basic_auth
(
self
):
def
test_post_json_passing_basic_auth
(
self
):
"""Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF"""
"""Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF"""
...
@@ -121,7 +121,7 @@ class BasicAuthTests(TestCase):
...
@@ -121,7 +121,7 @@ class BasicAuthTests(TestCase):
format
=
'json'
,
format
=
'json'
,
HTTP_AUTHORIZATION
=
auth
HTTP_AUTHORIZATION
=
auth
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
def
test_regression_handle_bad_base64_basic_auth_header
(
self
):
def
test_regression_handle_bad_base64_basic_auth_header
(
self
):
"""Ensure POSTing JSON over basic auth with incorrectly padded Base64 string is handled correctly"""
"""Ensure POSTing JSON over basic auth with incorrectly padded Base64 string is handled correctly"""
...
@@ -134,12 +134,12 @@ class BasicAuthTests(TestCase):
...
@@ -134,12 +134,12 @@ class BasicAuthTests(TestCase):
format
=
'json'
,
format
=
'json'
,
HTTP_AUTHORIZATION
=
auth
HTTP_AUTHORIZATION
=
auth
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_401_UNAUTHORIZED
)
assert
response
.
status_code
==
status
.
HTTP_401_UNAUTHORIZED
def
test_post_form_failing_basic_auth
(
self
):
def
test_post_form_failing_basic_auth
(
self
):
"""Ensure POSTing form over basic auth without correct credentials fails"""
"""Ensure POSTing form over basic auth without correct credentials fails"""
response
=
self
.
csrf_client
.
post
(
'/basic/'
,
{
'example'
:
'example'
})
response
=
self
.
csrf_client
.
post
(
'/basic/'
,
{
'example'
:
'example'
})
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_401_UNAUTHORIZED
)
assert
response
.
status_code
==
status
.
HTTP_401_UNAUTHORIZED
def
test_post_json_failing_basic_auth
(
self
):
def
test_post_json_failing_basic_auth
(
self
):
"""Ensure POSTing json over basic auth without correct credentials fails"""
"""Ensure POSTing json over basic auth without correct credentials fails"""
...
@@ -148,8 +148,8 @@ class BasicAuthTests(TestCase):
...
@@ -148,8 +148,8 @@ class BasicAuthTests(TestCase):
{
'example'
:
'example'
},
{
'example'
:
'example'
},
format
=
'json'
format
=
'json'
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_401_UNAUTHORIZED
)
assert
response
.
status_code
==
status
.
HTTP_401_UNAUTHORIZED
self
.
assertEqual
(
response
[
'WWW-Authenticate'
],
'Basic realm="api"'
)
assert
response
[
'WWW-Authenticate'
]
==
'Basic realm="api"'
@override_settings
(
ROOT_URLCONF
=
'tests.test_authentication'
)
@override_settings
(
ROOT_URLCONF
=
'tests.test_authentication'
)
...
@@ -175,9 +175,8 @@ class SessionAuthTests(TestCase):
...
@@ -175,9 +175,8 @@ class SessionAuthTests(TestCase):
cf. [#1810](https://github.com/tomchristie/django-rest-framework/pull/1810)
cf. [#1810](https://github.com/tomchristie/django-rest-framework/pull/1810)
"""
"""
response
=
self
.
csrf_client
.
get
(
'/auth/login/'
)
response
=
self
.
csrf_client
.
get
(
'/auth/login/'
)
self
.
assertContains
(
content
=
response
.
content
.
decode
(
'utf8'
)
response
,
'<label for="id_username">Username:</label>'
assert
'<label for="id_username">Username:</label>'
in
content
)
def
test_post_form_session_auth_failing_csrf
(
self
):
def
test_post_form_session_auth_failing_csrf
(
self
):
"""
"""
...
@@ -185,7 +184,7 @@ class SessionAuthTests(TestCase):
...
@@ -185,7 +184,7 @@ class SessionAuthTests(TestCase):
"""
"""
self
.
csrf_client
.
login
(
username
=
self
.
username
,
password
=
self
.
password
)
self
.
csrf_client
.
login
(
username
=
self
.
username
,
password
=
self
.
password
)
response
=
self
.
csrf_client
.
post
(
'/session/'
,
{
'example'
:
'example'
})
response
=
self
.
csrf_client
.
post
(
'/session/'
,
{
'example'
:
'example'
})
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
assert
response
.
status_code
==
status
.
HTTP_403_FORBIDDEN
def
test_post_form_session_auth_passing
(
self
):
def
test_post_form_session_auth_passing
(
self
):
"""
"""
...
@@ -198,7 +197,7 @@ class SessionAuthTests(TestCase):
...
@@ -198,7 +197,7 @@ class SessionAuthTests(TestCase):
response
=
self
.
non_csrf_client
.
post
(
response
=
self
.
non_csrf_client
.
post
(
'/session/'
,
{
'example'
:
'example'
}
'/session/'
,
{
'example'
:
'example'
}
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
def
test_put_form_session_auth_passing
(
self
):
def
test_put_form_session_auth_passing
(
self
):
"""
"""
...
@@ -211,14 +210,14 @@ class SessionAuthTests(TestCase):
...
@@ -211,14 +210,14 @@ class SessionAuthTests(TestCase):
response
=
self
.
non_csrf_client
.
put
(
response
=
self
.
non_csrf_client
.
put
(
'/session/'
,
{
'example'
:
'example'
}
'/session/'
,
{
'example'
:
'example'
}
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
def
test_post_form_session_auth_failing
(
self
):
def
test_post_form_session_auth_failing
(
self
):
"""
"""
Ensure POSTing form over session authentication without logged in user fails.
Ensure POSTing form over session authentication without logged in user fails.
"""
"""
response
=
self
.
csrf_client
.
post
(
'/session/'
,
{
'example'
:
'example'
})
response
=
self
.
csrf_client
.
post
(
'/session/'
,
{
'example'
:
'example'
})
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
assert
response
.
status_code
==
status
.
HTTP_403_FORBIDDEN
class
BaseTokenAuthTests
(
object
):
class
BaseTokenAuthTests
(
object
):
...
@@ -248,7 +247,7 @@ class BaseTokenAuthTests(object):
...
@@ -248,7 +247,7 @@ class BaseTokenAuthTests(object):
response
=
self
.
csrf_client
.
post
(
response
=
self
.
csrf_client
.
post
(
self
.
path
,
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
self
.
path
,
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
def
test_fail_post_form_passing_nonexistent_token_auth
(
self
):
def
test_fail_post_form_passing_nonexistent_token_auth
(
self
):
# use a nonexistent token key
# use a nonexistent token key
...
@@ -256,7 +255,7 @@ class BaseTokenAuthTests(object):
...
@@ -256,7 +255,7 @@ class BaseTokenAuthTests(object):
response
=
self
.
csrf_client
.
post
(
response
=
self
.
csrf_client
.
post
(
self
.
path
,
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
self
.
path
,
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_401_UNAUTHORIZED
)
assert
response
.
status_code
==
status
.
HTTP_401_UNAUTHORIZED
def
test_fail_post_form_passing_invalid_token_auth
(
self
):
def
test_fail_post_form_passing_invalid_token_auth
(
self
):
# add an 'invalid' unicode character
# add an 'invalid' unicode character
...
@@ -264,7 +263,7 @@ class BaseTokenAuthTests(object):
...
@@ -264,7 +263,7 @@ class BaseTokenAuthTests(object):
response
=
self
.
csrf_client
.
post
(
response
=
self
.
csrf_client
.
post
(
self
.
path
,
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
self
.
path
,
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_401_UNAUTHORIZED
)
assert
response
.
status_code
==
status
.
HTTP_401_UNAUTHORIZED
def
test_post_json_passing_token_auth
(
self
):
def
test_post_json_passing_token_auth
(
self
):
"""
"""
...
@@ -276,7 +275,7 @@ class BaseTokenAuthTests(object):
...
@@ -276,7 +275,7 @@ class BaseTokenAuthTests(object):
self
.
path
,
{
'example'
:
'example'
},
self
.
path
,
{
'example'
:
'example'
},
format
=
'json'
,
HTTP_AUTHORIZATION
=
auth
format
=
'json'
,
HTTP_AUTHORIZATION
=
auth
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
def
test_post_json_makes_one_db_query
(
self
):
def
test_post_json_makes_one_db_query
(
self
):
"""
"""
...
@@ -298,7 +297,7 @@ class BaseTokenAuthTests(object):
...
@@ -298,7 +297,7 @@ class BaseTokenAuthTests(object):
Ensure POSTing form over token auth without correct credentials fails
Ensure POSTing form over token auth without correct credentials fails
"""
"""
response
=
self
.
csrf_client
.
post
(
self
.
path
,
{
'example'
:
'example'
})
response
=
self
.
csrf_client
.
post
(
self
.
path
,
{
'example'
:
'example'
})
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_401_UNAUTHORIZED
)
assert
response
.
status_code
==
status
.
HTTP_401_UNAUTHORIZED
def
test_post_json_failing_token_auth
(
self
):
def
test_post_json_failing_token_auth
(
self
):
"""
"""
...
@@ -307,7 +306,7 @@ class BaseTokenAuthTests(object):
...
@@ -307,7 +306,7 @@ class BaseTokenAuthTests(object):
response
=
self
.
csrf_client
.
post
(
response
=
self
.
csrf_client
.
post
(
self
.
path
,
{
'example'
:
'example'
},
format
=
'json'
self
.
path
,
{
'example'
:
'example'
},
format
=
'json'
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_401_UNAUTHORIZED
)
assert
response
.
status_code
==
status
.
HTTP_401_UNAUTHORIZED
@override_settings
(
ROOT_URLCONF
=
'tests.test_authentication'
)
@override_settings
(
ROOT_URLCONF
=
'tests.test_authentication'
)
...
@@ -319,13 +318,13 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
...
@@ -319,13 +318,13 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
"""Ensure creating a token with no key will auto-assign a key"""
"""Ensure creating a token with no key will auto-assign a key"""
self
.
token
.
delete
()
self
.
token
.
delete
()
token
=
self
.
model
.
objects
.
create
(
user
=
self
.
user
)
token
=
self
.
model
.
objects
.
create
(
user
=
self
.
user
)
self
.
assertTrue
(
bool
(
token
.
key
)
)
assert
bool
(
token
.
key
)
def
test_generate_key_returns_string
(
self
):
def
test_generate_key_returns_string
(
self
):
"""Ensure generate_key returns a string"""
"""Ensure generate_key returns a string"""
token
=
self
.
model
()
token
=
self
.
model
()
key
=
token
.
generate_key
()
key
=
token
.
generate_key
()
self
.
assertTrue
(
isinstance
(
key
,
six
.
string_types
)
)
assert
isinstance
(
key
,
six
.
string_types
)
def
test_token_login_json
(
self
):
def
test_token_login_json
(
self
):
"""Ensure token login view using JSON POST works."""
"""Ensure token login view using JSON POST works."""
...
@@ -335,8 +334,8 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
...
@@ -335,8 +334,8 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
{
'username'
:
self
.
username
,
'password'
:
self
.
password
},
{
'username'
:
self
.
username
,
'password'
:
self
.
password
},
format
=
'json'
format
=
'json'
)
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
self
.
assertEqual
(
response
.
data
[
'token'
],
self
.
key
)
assert
response
.
data
[
'token'
]
==
self
.
key
def
test_token_login_json_bad_creds
(
self
):
def
test_token_login_json_bad_creds
(
self
):
"""
"""
...
@@ -349,22 +348,24 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
...
@@ -349,22 +348,24 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
{
'username'
:
self
.
username
,
'password'
:
"badpass"
},
{
'username'
:
self
.
username
,
'password'
:
"badpass"
},
format
=
'json'
format
=
'json'
)
)
self
.
assertEqual
(
response
.
status_code
,
400
)
assert
response
.
status_code
==
400
def
test_token_login_json_missing_fields
(
self
):
def
test_token_login_json_missing_fields
(
self
):
"""Ensure token login view using JSON POST fails if missing fields."""
"""Ensure token login view using JSON POST fails if missing fields."""
client
=
APIClient
(
enforce_csrf_checks
=
True
)
client
=
APIClient
(
enforce_csrf_checks
=
True
)
response
=
client
.
post
(
'/auth-token/'
,
response
=
client
.
post
(
'/auth-token/'
,
{
'username'
:
self
.
username
},
format
=
'json'
)
{
'username'
:
self
.
username
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
400
)
assert
response
.
status_code
==
400
def
test_token_login_form
(
self
):
def
test_token_login_form
(
self
):
"""Ensure token login view using form POST works."""
"""Ensure token login view using form POST works."""
client
=
APIClient
(
enforce_csrf_checks
=
True
)
client
=
APIClient
(
enforce_csrf_checks
=
True
)
response
=
client
.
post
(
'/auth-token/'
,
response
=
client
.
post
(
{
'username'
:
self
.
username
,
'password'
:
self
.
password
})
'/auth-token/'
,
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
{
'username'
:
self
.
username
,
'password'
:
self
.
password
}
self
.
assertEqual
(
response
.
data
[
'token'
],
self
.
key
)
)
assert
response
.
status_code
==
status
.
HTTP_200_OK
assert
response
.
data
[
'token'
]
==
self
.
key
@override_settings
(
ROOT_URLCONF
=
'tests.test_authentication'
)
@override_settings
(
ROOT_URLCONF
=
'tests.test_authentication'
)
...
@@ -397,8 +398,8 @@ class IncorrectCredentialsTests(TestCase):
...
@@ -397,8 +398,8 @@ class IncorrectCredentialsTests(TestCase):
permission_classes
=
()
permission_classes
=
()
)
)
response
=
view
(
request
)
response
=
view
(
request
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
assert
response
.
status_code
==
status
.
HTTP_403_FORBIDDEN
self
.
assertEqual
(
response
.
data
,
{
'detail'
:
'Bad credentials'
})
assert
response
.
data
==
{
'detail'
:
'Bad credentials'
}
class
FailingAuthAccessedInRenderer
(
TestCase
):
class
FailingAuthAccessedInRenderer
(
TestCase
):
...
@@ -435,7 +436,7 @@ class FailingAuthAccessedInRenderer(TestCase):
...
@@ -435,7 +436,7 @@ class FailingAuthAccessedInRenderer(TestCase):
request
=
factory
.
get
(
'/'
)
request
=
factory
.
get
(
'/'
)
response
=
self
.
view
(
request
)
response
=
self
.
view
(
request
)
content
=
response
.
render
()
.
content
content
=
response
.
render
()
.
content
self
.
assertEqual
(
content
,
b
'not authenticated'
)
assert
content
==
b
'not authenticated'
class
NoAuthenticationClassesTests
(
TestCase
):
class
NoAuthenticationClassesTests
(
TestCase
):
...
@@ -458,6 +459,5 @@ class NoAuthenticationClassesTests(TestCase):
...
@@ -458,6 +459,5 @@ class NoAuthenticationClassesTests(TestCase):
permission_classes
=
(
DummyPermission
,),
permission_classes
=
(
DummyPermission
,),
)
)
response
=
view
(
request
)
response
=
view
(
request
)
self
.
assertEqual
(
response
.
status_code
,
assert
response
.
status_code
==
status
.
HTTP_403_FORBIDDEN
status
.
HTTP_403_FORBIDDEN
)
assert
response
.
data
==
{
'detail'
:
'Dummy permission message'
}
self
.
assertEqual
(
response
.
data
,
{
'detail'
:
'Dummy permission message'
})
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