mirror of
https://github.com/welton89/RRBEC.git
synced 2026-04-05 05:25:40 +00:00
test(comandas): first test. views -> createComanda
This commit is contained in:
BIN
gestaoRaul/balcao/__pycache__/tests.cpython-313.pyc
Normal file
BIN
gestaoRaul/balcao/__pycache__/tests.cpython-313.pyc
Normal file
Binary file not shown.
BIN
gestaoRaul/categories/__pycache__/tests.cpython-313.pyc
Normal file
BIN
gestaoRaul/categories/__pycache__/tests.cpython-313.pyc
Normal file
Binary file not shown.
BIN
gestaoRaul/clients/__pycache__/tests.cpython-313.pyc
Normal file
BIN
gestaoRaul/clients/__pycache__/tests.cpython-313.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
1
gestaoRaul/comandas/tests/__init__.py
Normal file
1
gestaoRaul/comandas/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
default_app_config = 'comandas.apps.ComandasConfig' # Se você tiver um AppConfig
|
||||
BIN
gestaoRaul/comandas/tests/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
gestaoRaul/comandas/tests/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
gestaoRaul/comandas/tests/__pycache__/test_views.cpython-313.pyc
Normal file
BIN
gestaoRaul/comandas/tests/__pycache__/test_views.cpython-313.pyc
Normal file
Binary file not shown.
28
gestaoRaul/comandas/tests/test_models.py
Normal file
28
gestaoRaul/comandas/tests/test_models.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
|
||||
class ComandaTestCase(TestCase):
|
||||
def setUp(self):
|
||||
# Setup code for creating test instances of Comanda and other related models
|
||||
pass
|
||||
|
||||
def test_comanda_creation(self):
|
||||
# Test the creation of a Comanda instance
|
||||
pass
|
||||
|
||||
def test_comanda_str_method(self):
|
||||
# Test the __str__ method of the Comanda model
|
||||
pass
|
||||
|
||||
def test_comanda_total_value(self):
|
||||
# Test the total value calculation of a Comanda instance
|
||||
pass
|
||||
|
||||
def test_comanda_status_choices(self):
|
||||
# Test the status choices for the Comanda model
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
# Cleanup code after tests
|
||||
pass
|
||||
120
gestaoRaul/comandas/tests/test_views.py
Normal file
120
gestaoRaul/comandas/tests/test_views.py
Normal file
@@ -0,0 +1,120 @@
|
||||
|
||||
from django.test import TestCase, RequestFactory
|
||||
from django.urls import reverse
|
||||
from django.contrib.auth.models import User, Group
|
||||
from django.contrib.sessions.middleware import SessionMiddleware
|
||||
from django.contrib.messages.middleware import MessageMiddleware
|
||||
|
||||
from ..views import createComanda
|
||||
from ..models import Mesa, Comanda
|
||||
from mesas.models import Mesa
|
||||
from gestaoRaul.decorators import group_required
|
||||
|
||||
|
||||
class CreateComandaViewTest(TestCase):
|
||||
def setUp(self):
|
||||
self.factory = RequestFactory()
|
||||
self.garcom_group = Group.objects.create(name='Garçom')
|
||||
self.outro_group = Group.objects.create(name='OutroGrupo')
|
||||
self.garcom_user = User.objects.create_user(username='garcom', password='password')
|
||||
self.garcom_user.groups.add(self.garcom_group)
|
||||
self.outro_user = User.objects.create_user(username='outro', password='password')
|
||||
self.outro_user.groups.add(self.outro_group)
|
||||
self.mesa = Mesa.objects.create(name='teste')
|
||||
|
||||
def add_middleware(self, request):
|
||||
"""Adiciona middlewares necessários para request.user e mensagens."""
|
||||
middleware = SessionMiddleware(lambda x: x)
|
||||
middleware.process_request(request)
|
||||
request.session.save()
|
||||
message_middleware = MessageMiddleware(lambda x: x)
|
||||
message_middleware.process_request(request)
|
||||
return request
|
||||
|
||||
def test_garcom_can_access_and_create_comanda(self):
|
||||
"""Testa se um usuário no grupo 'Garçom' pode acessar e criar uma comanda."""
|
||||
request = self.factory.post(
|
||||
reverse('createComanda'), # Assumindo que sua URL para createComanda se chama 'createcomanda'
|
||||
{'name-comanda': 'Comanda Teste', 'select-mesa': self.mesa.id}
|
||||
)
|
||||
request.user = self.garcom_user
|
||||
request = self.add_middleware(request)
|
||||
|
||||
response = createComanda(request)
|
||||
|
||||
self.assertEqual(response.status_code, 302) # Verifica se houve um redirect
|
||||
self.assertIn(reverse('viewcomanda'), response.url) # Verifica se redirecionou para a view correta
|
||||
self.assertTrue(Comanda.objects.filter(name='Comanda Teste', mesa=self.mesa, user=self.garcom_user).exists())
|
||||
|
||||
def test_non_garcom_cannot_access_create_comanda(self):
|
||||
"""Testa se um usuário fora do grupo 'Garçom' não pode acessar a view."""
|
||||
request = self.factory.post(
|
||||
reverse('createComanda'),
|
||||
{'name-comanda': 'Comanda Teste', 'select-mesa': self.mesa.id}
|
||||
)
|
||||
request.user = self.outro_user
|
||||
request = self.add_middleware(request)
|
||||
|
||||
# Aplica o decorator diretamente para testar o acesso negado
|
||||
wrapped_view = group_required(groupName='Garçom')(createComanda)
|
||||
response = wrapped_view(request)
|
||||
|
||||
self.assertEqual(response.status_code, 403) # Verifica se o acesso foi negado (Forbidden)
|
||||
self.assertFalse(Comanda.objects.filter(name='Comanda Teste').exists())
|
||||
|
||||
def test_anonymous_user_cannot_access_create_comanda(self):
|
||||
"""Testa se um usuário anônimo não pode acessar a view."""
|
||||
request = self.factory.post(
|
||||
reverse('createComanda'),
|
||||
{'name-comanda': 'Comanda Teste', 'select-mesa': self.mesa.id}
|
||||
)
|
||||
request.user = None # Simula um usuário não autenticado
|
||||
request = self.add_middleware(request)
|
||||
|
||||
wrapped_view = group_required(groupName='Garçom')(createComanda)
|
||||
response = wrapped_view(request)
|
||||
|
||||
self.assertEqual(response.status_code, 302) # Deve redirecionar para a página de login (padrão do Django)
|
||||
self.assertTrue(response.url.startswith(reverse('login'))) # Verifica se redireciona para a página de login
|
||||
self.assertFalse(Comanda.objects.filter(name='Comanda Teste').exists())
|
||||
|
||||
def test_create_comanda_invalid_mesa(self):
|
||||
"""Testa o comportamento ao tentar criar uma comanda com uma mesa inválida."""
|
||||
request = self.factory.post(
|
||||
reverse('createComanda'),
|
||||
{'name-comanda': 'Comanda Inválida', 'select-mesa': 999} # ID de mesa inexistente
|
||||
)
|
||||
request.user = self.garcom_user
|
||||
request = self.add_middleware(request)
|
||||
# response = createComanda(request)
|
||||
|
||||
with self.assertRaises(Mesa.DoesNotExist):
|
||||
createComanda(request)
|
||||
# self.assertEqual(response.status_code, 400)
|
||||
self.assertFalse(Comanda.objects.filter(name='Comanda Inválida').exists())
|
||||
|
||||
def test_create_comanda_missing_data(self):
|
||||
"""Testa o comportamento ao tentar criar uma comanda com dados faltando."""
|
||||
# Sem 'name-comanda'
|
||||
request_sem_nome = self.factory.post(
|
||||
reverse('createComanda'),
|
||||
{'select-mesa': self.mesa.id}
|
||||
)
|
||||
request_sem_nome.user = self.garcom_user
|
||||
request_sem_nome = self.add_middleware(request_sem_nome)
|
||||
|
||||
# with self.assertRaises(TypeError): # A view espera 'name' e 'mesa_id' no POST
|
||||
# createComanda(request_sem_nome)
|
||||
self.assertFalse(Comanda.objects.exists())
|
||||
|
||||
# Sem 'select-mesa'
|
||||
request_sem_mesa = self.factory.post(
|
||||
reverse('createComanda'),
|
||||
{'name-comanda': 'Comanda Sem Mesa'}
|
||||
)
|
||||
request_sem_mesa.user = self.garcom_user
|
||||
request_sem_mesa = self.add_middleware(request_sem_mesa)
|
||||
|
||||
# with self.assertRaises(TypeError): # A view espera 'name' e 'mesa_id' no POST
|
||||
# createComanda(request_sem_mesa)
|
||||
self.assertFalse(Comanda.objects.exists())
|
||||
@@ -2,7 +2,8 @@ from decimal import Decimal
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from django.http import JsonResponse
|
||||
from django.http import JsonResponse, HttpResponseRedirect
|
||||
|
||||
from django.shortcuts import render, redirect
|
||||
from django.db.models import Count, F
|
||||
|
||||
@@ -71,12 +72,15 @@ def viewComanda(request):
|
||||
@group_required(groupName='Garçom')
|
||||
def createComanda(request):
|
||||
name = request.POST.get('name-comanda')
|
||||
if name == '' or name == None or request.POST.get('select-mesa') == '' or request.POST.get('select-mesa') == None:
|
||||
return HttpResponseRedirect(reverse('comandas'), status=400)
|
||||
mesa_id = int(request.POST.get('select-mesa'))
|
||||
mesa = Mesa.objects.get(id=mesa_id)
|
||||
comanda = Comanda(name=name, mesa=mesa, user=request.user)
|
||||
comanda.save()
|
||||
return redirect(reverse('viewcomanda') + f'?parametro={comanda.id}')
|
||||
|
||||
|
||||
@group_required(groupName='Garçom')
|
||||
def editComanda(request):
|
||||
name = request.POST.get('nameComanda')
|
||||
|
||||
Binary file not shown.
@@ -1,19 +1,22 @@
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.urls import reverse
|
||||
from django.http import HttpResponseForbidden, HttpResponseRedirect
|
||||
from django.shortcuts import render, redirect
|
||||
|
||||
|
||||
|
||||
def group_required(groupName):
|
||||
def decorator(view_function):
|
||||
def wrapper(request, *args, **kwargs):
|
||||
if request.user.is_authenticated:
|
||||
if request.user.groups.filter(name=groupName).exists():
|
||||
return view_function(request, *args, **kwargs)
|
||||
if request.user != None:
|
||||
if request.user.is_authenticated:
|
||||
if request.user.groups.filter(name=groupName).exists():
|
||||
return view_function(request, *args, **kwargs)
|
||||
else:
|
||||
return HttpResponseForbidden('tu nao tem acesso rapa')
|
||||
else:
|
||||
return HttpResponseForbidden('tu nao tem acesso rapa')
|
||||
return HttpResponseRedirect(reverse('login'), status=302)
|
||||
else:
|
||||
return redirect('login')
|
||||
print(f"Erro no decorator: usuario none")
|
||||
return HttpResponseRedirect(reverse('login'), status=302)
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
Reference in New Issue
Block a user