# Path: wunderhabit/tests/utils.py
# def get_user():
#     """
#     Returns a user which is connected with wunderlist and habitica.
#     """
# 
#     wunderlist = WunderlistFactory.create()
#     habitica = HabiticaFactory.create()
#     habitica.owner = wunderlist.owner
#     habitica.save()
#     return wunderlist.owner
# 
# Path: wunderlist/default.py
# AUTH_URL = 'https://www.wunderlist.com/oauth/authorize?client_id={client_id}&redirect_uri={url}&state={state}'
# AUTH_POST = 'https://www.wunderlist.com/oauth/access_token'
# AUTH_POST_CLIENT = 'client_id'
# AUTH_POST_SECRET = 'client_secret'
# AUTH_POST_CODE = 'code'
# AUTH_POST_TOKEN = 'access_token'
# AUTH_HEADER_CLIENT = 'X-Client-ID'
# AUTH_HEADER_TOKEN = 'X-Access-Token'
# GET_USER = 'https://a.wunderlist.com/api/v1/user'
# GET_LISTS = 'https://a.wunderlist.com/api/v1/lists'
# GET_WEBHOOKS = 'https://a.wunderlist.com/api/v1/webhooks?list_id={list_id}'
# POST_WEBHOOK = 'https://a.wunderlist.com/api/v1/webhooks'
# DELETE_WEBHOOK = 'https://a.wunderlist.com/api/v1/webhooks/{id}'
# JSON_ID = 'id'
# JSON_USER_ID = 'user_id'
# JSON_NAME = 'name'
# JSON_EMAIL = 'email'
# JSON_CREATED_AT = 'created_at'
# JSON_REVISION = 'revision'
# JSON_LIST_ID = 'list_id'
# JSON_URL = 'url'
# JSON_PROCESSOR_TYPE = 'processor_type'
# JSON_CONFIGURATION = 'configuration'
# JSON_SUBJECT = 'subject'
# JSON_TYPE = 'type'
# JSON_OPERATION = 'operation'
# JSON_BEFORE = 'before'
# JSON_AFTER = 'after'
# JSON_COMPLETED = 'completed'
# JSON_TITLE = 'title'
# OPERATION_CREATE = 'create'
# OPERATION_UPDATE = 'update'
# SUBJECT_TASK = 'task'
# SUBJECT_SUBTASK = 'subtask'
# SUBJECT_LIST = 'list'
# 
# Path: wunderlist/factories.py
# class ConnectionFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = Connection
# 
#     list_id = 42
#     token = get_random_string(32)
# 
# Path: wunderlist/views.py
# @csrf_exempt
# def webhook(request, hook_id):
#     connection = get_object_or_404(Connection, token=hook_id)
# 
#     if request.method != 'POST':
#         return HttpResponse(status=400)
# 
#     try:
#         data = json.loads(request.body)
#         operation = data.get(default.JSON_OPERATION)
#         user_id = int(data.get(default.JSON_USER_ID))
#         subject = data.get(default.JSON_SUBJECT)
#         subject_type = subject.get(default.JSON_TYPE)
#     except Exception:
#         return HttpResponse(status=400)
# 
#     # Check if connection is active
#     if not connection.is_active:
#         return HttpResponse(status=410)
# 
#     # Find Wunderlist user
#     try:
#         wunderlist = Wunderlist.objects.get(user_id=user_id)
#     except ObjectDoesNotExist:
#         return HttpResponse(status=401)
# 
#     # Validate user
#     user = connection.owner
#     if not user or not wunderlist or user != wunderlist.owner:
#         return HttpResponse(status=401)
# 
#     # Check if user is active
#     if not user.is_active:
#         return HttpResponse(status=403)
# 
#     # Check if a task or subtask has been added to the list
#     if operation == default.OPERATION_CREATE:
#         # New task has been added to list
#         return HttpResponse(status=200)
# 
#     # Check if a task has been updated (includes completion)
#     if operation == default.OPERATION_UPDATE:
#         try:
#             before = data.get(default.JSON_BEFORE)
#             before_completed = before.get(default.JSON_COMPLETED, False)
#             after = data.get(default.JSON_AFTER)
#             after_completed = after.get(default.JSON_COMPLETED, False)
#         except Exception:
#             return HttpResponse(status=400)
# 
#         if not before_completed and after_completed:
#             if subject_type == default.SUBJECT_TASK:
#                 # Task has been completed
#                 connection.score_up()
#                 return HttpResponse(status=200)
# 
#             elif subject_type == default.SUBJECT_SUBTASK:
#                 # Subtask has been completed
#                 connection.score_up()
#                 return HttpResponse(status=200)
# 
#     return HttpResponse(status=400)
# 
# Path: wh_habitica/tests/utils.py
# @pytest.fixture
# def mock_habitica_api(monkeypatch):
#     monkeypatch.setattr('habitica.api.Habitica.status', lambda x: API_STATUS_UP, raising=False)
#     monkeypatch.setattr('habitica.api.Habitica.user', lambda x: API_USER, raising=False)
#     monkeypatch.setattr('wh_habitica.api.HabiticaApi.post_task', lambda x, task_id: API_TASK, raising=False)


import json
import pytest
from django.core.urlresolvers import reverse
from django.http import Http404
from wunderhabit.tests.utils import get_user
from wunderlist import default
from wunderlist.factories import ConnectionFactory
from wunderlist.views import webhook
from wh_habitica.tests.utils import mock_habitica_api



USER_DICT = dict(username='tester', email='foo@bar.com')
INVALID_HOOK_TOKEN = '0000aUZ01eJYBhsIIVZotvc0dY9h0000'


