core: accounts: use django form

This commit is contained in:
starnakin 2023-10-25 16:10:23 +02:00
parent 4cda3de144
commit 2c7bbb5a5f
13 changed files with 83 additions and 62 deletions

View File

@ -0,0 +1,7 @@
from django import forms
from ..settings import *
class ChangePasswordForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
current_password = forms.CharField(label="current_password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)
new_password = forms.CharField(label="new_password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -0,0 +1,6 @@
from django import forms
from ..settings import *
class DeleteForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -0,0 +1,6 @@
from django import forms
from ..settings import *
class LoginForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -0,0 +1,6 @@
from django import forms
from ..settings import *
class RegisterForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -1,8 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
<input type="text" name="username" placeholder="username"> {{ form }}
<input type="text" name="password" placeholder="password">
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -1,8 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
<input type="text" name="username" placeholder="username"> {{ form }}
<input type="text" name="password" placeholder="password">
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -1,8 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
<input type="text" name="username" placeholder="username"> {{ form }}
<input type="text" name="password" placeholder="password">
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -20,12 +20,12 @@ class RegisterTest(TestCase):
def test_incomplet_form_no_username_no_password(self): def test_incomplet_form_no_username_no_password(self):
response: HttpResponse = self.client.post(self.url) response: HttpResponse = self.client.post(self.url)
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_incomplet_form_no_password(self): def test_incomplet_form_no_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username}) response: HttpResponse = self.client.post(self.url, {"username": self.username})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_incomplet_form_no_username(self): def test_incomplet_form_no_username(self):
response: HttpResponse = self.client.post(self.url, {"password": self.password}) response: HttpResponse = self.client.post(self.url, {"password": self.password})
@ -35,7 +35,7 @@ class RegisterTest(TestCase):
def test_incomplet_form_no_username(self): def test_incomplet_form_no_username(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username}) response: HttpResponse = self.client.post(self.url, {"username": self.username})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_normal_register(self): def test_normal_register(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password})
@ -45,22 +45,22 @@ class RegisterTest(TestCase):
def test_username_too_short(self): def test_username_too_short(self):
response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MIN_SIZE - (USERNAME_MIN_SIZE > 0)), "password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MIN_SIZE - (USERNAME_MIN_SIZE > 0)), "password": self.password})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_username_too_long(self): def test_username_too_long(self):
response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MAX_SIZE + 1), "password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MAX_SIZE + 1), "password": self.password})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_password_too_short(self): def test_password_too_short(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_password_too_long(self): def test_password_too_long(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MAX_SIZE + 1)}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MAX_SIZE + 1)})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_already_registered(self): def test_already_registered(self):
self.client.post(self.url, {"username": self.username, "password": self.password}) self.client.post(self.url, {"username": self.username, "password": self.password})
@ -246,17 +246,17 @@ class ChangePasswordTest(TestCase):
def test_no_new_password(self): def test_no_new_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_new_password_to_short(self): def test_new_password_to_short(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_new_password_to_long(self): def test_new_password_to_long(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MAX_SIZE + 1)}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MAX_SIZE + 1)})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_normal_change_password(self): def test_normal_change_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": self.new_password}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": self.new_password})

View File

@ -3,8 +3,8 @@ from django.urls import path
from .views import login, register, delete, change_password from .views import login, register, delete, change_password
urlpatterns = [ urlpatterns = [
path("login", login.Login.as_view(), name="login"), path("login", login.LoginView.as_view(), name="login"),
path("register", register.Register.as_view(), name="register"), path("register", register.RegisterView.as_view(), name="register"),
path("delete", delete.Delete.as_view(), name="delete"), path("delete", delete.DeleteView.as_view(), name="delete"),
path("change_password", change_password.ChangePassword.as_view(), name="change_password"), path("change_password", change_password.ChangePasswordView.as_view(), name="change_password"),
] ]

View File

