"""Authorization handler tests.""" import json import uuid import flexmock from oto import response from oto import status from auth import config from auth import handlers from auth.connectors import manifest from auth.logic import api from tests.fixtures import fixture_authorization URL = '/authorize?client_id={}&state={}' URL_REDIRECT = '/authorize?client_id={}&state={}&redirect={}' NOT_FOUND = response.create_not_found_response() def auto_fixture_get_authorization(response): """Automatically add a fixture to the authorization.""" mock = flexmock(api).should_receive('get_authorization') mock.and_return(response).once() def test_authorize_handler(fixture_client): """Test authorizing a client (renders the template).""" authorization_response = fixture_authorization.get_authorization_response() flexmock(manifest.manifest).should_receive('get_url').and_return('') data = authorization_response.message auto_fixture_get_authorization(authorization_response) (flexmock(handlers) .should_call('render_template') .with_args( 'login.html', public_key=data.get('public_key'), client_id=data.get('client_id'), state=data.get('state'), authorization_id=data.get('authorization_id'), redirect=None) .once()) url = URL.format(data.get('client_id'), data.get('state')) result = fixture_client.get(url) assert result.status_code == status.OK def test_authorize_handler_json(monkeypatch, fixture_client): """Test that Accept header with 'application/json' proxies response.""" authorization_response = fixture_authorization.get_authorization_response() flexmock(manifest.manifest).should_receive('get_url').and_return('') data = authorization_response.message auto_fixture_get_authorization(authorization_response) url = URL.format(data.get('client_id'), data.get('state')) result = fixture_client.get(url, headers={'Accept': 'application/json'}) assert result.data.decode('utf-8') == \ json.dumps(authorization_response.message) def test_authorize_handler_redirect(monkeypatch, fixture_client): """Test assigning the redirect to the template if provided and in QA.""" flexmock(config, ENVIRONMENT=config.QA_ENVIRONMENT) flexmock(manifest.manifest).should_receive('get_url').and_return('') authorization_response = fixture_authorization.get_authorization_response() auto_fixture_get_authorization(authorization_response) data = authorization_response.message custom_redirect = 'custom_redirect' (flexmock(handlers) .should_call('render_template') .with_args( 'login.html', public_key=data.get('public_key'), client_id=data.get('client_id'), state=data.get('state'), authorization_id=data.get('authorization_id'), redirect=custom_redirect) .once()) url = URL_REDIRECT.format( data.get('client_id'), data.get('state'), custom_redirect) result = fixture_client.get(url) assert result.status_code == status.OK def test_authorize_handler_redirect_on_prod(monkeypatch, fixture_client): """Test assinging redirect to the template if provided and in Prod. The assignment should not be made. Only QA environments are allowed to override the the redirect url. """ flexmock(config, ENVIRONMENT=config.PROD_ENVIRONMENT) flexmock(manifest.manifest).should_receive('get_url').and_return('') authorization_response = fixture_authorization.get_authorization_response() auto_fixture_get_authorization(authorization_response) data = authorization_response.message custom_redirect = 'custom_redirect' (flexmock(handlers) .should_call('render_template') .with_args( 'login.html', public_key=data.get('public_key'), client_id=data.get('client_id'), state=data.get('state'), authorization_id=data.get('authorization_id'), redirect=None) .once()) url = URL_REDIRECT.format( data.get('client_id'), data.get('state'), custom_redirect) result = fixture_client.get(url) assert result.status_code == status.OK def test_unsuccessful_authorize_handler(fixture_client): """Test unsuccessfully getting an authorization.""" (flexmock(api) .should_receive('get_authorization') .and_return(NOT_FOUND) .once()) url = URL.format(2000, str(uuid.uuid1())) result = fixture_client.get(url) content = json.loads(result.data.decode('utf8')) assert result.status_code == NOT_FOUND.status assert NOT_FOUND.errors == content