from django.core import mail
from django.test import TransactionTestCase as TestCase
from django.test.utils import override_settings
from django.urls import reverse
from django.utils.translation import gettext as _

from registration.models import RegistrationProfile

from newsreader.accounts.models import User
from newsreader.accounts.tests.factories import UserFactory


class RegistrationTestCase(TestCase):
    def setUp(self):
        self.url = reverse("accounts:register")
        self.success_url = reverse("accounts:register-complete")
        self.disallowed_url = reverse("accounts:register-closed")

    def test_simple(self):
        response = self.client.get(self.url)

        self.assertEquals(response.status_code, 200)

    def test_registration(self):
        data = {
            "email": "test@test.com",
            "password1": "test12456",
            "password2": "test12456",
        }

        response = self.client.post(self.url, data)
        self.assertRedirects(response, self.success_url)

        self.assertEquals(User.objects.count(), 1)
        self.assertEquals(RegistrationProfile.objects.count(), 1)

        user = User.objects.get()

        self.assertEquals(user.is_active, False)
        self.assertEquals(len(mail.outbox), 1)

    def test_existing_email(self):
        UserFactory(email="test@test.com")

        data = {
            "email": "test@test.com",
            "password1": "test12456",
            "password2": "test12456",
        }

        response = self.client.post(self.url, data)
        self.assertEquals(response.status_code, 200)

        self.assertEquals(User.objects.count(), 1)
        self.assertContains(response, _("User with this Email address already exists"))

    def test_pending_registration(self):
        data = {
            "email": "test@test.com",
            "password1": "test12456",
            "password2": "test12456",
        }

        response = self.client.post(self.url, data)
        self.assertRedirects(response, self.success_url)

        self.assertEquals(User.objects.count(), 1)
        self.assertEquals(RegistrationProfile.objects.count(), 1)

        user = User.objects.get()

        self.assertEquals(user.is_active, False)
        self.assertEquals(len(mail.outbox), 1)

        response = self.client.post(self.url, data)
        self.assertEquals(response.status_code, 200)
        self.assertContains(response, _("User with this Email address already exists"))

    def test_disabled_account(self):
        UserFactory(email="test@test.com", is_active=False)

        data = {
            "email": "test@test.com",
            "password1": "test12456",
            "password2": "test12456",
        }

        response = self.client.post(self.url, data)
        self.assertEquals(response.status_code, 200)

        self.assertEquals(User.objects.count(), 1)
        self.assertContains(response, _("User with this Email address already exists"))

    @override_settings(REGISTRATION_OPEN=False)
    def test_registration_closed(self):
        response = self.client.get(self.url)

        self.assertRedirects(response, self.disallowed_url)

        data = {
            "email": "test@test.com",
            "password1": "test12456",
            "password2": "test12456",
        }

        response = self.client.post(self.url, data)
        self.assertRedirects(response, self.disallowed_url)

        self.assertEquals(User.objects.count(), 0)
        self.assertEquals(RegistrationProfile.objects.count(), 0)
