2023-10-25 10:17:55 -04:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
# Create your tests here.
|
2023-11-11 13:50:14 -05:00
|
|
|
from rest_framework import status
|
2023-10-25 10:17:55 -04:00
|
|
|
from django.test.client import Client
|
2023-10-31 16:14:24 -04:00
|
|
|
from django.contrib.auth.models import User
|
2023-10-25 10:17:55 -04:00
|
|
|
from django.http import HttpResponse
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
class RegisterTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.client = Client()
|
|
|
|
|
2023-11-11 13:50:14 -05:00
|
|
|
self.url: str = "/accounts/register"
|
2023-10-25 10:17:55 -04:00
|
|
|
|
2023-10-31 16:14:24 -04:00
|
|
|
self.username: str = str(uuid.uuid4())
|
|
|
|
self.password: str = str(uuid.uuid4())
|
|
|
|
|
|
|
|
def test_normal_register(self):
|
|
|
|
response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
|
2023-11-11 13:50:14 -05:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
2023-10-25 10:17:55 -04:00
|
|
|
|
|
|
|
def test_incomplet_form_no_username_no_password(self):
|
|
|
|
response: HttpResponse = self.client.post(self.url)
|
2023-10-31 16:14:24 -04:00
|
|
|
errors: dict = eval(response.content)
|
2023-11-11 13:50:14 -05:00
|
|
|
errors_expected: dict = {'username': ['This field is required.'], 'password': ['This field is required.']}
|
2023-10-31 16:14:24 -04:00
|
|
|
self.assertEqual(errors, errors_expected)
|
2023-10-25 10:17:55 -04:00
|
|
|
|
|
|
|
def test_incomplet_form_no_password(self):
|
|
|
|
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
2023-10-31 16:14:24 -04:00
|
|
|
errors: dict = eval(response.content)
|
2023-11-11 13:50:14 -05:00
|
|
|
errors_expected: dict = {'password': ['This field is required.']}
|
2023-10-31 16:14:24 -04:00
|
|
|
self.assertEqual(errors, errors_expected)
|
2023-10-25 10:17:55 -04:00
|
|
|
|
|
|
|
def test_incomplet_form_no_username(self):
|
|
|
|
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
2023-10-31 16:14:24 -04:00
|
|
|
errors: dict = eval(response.content)
|
|
|
|
errors_expected: dict = {}
|
|
|
|
self.assertEqual(errors, errors_expected)
|
2023-10-25 10:17:55 -04:00
|
|
|
|
|
|
|
def test_incomplet_form_no_username(self):
|
2023-10-31 16:14:24 -04:00
|
|
|
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
|
|
|
errors: dict = eval(response.content)
|
2023-11-11 13:50:14 -05:00
|
|
|
errors_expected: dict = {'username': ['This field is required.']}
|
2023-10-31 16:14:24 -04:00
|
|
|
self.assertEqual(errors, errors_expected)
|
2023-10-25 10:17:55 -04:00
|
|
|
|
|
|
|
def test_already_registered(self):
|
2023-10-31 16:14:24 -04:00
|
|
|
User(username=self.username, password=self.password).save()
|
|
|
|
response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
|
|
|
|
errors: dict = eval(response.content)
|
2023-11-11 13:50:14 -05:00
|
|
|
errors_expected: dict = {'username': ['A user with that username already exists.']}
|
2023-10-31 16:14:24 -04:00
|
|
|
self.assertEqual(errors, errors_expected)
|