"""
User model test
"""
from django.test import TestCase

from .models import User

# Create your tests here.


class TestUser(TestCase):
    """
    Testing user api
    """

    @classmethod
    def setUp(cls):  # Create a user
        testuser1 = User.objects.create_user(
            email="<EMAIL>", password="<PASSWORD>"
        )
        testuser1.save()

    def test_login(self):
        """
        Testing user login
        """
        response = self.client.post(
            "/accounts/login/",
            {"email": "<EMAIL>", "password": "<PASSWORD>"},
            format="json",
        )
        self.assertEqual(response.status_code, 200)
        self.assertIn("key", response.json(), "Not contain key")

    def test_logout(self):
        """
        Testing user logout
        """
        response = self.client.post(
            "/accounts/login/",
            {"email": "<EMAIL>", "password": "<PASSWORD>"},
            format="json",
        )

        headers = {"HTTP_AUTHORIZATION": f"Token {response.json()['key']}"}
        response = self.client.post(
            "/accounts/logout/",
            **headers,
        )

        self.assertEqual(response.status_code, 200)

    def test_auth(self):
        """
        Testing user auth after login
        """
        response = self.client.post(
            "/accounts/login/",
            {"email": "<EMAIL>", "password": "<PASSWORD>"},
            format="json",
        )

        headers = {"HTTP_AUTHORIZATION": f"Token {response.json()['key']}"}
        response = self.client.get(
            "/accounts/test_auth/",
            **headers,
        )
        self.assertEqual(response.status_code, 200)
