2023-10-25 10:17:55 -04:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
# Create your tests here.
|
|
|
|
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
|
|
|
|
|
|
|
|
from ..status_code import *
|
|
|
|
|
|
|
|
class RegisterTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.client = Client()
|
|
|
|
|
|
|
|
self.url: str = "/api/accounts/register"
|
|
|
|
|
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})
|
|
|
|
response_text: str = response.content.decode('utf-8')
|
|
|
|
self.assertEqual(USER_ADDED, response_text)
|
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)
|
|
|
|
errors_expected: dict = {'username': [USERNAME_MISSING], 'password': [PASSWORD_MISSING]}
|
|
|
|
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)
|
|
|
|
errors_expected: dict = {'password': [PASSWORD_MISSING]}
|
|
|
|
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)
|
|
|
|
errors_expected: dict = {'username': [USERNAME_MISSING]}
|
|
|
|
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)
|
|
|
|
errors_expected: dict = {'username': [USERNAME_ALREADY_USED]}
|
|
|
|
self.assertEqual(errors, errors_expected)
|