from django.test import TestCase
from django.urls import reverse

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


class SettingsViewTestCase(TestCase):
    def setUp(self):
        self.user = UserFactory(email="test@test.nl", password="test")
        self.client.force_login(self.user)

        self.url = reverse("accounts:settings:home")

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

        self.assertEquals(response.status_code, 200)

    def test_user_credential_change(self):
        response = self.client.post(
            reverse("accounts:settings:home"),
            {"first_name": "First name", "last_name": "Last name"},
        )

        user = User.objects.get()

        self.assertRedirects(response, reverse("accounts:settings:home"))

        self.assertEquals(user.first_name, "First name")
        self.assertEquals(user.last_name, "Last name")
