diff --git a/doc/requests.org b/doc/requests.org new file mode 100644 index 0000000..b0c38fa --- /dev/null +++ b/doc/requests.org @@ -0,0 +1,76 @@ +* Requests +Ejemplo de request contra la api usando [[https://github.com/federicotdn/verb][verb]] + +** Autenticación :verb: +template http://localhost:7000/api +Content-Type: application/json; +*** Solicitar token +post /token/ + +{ + "username": "admin", + "password": "123" +} +**** respuesta +#+begin_src json +{ + "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTc3MTE4NzYxOSwiaWF0IjoxNzcxMTAxMjE5LCJqdGkiOiI5ZTgzNGRlM2QzMmQ0NmQyODEwZGQ2MjI2ODUwNjgzNyIsInVzZXJfaWQiOiIyIn0.JaUOqEAZ2T8vVT36mXfweMmYjEWsP7toD07jeeyrl1k", + "access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzMDE5LCJpYXQiOjE3NzExMDEyMTksImp0aSI6ImFmOWFjNGM1MzBiZjQ4ZGE4Yzg2MWFjYzIzNjQ3NjU3IiwidXNlcl9pZCI6IjIifQ.6wH5sx1fyFn3Wt3DVZGYbiYi79rGthUZkgGmTqzebXc" +} +#+end_src +*** Perfil de usuario +get /users/me/ +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzMDE5LCJpYXQiOjE3NzExMDEyMTksImp0aSI6ImFmOWFjNGM1MzBiZjQ4ZGE4Yzg2MWFjYzIzNjQ3NjU3IiwidXNlcl9pZCI6IjIifQ.6wH5sx1fyFn3Wt3DVZGYbiYi79rGthUZkgGmTqzebXc +**** Respuesta +#+begin_src json +{ + "id": 2, + "username": "admin", + "email": "correo@example.com", + "first_name": "", + "last_name": "" +} +#+end_src +*** Renovar token +post /token/refresh/ + +{ + "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTc3MTE4NzYxOSwiaWF0IjoxNzcxMTAxMjE5LCJqdGkiOiI5ZTgzNGRlM2QzMmQ0NmQyODEwZGQ2MjI2ODUwNjgzNyIsInVzZXJfaWQiOiIyIn0.JaUOqEAZ2T8vVT36mXfweMmYjEWsP7toD07jeeyrl1k" +} +**** response +#+begin_src json +{ + "access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzNjA1LCJpYXQiOjE3NzExMDE4MDUsImp0aSI6ImJjZTY5ZTA3MTIyOTQxMTg5NmFjYzk1ZDNiOThhMTI0IiwidXNlcl9pZCI6IjIifQ.b4Z1c_Yi5tsLZ-7F0KZcM2tai-f1VeaE881j2pKDwYA" +} +#+end_src +** Don confiao :verb: +template http://localhost:7000/don_confiao/api/ +Content-Type: application/json; +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzNjA1LCJpYXQiOjE3NzExMDE4MDUsImp0aSI6ImJjZTY5ZTA3MTIyOTQxMTg5NmFjYzk1ZDNiOThhMTI0IiwidXNlcl9pZCI6IjIifQ.b4Z1c_Yi5tsLZ-7F0KZcM2tai-f1VeaE881j2pKDwYA +*** todas las rutas +get +**** response +#+begin_src json +{ + "sales": "http://localhost:7000/don_confiao/api/sales/", + "customers": "http://localhost:7000/don_confiao/api/customers/", + "products": "http://localhost:7000/don_confiao/api/products/", + "reconciliate_jar": "http://localhost:7000/don_confiao/api/reconciliate_jar/" +} +#+end_src +*** customers +get customers/ +**** response +#+begin_src json +[ + { + "id": 1, + "name": "Consumidor Final", + "address": "", + "email": "", + "phone": "", + "external_id": "2753" + }, + ... +] +#+end_src diff --git a/requirements.txt b/requirements.txt index 360a414..1926c0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Django==5.0.6 djangorestframework django-cors-headers +djangorestframework-simplejwt sabatron-tryton-rpc-client==7.4.0 diff --git a/tienda_ilusion/don_confiao/tests/Mixins.py b/tienda_ilusion/don_confiao/tests/Mixins.py index 7957827..37176d9 100644 --- a/tienda_ilusion/don_confiao/tests/Mixins.py +++ b/tienda_ilusion/don_confiao/tests/Mixins.py @@ -1,10 +1,19 @@ from django.contrib.auth.models import User +from rest_framework_simplejwt.tokens import RefreshToken +from rest_framework.test import APIClient class LoginMixin: def login(self): - username = 'nombre_usuario' - password = 'contraseña' - email = 'correo@example.com' - self.user = User.objects.create_user(username, email, password) - self.client.login(username=username, password=password) + self.user = User.objects.create_superuser( + username='admin', + email='admin@example.com', + password='adminpass' + ) + + refresh = RefreshToken.for_user(self.user) + self.access_token = str(refresh.access_token) + + self.client = APIClient() + self.client.credentials( + HTTP_AUTHORIZATION=f'Bearer {self.access_token}') diff --git a/tienda_ilusion/tienda_ilusion/settings.py b/tienda_ilusion/tienda_ilusion/settings.py index df1a981..6678e0b 100644 --- a/tienda_ilusion/tienda_ilusion/settings.py +++ b/tienda_ilusion/tienda_ilusion/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/5.0/ref/settings/ """ import os +from datetime import timedelta from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -44,7 +45,9 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', + 'rest_framework.authtoken', 'corsheaders', + 'users', # 'don_confiao' ] @@ -57,7 +60,6 @@ MIDDLEWARE = [ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'corsheaders.middleware.CorsMiddleware', - 'django.middleware.common.CommonMiddleware', ] ROOT_URLCONF = 'tienda_ilusion.urls' @@ -137,11 +139,19 @@ FIXTURE_DIRS = ['don_confiao/tests/Fixtures'] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ - 'rest_framework.authentication.SessionAuthentication', + "rest_framework_simplejwt.authentication.JWTAuthentication", ], 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticated', ], } -LOGOUT_REDIRECT_URL = '/start/' +SIMPLE_JWT = { + "ACCESS_TOKEN_LIFETIME": timedelta(minutes=30), + "REFRESH_TOKEN_LIFETIME": timedelta(days=1), + "AUTH_HEADER_TYPES": ("Bearer",), +} + +# CORS_ALLOWED_ORIGINS = [ +# "http://localhost:5173", +# ] diff --git a/tienda_ilusion/tienda_ilusion/templates/registration/login.html b/tienda_ilusion/tienda_ilusion/templates/registration/login.html deleted file mode 100644 index 0ec0e16..0000000 --- a/tienda_ilusion/tienda_ilusion/templates/registration/login.html +++ /dev/null @@ -1,5 +0,0 @@ -
diff --git a/tienda_ilusion/tienda_ilusion/templates/registration/profile.html b/tienda_ilusion/tienda_ilusion/templates/registration/profile.html deleted file mode 100644 index e70c7fe..0000000 --- a/tienda_ilusion/tienda_ilusion/templates/registration/profile.html +++ /dev/null @@ -1,27 +0,0 @@ - - - -Nombre de usuario: {{ user.username }}
-Email: {{ user.email }}
- -