Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- urlpatterns = [
- url(r'^user-id/$', views.UserIDView.as_view(), name='user-id'),
- ...
- from oauth2_provider.ext.rest_framework import TokenHasReadWriteScope
- class StdPerm(TokenHasReadWriteScope):
- pass
- StdPermClasses = (IsAuthenticated, StdPerm)
- class UserIDView(APIView):
- permission_classes = StdPermClasses
- renderer_classes = (JSONRenderer,)
- def get(self, request, format=None):
- return Response({'id': request.user.id})
- from django.contrib.auth.models import User
- from django.core.urlresolvers import reverse
- from rest_framework import status
- from rest_framework.test import APITestCase
- class CreateUserTest(APITestCase):
- def setUp(self):
- self.user = User.objects.create_user('daniel', 'daniel@test.com',
- password='daniel')
- self.user.save()
- def test_get_user_id(self):
- self.client.login(username='daniel', password='daniel')
- self.client.force_authenticate(user=self.user)
- response = self.client.get(reverse('user-id'))
- self.assertEqual(response.status_code, status.HTTP_200_OK)
- curl -X GET "http://127.0.0.1:8000/user-id/" -H "Authorization: Bearer b3SlzXlpRSxURyh2BltwdHmhrlqNyt"
- token = Token.objects.create(user=self.user)
- self.client.force_authenticate(user=self.user, token=token)
- assert False, ('TokenHasScope requires either the' AssertionError:
- TokenHasScope requires either the`oauth2_provider.rest_framework
- .OAuth2Authentication` authentication class to be used.
- app = Application(
- client_type='confidential',
- authorization_grant_type='password',
- name='MyAppTest',
- user_id=1
- )
- app.save()
- app = Application.objects.get(name='MyAppTest')
- token = generate_token()
- expires = now() + timedelta(seconds=oauth2_settings.
- ACCESS_TOKEN_EXPIRE_SECONDS)
- scope = 'read write'
- access_token = AccessToken.objects.create(
- user=self.user,
- application=app,
- expires=expires,
- token=token,
- scope=scope
- )
- self.client.force_authenticate(user=self.user, token=access_token)
- from django.contrib.auth.models import User
- from django.core.urlresolvers import reverse
- from rest_framework import status
- from rest_framework.test import APITestCase
- from oauth2_provider.settings import oauth2_settings
- from oauthlib.common import generate_token
- from oauth2_provider.models import AccessToken, Application
- from django.utils.timezone import now, timedelta
- from oauth2_provider.settings import oauth2_settings
- from oauth2_provider.models import get_access_token_model,
- get_application_model
- from django.contrib.auth import get_user_model
- from django.utils import timezone
- from rest_framework.test import APITestCase
- Application = get_application_model()
- AccessToken = get_access_token_model()
- UserModel = get_user_model()
- class Test_mytest(APITestCase):
- def setUp(self):
- oauth2_settings._SCOPES = ["read", "write", "scope1", "scope2", "resource1"]
- self.test_user = UserModel.objects.create_user("test_user", "test@example.com", "123456")
- self.application = Application.objects.create(
- name="Test Application",
- redirect_uris="http://localhost http://example.com http://example.org",
- user=self.test_user,
- client_type=Application.CLIENT_CONFIDENTIAL,
- authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
- )
- self.access_token = AccessToken.objects.create(
- user=self.test_user,
- scope="read write",
- expires=timezone.now() + timezone.timedelta(seconds=300),
- token="secret-access-token-key",
- application=self.application
- )
- # read or write as per your choice
- self.access_token.scope = "read"
- self.access_token.save()
- # correct token and correct scope
- self.auth = "Bearer {0}".format(self.access_token.token)
- def test_success_response(self):
- url = reverse('my_url',)
- # Obtaining the POST response for the input data
- response = self.client.get(url, HTTP_AUTHORIZATION=self.auth)
- # checking wether the response is success
- self.assertEqual(response.status_code, status.HTTP_200_OK)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement