Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 920e07e

Browse files
committed
authorization api tests and minor fixes
1 parent dbfe8c1 commit 920e07e

File tree

6 files changed

+149
-62
lines changed

6 files changed

+149
-62
lines changed

auth0/v2/authentication/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from .database import Database
22
from .delegated import Delegated
33
from .enterprise import Enterprise
4-
from .link import Link
54
from .passwordless import Passwordless
65
from .social import Social
76
from .users import Users

auth0/v2/authentication/authorize_client.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@ def __init__(self, domain):
1313
self.domain = domain
1414

1515
def authorize(self, client_id, audience=None, state=None, redirect_uri=None,
16-
response_type='code', scope='openid'):
16+
response_type='code', scope='openid'):
1717
"""Authorization code grant
1818
1919
This is the OAuth 2.0 grant that regular web apps utilize in order to access an API.
2020
"""
21+
params = {
22+
'client_id': client_id,
23+
'audience': audience,
24+
'response_type': response_type,
25+
'scope': scope,
26+
'state': state,
27+
'redirect_uri': redirect_uri
28+
}
2129

2230
return self.get(
2331
'https://%s/authorize' % self.domain,
24-
data={
25-
'client_id': client_id,
26-
'audience': audience,
27-
'response_type': response_type,
28-
'scope': scope,
29-
'state': state,
30-
'redirect_uri': redirect_uri,
31-
},
32-
headers={'Content-Type': 'application/json'}
33-
)
32+
params=params)
3433

auth0/v2/authentication/get_token.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ def __init__(self, domain):
1313
self.domain = domain
1414

1515
def authorization_code(self, client_id, client_secret, code,
16-
grant_type='authorization_code', redirect_uri):
16+
redirect_uri, grant_type='authorization_code'):
1717
"""Authorization code grant
18-
19-
This is the OAuth 2.0 grant that regular web apps utilize in order
20-
to access an API. Use this endpoint to exchange an Authorization Code
18+
19+
This is the OAuth 2.0 grant that regular web apps utilize in order
20+
to access an API. Use this endpoint to exchange an Authorization Code
2121
for a Token.
2222
2323
Args:
@@ -27,10 +27,10 @@ def authorization_code(self, client_id, client_secret, code,
2727
client_id (str): your application's client Id
2828
2929
client_secret (str): you application's client Secret
30-
30+
3131
code (str): The Authorization Code received from the /authorize Calls
3232
33-
redirect_uri (srt, optional): This is required only if it was set at
33+
redirect_uri (srt, optional): This is required only if it was set at
3434
the GET /authorize endpoint. The values must match
3535
3636
Returns:
@@ -50,12 +50,12 @@ def authorization_code(self, client_id, client_secret, code,
5050
)
5151

5252
def client_credentials(self, client_id, client_secret, audience,
53-
grant_type='client_credentials'):
53+
grant_type='client_credentials'):
5454
"""Client credentials grant
55-
56-
This is the OAuth 2.0 grant that server processes utilize in
57-
order to access an API. Use this endpoint to directly request
58-
an access_token by using the Client Credentials (a Client Id and
55+
56+
This is the OAuth 2.0 grant that server processes utilize in
57+
order to access an API. Use this endpoint to directly request
58+
an access_token by using the Client Credentials (a Client Id and
5959
a Client Secret).
6060
6161
Args:
@@ -65,7 +65,7 @@ def client_credentials(self, client_id, client_secret, audience,
6565
client_id (str): your application's client Id
6666
6767
client_secret (str): you application's client Secret
68-
68+
6969
audience (str): The unique identifier of the target API you want to access.
7070
7171
Returns:
@@ -81,18 +81,18 @@ def client_credentials(self, client_id, client_secret, audience,
8181
'grant_type': grant_type,
8282
},
8383
headers={'Content-Type': 'application/json'}
84-
)
84+
)
8585

