""" test for app action functionality """
from unittest.mock import patch

from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from django.http.response import Http404
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory

from bookwyrm import models, views
from bookwyrm.settings import DOMAIN
from bookwyrm.tests.validate_html import validate_html


# pylint: disable=too-many-public-methods
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
class RegisterViews(TestCase):
    """login and password management"""

    @classmethod
    def setUpTestData(self):  # pylint: disable=bad-classmethod-argument
        """we need basic test data and mocks"""
        with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
            "bookwyrm.activitystreams.populate_stream_task.delay"
        ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
            self.local_user = models.User.objects.create_user(
                "mouse@your.domain.here",
                "mouse@mouse.com",
                "password",
                local=True,
                localname="mouse",
            )
        self.settings = models.SiteSettings.objects.create(
            id=1, require_confirm_email=False, allow_registration=True
        )

    def setUp(self):
        """individual test setup"""
        self.factory = RequestFactory()
        self.anonymous_user = AnonymousUser
        self.anonymous_user.is_authenticated = False

    def test_get_redirect(self, *_):
        """there's no dedicated registration page"""
        view = views.Register.as_view()
        request = self.factory.get("register/")
        response = view(request)
        self.assertEqual(response.status_code, 302)

    def test_register(self, *_):
        """create a user"""
        view = views.Register.as_view()
        self.assertEqual(models.User.objects.count(), 1)
        request = self.factory.post(
            "register/",
            {
                "localname": "nutria-user.user_nutria",
                "password": "mouseword",
                "email": "aa@bb.cccc",
                "preferred_timezone": "Europe/Berlin",
            },
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        self.assertEqual(models.User.objects.count(), 2)
        self.assertEqual(response.status_code, 302)
        nutria = models.User.objects.last()
        self.assertEqual(nutria.username, f"nutria-user.user_nutria@{DOMAIN}")
        self.assertEqual(nutria.localname, "nutria-user.user_nutria")
        self.assertEqual(nutria.local, True)
        self.assertEqual(nutria.preferred_timezone, "Europe/Berlin")

    @patch("bookwyrm.emailing.send_email.delay")
    def test_register_email_confirm(self, *_):
        """create a user"""
        self.settings.require_confirm_email = True
        self.settings.save()

        view = views.Register.as_view()
        self.assertEqual(models.User.objects.count(), 1)
        request = self.factory.post(
            "register/",
            {
                "localname": "nutria",
                "password": "mouseword",
                "email": "aa@bb.cccc",
            },
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        self.assertEqual(response.status_code, 302)
        nutria = models.User.objects.get(localname="nutria")
        self.assertEqual(nutria.username, f"nutria@{DOMAIN}")
        self.assertEqual(nutria.local, True)

        self.assertFalse(nutria.is_active)
        self.assertEqual(nutria.deactivation_reason, "pending")
        self.assertIsNotNone(nutria.confirmation_code)

    def test_register_trailing_space(self, *_):
        """django handles this so weirdly"""
        view = views.Register.as_view()
        request = self.factory.post(
            "register/",
            {"localname": "nutria ", "password": "mouseword", "email": "aa@bb.ccc"},
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        self.assertEqual(models.User.objects.count(), 2)
        self.assertEqual(response.status_code, 302)
        nutria = models.User.objects.last()
        self.assertEqual(nutria.username, f"nutria@{DOMAIN}")
        self.assertEqual(nutria.localname, "nutria")
        self.assertEqual(nutria.local, True)

    def test_register_invalid_email(self, *_):
        """gotta have an email"""
        view = views.Register.as_view()
        self.assertEqual(models.User.objects.count(), 1)
        request = self.factory.post(
            "register/", {"localname": "nutria", "password": "mouseword", "email": "aa"}
        )
        response = view(request)
        self.assertEqual(models.User.objects.count(), 1)
        validate_html(response.render())

    def test_register_invalid_password(self, *_):
        """gotta have an email"""
        view = views.Register.as_view()
        self.assertEqual(models.User.objects.count(), 1)
        request = self.factory.post(
            "register/", {"localname": "nutria", "password": "password", "email": "aa"}
        )
        response = view(request)
        self.assertEqual(models.User.objects.count(), 1)
        validate_html(response.render())

    def test_register_error_and_invite(self, *_):
        """redirect to the invite page"""
        view = views.Register.as_view()
        self.settings.allow_registration = False
        self.settings.save()
        models.SiteInvite.objects.create(
            code="testcode", user=self.local_user, use_limit=1
        )
        self.assertEqual(models.SiteInvite.objects.get().times_used, 0)

        request = self.factory.post(
            "register/",
            {
                "localname": "nutria",
                "password": "mouseword",
                "email": "",
                "invite_code": "testcode",
            },
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        response = view(request)
        validate_html(response.render())

    def test_register_username_in_use(self, *_):
        """that username is taken"""
        view = views.Register.as_view()
        self.assertEqual(models.User.objects.count(), 1)
        request = self.factory.post(
            "register/",
            {"localname": "mouse", "password": "mouseword", "email": "aa@bb.ccc"},
        )
        response = view(request)
        self.assertEqual(models.User.objects.count(), 1)
        validate_html(response.render())

    def test_register_invalid_username(self, *_):
        """gotta have an email"""
        view = views.Register.as_view()
        self.assertEqual(models.User.objects.count(), 1)
        request = self.factory.post(
            "register/",
            {"localname": "nut@ria", "password": "mouseword", "email": "aa@bb.ccc"},
        )
        response = view(request)
        self.assertEqual(models.User.objects.count(), 1)
        validate_html(response.render())

        request = self.factory.post(
            "register/",
            {"localname": "nutr ia", "password": "mouseword", "email": "aa@bb.ccc"},
        )
        response = view(request)
        self.assertEqual(models.User.objects.count(), 1)
        validate_html(response.render())

        request = self.factory.post(
            "register/",
            {"localname": "nut@ria", "password": "mouseword", "email": "aa@bb.ccc"},
        )
        response = view(request)
        self.assertEqual(models.User.objects.count(), 1)
        validate_html(response.render())

    def test_register_default_preferred_timezone(self, *_):
        """invalid preferred timezone strings should just default to UTC"""
        view = views.Register.as_view()
        self.assertEqual(models.User.objects.count(), 1)

        request = self.factory.post(
            "register/",
            {
                "localname": "nutria1",
                "password": "mouseword",
                "email": "aa1@bb.cccc",
                "preferred_timezone": "invalid-tz",
            },
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(models.User.objects.count(), 2)
        nutria = models.User.objects.last()
        self.assertEqual(nutria.preferred_timezone, "UTC")

        request = self.factory.post(
            "register/",
            {
                "localname": "nutria2",
                "password": "mouseword",
                "email": "aa2@bb.cccc",
                "preferred_timezone": "",
            },
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(models.User.objects.count(), 3)
        nutria = models.User.objects.last()
        self.assertEqual(nutria.preferred_timezone, "UTC")

        request = self.factory.post(
            "register/",
            {
                "localname": "nutria3",
                "password": "mouseword",
                "email": "aa3@bb.cccc",
            },
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(models.User.objects.count(), 4)
        nutria = models.User.objects.last()
        self.assertEqual(nutria.preferred_timezone, "UTC")

    def test_register_closed_instance(self, *_):
        """you can't just register"""
        view = views.Register.as_view()
        self.settings.allow_registration = False
        self.settings.save()
        request = self.factory.post(
            "register/",
            {"localname": "nutria ", "password": "mouseword", "email": "aa@bb.ccc"},
        )
        with self.assertRaises(PermissionDenied):
            view(request)

    def test_register_blocked_domain(self, *_):
        """you can't register with a blocked domain"""
        view = views.Register.as_view()
        models.EmailBlocklist.objects.create(domain="gmail.com")

        # one that fails
        request = self.factory.post(
            "register/",
            {"localname": "nutria ", "password": "mouseword", "email": "aa@gmail.com"},
        )
        result = view(request)
        self.assertEqual(result.status_code, 302)
        self.assertFalse(models.User.objects.filter(email="aa@gmail.com").exists())

        # one that succeeds
        request = self.factory.post(
            "register/",
            {"localname": "nutria ", "password": "mouseword", "email": "aa@bleep.com"},
        )
        with patch("bookwyrm.views.landing.register.login"):
            result = view(request)
        self.assertEqual(result.status_code, 302)
        self.assertTrue(models.User.objects.filter(email="aa@bleep.com").exists())

    def test_register_invite(self, *_):
        """you can't just register"""
        view = views.Register.as_view()
        self.settings.allow_registration = False
        self.settings.save()
        models.SiteInvite.objects.create(
            code="testcode", user=self.local_user, use_limit=1
        )
        self.assertEqual(models.SiteInvite.objects.get().times_used, 0)

        request = self.factory.post(
            "register/",
            {
                "localname": "nutria",
                "password": "mouseword",
                "email": "aa@bb.ccc",
                "invite_code": "testcode",
            },
        )
        with patch("bookwyrm.views.landing.register.login"):
            response = view(request)
        self.assertEqual(models.User.objects.count(), 2)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(models.SiteInvite.objects.get().times_used, 1)

        # invite already used to max capacity
        request = self.factory.post(
            "register/",
            {
                "localname": "nutria2",
                "password": "mouseword",
                "email": "aa@bb.ccc",
                "invite_code": "testcode",
            },
        )
        with self.assertRaises(PermissionDenied):
            response = view(request)
        self.assertEqual(models.User.objects.count(), 2)

        # bad invite code
        request = self.factory.post(
            "register/",
            {
                "localname": "nutria3",
                "password": "mouseword",
                "email": "aa@bb.ccc",
                "invite_code": "dkfkdjgdfkjgkdfj",
            },
        )
        with self.assertRaises(Http404):
            response = view(request)
        self.assertEqual(models.User.objects.count(), 2)

    def test_confirm_email_code_get(self, *_):
        """there are so many views, this just makes sure it LOADS"""
        self.settings.require_confirm_email = True
        self.settings.save()

        self.local_user.is_active = False
        self.local_user.allow_reactivation = True
        self.local_user.deactivation_reason = "pending"
        self.local_user.confirmation_code = "12345"
        self.local_user.save(
            broadcast=False,
            update_fields=[
                "is_active",
                "allow_reactivation",
                "deactivation_reason",
                "confirmation_code",
            ],
        )
        view = views.ConfirmEmailCode.as_view()
        request = self.factory.get("")
        request.user = self.anonymous_user

        result = view(request, "12345")
        self.assertEqual(result.url, "/login/confirmed")
        self.assertEqual(result.status_code, 302)

        self.local_user.refresh_from_db()
        self.assertTrue(self.local_user.is_active)
        self.assertIsNone(self.local_user.deactivation_reason)

        request.user = self.local_user
        result = view(request, "12345")
        self.assertEqual(result.url, "/")
        self.assertEqual(result.status_code, 302)

    def test_confirm_email_code_get_invalid_code(self, *_):
        """there are so many views, this just makes sure it LOADS"""
        self.settings.require_confirm_email = True
        self.settings.save()

        self.local_user.is_active = False
        self.local_user.deactivation_reason = "pending"
        self.local_user.confirmation_code = "12345"
        self.local_user.save(
            broadcast=False,
            update_fields=["is_active", "deactivation_reason", "confirmation_code"],
        )
        view = views.ConfirmEmailCode.as_view()
        request = self.factory.get("")
        request.user = self.anonymous_user

        result = view(request, "abcde")
        self.assertIsInstance(result, TemplateResponse)
        validate_html(result.render())
        self.assertEqual(result.status_code, 200)
        self.assertFalse(self.local_user.is_active)
        self.assertEqual(self.local_user.deactivation_reason, "pending")

    def test_confirm_email_get(self, *_):
        """there are so many views, this just makes sure it LOADS"""
        self.settings.require_confirm_email = True
        self.settings.save()

        login = views.ConfirmEmail.as_view()
        request = self.factory.get("")
        request.user = self.anonymous_user

        result = login(request)
        self.assertIsInstance(result, TemplateResponse)
        validate_html(result.render())
        self.assertEqual(result.status_code, 200)

        request.user = self.local_user
        result = login(request)
        self.assertEqual(result.url, "/")
        self.assertEqual(result.status_code, 302)

    def test_confirm_email_post(self, *_):
        """send the email"""
        self.settings.require_confirm_email = True
        self.settings.save()
        view = views.ConfirmEmail.as_view()
        models.SiteInvite.objects.create(
            code="testcode", user=self.local_user, use_limit=1
        )
        request = self.factory.post("", {"code": "testcode"})
        request.user = self.anonymous_user

        result = view(request)
        validate_html(result.render())

    def test_resend_link_get(self, *_):
        """try again"""
        request = self.factory.get("")
        request.user = self.anonymous_user
        result = views.ResendConfirmEmail.as_view()(request)
        validate_html(result.render())

    def test_resend_link_post(self, *_):
        """try again"""
        request = self.factory.post("", {"email": "mouse@mouse.com"})
        request.user = self.anonymous_user
        with patch("bookwyrm.emailing.send_email") as mock:
            views.ResendConfirmEmail.as_view()(request)
        self.assertEqual(mock.call_count, 1)
