docker setup
This commit is contained in:
0
srcs/accounts/__init__.py
Normal file
0
srcs/accounts/__init__.py
Normal file
3
srcs/accounts/admin.py
Normal file
3
srcs/accounts/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
srcs/accounts/apps.py
Normal file
6
srcs/accounts/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'accounts'
|
7
srcs/accounts/serializers/change_password.py
Normal file
7
srcs/accounts/serializers/change_password.py
Normal file
@ -0,0 +1,7 @@
|
||||
from rest_framework.serializers import Serializer, CharField
|
||||
|
||||
class ChangePasswordSerializer(Serializer):
|
||||
|
||||
current_password = CharField()
|
||||
new_password = CharField()
|
||||
|
12
srcs/accounts/serializers/login.py
Normal file
12
srcs/accounts/serializers/login.py
Normal file
@ -0,0 +1,12 @@
|
||||
from rest_framework.serializers import Serializer, CharField
|
||||
from django.contrib.auth import authenticate
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
class LoginSerializer(Serializer):
|
||||
|
||||
username = CharField()
|
||||
password = CharField()
|
||||
|
||||
def get_user(self, data):
|
||||
user = authenticate(username=data['username'], password=data['password'])
|
||||
return user
|
12
srcs/accounts/serializers/register.py
Normal file
12
srcs/accounts/serializers/register.py
Normal file
@ -0,0 +1,12 @@
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class RegisterSerialiser(ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['username', 'password']
|
||||
|
||||
def create(self, data):
|
||||
user_obj = User.objects.create_user(username=data['username'], password=data['password'])
|
||||
user_obj.save()
|
||||
return user_obj
|
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)
|
12
srcs/accounts/urls.py
Normal file
12
srcs/accounts/urls.py
Normal file
@ -0,0 +1,12 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import register, login, logout, delete, change_password
|
||||
|
||||
urlpatterns = [
|
||||
path("register", register.RegisterView.as_view(), name="register"),
|
||||
path("login", login.LoginView.as_view(), name="login"),
|
||||
path("logout", logout.LogoutView.as_view(), name="logout"),
|
||||
path("delete", delete.DeleteView.as_view(), name="delete"),
|
||||
path("change_password", change_password.ChangePasswordView.as_view(), name="change_password")
|
||||
|
||||
]
|
25
srcs/accounts/views/change_password.py
Normal file
25
srcs/accounts/views/change_password.py
Normal file
@ -0,0 +1,25 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import permissions, status
|
||||
from django.http import HttpRequest
|
||||
from django.contrib.auth import login
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from ..serializers.change_password import ChangePasswordSerializer
|
||||
|
||||
class ChangePasswordView(APIView):
|
||||
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
data = request.data
|
||||
|
||||
serializer = ChangePasswordSerializer(data=data)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
user: User = request.user
|
||||
if (user.check_password(data['current_password']) == 0):
|
||||
return Response({'current_password': "The password is not right."}, status=status.HTTP_200_OK)
|
||||
user.set_password(data["new_password"])
|
||||
return Response('password changed', status=status.HTTP_200_OK)
|
12
srcs/accounts/views/delete.py
Normal file
12
srcs/accounts/views/delete.py
Normal file
@ -0,0 +1,12 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework import permissions, status
|
||||
from rest_framework.response import Response
|
||||
from django.http import HttpRequest
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
class DeleteView(APIView):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
def post(self, request: HttpRequest):
|
||||
request.user.delete()
|
||||
return Response("user deleted", status=status.HTTP_200_OK)
|
23
srcs/accounts/views/login.py
Normal file
23
srcs/accounts/views/login.py
Normal file
@ -0,0 +1,23 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import permissions, status
|
||||
from django.http import HttpRequest
|
||||
from django.contrib.auth import login
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
from ..serializers.login import LoginSerializer
|
||||
|
||||
class LoginView(APIView):
|
||||
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
data = request.data
|
||||
serializer = LoginSerializer(data=data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
user = serializer.get_user(data)
|
||||
if user is None:
|
||||
return Response({'user': ['Username or password wrong.']}, status.HTTP_200_OK)
|
||||
login(request, user)
|
||||
return Response('user connected', status=status.HTTP_200_OK)
|
13
srcs/accounts/views/logout.py
Normal file
13
srcs/accounts/views/logout.py
Normal file
@ -0,0 +1,13 @@
|
||||
from rest_framework.views import APIView
|
||||
from django.contrib.auth import logout
|
||||
from rest_framework import permissions, status
|
||||
from rest_framework.response import Response
|
||||
from django.http import HttpRequest
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
class LogoutView(APIView):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
def post(self, request: HttpRequest):
|
||||
logout(request)
|
||||
return Response("user unlogged", status=status.HTTP_200_OK)
|
16
srcs/accounts/views/register.py
Normal file
16
srcs/accounts/views/register.py
Normal file
@ -0,0 +1,16 @@
|
||||
from rest_framework import permissions, status
|
||||
from ..serializers.register import RegisterSerialiser
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from django.http import HttpRequest
|
||||
|
||||
class RegisterView(APIView):
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
def post(self, request: HttpRequest):
|
||||
data = request.data
|
||||
serializer = RegisterSerialiser(data=data)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
user = serializer.create(data)
|
||||
if user:
|
||||
return Response("user created", status=status.HTTP_201_CREATED)
|
||||
return Response(status=status.HTTP_400_BAD_REQUEST)
|
Reference in New Issue
Block a user