docker setup
This commit is contained in:
4
srcs/accounts/tests/__init__.py
Normal file
4
srcs/accounts/tests/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
from .register import *
|
||||
from .login import *
|
||||
from .change_password import *
|
||||
from .delete import *
|
31
srcs/accounts/tests/change_password.py
Normal file
31
srcs/accounts/tests/change_password.py
Normal file
@ -0,0 +1,31 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import uuid
|
||||
|
||||
class ChangePasswordTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url = "/accounts/change_password"
|
||||
|
||||
self.username: str = str(uuid.uuid4())
|
||||
self.password: str = str(uuid.uuid4())
|
||||
self.new_password: str = str(uuid.uuid4())
|
||||
|
||||
User.objects.create_user(username = self.username, password = self.password)
|
||||
|
||||
def test_normal(self):
|
||||
self.client.login(username = self.username, password = self.password)
|
||||
response: HttpResponse = self.client.post(self.url, {"current_password": self.password, "new_password": self.new_password})
|
||||
response_text: str = response.content.decode('utf-8')
|
||||
self.assertEqual(response_text, '"password changed"')
|
||||
|
||||
def test_nologged(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"current_password": self.password, "new_password": self.new_password})
|
||||
errors: dict = eval(response.content)
|
||||
self.assertDictEqual(errors, {'detail': 'Authentication credentials were not provided.'})
|
32
srcs/accounts/tests/delete.py
Normal file
32
srcs/accounts/tests/delete.py
Normal file
@ -0,0 +1,32 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import uuid
|
||||
|
||||
class DeleteTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url = "/accounts/delete"
|
||||
|
||||
self.username: str = str(uuid.uuid4())
|
||||
self.password: str = str(uuid.uuid4())
|
||||
|
||||
user: User = User.objects.create_user(username=self.username, password=self.password)
|
||||
self.client.login(username=self.username, password=self.password)
|
||||
|
||||
|
||||
def test_normal_delete(self):
|
||||
response: HttpResponse = self.client.post(self.url)
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, '"user deleted"')
|
||||
|
||||
def test_no_logged(self):
|
||||
self.client.logout()
|
||||
response: HttpResponse = self.client.post(self.url)
|
||||
errors: dict = eval(response.content)
|
||||
self.assertDictEqual(errors, {"detail":"Authentication credentials were not provided."})
|
53
srcs/accounts/tests/login.py
Normal file
53
srcs/accounts/tests/login.py
Normal file
@ -0,0 +1,53 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
class LoginTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url = "/accounts/login"
|
||||
|
||||
self.username: str = str(uuid.uuid4())
|
||||
self.password: str = str(uuid.uuid4())
|
||||
|
||||
User.objects.create_user(username=self.username, password=self.password)
|
||||
|
||||
def test_normal_login(self):
|
||||
response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
|
||||
response_text = response.content.decode('utf-8')
|
||||
#self.assertEqual(response_text, 'user connected')
|
||||
|
||||
def test_invalid_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'user': ['Username or password wrong.']}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_invalid_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'user': ['Username or password wrong.']}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_invalid_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': ['This field is required.']}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_invalid_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'password': ['This field is required.']}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_invalid_no_password_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': ['This field is required.'], 'password': ['This field is required.']}
|
||||
self.assertEqual(errors, errors_expected)
|
17
srcs/accounts/tests/logout.py
Normal file
17
srcs/accounts/tests/logout.py
Normal file
@ -0,0 +1,17 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from django.test.client import Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth import login
|
||||
|
||||
class LoginTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url = "/accounts/logout"
|
||||
|
||||
self.client.login()
|
||||
|
||||
def test_normal_logout(self):
|
||||
response: HttpResponse = self.client.post(self.url)
|
||||
self.assertNotIn('_auth_user_id', self.client.session)
|
52
srcs/accounts/tests/register.py
Normal file
52
srcs/accounts/tests/register.py
Normal file
@ -0,0 +1,52 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from rest_framework import status
|
||||
from django.test.client import Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
class RegisterTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url: str = "/accounts/register"
|
||||
|
||||
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})
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
def test_incomplet_form_no_username_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': ['This field is required.'], 'password': ['This field is required.']}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_incomplet_form_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'password': ['This field is required.']}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_incomplet_form_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_incomplet_form_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': ['This field is required.']}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_already_registered(self):
|
||||
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': ['A user with that username already exists.']}
|
||||
self.assertEqual(errors, errors_expected)
|
Reference in New Issue
Block a user