-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from James1345/develop
Release 3.0.0: Big performance improve
- Loading branch information
Showing
7 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.10 on 2016-08-18 09:32 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def cleanup_tokens(apps, schema_editor): | ||
AuthToken = apps.get_model('knox', 'AuthToken') | ||
AuthToken.objects.filter(token_key__isnull=True).delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('knox', '0005_authtoken_token_key'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(cleanup_tokens), | ||
migrations.AlterField( | ||
model_name='authtoken', | ||
name='token_key', | ||
field=models.CharField(db_index=True, default='', max_length=8), | ||
preserve_default=False, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,13 +37,14 @@ def test_logout_deletes_keys(self): | |
username, password = 'root', 'toor' | ||
user = User.objects.create_user( | ||
username, '[email protected]', password) | ||
token = AuthToken.objects.create(user=user) | ||
self.assertEqual(AuthToken.objects.count(), 1) | ||
for _ in range(2): | ||
token = AuthToken.objects.create(user=user) | ||
self.assertEqual(AuthToken.objects.count(), 2) | ||
|
||
url = reverse('knox_logout') | ||
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % token)) | ||
self.client.post(url, {}, format='json') | ||
self.assertEqual(AuthToken.objects.count(), 0) | ||
self.assertEqual(AuthToken.objects.count(), 1, 'other tokens should remain after logout') | ||
|
||
def test_logout_all_deletes_keys(self): | ||
self.assertEqual(AuthToken.objects.count(), 0) | ||
|
@@ -59,6 +60,36 @@ def test_logout_all_deletes_keys(self): | |
self.client.post(url, {}, format='json') | ||
self.assertEqual(AuthToken.objects.count(), 0) | ||
|
||
def test_logout_all_deletes_only_targets_keys(self): | ||
self.assertEqual(AuthToken.objects.count(), 0) | ||
username, password = 'root', 'toor' | ||
user = User.objects.create_user( | ||
username, '[email protected]', password) | ||
user2 = User.objects.create_user( | ||
'user2', '[email protected]', password) | ||
for _ in range(10): | ||
token = AuthToken.objects.create(user=user) | ||
token2 = AuthToken.objects.create(user=user2) | ||
self.assertEqual(AuthToken.objects.count(), 20) | ||
|
||
url = reverse('knox_logoutall') | ||
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % token)) | ||
self.client.post(url, {}, format='json') | ||
self.assertEqual(AuthToken.objects.count(), 10, 'tokens from other users should not be affected by logout all') | ||
|
||
def test_expired_tokens_login_fails(self): | ||
self.assertEqual(AuthToken.objects.count(), 0) | ||
username, password = 'root', 'toor' | ||
user = User.objects.create_user( | ||
username, '[email protected]', password) | ||
token = AuthToken.objects.create( | ||
user=user, expires=datetime.timedelta(seconds=0)) | ||
url = reverse('api-root') | ||
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % token)) | ||
response = self.client.post(url, {}, format='json') | ||
self.assertEqual(response.status_code, 401) | ||
self.assertEqual(response.data, {"detail": "Invalid token."}) | ||
|
||
def test_expired_tokens_deleted(self): | ||
self.assertEqual(AuthToken.objects.count(), 0) | ||
username, password = 'root', 'toor' | ||
|
@@ -83,14 +114,19 @@ def test_update_token_key(self): | |
user = User.objects.create_user( | ||
username, '[email protected]', password) | ||
token = AuthToken.objects.create(user) | ||
auth_token = AuthToken.objects.first() | ||
auth_token.token_key = None | ||
auth_token.save() | ||
rf = APIRequestFactory() | ||
request = rf.get('/') | ||
request.META = {'HTTP_AUTHORIZATION': 'Token {}'.format(token)} | ||
TokenAuthentication().authenticate(request) | ||
auth_token = AuthToken.objects.get(digest=auth_token.digest) | ||
(user, auth_token) = TokenAuthentication().authenticate(request) | ||
self.assertEqual( | ||
token[:CONSTANTS.TOKEN_KEY_LENGTH], | ||
auth_token.token_key) | ||
|
||
def test_invalid_token_length_returns_401_code(self): | ||
invalid_token = "1" * (CONSTANTS.TOKEN_KEY_LENGTH - 1) | ||
url = reverse('api-root') | ||
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % invalid_token)) | ||
response = self.client.post(url, {}, format='json') | ||
self.assertEqual(response.status_code, 401) | ||
self.assertEqual(response.data, {"detail": "Invalid token."}) | ||
|