|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +import datetime |
| 4 | + |
| 5 | +from django.test import TestCase, RequestFactory |
| 6 | +from django.core.urlresolvers import reverse |
| 7 | +from django.utils import timezone |
| 8 | + |
| 9 | +from ..compat import urlencode, get_user_model |
| 10 | +from ..models import get_application_model, AccessToken, RefreshToken |
| 11 | +from ..settings import oauth2_settings |
| 12 | + |
| 13 | +from .test_utils import TestCaseUtils |
| 14 | + |
| 15 | + |
| 16 | +Application = get_application_model() |
| 17 | +UserModel = get_user_model() |
| 18 | + |
| 19 | + |
| 20 | +class BaseTest(TestCaseUtils, TestCase): |
| 21 | + def setUp(self): |
| 22 | + self.factory = RequestFactory() |
| 23 | + self. test_user = UserModel. objects. create_user( "test_user", "[email protected]", "123456") |
| 24 | + self. dev_user = UserModel. objects. create_user( "dev_user", "[email protected]", "123456") |
| 25 | + |
| 26 | + self.application = Application( |
| 27 | + name="Test Application", |
| 28 | + redirect_uris="http://localhost http://example.com http://example.it", |
| 29 | + user=self.dev_user, |
| 30 | + client_type=Application.CLIENT_CONFIDENTIAL, |
| 31 | + authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE, |
| 32 | + ) |
| 33 | + self.application.save() |
| 34 | + |
| 35 | + oauth2_settings._SCOPES = ['read', 'write'] |
| 36 | + |
| 37 | + def tearDown(self): |
| 38 | + self.application.delete() |
| 39 | + self.test_user.delete() |
| 40 | + self.dev_user.delete() |
| 41 | + |
| 42 | + |
| 43 | +class TestRevocationView(BaseTest): |
| 44 | + def test_revoke_access_token(self): |
| 45 | + """ |
| 46 | +
|
| 47 | + """ |
| 48 | + tok = AccessToken.objects.create(user=self.test_user, token='1234567890', |
| 49 | + application=self.application, |
| 50 | + expires=timezone.now()+datetime.timedelta(days=1), |
| 51 | + scope='read write') |
| 52 | + query_string = urlencode({ |
| 53 | + 'client_id': self.application.client_id, |
| 54 | + 'client_secret': self.application.client_secret, |
| 55 | + 'token': tok.token, |
| 56 | + }) |
| 57 | + url = "{url}?{qs}".format(url=reverse('oauth2_provider:revoke-token'), qs=query_string) |
| 58 | + response = self.client.post(url) |
| 59 | + self.assertEqual(response.status_code, 200) |
| 60 | + self.assertFalse(AccessToken.objects.filter(id=tok.id).exists()) |
| 61 | + |
| 62 | + def test_revoke_access_token_with_hint(self): |
| 63 | + """ |
| 64 | +
|
| 65 | + """ |
| 66 | + tok = AccessToken.objects.create(user=self.test_user, token='1234567890', |
| 67 | + application=self.application, |
| 68 | + expires=timezone.now()+datetime.timedelta(days=1), |
| 69 | + scope='read write') |
| 70 | + query_string = urlencode({ |
| 71 | + 'client_id': self.application.client_id, |
| 72 | + 'client_secret': self.application.client_secret, |
| 73 | + 'token': tok.token, |
| 74 | + 'token_type_hint': 'access_token' |
| 75 | + }) |
| 76 | + url = "{url}?{qs}".format(url=reverse('oauth2_provider:revoke-token'), qs=query_string) |
| 77 | + response = self.client.post(url) |
| 78 | + self.assertEqual(response.status_code, 200) |
| 79 | + self.assertFalse(AccessToken.objects.filter(id=tok.id).exists()) |
| 80 | + |
| 81 | + def test_revoke_access_token_with_invalid_hint(self): |
| 82 | + """ |
| 83 | +
|
| 84 | + """ |
| 85 | + tok = AccessToken.objects.create(user=self.test_user, token='1234567890', |
| 86 | + application=self.application, |
| 87 | + expires=timezone.now()+datetime.timedelta(days=1), |
| 88 | + scope='read write') |
| 89 | + # invalid hint should have no effect |
| 90 | + query_string = urlencode({ |
| 91 | + 'client_id': self.application.client_id, |
| 92 | + 'client_secret': self.application.client_secret, |
| 93 | + 'token': tok.token, |
| 94 | + 'token_type_hint': 'bad_hint' |
| 95 | + }) |
| 96 | + url = "{url}?{qs}".format(url=reverse('oauth2_provider:revoke-token'), qs=query_string) |
| 97 | + response = self.client.post(url) |
| 98 | + self.assertEqual(response.status_code, 200) |
| 99 | + self.assertFalse(AccessToken.objects.filter(id=tok.id).exists()) |
| 100 | + |
| 101 | + def test_revoke_refresh_token(self): |
| 102 | + """ |
| 103 | +
|
| 104 | + """ |
| 105 | + tok = AccessToken.objects.create(user=self.test_user, token='1234567890', |
| 106 | + application=self.application, |
| 107 | + expires=timezone.now()+datetime.timedelta(days=1), |
| 108 | + scope='read write') |
| 109 | + rtok = RefreshToken.objects.create(user=self.test_user, token='999999999', |
| 110 | + application=self.application, access_token=tok) |
| 111 | + query_string = urlencode({ |
| 112 | + 'client_id': self.application.client_id, |
| 113 | + 'client_secret': self.application.client_secret, |
| 114 | + 'token': rtok.token, |
| 115 | + }) |
| 116 | + url = "{url}?{qs}".format(url=reverse('oauth2_provider:revoke-token'), qs=query_string) |
| 117 | + response = self.client.post(url) |
| 118 | + self.assertEqual(response.status_code, 200) |
| 119 | + self.assertFalse(RefreshToken.objects.filter(id=rtok.id).exists()) |
0 commit comments