2017-03-16 21:05:26 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
from auth.auth_context import (
|
|
|
|
get_authenticated_user, get_grant_context, get_validated_token, get_validated_oauth_token)
|
2017-03-16 21:05:26 +00:00
|
|
|
from auth.validateresult import AuthKind, ValidateResult
|
|
|
|
from data import model
|
2017-04-24 17:49:29 +00:00
|
|
|
from test.fixtures import *
|
2017-03-16 21:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_user():
|
|
|
|
return model.user.get_user('devtable')
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
|
2017-03-16 21:05:26 +00:00
|
|
|
def get_robot():
|
|
|
|
robot, _ = model.user.create_robot('somebot', get_user())
|
|
|
|
return robot
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
|
2017-03-16 21:05:26 +00:00
|
|
|
def get_token():
|
|
|
|
return model.token.create_delegate_token('devtable', 'simple', 'sometoken')
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
|
2017-03-16 21:05:26 +00:00
|
|
|
def get_oauthtoken():
|
|
|
|
user = model.user.get_user('devtable')
|
|
|
|
return list(model.oauth.list_access_tokens_for_user(user))[0]
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
|
2017-03-16 21:05:26 +00:00
|
|
|
def get_signeddata():
|
|
|
|
return {'grants': {'a': 'b'}, 'user_context': {'c': 'd'}}
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
|
2017-03-16 21:05:26 +00:00
|
|
|
@pytest.mark.parametrize('get_entity,entity_kind', [
|
|
|
|
(get_user, 'user'),
|
|
|
|
(get_robot, 'robot'),
|
|
|
|
(get_token, 'token'),
|
|
|
|
(get_oauthtoken, 'oauthtoken'),
|
2017-07-05 19:45:07 +00:00
|
|
|
(get_signeddata, 'signed_data'),])
|
2017-03-16 21:05:26 +00:00
|
|
|
def test_apply_context(get_entity, entity_kind, app):
|
|
|
|
assert get_authenticated_user() is None
|
|
|
|
assert get_validated_token() is None
|
|
|
|
assert get_validated_oauth_token() is None
|
|
|
|
assert get_grant_context() is None
|
|
|
|
|
|
|
|
entity = get_entity()
|
|
|
|
args = {}
|
|
|
|
args[entity_kind] = entity
|
|
|
|
|
|
|
|
result = ValidateResult(AuthKind.basic, **args)
|
|
|
|
result.apply_to_context()
|
|
|
|
|
|
|
|
expected_user = entity if entity_kind == 'user' or entity_kind == 'robot' else None
|
|
|
|
if entity_kind == 'oauthtoken':
|
|
|
|
expected_user = entity.authorized_user
|
|
|
|
|
|
|
|
expected_token = entity if entity_kind == 'token' else None
|
|
|
|
expected_oauth = entity if entity_kind == 'oauthtoken' else None
|
|
|
|
|
|
|
|
fake_grant = {
|
2017-07-05 19:45:07 +00:00
|
|
|
'user': {
|
|
|
|
'c': 'd'},
|
|
|
|
'kind': 'user',}
|
2017-03-16 21:05:26 +00:00
|
|
|
expected_grant = fake_grant if entity_kind == 'signed_data' else None
|
|
|
|
|
|
|
|
assert get_authenticated_user() == expected_user
|
|
|
|
assert get_validated_token() == expected_token
|
|
|
|
assert get_validated_oauth_token() == expected_oauth
|
|
|
|
assert get_grant_context() == expected_grant
|