Se adiciona autenticación a django #30

Merged
mono merged 6 commits from add_authentication_#29 into main 2025-12-13 17:35:35 -05:00
15 changed files with 137 additions and 30 deletions

View File

@@ -0,0 +1,10 @@
from django.contrib.auth.models import User
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)

View File

@@ -1,20 +1,21 @@
from django.test import TestCase, Client from django.test import TestCase
from ..models import AdminCode from ..models import AdminCode
from .Mixins import LoginMixin
import json import json
class TestAdminCode(TestCase): class TestAdminCode(TestCase, LoginMixin):
def setUp(self): def setUp(self):
self.login()
self.valid_code = 'some valid code' self.valid_code = 'some valid code'
admin_code = AdminCode() admin_code = AdminCode()
admin_code.value = self.valid_code admin_code.value = self.valid_code
admin_code.clean() admin_code.clean()
admin_code.save() admin_code.save()
self.client = Client()
def test_validate_code(self): def test_validate_code(self):
url = '/don_confiao/api/admin_code/validate/' + self.valid_code url = '/don_confiao/api/admin_code/validate/' + self.valid_code
response = self.client.get(url) response = self.client.get(url)

View File

@@ -2,14 +2,16 @@ import json
import csv import csv
import io import io
from django.urls import reverse
from rest_framework import status from rest_framework import status
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from ..models import Sale, Product, Customer from ..models import Sale, Product, Customer
from .Mixins import LoginMixin
class TestAPI(APITestCase): class TestAPI(APITestCase, LoginMixin):
def setUp(self): def setUp(self):
self.login()
self.product = Product.objects.create( self.product = Product.objects.create(
name='Panela', name='Panela',
price=5000, price=5000,

View File

@@ -1,12 +1,15 @@
import json import json
from unittest.mock import patch from unittest.mock import patch
from django.test import Client, TestCase from django.test import TestCase
from ..models import Customer from ..models import Customer
from .Mixins import LoginMixin
class TestCustomersFromTryton(TestCase): class TestCustomersFromTryton(TestCase, LoginMixin):
def setUp(self): def setUp(self):
self.login()
self.customer = Customer.objects.create( self.customer = Customer.objects.create(
name='Calos', name='Calos',
external_id=5 external_id=5

View File

@@ -2,13 +2,16 @@ import csv
import json import json
from unittest.mock import patch from unittest.mock import patch
from django.test import TestCase, Client from django.test import TestCase
from django.urls import reverse
from ..models import Sale, SaleLine, Product, Customer from ..models import Sale, SaleLine, Product, Customer
from .Mixins import LoginMixin
class TestExportarVentasParaTryton(TestCase):
class TestExportarVentasParaTryton(TestCase, LoginMixin):
def setUp(self): def setUp(self):
self.login()
self.product = Product.objects.create( self.product = Product.objects.create(
name='Panela', name='Panela',
price=5000, price=5000,
@@ -41,9 +44,8 @@ class TestExportarVentasParaTryton(TestCase):
) )
def test_exportar_ventas_para_tryton(self): def test_exportar_ventas_para_tryton(self):
client = Client()
url = '/don_confiao/exportar_ventas_para_tryton' url = '/don_confiao/exportar_ventas_para_tryton'
response = client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'text/csv') self.assertEqual(response['Content-Type'], 'text/csv')
csv_content = response.content.decode('utf-8') csv_content = response.content.decode('utf-8')
@@ -82,12 +84,11 @@ class TestExportarVentasParaTryton(TestCase):
@patch('sabatron_tryton_rpc_client.client.Client.call') @patch('sabatron_tryton_rpc_client.client.Client.call')
@patch('sabatron_tryton_rpc_client.client.Client.connect') @patch('sabatron_tryton_rpc_client.client.Client.connect')
def test_send_sales_to_tryton(self, mock_connect, mock_call): def test_send_sales_to_tryton(self, mock_connect, mock_call):
client = Client()
external_id = '23423' external_id = '23423'
url = '/don_confiao/api/enviar_ventas_a_tryton' url = '/don_confiao/api/enviar_ventas_a_tryton'
mock_connect.return_value = None mock_connect.return_value = None
mock_call.return_value = [external_id] mock_call.return_value = [external_id]
response = client.post(url) response = self.client.post(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
content = json.loads(response.content.decode('utf-8')) content = json.loads(response.content.decode('utf-8'))

View File

@@ -1,18 +1,19 @@
from django.test import TestCase, Client from django.test import TestCase
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from ..models import Sale, Product, SaleLine, Customer, ReconciliationJar from ..models import Sale, Product, SaleLine, Customer, ReconciliationJar
from .Mixins import LoginMixin
import json import json
class TestJarReconcliation(TestCase): class TestJarReconcliation(TestCase, LoginMixin):
def setUp(self): def setUp(self):
self.login()
customer = Customer() customer = Customer()
customer.name = 'Alejo Mono' customer.name = 'Alejo Mono'
customer.save() customer.save()
self.client = Client()
purchase = Sale() purchase = Sale()
purchase.customer = customer purchase.customer = customer
purchase.date = "2024-07-30" purchase.date = "2024-07-30"

View File

@@ -1,10 +1,10 @@
from django.test import Client, TestCase from django.test import TestCase
from .Mixins import LoginMixin
# from ..models import PaymentMethods
class TestPaymentMethods(TestCase): class TestPaymentMethods(TestCase, LoginMixin):
def setUp(self): def setUp(self):
self.client = Client() self.login()
def test_keys_in_payment_methods_to_select(self): def test_keys_in_payment_methods_to_select(self):
response = self.client.get( response = self.client.get(

View File

@@ -2,12 +2,15 @@ import json
from decimal import Decimal from decimal import Decimal
from unittest.mock import patch from unittest.mock import patch
from django.test import Client, TestCase from django.test import TestCase
from ..models import ProductCategory, Product from ..models import Product
from .Mixins import LoginMixin
class TestProductsFromTryton(TestCase): class TestProductsFromTryton(TestCase, LoginMixin):
def setUp(self): def setUp(self):
self.login()
self.product = Product.objects.create( self.product = Product.objects.create(
name='Panela', name='Panela',
price=5000, price=5000,

View File

@@ -1,14 +1,16 @@
from django.test import TestCase, Client from django.test import TestCase
from ..models import Sale, Product, SaleLine, Customer from ..models import Sale, Product, SaleLine, Customer
from .Mixins import LoginMixin
class TestSummaryViewPurchase(TestCase): class TestSummaryViewPurchase(TestCase, LoginMixin):
def setUp(self): def setUp(self):
self.login()
customer = Customer() customer = Customer()
customer.name = 'Alejo Mono' customer.name = 'Alejo Mono'
customer.save() customer.save()
self.client = Client()
purchase = Sale() purchase = Sale()
purchase.customer = customer purchase.customer = customer
purchase.date = "2024-07-30" purchase.date = "2024-07-30"

View File

@@ -65,7 +65,7 @@ ROOT_URLCONF = 'tienda_ilusion.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'tienda_ilusion/templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
@@ -134,3 +134,14 @@ STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
FIXTURE_DIRS = ['don_confiao/tests/Fixtures'] FIXTURE_DIRS = ['don_confiao/tests/Fixtures']
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
}
LOGOUT_REDIRECT_URL = '/start/'

View File

@@ -0,0 +1,5 @@
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Iniciar sesión</button>
</form>

View File

@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<title>Perfil de usuario</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<h1>Perfil de usuario</h1>
<p>Nombre de usuario: {{ user.username }}</p>
<p>Email: {{ user.email }}</p>
<form action="{% url 'logout' %}" method="post" style="display: inline-block;">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Cerrar sesión</button>
</form>
</div>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>Bienvenido a la tienda la ilusión</title>
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<h1>Bienvenido a la tienda la ilusion</h1>
<a href="{% url 'login' %}">Login</a>
</div>
</div>
</div>
</body>
</html>

View File

@@ -16,11 +16,15 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import include, path from django.urls import include, path
from . import views
app_name = "don_confiao" app_name = "don_confiao"
urlpatterns = [ urlpatterns = [
path("don_confiao/", include("don_confiao.urls")), path("don_confiao/", include("don_confiao.urls")),
path("accounts/", include("django.contrib.auth.urls")),
path('accounts/profile/', views.ProfileView.as_view(), name='profile'),
path('start/', views.StartView.as_view(), name='start'),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]

View File

@@ -0,0 +1,15 @@
from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin
class ProfileView(LoginRequiredMixin, TemplateView):
template_name = 'registration/profile.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user'] = self.request.user
return context
class StartView(TemplateView):
template_name = 'start.html'