from __future__ import unicode_literals

try:
    from unittest.mock import patch
except ImportError:
    from mock import patch

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.urls import reverse

from rest_framework.test import APIRequestFactory, force_authenticate

from django_comments_xtd import django_comments
from django_comments_xtd.api.views import CommentCreate
from django_comments_xtd.tests.models import Article, Diary


request_factory = APIRequestFactory()


def post_comment(data, auth_user=None):
    request = request_factory.post(reverse('comments-xtd-api-create'), data)
    if auth_user:
        force_authenticate(request, user=auth_user)
    view = CommentCreate.as_view()
    return view(request)


class CommentCreateTestCase(TestCase):
    def setUp(self):
        patcher = patch('django_comments_xtd.views.send_mail')
        self.mock_mailer = patcher.start()
        self.article = Article.objects.create(
            title="October", slug="october", body="What I did on October...")
        self.form = django_comments.get_form()(self.article)

    def test_post_returns_2xx_response(self):
        data = {"name": "Bob", "email": "fulanito@detal.com",
                "followup": True, "reply_to": 0, "level": 1, "order": 1,
                "comment": "Es war einmal eine kleine...",
                "honeypot": ""}
        data.update(self.form.initial)
        response = post_comment(data)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.mock_mailer.call_count, 1)

    def test_post_returns_4xx_response(self):
        # It uses an authenticated user, but the user has no mail address.
        self.user = User.objects.create_user("bob", "", "pwd")
        data = {"name": "", "email": "",
                "followup": True, "reply_to": 0, "level": 1, "order": 1,
                "comment": "Es war einmal eine kleine...",
                "honeypot": ""}
        data.update(self.form.initial)
        response = post_comment(data, auth_user=self.user)
        self.assertEqual(response.status_code, 400)
        self.assertTrue('name' in response.data)
        self.assertTrue('email' in response.data)
        self.assertEqual(self.mock_mailer.call_count, 0)

[((554, 573), 'rest_framework.test.APIRequestFactory', 'APIRequestFactory', ([], {}), '()\n', (571, 573), False, 'from rest_framework.test import APIRequestFactory, force_authenticate\n'), ((774, 797), 'django_comments_xtd.api.views.CommentCreate.as_view', 'CommentCreate.as_view', ([], {}), '()\n', (795, 797), False, 'from django_comments_xtd.api.views import CommentCreate\n'), ((651, 685), 'django.urls.reverse', 'reverse', (['"""comments-xtd-api-create"""'], {}), "('comments-xtd-api-create')\n", (658, 685), False, 'from django.urls import reverse\n'), ((719, 762), 'rest_framework.test.force_authenticate', 'force_authenticate', (['request'], {'user': 'auth_user'}), '(request, user=auth_user)\n', (737, 762), False, 'from rest_framework.test import APIRequestFactory, force_authenticate\n'), ((903, 947), 'mock.patch', 'patch', (['"""django_comments_xtd.views.send_mail"""'], {}), "('django_comments_xtd.views.send_mail')\n", (908, 947), False, 'from mock import patch\n'), ((1014, 1107), 'django_comments_xtd.tests.models.Article.objects.create', 'Article.objects.create', ([], {'title': '"""October"""', 'slug': '"""october"""', 'body': '"""What I did on October..."""'}), "(title='October', slug='october', body=\n    'What I did on October...')\n", (1036, 1107), False, 'from django_comments_xtd.tests.models import Article, Diary\n'), ((1778, 1820), 'django.contrib.auth.models.User.objects.create_user', 'User.objects.create_user', (['"""bob"""', '""""""', '"""pwd"""'], {}), "('bob', '', 'pwd')\n", (1802, 1820), False, 'from django.contrib.auth.models import User\n'), ((1136, 1162), 'django_comments_xtd.django_comments.get_form', 'django_comments.get_form', ([], {}), '()\n', (1160, 1162), False, 'from django_comments_xtd import django_comments\n')]