86-
def login(self, client_id, client_secret, username, password, scope, realm
87-
audience, grant_type='http://auth0.com/oauth/grant-type/password-realm'):
86+
def login(self, client_id, client_secret, username, password, scope, realm,
87+
audience, grant_type='http://auth0.com/oauth/grant-type/password-realm'):
8888
"""Calls oauth/token endpoint with password-realm grant type
8989
9090
91-
This is the OAuth 2.0 grant that highly trusted apps utilize in order
92-
to access an API. In this flow the end-user is asked to fill in credentials
93-
(username/password) typically using an interactive form in the user-agent
94-
(browser). This information is later on sent to the client and Auth0.
95-
It is therefore imperative that the client is absolutely trusted with
91+
This is the OAuth 2.0 grant that highly trusted apps utilize in order
92+
to access an API. In this flow the end-user is asked to fill in credentials
93+
(username/password) typically using an interactive form in the user-agent
94+
(browser). This information is later on sent to the client and Auth0.
95+
It is therefore imperative that the client is absolutely trusted with
9696
this information.
9797
9898
Args:
@@ -102,20 +102,19 @@ def login(self, client_id, client_secret, username, password, scope, realm
102102
client_id (str): your application's client Id
103103
104104
client_secret (str): you application's client Secret
105-
105+
106106
audience (str): The unique identifier of the target API you want to access.
107107
108108
username (str): Resource owner's identifier
109109
110110
password (str): resource owner's Secret
111-
112-
scope(srt): String value of the different scopes the client is asking for.
111+
112+
scope(srt): String value of the different scopes the client is asking for.
113113
Multiple scopes are separated with whitespace.
114114
115-
realm (str): String value of the realm the user belongs.
115+
realm (str): String value of the realm the user belongs.
116116
Set this if you want to add realm support at this grant.
117117
118-
119118
Returns:
120119
access_token, id_token
121120
"""
@@ -134,4 +133,3 @@ def login(self, client_id, client_secret, username, password, scope, realm
134133
},
135134
headers={'Content-Type': 'application/json'}
136135
)
137-
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unittest
2+
import mock
3+
from ...authentication.authorize_client import AuthorizeClient
4+
5+
6+
class TestAuthorizeClient(unittest.TestCase):
7+
8+
@mock.patch('auth0.v2.authentication.authorize_client.AuthorizeClient.get')
9+
def test_login(self, mock_get):
10+
11+
a = AuthorizeClient('my.domain.com')
12+
13+
a.authorize(client_id='cid',
14+
audience='https://test.com/api',
15+
state='st',
16+
redirect_uri='http://localhost',
17+
response_type='token',
18+
scope='openid profile')
19+
20+
args, kwargs = mock_get.call_args
21+
22+
self.assertEqual(args[0], 'https://my.domain.com/authorize')
23+
self.assertEqual(kwargs['params'], {
24+
'client_id': 'cid',
25+
'audience': 'https://test.com/api',
26+
'state': 'st',
27+
'redirect_uri': 'http://localhost',
28+
'response_type': 'token',
29+
'scope': 'openid profile'
30+
})
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import unittest
2+
import mock
3+
from ...authentication.get_token import GetToken
4+
5+
6+
class TestGetToken(unittest.TestCase):
7+
8+
@mock.patch('auth0.v2.authentication.get_token.GetToken.post')
9+
def test_authorization_code(self, mock_post):
10+
11+
g = GetToken('my.domain.com')
12+
13+
g.authorization_code(client_id='cid',
14+
client_secret='clsec',
15+
code='cd',
16+
grant_type='gt',
17+
redirect_uri='idt')
18+
19+
args, kwargs = mock_post.call_args
20+
21+
self.assertEqual(args[0], 'https://my.domain.com/oauth/token')
22+
self.assertEqual(kwargs['data'], {
23+
'client_id': 'cid',
24+
'client_secret': 'clsec',
25+
'code': 'cd',
26+
'grant_type': 'gt',
27+
'redirect_uri': 'idt'
28+
})
29+
self.assertEqual(kwargs['headers'], {
30+
'Content-Type': 'application/json'
31+
})
32+
33+
@mock.patch('auth0.v2.authentication.get_token.GetToken.post')
34+
def test_client_credentials(self, mock_post):
35+
36+
g = GetToken('my.domain.com')
37+
38+
g.client_credentials(client_id='cid',
39+
client_secret='clsec',
40+
audience='aud',
41+
grant_type='gt')
42+
43+
args, kwargs = mock_post.call_args
44+
45+
self.assertEqual(args[0], 'https://my.domain.com/oauth/token')
46+
self.assertEqual(kwargs['data'], {
47+
'client_id': 'cid',
48+
'client_secret': 'clsec',
49+
'audience': 'aud',
50+
'grant_type': 'gt'
51+
})
52+
self.assertEqual(kwargs['headers'], {
53+
'Content-Type': 'application/json'
54+
})
55+
56+
@mock.patch('auth0.v2.authentication.get_token.GetToken.post')
57+
def test_login(self, mock_post):
58+
59+
g = GetToken('my.domain.com')
60+
61+
g.login(client_id='cid',
62+
client_secret='clsec',
63+
username='usrnm',
64+
password='pswd',
65+
scope='http://test.com/api',
66+
realm='rlm',
67+
audience='aud',
68+
grant_type='gt')
69+
70+
args, kwargs = mock_post.call_args
71+
72+
self.assertEqual(args[0], 'https://my.domain.com/oauth/token')
73+
self.assertEqual(kwargs['data'], {
74+
'client_id': 'cid',
75+
'client_secret': 'clsec',
76+
'username':'usrnm',
77+
'password':'pswd',
78+
'scope':'http://test.com/api',
79+
'realm':'rlm',
80+
'audience': 'aud',
81+
'grant_type': 'gt'
82+
})
83+
self.assertEqual(kwargs['headers'], {
84+
'Content-Type': 'application/json'
85+
})

auth0/v2/test/authentication/test_link.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)