Compare commits
No commits in common. "a6a86a67a5a9f94e8d366451721e742a034f5198" and "3c96defea3ae514cc6ceb69ab0d8fef0a8164b8f" have entirely different histories.
a6a86a67a5
...
3c96defea3
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.env
|
||||
|
||||
*.pyc
|
23
README.md
Normal file
23
README.md
Normal file
@ -0,0 +1,23 @@
|
||||
# Python lib
|
||||
A python lib to interract with the api more easier
|
||||
|
||||
## Installation
|
||||
|
||||
- Clone the project:
|
||||
``` bash
|
||||
git clone https://git.chauvet.pro/michel/ft_transcendence
|
||||
cd ft_transcendence
|
||||
git switch python-api
|
||||
```
|
||||
- Create python virtual environnement.
|
||||
``` bash
|
||||
python3 -m venv .env
|
||||
```
|
||||
- Source the environnement.
|
||||
``` bash
|
||||
source .env/bin/activate
|
||||
```
|
||||
- Install the requirements
|
||||
``` bash
|
||||
pip install -r requirements.txt
|
||||
```
|
4
django/.gitignore
vendored
4
django/.gitignore
vendored
@ -1,4 +0,0 @@
|
||||
.env
|
||||
*.pyc
|
||||
db.sqlite3
|
||||
**/migrations/*
|
@ -1,3 +0,0 @@
|
||||
asgiref==3.7.2
|
||||
Django==4.2.6
|
||||
sqlparse==0.4.4
|
@ -1,3 +0,0 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
@ -1,6 +0,0 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'accounts'
|
@ -1,7 +0,0 @@
|
||||
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)
|
@ -1,6 +0,0 @@
|
||||
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)
|
@ -1,6 +0,0 @@
|
||||
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)
|
@ -1,6 +0,0 @@
|
||||
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)
|
@ -1,3 +0,0 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
@ -1,4 +0,0 @@
|
||||
PASSWORD_MIN_SIZE = 3
|
||||
PASSWORD_MAX_SIZE = 128
|
||||
USERNAME_MIN_SIZE = 3
|
||||
USERNAME_MAX_SIZE = 40
|
@ -1,9 +0,0 @@
|
||||
INVALID_USERNAME: str = "error: username invalid"
|
||||
INVALID_PASSWORD: str = "error: password invalid"
|
||||
INVALID_USERNAME_PASSWORD: str = "error: username or password invalid"
|
||||
USERNAME_ALREADY_USED: str = "error: username already used"
|
||||
|
||||
USER_ADDED: str = "ok: user added"
|
||||
USER_DELETED: str = "ok: account has been deleted"
|
||||
USER_VALID: str = "ok: account valid"
|
||||
PASSWORD_UPDATED: str = "ok: password has been updated"
|
@ -1,9 +0,0 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
<input type="text" name="username" placeholder="username">
|
||||
<input type="text" name="current_password" placeholder="current_password">
|
||||
<input type="text" name="new_password" placeholder="new_password">
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,7 +0,0 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,7 +0,0 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,7 +0,0 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,4 +0,0 @@
|
||||
from .register import *
|
||||
from .login import *
|
||||
from .change_password import *
|
||||
from .delete import *
|
@ -1,76 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class ChangePasswordTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url = "/api/accounts/change_password"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
self.new_password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
|
||||
self.client.post("/api/accounts/register", {"username": self.username, "password": self.password})
|
||||
|
||||
def test_normal_login(self):
|
||||
response: HttpResponse = self.client.post("/api/accounts/login", {"username": self.username, "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, USER_VALID)
|
||||
|
||||
def test_invalid_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_no_new_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
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_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
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_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
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_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, PASSWORD_UPDATED)
|
@ -1,55 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class DeleteTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url = "/api/accounts/delete"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
|
||||
self.client.post("/api/accounts/register", {"username": self.username, "password": self.password})
|
||||
|
||||
def test_invalid_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_normal_delete(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, USER_DELETED)
|
@ -1,50 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class LoginTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url = "/api/accounts/login"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
|
||||
self.client.post("/api/accounts/register", {"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: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, USER_VALID)
|
||||
|
||||
def test_invalid_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_invalid_no_password_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
@ -1,69 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class RegisterTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
self.url: str = "/api/accounts/register"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
|
||||
def test_incomplet_form_no_username_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url)
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_incomplet_form_no_password(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_incomplet_form_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, "error: username invalid")
|
||||
|
||||
def test_incomplet_form_no_username(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
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(response_text, USER_ADDED)
|
||||
|
||||
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_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_username_too_long(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MAX_SIZE + 1), "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
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_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_password_too_long(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MAX_SIZE + 1)})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
|
||||
def test_already_registered(self):
|
||||
self.client.post(self.url, {"username": self.username, "password": self.password})
|
||||
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, USERNAME_ALREADY_USED)
|
@ -1,10 +0,0 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import login, register, delete, change_password
|
||||
|
||||
urlpatterns = [
|
||||
path("login", login.LoginView.as_view(), name="login"),
|
||||
path("register", register.RegisterView.as_view(), name="register"),
|
||||
path("delete", delete.DeleteView.as_view(), name="delete"),
|
||||
path("change_password", change_password.ChangePasswordView.as_view(), name="change_password"),
|
||||
]
|
@ -1,36 +0,0 @@
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
from django.http import HttpResponse, HttpRequest
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.query import QuerySet
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
from ..forms.change_password import ChangePasswordForm
|
||||
|
||||
class ChangePasswordView(View):
|
||||
def get(self, request: HttpRequest):
|
||||
return render(request, "change_password.html")
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
|
||||
form: ChangePasswordForm = ChangePasswordForm(request.POST)
|
||||
if not form.is_valid():
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
username: str = form.cleaned_data['username']
|
||||
current_password: str = form.cleaned_data['current_password']
|
||||
new_password: str = form.cleaned_data['new_password']
|
||||
|
||||
query: QuerySet = User.objects.filter(username=username)
|
||||
if (not query.exists()):
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
user: User = User.objects.get(username=username)
|
||||
if (not user.check_password(current_password)):
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
user.set_password(new_password)
|
||||
user.save()
|
||||
|
||||
return HttpResponse(PASSWORD_UPDATED)
|
@ -1,35 +0,0 @@
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
from django.http import HttpResponse, HttpRequest
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.query import QuerySet
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
from ..forms.delete import DeleteForm
|
||||
|
||||
class DeleteView(View):
|
||||
def get(self, request: HttpRequest):
|
||||
return render(request, "delete.html", {"form": DeleteForm})
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
|
||||
form: DeleteForm = DeleteForm(request.POST)
|
||||
if (not form.is_valid()):
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
username: str = form.cleaned_data['username']
|
||||
password: str = form.cleaned_data['password']
|
||||
|
||||
query: QuerySet = User.objects.filter(username=username)
|
||||
if (not query.exists()):
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
user: User = User.objects.get(username=username)
|
||||
if (not user.check_password(password)):
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
user.delete()
|
||||
|
||||
return HttpResponse(USER_DELETED)
|
||||
|
@ -1,31 +0,0 @@
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.query import QuerySet
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
from ..forms.login import LoginForm
|
||||
|
||||
class LoginView(View):
|
||||
def get(self, request):
|
||||
return render(request, "login.html", {"form": LoginForm})
|
||||
|
||||
def post(self, request):
|
||||
form: LoginForm = LoginForm(request.POST)
|
||||
if not form.is_valid():
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
username: str = form.cleaned_data["username"]
|
||||
password: str = form.cleaned_data["password"]
|
||||
|
||||
query: QuerySet = User.objects.filter(username=username)
|
||||
if (not query.exists()):
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
user: User = User.objects.get(username=username)
|
||||
if (not user.check_password(password)):
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
return HttpResponse(USER_VALID)
|
@ -1,29 +0,0 @@
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
from django.http import HttpResponse, HttpRequest
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.query import QuerySet
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
from ..forms.register import RegisterForm
|
||||
|
||||
class RegisterView(View):
|
||||
def get(self, request: HttpRequest):
|
||||
return render(request, "register.html", {"form": RegisterForm})
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
form: RegisterForm = RegisterForm(request.POST)
|
||||
if not form.is_valid():
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
|
||||
username: str = form.cleaned_data["username"]
|
||||
password: str = form.cleaned_data["password"]
|
||||
|
||||
if User.objects.filter(username=username).exists():
|
||||
return HttpResponse(USERNAME_ALREADY_USED)
|
||||
|
||||
user = User.objects.create_user(username, password=password)
|
||||
user.save()
|
||||
|
||||
return HttpResponse(USER_ADDED)
|
@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Run administrative tasks."""
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,3 +0,0 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
@ -1,6 +0,0 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ProfilesConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'profiles'
|
@ -1,3 +0,0 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
@ -1,3 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
@ -1,7 +0,0 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path("<str:uuid>", views.ProfilePage.as_view(), name="profile_page"),
|
||||
]
|
@ -1,3 +0,0 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
@ -1,16 +0,0 @@
|
||||
"""
|
||||
ASGI config for trancendence project.
|
||||
|
||||
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings')
|
||||
|
||||
application = get_asgi_application()
|
@ -1,128 +0,0 @@
|
||||
"""
|
||||
Django settings for trancendence project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 4.2.6.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.2/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/4.2/ref/settings/
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'django-insecure-18!@88-wm-!skec9^n-85n(f$my^#mh3!#@f=_e@=*arh_yyjj'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
CSRF_TRUSTED_ORIGINS = ['https://code.chauvet.pro', 'https://django.chauvet.pro']
|
||||
|
||||
ALLOWED_HOSTS = ["*"]
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'accounts.apps.AccountsConfig',
|
||||
'profiles.apps.ProfilesConfig',
|
||||
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'trancendence.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'trancendence.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': BASE_DIR / 'db.sqlite3',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/4.2/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/4.2/howto/static-files/
|
||||
|
||||
STATIC_URL = 'static/'
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
@ -1,24 +0,0 @@
|
||||
"""
|
||||
URL configuration for trancendence project.
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/4.2/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/accounts/', include('accounts.urls')),
|
||||
path('api/profiles/', include('accounts.urls')),
|
||||
]
|
@ -1,16 +0,0 @@
|
||||
"""
|
||||
WSGI config for trancendence project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings')
|
||||
|
||||
application = get_wsgi_application()
|
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@ -0,0 +1,5 @@
|
||||
certifi==2023.7.22
|
||||
charset-normalizer==3.3.2
|
||||
idna==3.4
|
||||
requests==2.31.0
|
||||
urllib3==2.0.7
|
16
src/Accounts.py
Normal file
16
src/Accounts.py
Normal file
@ -0,0 +1,16 @@
|
||||
import urls
|
||||
from requests import Response
|
||||
|
||||
class Account:
|
||||
|
||||
def __init__(self, client):
|
||||
self._client = client
|
||||
|
||||
def create(self, username: str, password: str):
|
||||
response: Response = self._client._post(urls.accounts_register, {'username': username, 'password': password})
|
||||
return response.content
|
||||
|
||||
def delete(self):
|
||||
assert self._client.is_authentificate
|
||||
response: Response = self._client._post(urls.accounts_delete, {})
|
||||
return response.content
|
38
src/Client.py
Normal file
38
src/Client.py
Normal file
@ -0,0 +1,38 @@
|
||||
import requests
|
||||
from requests import Response, Request, Session
|
||||
|
||||
import Profiles
|
||||
import Accounts
|
||||
|
||||
import urls
|
||||
|
||||
class Client:
|
||||
def __init__(self, url: str):
|
||||
self.url: str = url
|
||||
self.token: str = None
|
||||
self.session: Session = Session()
|
||||
self.accounts: Account = Accounts.Account(self)
|
||||
self.profiles = Profiles.Profiles(self)
|
||||
|
||||
def is_authentificate(self):
|
||||
return (not self.token is None)
|
||||
|
||||
def login(self, username: str, password: str):
|
||||
response: Response = self._post(urls.accounts_login, {'username': username, 'password': password})
|
||||
return response.content
|
||||
|
||||
def _post(self, uri: str, data: dict = {}):
|
||||
url: str = self.url + uri
|
||||
if self.token is None:
|
||||
response: Response = self.session.get(url)
|
||||
self.token = response.cookies.get('csrftoken')
|
||||
data.update({'csrfmiddlewaretoken': self.token})
|
||||
response: Response = self.session.post(url, data = data, headers = dict(Referer=url))
|
||||
self.token = response.cookies.get('csrftoken')
|
||||
return response
|
||||
|
||||
def _get(self, uri: str):
|
||||
url: str = self.url + uri
|
||||
response: Response = self.session.get(url)
|
||||
self.token = response.cookies.get('csrftoken')
|
||||
return response
|
27
src/Profile.py
Normal file
27
src/Profile.py
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
|
||||
class Profile:
|
||||
|
||||
def __init__(self, data: dict = None, username: str = None, title: str = None):
|
||||
|
||||
if (data is None):
|
||||
self._from_value(username, title)
|
||||
else:
|
||||
self._from_dict(data)
|
||||
|
||||
def _from_value(self, username: str, title: str):
|
||||
self.username = username
|
||||
self.title = title
|
||||
return self
|
||||
|
||||
def _from_dict(self, data: dict):
|
||||
self._from_value(data.get('username'), data.get('title'))
|
||||
return self
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Profile):
|
||||
return self.username == other.username and self.title == other.title
|
||||
return False
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
17
src/Profiles.py
Normal file
17
src/Profiles.py
Normal file
@ -0,0 +1,17 @@
|
||||
import urls
|
||||
import Client
|
||||
|
||||
from requests import Response
|
||||
import Profile
|
||||
|
||||
class Profiles:
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
def get(self, user_id: int):
|
||||
response: Response = self.client._get(urls.profiles_page + str(user_id))
|
||||
if response.status_code == 404:
|
||||
return None
|
||||
content: dict = eval(response.content)
|
||||
return Profile.Profile(data = content)
|
65
src/tests.py
Normal file
65
src/tests.py
Normal file
@ -0,0 +1,65 @@
|
||||
import Client
|
||||
import Profile
|
||||
from urls import *
|
||||
|
||||
from uuid import uuid4
|
||||
import os
|
||||
|
||||
def test(value, expected_value, title, description = None):
|
||||
print(title, end=" ")
|
||||
if (value == expected_value):
|
||||
print("[OK]")
|
||||
return
|
||||
print ("[ERROR]")
|
||||
print ("expected", expected_value, ", got", value)
|
||||
if not description is None:
|
||||
print (description)
|
||||
|
||||
def test_accounts_register(client, username, password):
|
||||
print ("REGISTER")
|
||||
|
||||
test(client.accounts.create(username, password), b'ok: user added', 'normal', None)
|
||||
|
||||
print()
|
||||
|
||||
def test_accounts_login(client, username, password):
|
||||
|
||||
print ("LOGIN")
|
||||
|
||||
test(client.login(username, password), b'ok: account valid', "normal", None)
|
||||
|
||||
print()
|
||||
|
||||
def test_accounts_delete(client):
|
||||
|
||||
print ("DELETE")
|
||||
|
||||
test(client.accounts.delete(), b'ok: account has been deleted', 'normal')
|
||||
|
||||
print()
|
||||
|
||||
def test_profile_get(client):
|
||||
|
||||
print ("GET")
|
||||
|
||||
test(client.profiles.get(1), Profile.Profile(username="997e13f5-474d-4fea-b55a-ad8a27b9534b", title=""), "normal")
|
||||
|
||||
print()
|
||||
|
||||
def tests():
|
||||
|
||||
username = uuid4()
|
||||
password = uuid4()
|
||||
|
||||
client = Client.Client("http://0.0.0.0:8000/")
|
||||
|
||||
print("ACCOUNTS".center(os.get_terminal_size()[0], '-'))
|
||||
test_accounts_register(client, username, password)
|
||||
test_accounts_login(client, username, password)
|
||||
test_accounts_delete(client)
|
||||
|
||||
print("ACCOUNTS".center(os.get_terminal_size()[0], '-'))
|
||||
test_profile_get(client)
|
||||
|
||||
if __name__ == "__main__":
|
||||
tests()
|
9
src/urls.py
Normal file
9
src/urls.py
Normal file
@ -0,0 +1,9 @@
|
||||
api: str = "api/"
|
||||
|
||||
accounts: str = api + "accounts/"
|
||||
accounts_login: str = accounts + "login"
|
||||
accounts_delete: str = accounts + "delete"
|
||||
accounts_register: str = accounts + "register"
|
||||
|
||||
profiles: str = api + "profiles/"
|
||||
profiles_page: str = profiles
|
Loading…
Reference in New Issue
Block a user