# Path: invenio_oauthclient/contrib/orcid.py
# def account_info(remote, resp):
#     """Retrieve remote account information used to find local user.
# 
#     It returns a dictionary with the following structure:
# 
#     .. code-block:: python
# 
#         {
#             'user': {
#                 'profile': {
#                     'full_name': 'Full Name',
#                 },
#             },
#             'external_id': 'github-unique-identifier',
#             'external_method': 'github',
#         }
# 
#     :param remote: The remote application.
#     :param resp: The response.
#     :returns: A dictionary with the user information.
#     """
#     orcid = resp.get('orcid')
# 
#     return {
#         'external_id': orcid,
#         'external_method': 'orcid',
#         'user': {
#             'profile': {
#                 'full_name': resp.get('name'),
#             },
#         },
#     }
# 
# Path: invenio_oauthclient/handlers/utils.py
# def token_session_key(remote_app):
#     """Generate a session key used to store the token for a remote app.
# 
#     :param remote_app: The remote application.
#     :returns: The session key.
#     """
#     return '%s_%s' % (current_app.config['OAUTHCLIENT_SESSION_KEY_PREFIX'],
#                       remote_app)


import httpretty
from flask import session, url_for
from flask_login import current_user
from flask_security import login_user
from flask_security.utils import hash_password
from helpers import get_state, mock_response
from invenio_accounts.models import User
from invenio_db import db
from six.moves.urllib_parse import parse_qs, urlparse
from invenio_oauthclient.contrib.orcid import account_info
from invenio_oauthclient.handlers import token_session_key
from invenio_oauthclient.models import RemoteAccount, RemoteToken, UserIdentity


def test_authorized_signup(app_with_userprofiles, example_orcid, orcid_bio):
    """Test authorized callback with sign-up."""
    app = app_with_userprofiles
    example_data, example_account_info = example_orcid
    example_email = 'orcidtest@inveniosoftware.org'

    with app.test_client() as c:
        # Ensure remote apps have been loaded (due to before first
        # request)
        c.get(url_for('invenio_oauthclient.login', remote_app='orcid'))

        mock_response(app.extensions['oauthlib.client'], 'orcid', example_data)

        # User authorized the requests and is redirect back
        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='orcid', code='test',
                    state=get_state('orcid')))
        assert resp.status_code == 302
        assert resp.location == (
            'http://localhost' +
            url_for('invenio_oauthclient.signup', remote_app='orcid')
        )

        # User load sign-up page.
        resp = c.get(url_for('invenio_oauthclient.signup', remote_app='orcid'))
        assert resp.status_code == 200


# Path: invenio_oauthclient/contrib/orcid.py
# def account_info(remote, resp):
#     """Retrieve remote account information used to find local user.
# 
#     It returns a dictionary with the following structure:
# 
#     .. code-block:: python
# 
#         {
#             'user': {
#                 'profile': {
#                     'full_name': 'Full Name',
#                 },
#             },
#             'external_id': 'github-unique-identifier',
#             'external_method': 'github',
#         }
# 
#     :param remote: The remote application.
#     :param resp: The response.
#     :returns: A dictionary with the user information.
#     """
#     orcid = resp.get('orcid')
# 
#     return {
#         'external_id': orcid,
#         'external_method': 'orcid',
#         'user': {
#             'profile': {
#                 'full_name': resp.get('name'),
#             },
#         },
#     }
# 
# Path: invenio_oauthclient/handlers/utils.py
# def token_session_key(remote_app):
#     """Generate a session key used to store the token for a remote app.
# 
#     :param remote_app: The remote application.
#     :returns: The session key.
#     """
#     return '%s_%s' % (current_app.config['OAUTHCLIENT_SESSION_KEY_PREFIX'],
#                       remote_app)


# Path: tests/test_contrib_orcid.py
import httpretty
from flask import session, url_for
from flask_login import current_user
from flask_security import login_user
from flask_security.utils import hash_password
from helpers import get_state, mock_response
from invenio_accounts.models import User
from invenio_db import db
from six.moves.urllib_parse import parse_qs, urlparse
from invenio_oauthclient.contrib.orcid import account_info
from invenio_oauthclient.handlers import token_session_key
from invenio_oauthclient.models import RemoteAccount, RemoteToken, UserIdentity



def test_authorized_signup(app_with_userprofiles, example_orcid, orcid_bio):
    """Test authorized callback with sign-up."""
    app = app_with_userprofiles
    example_data, example_account_info = example_orcid
    example_email = 'orcidtest@inveniosoftware.org'

    with app.test_client() as c:
        # Ensure remote apps have been loaded (due to before first
        # request)
        c.get(url_for('invenio_oauthclient.login', remote_app='orcid'))

        mock_response(app.extensions['oauthlib.client'], 'orcid', example_data)

        # User authorized the requests and is redirect back
        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='orcid', code='test',
                    state=get_state('orcid')))
        assert resp.status_code == 302
        assert resp.location == (
            'http://localhost' +
            url_for('invenio_oauthclient.signup', remote_app='orcid')
        )

        # User load sign-up page.
        resp = c.get(url_for('invenio_oauthclient.signup', remote_app='orcid'))
        assert resp.status_code == 200


        account_info = session[token_session_key('orcid') + '_account_info']