|
5 | 5 | from django.contrib.contenttypes.models import ContentType
|
6 | 6 | from django.test import TestCase
|
7 | 7 |
|
| 8 | +from ..settings import oauth2_settings |
8 | 9 | from ..models import get_application_model
|
9 | 10 | from ..compat import get_user_model
|
10 | 11 |
|
@@ -46,6 +47,58 @@ def test_application_registration_user(self):
|
46 | 47 | self.assertEqual(app.user.username, "foo_user")
|
47 | 48 |
|
48 | 49 |
|
| 50 | +class TestApplicationRegistrationViewPermissions(BaseTest): |
| 51 | + def setUp(self): |
| 52 | + super(TestApplicationRegistrationViewPermissions, self).setUp() |
| 53 | + oauth2_settings.APPLICATION_REGISTRATION_PERMISSIONS = { |
| 54 | + 'all': ('oauth2_provider.add_application', ), |
| 55 | + } |
| 56 | + |
| 57 | + def tearDown(self): |
| 58 | + super(TestApplicationRegistrationViewPermissions, self).tearDown() |
| 59 | + oauth2_settings.APPLICATION_REGISTRATION_PERMISSIONS = None |
| 60 | + |
| 61 | + def test_application_registration_user_with_permission(self): |
| 62 | + self.client.login(username="foo_user", password="123456") |
| 63 | + |
| 64 | + form_data = { |
| 65 | + 'name': 'Foo app', |
| 66 | + 'client_id': 'client_id', |
| 67 | + 'client_secret': 'client_secret', |
| 68 | + 'client_type': Application.CLIENT_CONFIDENTIAL, |
| 69 | + 'redirect_uris': 'http://example.com', |
| 70 | + 'authorization_grant_type': Application.GRANT_AUTHORIZATION_CODE |
| 71 | + } |
| 72 | + |
| 73 | + response = self.client.post(reverse('oauth2_provider:register'), form_data) |
| 74 | + self.assertEqual(response.status_code, 302) |
| 75 | + self.assertEqual(response.has_header('Location'), True) |
| 76 | + self.assertEqual(response['Location'].endswith("/o/applications/1/"), True) |
| 77 | + |
| 78 | + app = Application.objects.get(name="Foo app") |
| 79 | + self.assertEqual(app.user.username, "foo_user") |
| 80 | + |
| 81 | + def test_application_registration_user_without_permission(self): |
| 82 | + self.client.login(username="bar_user", password="123456") |
| 83 | + |
| 84 | + form_data = { |
| 85 | + 'name': 'Bar app', |
| 86 | + 'client_id': 'client_id', |
| 87 | + 'client_secret': 'client_secret', |
| 88 | + 'client_type': Application.CLIENT_CONFIDENTIAL, |
| 89 | + 'redirect_uris': 'http://example.com', |
| 90 | + 'authorization_grant_type': Application.GRANT_AUTHORIZATION_CODE |
| 91 | + } |
| 92 | + |
| 93 | + response = self.client.post(reverse('oauth2_provider:register'), form_data) |
| 94 | + self.assertEqual(response.status_code, 302) |
| 95 | + self.assertEqual(response.has_header('Location'), True) |
| 96 | + self.assertEqual(response['Location'].endswith("/accounts/login/?next=/o/applications/register/"), True) |
| 97 | + |
| 98 | + with self.assertRaises(Application.DoesNotExist): |
| 99 | + app = Application.objects.get(name="Bar app") |
| 100 | + |
| 101 | + |
49 | 102 | class TestApplicationViews(BaseTest):
|
50 | 103 | def _create_application(self, name, user):
|
51 | 104 | app = Application.objects.create(
|
|
0 commit comments