import json

from pytest import fixture, mark

from ..cilogon import CILogonOAuthenticator

from .mocks import setup_oauth_mock


def user_model(username):
    """Return a user model"""
    return {
        'eppn': username + '@serenity.space',
    }


@fixture
def cilogon_client(client):
    setup_oauth_mock(client,
                     host='cilogon.org',
                     access_token_path='/oauth2/token',
                     user_path='/oauth2/userinfo',
                     token_type='token',
                     )
    return client


async def test_cilogon(cilogon_client):
    authenticator = CILogonOAuthenticator()
    handler = cilogon_client.handler_for_user(user_model('wash'))
    user_info = await authenticator.authenticate(handler)
    print(json.dumps(user_info, sort_keys=True, indent=4))
    name = user_info['name']
    assert name == 'wash@serenity.space'
    auth_state = user_info['auth_state']
    assert 'access_token' in auth_state
    assert 'token_response' in auth_state
    assert auth_state == {
        'access_token': auth_state['access_token'],
        'cilogon_user': user_model('wash'),
        'token_response': auth_state['token_response'],
    }

@@ -38,16 +38,59 @@
 re, mark
+, raises%0A%0Afrom tornado.web import HTTPError
 %0A%0Afrom .
@@ -289,16 +289,171 @@
     %7D%0A%0A%0A
+def alternative_user_model(username, claimname):%0A    %22%22%22Return a user model with alternate claim name%22%22%22%0A    return %7B%0A        claimname: username,%0A    %7D%0A%0A%0A
 @fixture
@@ -1379,16 +1379,1919 @@
 sponse'%5D,%0A    %7D%0A
+%0A%0Aasync def test_cilogon_alternate_claim(cilogon_client):%0A    authenticator = CILogonOAuthenticator(username_claim='uid')%0A    handler = cilogon_client.handler_for_user(%0A        alternative_user_model('jtkirk@ufp.gov', 'uid'))%0A    user_info = await authenticator.authenticate(handler)%0A    print(json.dumps(user_info, sort_keys=True, indent=4))%0A    name = user_info%5B'name'%5D%0A    assert name == 'jtkirk@ufp.gov'%0A    auth_state = user_info%5B'auth_state'%5D%0A    assert 'access_token' in auth_state%0A    assert 'token_response' in auth_state%0A    assert auth_state == %7B%0A        'access_token': auth_state%5B'access_token'%5D,%0A        'cilogon_user': alternative_user_model('jtkirk@ufp.gov',%0A                                               'uid'),%0A        'token_response': auth_state%5B'token_response'%5D,%0A    %7D%0A%0A%0Aasync def test_cilogon_additional_claim(cilogon_client):%0A    authenticator = CILogonOAuthenticator(additional_username_claims=%5B'uid'%5D)%0A    handler = cilogon_client.handler_for_user(%0A        alternative_user_model('jtkirk@ufp.gov', 'uid'))%0A    user_info = await authenticator.authenticate(handler)%0A    print(json.dumps(user_info, sort_keys=True, indent=4))%0A    name = user_info%5B'name'%5D%0A    assert name == 'jtkirk@ufp.gov'%0A    auth_state = user_info%5B'auth_state'%5D%0A    assert 'access_token' in auth_state%0A    assert 'token_response' in auth_state%0A    assert auth_state == %7B%0A        'access_token': auth_state%5B'access_token'%5D,%0A        'cilogon_user': alternative_user_model('jtkirk@ufp.gov',%0A                                               'uid'),%0A        'token_response': auth_state%5B'token_response'%5D,%0A    %7D%0A%0A%0Aasync def test_cilogon_missing_alternate_claim(cilogon_client):%0A    authenticator = CILogonOAuthenticator()%0A    handler = cilogon_client.handler_for_user(%0A        alternative_user_model('jtkirk@ufp.gov', 'uid'))%0A    with raises(HTTPError):%0A        user_info = await authenticator.authenticate(handler)%0A

Add tests to exercise new claim functionality.