forked from hiidef/oauth2app
/
base.py
72 lines (64 loc) · 2.33 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#-*- coding: utf-8 -*-
try: import simplejson as json
except ImportError: import json
try:
from django.contrib.auth import get_user_model # Django 1.5+
User = get_user_model()
except:
from django.contrib.auth.models import User
from oauth2app.models import Client
from django.test.client import Client as DjangoTestClient
from django.utils import unittest
from base64 import b64encode
from urlparse import urlparse, parse_qs
from urllib import urlencode
USER_USERNAME = "testuser"
USER_PASSWORD = "testpassword"
USER_EMAIL = "user@example.com"
USER_FIRSTNAME = "Foo"
USER_LASTNAME = "Bar"
CLIENT_USERNAME = "client"
CLIENT_EMAIL = "client@example.com"
REDIRECT_URI = "http://example.com/callback"
class BaseTestCase(unittest.TestCase):
user = None
client_holder = None
client_application = None
def setUp(self):
self.user = User.objects.create_user(
USER_USERNAME,
USER_EMAIL,
USER_PASSWORD)
self.user.first_name = USER_FIRSTNAME
self.user.last_name = USER_LASTNAME
self.user.save()
self.client = User.objects.create_user(CLIENT_USERNAME, CLIENT_EMAIL)
self.client_application = Client.objects.create(
name="TestApplication",
user=self.client)
def tearDown(self):
self.user.delete()
self.client.delete()
self.client_application.delete()
def get_token(self):
user = DjangoTestClient()
user.login(username=USER_USERNAME, password=USER_PASSWORD)
parameters = {
"client_id":self.client_application.key,
"redirect_uri":REDIRECT_URI,
"response_type":"code"}
response = user.get("/oauth2/authorize_no_scope?%s" % urlencode(parameters))
qs = parse_qs(urlparse(response['location']).query)
code = qs['code']
client = DjangoTestClient()
parameters = {
"client_id":self.client_application.key,
"grant_type":"authorization_code",
"code":code,
"redirect_uri":REDIRECT_URI}
basic_auth = b64encode("%s:%s" % (self.client_application.key, self.client_application.secret))
response = client.get(
"/oauth2/token",
parameters,
HTTP_AUTHORIZATION="Basic %s" % basic_auth)
return json.loads(response.content)["access_token"]