def get_invalid_webhook_body():
    return {

# Path: wunderhabit/tests/utils.py
# def get_user():
#     """
#     Returns a user which is connected with wunderlist and habitica.
#     """
# 
#     wunderlist = WunderlistFactory.create()
#     habitica = HabiticaFactory.create()
#     habitica.owner = wunderlist.owner
#     habitica.save()
#     return wunderlist.owner
# 
# Path: wunderlist/default.py
# AUTH_URL = 'https://www.wunderlist.com/oauth/authorize?client_id={client_id}&redirect_uri={url}&state={state}'
# AUTH_POST = 'https://www.wunderlist.com/oauth/access_token'
# AUTH_POST_CLIENT = 'client_id'
# AUTH_POST_SECRET = 'client_secret'
# AUTH_POST_CODE = 'code'
# AUTH_POST_TOKEN = 'access_token'
# AUTH_HEADER_CLIENT = 'X-Client-ID'
# AUTH_HEADER_TOKEN = 'X-Access-Token'
# GET_USER = 'https://a.wunderlist.com/api/v1/user'
# GET_LISTS = 'https://a.wunderlist.com/api/v1/lists'
# GET_WEBHOOKS = 'https://a.wunderlist.com/api/v1/webhooks?list_id={list_id}'
# POST_WEBHOOK = 'https://a.wunderlist.com/api/v1/webhooks'
# DELETE_WEBHOOK = 'https://a.wunderlist.com/api/v1/webhooks/{id}'
# JSON_ID = 'id'
# JSON_USER_ID = 'user_id'
# JSON_NAME = 'name'
# JSON_EMAIL = 'email'
# JSON_CREATED_AT = 'created_at'
# JSON_REVISION = 'revision'
# JSON_LIST_ID = 'list_id'
# JSON_URL = 'url'
# JSON_PROCESSOR_TYPE = 'processor_type'
# JSON_CONFIGURATION = 'configuration'
# JSON_SUBJECT = 'subject'
# JSON_TYPE = 'type'
# JSON_OPERATION = 'operation'
# JSON_BEFORE = 'before'
# JSON_AFTER = 'after'
# JSON_COMPLETED = 'completed'
# JSON_TITLE = 'title'
# OPERATION_CREATE = 'create'
# OPERATION_UPDATE = 'update'
# SUBJECT_TASK = 'task'
# SUBJECT_SUBTASK = 'subtask'
# SUBJECT_LIST = 'list'
# 
# Path: wunderlist/factories.py
# class ConnectionFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = Connection
# 
#     list_id = 42
#     token = get_random_string(32)
# 
# Path: wunderlist/views.py
# @csrf_exempt
# def webhook(request, hook_id):
#     connection = get_object_or_404(Connection, token=hook_id)
# 
#     if request.method != 'POST':
#         return HttpResponse(status=400)
# 
#     try:
#         data = json.loads(request.body)
#         operation = data.get(default.JSON_OPERATION)
#         user_id = int(data.get(default.JSON_USER_ID))
#         subject = data.get(default.JSON_SUBJECT)
#         subject_type = subject.get(default.JSON_TYPE)
#     except Exception:
#         return HttpResponse(status=400)
# 
#     # Check if connection is active
#     if not connection.is_active:
#         return HttpResponse(status=410)
# 
#     # Find Wunderlist user
#     try:
#         wunderlist = Wunderlist.objects.get(user_id=user_id)
#     except ObjectDoesNotExist:
#         return HttpResponse(status=401)
# 
#     # Validate user
#     user = connection.owner
#     if not user or not wunderlist or user != wunderlist.owner:
#         return HttpResponse(status=401)
# 
#     # Check if user is active
#     if not user.is_active:
#         return HttpResponse(status=403)
# 
#     # Check if a task or subtask has been added to the list
#     if operation == default.OPERATION_CREATE:
#         # New task has been added to list
#         return HttpResponse(status=200)
# 
#     # Check if a task has been updated (includes completion)
#     if operation == default.OPERATION_UPDATE:
#         try:
#             before = data.get(default.JSON_BEFORE)
#             before_completed = before.get(default.JSON_COMPLETED, False)
#             after = data.get(default.JSON_AFTER)
#             after_completed = after.get(default.JSON_COMPLETED, False)
#         except Exception:
#             return HttpResponse(status=400)
# 
#         if not before_completed and after_completed:
#             if subject_type == default.SUBJECT_TASK:
#                 # Task has been completed
#                 connection.score_up()
#                 return HttpResponse(status=200)
# 
#             elif subject_type == default.SUBJECT_SUBTASK:
#                 # Subtask has been completed
#                 connection.score_up()
#                 return HttpResponse(status=200)
# 
#     return HttpResponse(status=400)
# 
# Path: wh_habitica/tests/utils.py
# @pytest.fixture
# def mock_habitica_api(monkeypatch):
#     monkeypatch.setattr('habitica.api.Habitica.status', lambda x: API_STATUS_UP, raising=False)
#     monkeypatch.setattr('habitica.api.Habitica.user', lambda x: API_USER, raising=False)
#     monkeypatch.setattr('wh_habitica.api.HabiticaApi.post_task', lambda x, task_id: API_TASK, raising=False)


# Path: wunderlist/tests/test_views.py
import json
import pytest
from django.core.urlresolvers import reverse
from django.http import Http404
from wunderhabit.tests.utils import get_user
from wunderlist import default
from wunderlist.factories import ConnectionFactory
from wunderlist.views import webhook
from wh_habitica.tests.utils import mock_habitica_api




USER_DICT = dict(username='tester', email='foo@bar.com')
INVALID_HOOK_TOKEN = '0000aUZ01eJYBhsIIVZotvc0dY9h0000'


def get_invalid_webhook_body():
    return {

        default.JSON_OPERATION: default.OPERATION_CREATE,