# Path: wh_habitica/default.py
# AUTH_URL = 'url'
# AUTH_HEADER_CLIENT = 'x-api-user'
# AUTH_HEADER_TOKEN = 'x-api-key'
# API_HOST = 'https://habitica.com'
# API_BASE_URL = API_HOST + '/api/v3'
# GET_STATUS = API_BASE_URL + '/status'
# GET_USER = API_BASE_URL + '/user'
# GET_USER_ANONYMIZED = API_BASE_URL + '/user/anonymized'
# POST_TASK = API_BASE_URL + '/tasks/user/{id}/{direction}'
# JSON_STATUS = 'status'
# JSON_UP = 'up'
# JSON_DOWN = 'down'
# JSON_ID = 'id'
# JSON_DELTA = 'delta'
# JSON_BUFFS = 'buffs'
# JSON_AUTH = 'auth'
# JSON_LOCAL = 'local'
# JSON_FACEBOOK = 'facebook'
# JSON_GOOGLE = 'google'
# JSON_GOOGLE_EMAILS = 'emails'
# JSON_DISPLAY_NAME = 'displayName'
# JSON_FORMAT_JSON = '_json'
# JSON_USERNAME = 'username'
# JSON_EMAIL = 'email'
# JSON_NAME = 'name'
# JSON_VALUE = 'value'
# JSON_TYPE = 'type'
# 
# Path: wh_habitica/forms.py
# class AuthForm(forms.ModelForm):
#     """
#     Form to enter and validate the Habitica authentication credentials.
#     """
# 
#     AUTH_ERROR = _('Could not authenticate to Habitica. Please check the User '
#                    'ID and the API Token.')
#     HABITICA_ERROR = _('Something went wrong while loading Habitica user data.'
#                        ' Please try again or contact the admin.')
# 
#     class Meta:
#         model = Habitica
#         fields = ['user_id', 'api_token']
# 
#     def clean(self):
#         cleaned_data = super(AuthForm, self).clean()
# 
#         api = HabiticaApi(cleaned_data['user_id'], cleaned_data['api_token'])
#         user_details = api.get_user_details()
# 
#         # Validate authentication
#         if not user_details or cleaned_data['user_id'] != user_details[default.JSON_ID]:
#             raise forms.ValidationError(self.AUTH_ERROR)
# 
#         # Get optional user details
#         try:
#             self.instance.name = user_details[default.JSON_NAME]
#             self.instance.email = user_details[default.JSON_EMAIL]
#         except (ValueError, KeyError):
#             logger.exception('Could not get user details: %s', str(user_details))
# 
#         return cleaned_data
# 
# Path: wunderhabit/factories.py
# class UserFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = get_user_model()
# 
#     username = factory.Sequence(lambda n: "user_%d" % n)
#     first_name = 'John'
#     last_name = 'Doe'
# 
# Path: wh_habitica/tests/utils.py
# API_STATUS_UP = {default.JSON_STATUS: default.JSON_UP}
# LOCAL_NAME = 'John Local'
# FACEBOOK_NAME = 'John Facebook'
# FACEBOOK_ID = '1337'
# GOOGLE_NAME = 'John Google'
# USER_EMAIL = 'john@doe.com'
# API_USER = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_FACEBOOK: {},
#         default.JSON_GOOGLE: {},
#         default.JSON_LOCAL: {default.JSON_USERNAME: LOCAL_NAME, default.JSON_EMAIL: USER_EMAIL},
#     }
# }
# API_USER_FACEBOOK = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_LOCAL: {},
#         default.JSON_GOOGLE: {},
#         default.JSON_FACEBOOK: {
#             default.JSON_DISPLAY_NAME: FACEBOOK_NAME,
#             default.JSON_ID: FACEBOOK_ID,
#         }
#     }
# }
# API_USER_GOOGLE = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_LOCAL: {},
#         default.JSON_FACEBOOK: {},
#         default.JSON_GOOGLE: {
#             default.JSON_DISPLAY_NAME: GOOGLE_NAME,
#             default.JSON_GOOGLE_EMAILS: [{default.JSON_TYPE: u'account', default.JSON_VALUE: USER_EMAIL}],
#         }
#     }
# }
# API_USER_INVALID = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_FACEBOOK: {},
#         default.JSON_GOOGLE: {},
#         default.JSON_LOCAL: {},
#         'Invalid_Provider': {default.JSON_USERNAME: LOCAL_NAME, default.JSON_EMAIL: USER_EMAIL},
#     }
# }
# API_TASK = {default.JSON_DELTA: 1}
# def mock_habitica_api(monkeypatch):
# def mock_habitica_api_facebook(monkeypatch):
# def mock_habitica_api_google(monkeypatch):
# def mock_habitica_api_invalid_provider(monkeypatch):


