forked from jpadilla/django-rest-framework-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
APDS-61 - [BE] add API to list and delete permanent tokens #1
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e97ce57
APDS-61 - add API to list and delete permanent tokens
poxip 2336e40
CR
poxip fa4b735
Use router
poxip 08ef62d
Revert tox.ini
poxip 7ef94d3
Revert Readme
poxip 0473d40
Use format='json' in requests
poxip 0d40e85
Modify client.login() call
poxip 3591e39
Add auto_now to Device.last_request_datetime
poxip cc6d0b9
Revert .travis.yml
poxip 65e0118
Tox: remove py33 and add py36
poxip 3a53d9f
tox: revert and add Python 3.3
poxip d2831b0
Do not test under Python 3.3
poxip ce4dc3f
CR
poxip 7ba2cf6
APDS-65 - [BE] modify login API to generate permanent token (#2)
poxip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.3 on 2017-07-21 08:06 | ||
from __future__ import unicode_literals | ||
import uuid | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Device', | ||
fields=[ | ||
('permanent_token', models.CharField(max_length=255, unique=True, serialize=False)), | ||
('jwt_secret', models.UUIDField(default=uuid.uuid4, editable=False)), | ||
('created', models.DateTimeField(auto_now_add=True)), | ||
('name', models.CharField(max_length=255, verbose_name='Device name')), | ||
('details', models.CharField(blank=True, max_length=255, verbose_name='Device details')), | ||
('last_request_datetime', models.DateTimeField(auto_now=True)), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
Empty file.
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 |
---|---|---|
@@ -1 +1,18 @@ | ||
# Just to keep things like ./manage.py test happy | ||
import uuid | ||
|
||
from django.conf import settings | ||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
class Device(models.Model): | ||
""" | ||
Device model used for permanent token authentication | ||
""" | ||
permanent_token = models.CharField(max_length=255, unique=True) | ||
jwt_secret = models.UUIDField(default=uuid.uuid4, editable=False) | ||
created = models.DateTimeField(auto_now_add=True) | ||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) | ||
name = models.CharField(_('Device name'), max_length=255) | ||
details = models.CharField(_('Device details'), max_length=255, blank=True) | ||
last_request_datetime = models.DateTimeField(auto_now=True) | ||
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
from rest_framework_jwt import utils, views | ||
from rest_framework_jwt.compat import get_user_model | ||
from rest_framework_jwt.models import Device | ||
from rest_framework_jwt.settings import api_settings, DEFAULTS | ||
|
||
from . import utils as test_utils | ||
|
@@ -492,3 +493,46 @@ def test_refresh_jwt_after_refresh_expiration(self): | |
def tearDown(self): | ||
# Restore original settings | ||
api_settings.JWT_ALLOW_REFRESH = DEFAULTS['JWT_ALLOW_REFRESH'] | ||
|
||
|
||
class DeviceViewTests(TokenTestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add devices for not your user to make sure we are not showing it and can't delete. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in the CR commit. |
||
def setUp(self): | ||
super(DeviceViewTests, self).setUp() | ||
self.device = Device.objects.create( | ||
user=self.user, permanent_token='somestring2', name='Android', | ||
last_request_datetime=datetime.now()) | ||
self.user2 = User.objects.create_user(email='[email protected]', username='jsmith', password='password') | ||
self.device2 = Device.objects.create( | ||
user=self.user2, permanent_token='somestring98', name='Android', | ||
last_request_datetime=datetime.now()) | ||
|
||
def _login(self, client): | ||
client.credentials(HTTP_AUTHORIZATION='JWT {}'.format(self.get_token())) | ||
return client.login(**self.data) | ||
|
||
def test_device_delete(self): | ||
client = APIClient(enforce_csrf_checks=True) | ||
# test accessing without being logged in | ||
response = client.delete('/devices/{}/'.format(self.device.id)) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
self._login(client) | ||
# try removing device linked to other user | ||
response = client.delete('/devices/{}/'.format(self.device2.id)) | ||
self.assertEqual(response.status_code, 404) | ||
# test regular case | ||
self.assertEqual(Device.objects.filter(id=self.device.id).count(), 1) | ||
response = client.delete('/devices/{}/'.format(self.device.id)) | ||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(Device.objects.filter(id=self.device.id).count(), 0) | ||
|
||
def test_device_list(self): | ||
client = APIClient(enforce_csrf_checks=True) | ||
self._login(client) | ||
response = client.get('/devices/', format='json') | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(len(response.data), 1) | ||
self.assertEqual(set(response.data[0].keys()), { | ||
'id', 'created', 'name', 'details', 'last_request_datetime' | ||
}) | ||
self.assertEqual(response.data[0]['id'], self.device.id) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing empty line at the end of file.
Pylint and pep8 will complain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub does not show the last empty line as far as I remember. The flake8 task in tox did not complain, see: https://travis-ci.org/ArabellaTech/django-rest-framework-jwt/jobs/256993179