<filename>backend/accounts/tests.py<gh_stars>0
import json

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

from .serializers import UserSerializer

from rest_framework import status
from rest_framework.test import APIClient
from django_session_jwt.test import Client


class RegisterUserTestCase(TestCase):
    client = Client()

    def test_create_user(self):
        url = reverse("user-list")

        username = "matt"
        password = "<PASSWORD>"
        email = "<EMAIL>"
        first_name = "Matthew"
        last_name = "Drayton"

        # Creating a new account
        response = self.client.post(
            url,
            {
                "username": username,
                "password": password,
                "email": email,
                "first_name": first_name,
                "last_name": last_name,
            },
            format="json",
        )
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # Creating a duplicate account
        response = self.client.post(
            url,
            {
                "username": username,
                "password": password,
                "email": email,
                "first_name": first_name,
                "last_name": last_name,
            },
            format="json",
        )
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        # Check user has been added to database
        User = get_user_model()
        user_exists = User.objects.filter(
            username=username, email=email, first_name=first_name, last_name=last_name
        ).exists()
        self.assertTrue(user_exists)

        # Check we can log in successfully
        logged_in = self.client.login(username=username, password=password)
        self.assertNotEqual(logged_in, None)


class LoginUserTestCase(TestCase):
    client = APIClient()

    def setUp(self):
        User = get_user_model()
        User.objects.create_user(username="matt", password="<PASSWORD>")

    def test_login_logout(self):
        url = reverse("login")
        # Test valid login
        response = self.client.post(
            url, {"username": "matt", "password": "<PASSWORD>"}, format="json"
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        user = response.wsgi_request.user

        url = reverse("logout")
        response = self.client.get(url, format="json")
        self.assertNotEqual(user, response.wsgi_request.user)

    def test_invalid_login(self):
        url = reverse("login")
        response = self.client.post(
            url, {"username": "fake", "password": "<PASSWORD>"}, format="json"
        )
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)


class GetUsersTestCase(TestCase):

    client = APIClient()

    def setUp(self):
        User = get_user_model()
        User.objects.create(
            username="casperBD",
            first_name="Casper",
            last_name="<NAME>",
            email="<EMAIL>",
        )
        User.objects.create(
            username="muffinG",
            first_name="Muffin",
            last_name="Gradane",
            email="<EMAIL>",
        )
        User.objects.create(
            username="ramboL",
            first_name="Rambo",
            last_name="Labrador",
            email="<EMAIL>",
        )

    def test_get_users(self):
        """
        Test that the user-list route returns a list of users.
        """
        # Setup.
        url = reverse("user-list")
        # Run.
        response = self.client.get(url, format="json")
        # Check.
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data,
            [
                {
                    "url": "http://testserver/api/users/1/",
                    "id": 1,
                    "username": "casperBD",
                    "first_name": "Casper",
                    "last_name": "<NAME>",
                    "email": "<EMAIL>",
                },
                {
                    "url": "http://testserver/api/users/2/",
                    "id": 2,
                    "username": "muffinG",
                    "first_name": "Muffin",
                    "last_name": "Gradane",
                    "email": "<EMAIL>",
                },
                {
                    "url": "http://testserver/api/users/3/",
                    "id": 3,
                    "username": "ramboL",
                    "first_name": "Rambo",
                    "last_name": "Labrador",
                    "email": "<EMAIL>",
                },
            ],
        )
