import datetime

from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from django.utils.translation import gettext as _

from registration.models import RegistrationProfile

from newsreader.accounts.models import User


class ActivationTestCase(TestCase):
    def setUp(self):
        self.register_url = reverse("accounts:register")
        self.register_success_url = reverse("accounts:register-complete")
        self.success_url = reverse("accounts:activate-complete")

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

        response = self.client.post(self.register_url, data)
        self.assertRedirects(response, self.register_success_url)

        register_profile = RegistrationProfile.objects.get()

        kwargs = {"activation_key": register_profile.activation_key}
        response = self.client.get(reverse("accounts:activate", kwargs=kwargs))

        self.assertRedirects(response, self.success_url)

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

        response = self.client.post(self.register_url, data)

        register_profile = RegistrationProfile.objects.get()
        user = register_profile.user

        user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
        user.save()

        kwargs = {"activation_key": register_profile.activation_key}
        response = self.client.get(reverse("accounts:activate", kwargs=kwargs))

        self.assertEqual(200, response.status_code)
        self.assertContains(response, _("Account activation failed"))

        user.refresh_from_db()
        self.assertFalse(user.is_active)

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

        response = self.client.post(self.register_url, data)
        self.assertRedirects(response, self.register_success_url)

        kwargs = {"activation_key": "not-a-valid-key"}
        response = self.client.get(reverse("accounts:activate", kwargs=kwargs))

        self.assertContains(response, _("Account activation failed"))

        user = User.objects.get()

        self.assertEquals(user.is_active, False)

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

        response = self.client.post(self.register_url, data)
        self.assertRedirects(response, self.register_success_url)

        register_profile = RegistrationProfile.objects.get()

        kwargs = {"activation_key": register_profile.activation_key}
        response = self.client.get(reverse("accounts:activate", kwargs=kwargs))

        self.assertRedirects(response, self.success_url)

        # try this a second time
        response = self.client.get(reverse("accounts:activate", kwargs=kwargs))

        self.assertRedirects(response, self.success_url)
