django-oidc-provider/oidc_provider/tests/utils.py

51 lines
1.1 KiB
Python
Raw Normal View History

2015-02-11 18:37:51 +00:00
from django.contrib.auth.models import User
2015-02-18 18:07:22 +00:00
from oidc_provider.models import *
2015-02-11 18:37:51 +00:00
def create_fake_user():
"""
Create a test user.
2015-02-11 18:37:51 +00:00
Return a User object.
"""
user = User()
user.username = 'johndoe'
user.email = 'johndoe@example.com'
user.set_password('1234')
2015-02-11 18:37:51 +00:00
user.save()
2015-02-11 18:37:51 +00:00
return user
2015-02-11 18:37:51 +00:00
def create_fake_client(response_type):
"""
Create a test client, response_type argument MUST be:
'code', 'id_token' or 'id_token token'.
Return a Client object.
"""
client = Client()
client.name = 'Some Client'
client.client_id = '123'
client.client_secret = '456'
client.response_type = response_type
client.redirect_uris = ['http://example.com/']
client.save()
return client
def is_code_valid(url, user, client):
"""
Check if the code inside the url is valid.
"""
try:
code = (url.split('code='))[1].split('&')[0]
code = Code.objects.get(code=code)
is_code_ok = (code.client == client) and \
(code.user == user)
except:
is_code_ok = False
return is_code_ok