@ -1,24 +1,26 @@
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.http import HttpResponse from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from ..status_code import * from ..status_code import *
from ..settings import * from ..settings import *
from ..forms.change_password import ChangePasswordForm
class ChangePassword(View): class ChangePasswordView(View):
def get(self, request): def get(self, request: HttpRequest):
return render(request, "change_password.html") return render(request, "change_password.html")
def post(self, request): def post(self, request: HttpRequest):
username = request.POST.get("username")
if (username == None): form: ChangePasswordForm = ChangePasswordForm(request.POST)
if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD) return HttpResponse(INVALID_USERNAME_PASSWORD)
current_password = request.POST.get("current_password") username: str = form.cleaned_data['username']
if (current_password == None): current_password: str = form.cleaned_data['current_password']
return HttpResponse(INVALID_USERNAME_PASSWORD) new_password: str = form.cleaned_data['new_password']
query: QuerySet = User.objects.filter(username=username) query: QuerySet = User.objects.filter(username=username)
if (not query.exists()): if (not query.exists()):
@ -28,10 +30,6 @@ class ChangePassword(View):
if (not user.check_password(current_password)): if (not user.check_password(current_password)):
return HttpResponse(INVALID_USERNAME_PASSWORD) return HttpResponse(INVALID_USERNAME_PASSWORD)
new_password = request.POST.get("new_password")
if (new_password == None or not PASSWORD_MAX_SIZE >= len(new_password) >= PASSWORD_MIN_SIZE):
return HttpResponse(INVALID_PASSWORD)
user.set_password(new_password) user.set_password(new_password)
user.save() user.save()

View File

@ -1,24 +1,25 @@
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.http import HttpResponse from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from ..status_code import * from ..status_code import *
from ..settings import * from ..settings import *
from ..forms.delete import DeleteForm
class Delete(View): class DeleteView(View):
def get(self, request): def get(self, request: HttpRequest):
return render(request, "delete.html") return render(request, "delete.html", {"form": DeleteForm})
def post(self, request): def post(self, request: HttpRequest):
username = request.POST.get("username")
if (username == None): form: DeleteForm = DeleteForm(request.POST)
if (not form.is_valid()):
return HttpResponse(INVALID_USERNAME_PASSWORD) return HttpResponse(INVALID_USERNAME_PASSWORD)
password = request.POST.get("password") username: str = form.cleaned_data['username']
if (password == None): password: str = form.cleaned_data['password']
return HttpResponse(INVALID_USERNAME_PASSWORD)
query: QuerySet = User.objects.filter(username=username) query: QuerySet = User.objects.filter(username=username)
if (not query.exists()): if (not query.exists()):

View File

@ -4,22 +4,21 @@ from django.http import HttpResponse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from ..status_code import * from ..status_code import *
from ..settings import * from ..settings import *
from ..forms.login import LoginForm
class Login(View): class LoginView(View):
def get(self, request): def get(self, request):
return render(request, "login.html") return render(request, "login.html", {"form": LoginForm})
def post(self, request): def post(self, request):
username = request.POST.get("username") form: LoginForm = LoginForm(request.POST)
if (username == None): if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD) return HttpResponse(INVALID_USERNAME_PASSWORD)
password = request.POST.get("password") username: str = form.cleaned_data["username"]
if (password == None): password: str = form.cleaned_data["password"]
return HttpResponse(INVALID_USERNAME_PASSWORD)
query: QuerySet = User.objects.filter(username=username) query: QuerySet = User.objects.filter(username=username)
if (not query.exists()): if (not query.exists()):

View File

@ -1,23 +1,24 @@
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.http import HttpResponse from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from ..status_code import * from ..status_code import *
from ..settings import * from ..settings import *
from ..forms.register import RegisterForm
class Register(View): class RegisterView(View):
def get(self, request): def get(self, request: HttpRequest):
return render(request, "register.html") return render(request, "register.html", {"form": RegisterForm})
def post(self, request): def post(self, request: HttpRequest):
password = request.POST.get("password") form: RegisterForm = RegisterForm(request.POST)
if (password == None or not PASSWORD_MAX_SIZE >= len(password) >= PASSWORD_MIN_SIZE): if not form.is_valid():
return HttpResponse(INVALID_PASSWORD) return HttpResponse(INVALID_USERNAME_PASSWORD)
username = request.POST.get("username")
if (username == None or not USERNAME_MAX_SIZE >= len(username) >= USERNAME_MIN_SIZE): username: str = form.cleaned_data["username"]
return HttpResponse(INVALID_USERNAME) password: str = form.cleaned_data["password"]
if User.objects.filter(username=username).exists(): if User.objects.filter(username=username).exists():
return HttpResponse(USERNAME_ALREADY_USED) return HttpResponse(USERNAME_ALREADY_USED)