63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
import pytest
|
|
|
|
from auth.auth_context import (get_authenticated_user, get_grant_context, get_validated_token,
|
|
get_validated_oauth_token)
|
|
from auth.validateresult import AuthKind, ValidateResult
|
|
from data import model
|
|
from test.fixtures import *
|
|
|
|
|
|
def get_user():
|
|
return model.user.get_user('devtable')
|
|
|
|
def get_robot():
|
|
robot, _ = model.user.create_robot('somebot', get_user())
|
|
return robot
|
|
|
|
def get_token():
|
|
return model.token.create_delegate_token('devtable', 'simple', 'sometoken')
|
|
|
|
def get_oauthtoken():
|
|
user = model.user.get_user('devtable')
|
|
return list(model.oauth.list_access_tokens_for_user(user))[0]
|
|
|
|
def get_signeddata():
|
|
return {'grants': {'a': 'b'}, 'user_context': {'c': 'd'}}
|
|
|
|
@pytest.mark.parametrize('get_entity,entity_kind', [
|
|
(get_user, 'user'),
|
|
(get_robot, 'robot'),
|
|
(get_token, 'token'),
|
|
(get_oauthtoken, 'oauthtoken'),
|
|
(get_signeddata, 'signed_data'),
|
|
])
|
|
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 = {
|
|
'user': {'c': 'd'},
|
|
'kind': 'user',
|
|
}
|
|
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
|