Advertisement
Guest User

Untitled

a guest
May 17th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. urlpatterns = [
  2. url(r'^user-id/$', views.UserIDView.as_view(), name='user-id'),
  3. ...
  4.  
  5. from oauth2_provider.ext.rest_framework import TokenHasReadWriteScope
  6.  
  7. class StdPerm(TokenHasReadWriteScope):
  8. pass
  9.  
  10. StdPermClasses = (IsAuthenticated, StdPerm)
  11.  
  12. class UserIDView(APIView):
  13. permission_classes = StdPermClasses
  14. renderer_classes = (JSONRenderer,)
  15. def get(self, request, format=None):
  16. return Response({'id': request.user.id})
  17.  
  18. from django.contrib.auth.models import User
  19. from django.core.urlresolvers import reverse
  20. from rest_framework import status
  21. from rest_framework.test import APITestCase
  22.  
  23. class CreateUserTest(APITestCase):
  24. def setUp(self):
  25. self.user = User.objects.create_user('daniel', 'daniel@test.com',
  26. password='daniel')
  27. self.user.save()
  28. def test_get_user_id(self):
  29. self.client.login(username='daniel', password='daniel')
  30. self.client.force_authenticate(user=self.user)
  31. response = self.client.get(reverse('user-id'))
  32. self.assertEqual(response.status_code, status.HTTP_200_OK)
  33.  
  34. curl -X GET "http://127.0.0.1:8000/user-id/" -H "Authorization: Bearer b3SlzXlpRSxURyh2BltwdHmhrlqNyt"
  35.  
  36. token = Token.objects.create(user=self.user)
  37. self.client.force_authenticate(user=self.user, token=token)
  38.  
  39. assert False, ('TokenHasScope requires either the' AssertionError:
  40. TokenHasScope requires either the`oauth2_provider.rest_framework
  41. .OAuth2Authentication` authentication class to be used.
  42.  
  43. app = Application(
  44. client_type='confidential',
  45. authorization_grant_type='password',
  46. name='MyAppTest',
  47. user_id=1
  48. )
  49. app.save()
  50.  
  51. app = Application.objects.get(name='MyAppTest')
  52. token = generate_token()
  53. expires = now() + timedelta(seconds=oauth2_settings.
  54. ACCESS_TOKEN_EXPIRE_SECONDS)
  55. scope = 'read write'
  56. access_token = AccessToken.objects.create(
  57. user=self.user,
  58. application=app,
  59. expires=expires,
  60. token=token,
  61. scope=scope
  62. )
  63.  
  64. self.client.force_authenticate(user=self.user, token=access_token)
  65.  
  66. from django.contrib.auth.models import User
  67. from django.core.urlresolvers import reverse
  68. from rest_framework import status
  69. from rest_framework.test import APITestCase
  70. from oauth2_provider.settings import oauth2_settings
  71. from oauthlib.common import generate_token
  72. from oauth2_provider.models import AccessToken, Application
  73. from django.utils.timezone import now, timedelta
  74.  
  75. from oauth2_provider.settings import oauth2_settings
  76. from oauth2_provider.models import get_access_token_model,
  77. get_application_model
  78. from django.contrib.auth import get_user_model
  79. from django.utils import timezone
  80. from rest_framework.test import APITestCase
  81.  
  82. Application = get_application_model()
  83. AccessToken = get_access_token_model()
  84. UserModel = get_user_model()
  85.  
  86. class Test_mytest(APITestCase):
  87.  
  88. def setUp(self):
  89.  
  90. oauth2_settings._SCOPES = ["read", "write", "scope1", "scope2", "resource1"]
  91.  
  92. self.test_user = UserModel.objects.create_user("test_user", "test@example.com", "123456")
  93.  
  94. self.application = Application.objects.create(
  95. name="Test Application",
  96. redirect_uris="http://localhost http://example.com http://example.org",
  97. user=self.test_user,
  98. client_type=Application.CLIENT_CONFIDENTIAL,
  99. authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
  100. )
  101.  
  102. self.access_token = AccessToken.objects.create(
  103. user=self.test_user,
  104. scope="read write",
  105. expires=timezone.now() + timezone.timedelta(seconds=300),
  106. token="secret-access-token-key",
  107. application=self.application
  108. )
  109. # read or write as per your choice
  110. self.access_token.scope = "read"
  111. self.access_token.save()
  112.  
  113. # correct token and correct scope
  114. self.auth = "Bearer {0}".format(self.access_token.token)
  115.  
  116. def test_success_response(self):
  117.  
  118. url = reverse('my_url',)
  119.  
  120. # Obtaining the POST response for the input data
  121. response = self.client.get(url, HTTP_AUTHORIZATION=self.auth)
  122.  
  123. # checking wether the response is success
  124. self.assertEqual(response.status_code, status.HTTP_200_OK)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement