Add tests for userinfo claims.
This commit is contained in:
parent
5020ccde80
commit
18b492d1db
3 changed files with 48 additions and 2 deletions
|
@ -58,3 +58,4 @@ TEMPLATE_DIRS = (
|
|||
|
||||
SITE_URL = 'http://localhost:8000'
|
||||
OIDC_RSA_KEY_FOLDER = os.path.dirname(__file__)
|
||||
OIDC_USERINFO = 'oidc_provider.tests.app.utils.FakeUserInfo'
|
||||
|
|
|
@ -8,6 +8,7 @@ from oidc_provider.models import *
|
|||
|
||||
FAKE_NONCE = 'cb584e44c43ed6bd0bc2d9c7e242837d'
|
||||
|
||||
|
||||
def create_fake_user():
|
||||
"""
|
||||
Create a test user.
|
||||
|
@ -23,6 +24,7 @@ def create_fake_user():
|
|||
|
||||
return user
|
||||
|
||||
|
||||
def create_fake_client(response_type):
|
||||
"""
|
||||
Create a test client, response_type argument MUST be:
|
||||
|
@ -41,6 +43,7 @@ def create_fake_client(response_type):
|
|||
|
||||
return client
|
||||
|
||||
|
||||
def is_code_valid(url, user, client):
|
||||
"""
|
||||
Check if the code inside the url is valid.
|
||||
|
@ -56,3 +59,23 @@ def is_code_valid(url, user, client):
|
|||
is_code_ok = False
|
||||
|
||||
return is_code_ok
|
||||
|
||||
|
||||
class FakeUserInfo(object):
|
||||
|
||||
given_name = 'John'
|
||||
family_name = 'Doe'
|
||||
nickname = 'johndoe'
|
||||
website = 'http://johndoe.com'
|
||||
|
||||
phone_number = '+49-89-636-48018'
|
||||
phone_number_verified = True
|
||||
|
||||
address_street_address = 'Evergreen 742'
|
||||
address_locality = 'Glendive'
|
||||
address_region = 'Montana'
|
||||
address_country = 'United States'
|
||||
|
||||
@classmethod
|
||||
def get_by_user(cls, user):
|
||||
return cls()
|
||||
|
|
|
@ -22,7 +22,7 @@ class UserInfoTestCase(TestCase):
|
|||
self.user = create_fake_user()
|
||||
self.client = create_fake_client(response_type='code')
|
||||
|
||||
def _create_token(self):
|
||||
def _create_token(self, extra_scope=[]):
|
||||
"""
|
||||
Generate a valid token.
|
||||
"""
|
||||
|
@ -33,7 +33,7 @@ class UserInfoTestCase(TestCase):
|
|||
user=self.user,
|
||||
client=self.client,
|
||||
id_token_dic=id_token_dic,
|
||||
scope=['openid', 'email'])
|
||||
scope=['openid', 'email'] + extra_scope)
|
||||
token.save()
|
||||
|
||||
return token
|
||||
|
@ -114,3 +114,25 @@ class UserInfoTestCase(TestCase):
|
|||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(bool(response.content), True)
|
||||
|
||||
def test_user_claims_in_response(self):
|
||||
token = self._create_token(extra_scope=['profile'])
|
||||
response = self._post_request(token.access_token)
|
||||
response_dic = json.loads(response.content.decode('utf-8'))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(bool(response.content), True)
|
||||
self.assertEqual('given_name' in response_dic, True,
|
||||
msg='"given_name" claim should be in response.')
|
||||
self.assertEqual('profile' in response_dic, False,
|
||||
msg='"profile" claim should not be in response.')
|
||||
|
||||
# Now adding `address` scope.
|
||||
token = self._create_token(extra_scope=['profile', 'address'])
|
||||
response = self._post_request(token.access_token)
|
||||
response_dic = json.loads(response.content.decode('utf-8'))
|
||||
|
||||
self.assertEqual('address' in response_dic, True,
|
||||
msg='"address" claim should be in response.')
|
||||
self.assertEqual('country' in response_dic['address'], True,
|
||||
msg='"country" claim should be in response.')
|
||||
|
|
Loading…
Reference in a new issue