import json
import logging
import pytest
import responses
from wh_habitica import default
from wh_habitica.forms import AuthForm
from wunderhabit.factories import UserFactory
from wh_habitica.tests import utils
        # Local auth provider
        data = utils.API_USER
        data[default.JSON_ID] = request.headers.get(default.AUTH_HEADER_CLIENT)

    return 200, {}, json.dumps({'data': data})


@pytest.fixture
def get_user():
    responses.add_callback(
        responses.GET, default.GET_USER,
        callback=get_user_callback,
        content_type='application/json',
    )


@pytest.fixture
def api_error():
    responses.add(
        responses.GET, default.GET_USER,
        body=Exception('Some Habitica API Error occurred!'),
        content_type='application/json',
    )


@responses.activate
def test_auth_error(get_user):
    # bad api auth result
    res = AuthForm(data=dict(user_id='foo', api_token='baz'))
    assert not res.is_valid()

# Path: wh_habitica/default.py
# AUTH_URL = 'url'
# AUTH_HEADER_CLIENT = 'x-api-user'
# AUTH_HEADER_TOKEN = 'x-api-key'
# API_HOST = 'https://habitica.com'
# API_BASE_URL = API_HOST + '/api/v3'
# GET_STATUS = API_BASE_URL + '/status'
# GET_USER = API_BASE_URL + '/user'
# GET_USER_ANONYMIZED = API_BASE_URL + '/user/anonymized'
# POST_TASK = API_BASE_URL + '/tasks/user/{id}/{direction}'
# JSON_STATUS = 'status'
# JSON_UP = 'up'
# JSON_DOWN = 'down'
# JSON_ID = 'id'
# JSON_DELTA = 'delta'
# JSON_BUFFS = 'buffs'
# JSON_AUTH = 'auth'
# JSON_LOCAL = 'local'
# JSON_FACEBOOK = 'facebook'
# JSON_GOOGLE = 'google'
# JSON_GOOGLE_EMAILS = 'emails'
# JSON_DISPLAY_NAME = 'displayName'
# JSON_FORMAT_JSON = '_json'
# JSON_USERNAME = 'username'
# JSON_EMAIL = 'email'
# JSON_NAME = 'name'
# JSON_VALUE = 'value'
# JSON_TYPE = 'type'
# 
# Path: wh_habitica/forms.py
# class AuthForm(forms.ModelForm):
#     """
#     Form to enter and validate the Habitica authentication credentials.
#     """
# 
#     AUTH_ERROR = _('Could not authenticate to Habitica. Please check the User '
#                    'ID and the API Token.')
#     HABITICA_ERROR = _('Something went wrong while loading Habitica user data.'
#                        ' Please try again or contact the admin.')
# 
#     class Meta:
#         model = Habitica
#         fields = ['user_id', 'api_token']
# 
#     def clean(self):
#         cleaned_data = super(AuthForm, self).clean()
# 
#         api = HabiticaApi(cleaned_data['user_id'], cleaned_data['api_token'])
#         user_details = api.get_user_details()
# 
#         # Validate authentication
#         if not user_details or cleaned_data['user_id'] != user_details[default.JSON_ID]:
#             raise forms.ValidationError(self.AUTH_ERROR)
# 
#         # Get optional user details
#         try:
#             self.instance.name = user_details[default.JSON_NAME]
#             self.instance.email = user_details[default.JSON_EMAIL]
#         except (ValueError, KeyError):
#             logger.exception('Could not get user details: %s', str(user_details))
# 
#         return cleaned_data
# 
# Path: wunderhabit/factories.py
# class UserFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = get_user_model()
# 
#     username = factory.Sequence(lambda n: "user_%d" % n)
#     first_name = 'John'
#     last_name = 'Doe'
# 
# Path: wh_habitica/tests/utils.py
# API_STATUS_UP = {default.JSON_STATUS: default.JSON_UP}
# LOCAL_NAME = 'John Local'
# FACEBOOK_NAME = 'John Facebook'
# FACEBOOK_ID = '1337'
# GOOGLE_NAME = 'John Google'
# USER_EMAIL = 'john@doe.com'
# API_USER = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_FACEBOOK: {},
#         default.JSON_GOOGLE: {},
#         default.JSON_LOCAL: {default.JSON_USERNAME: LOCAL_NAME, default.JSON_EMAIL: USER_EMAIL},
#     }
# }
# API_USER_FACEBOOK = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_LOCAL: {},
#         default.JSON_GOOGLE: {},
#         default.JSON_FACEBOOK: {
#             default.JSON_DISPLAY_NAME: FACEBOOK_NAME,
#             default.JSON_ID: FACEBOOK_ID,
#         }
#     }
# }
# API_USER_GOOGLE = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_LOCAL: {},
#         default.JSON_FACEBOOK: {},
#         default.JSON_GOOGLE: {
#             default.JSON_DISPLAY_NAME: GOOGLE_NAME,
#             default.JSON_GOOGLE_EMAILS: [{default.JSON_TYPE: u'account', default.JSON_VALUE: USER_EMAIL}],
#         }
#     }
# }
# API_USER_INVALID = {
#     default.JSON_ID: 42,
#     default.JSON_AUTH: {
#         default.JSON_FACEBOOK: {},
#         default.JSON_GOOGLE: {},
#         default.JSON_LOCAL: {},
#         'Invalid_Provider': {default.JSON_USERNAME: LOCAL_NAME, default.JSON_EMAIL: USER_EMAIL},
#     }
# }
# API_TASK = {default.JSON_DELTA: 1}
# def mock_habitica_api(monkeypatch):
# def mock_habitica_api_facebook(monkeypatch):
# def mock_habitica_api_google(monkeypatch):
# def mock_habitica_api_invalid_provider(monkeypatch):


# Path: wh_habitica/tests/test_forms.py
import json
import logging
import pytest
import responses
from wh_habitica import default
from wh_habitica.forms import AuthForm
from wunderhabit.factories import UserFactory
from wh_habitica.tests import utils

        # Local auth provider
        data = utils.API_USER
        data[default.JSON_ID] = request.headers.get(default.AUTH_HEADER_CLIENT)

    return 200, {}, json.dumps({'data': data})


@pytest.fixture
def get_user():
    responses.add_callback(
        responses.GET, default.GET_USER,
        callback=get_user_callback,
        content_type='application/json',
    )


@pytest.fixture
def api_error():
    responses.add(
        responses.GET, default.GET_USER,
        body=Exception('Some Habitica API Error occurred!'),
        content_type='application/json',
    )


@responses.activate
def test_auth_error(get_user):
    # bad api auth result
    res = AuthForm(data=dict(user_id='foo', api_token='baz'))
    assert not res.is_valid()

    assert res.errors == {'__all__': [AuthForm.AUTH_ERROR]}