Skip to content

Commit

Permalink
func: Cria os testes
Browse files Browse the repository at this point in the history
  • Loading branch information
WellingtonLFaria committed Sep 27, 2024
1 parent 66558d6 commit f1af1e4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
4 changes: 0 additions & 4 deletions src/tupan/tupan/pytest.ini

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.1 on 2024-09-20 12:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('usuarios', '0005_usuario_is_staff_usuario_is_superuser'),
]

operations = [
migrations.AddField(
model_name='usuario',
name='groups',
field=models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups'),
),
migrations.AddField(
model_name='usuario',
name='user_permissions',
field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions'),
),
]
10 changes: 1 addition & 9 deletions src/tupan/usuarios/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
from django.utils.translation import gettext_lazy as _
from django.utils import timezone

class Base(models.Model):
criacao = models.DateTimeField(auto_now_add=True)
alterado = models.DateTimeField(auto_now=True)
ativo = models.BooleanField(default=True)

class Meta:
abstract = True

class UsuarioManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
Expand All @@ -27,7 +19,7 @@ def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
return self.create_user(email, password, **extra_fields)

class Usuario(AbstractBaseUser):
class Usuario(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
password = models.CharField(_('password'), max_length=255)
ativo = models.BooleanField(_('active'), default=True)
Expand Down
30 changes: 28 additions & 2 deletions src/tupan/usuarios/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
from django.test import TestCase
import pytest
from .models import Usuario

# Create your tests here.
class TestUsuario:

@pytest.mark.django_db
def test_criacao_usuarios(self):
user1 = Usuario.objects.create_user(email='user@gmail.com', password='senha1')
user2 = Usuario.objects.create_user(email='well@gmail.com', password='senha2')

assert Usuario.objects.count() == 2
assert user1.email == 'user@gmail.com'
assert user2.email == 'well@gmail.com'

assert user1.check_password("senha1")
assert user2.check_password("senha2")

assert user1.ativo == True
assert user2.ativo == True

@pytest.mark.django_db
def test_listagem_usuarios(self):
user1 = Usuario.objects.create_user(email='user@gmail.com', password='senha1')
user2 = Usuario.objects.create_user(email='well@gmail.com', password='senha2')

usuarios = Usuario.objects.all()
assert len(usuarios) == 2
assert user1 in usuarios
assert user2 in usuarios
13 changes: 12 additions & 1 deletion src/tupan/usuarios/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@

class UsuarioList(APIView):
"""
List all users, or create a new user.
Lista, cria, atualiza e deleta os usuários.
"""
def get(self, request, format=None):
usuarios = Usuario.objects.all()
serializer = UsuarioSerializer(usuarios, many=True)
return Response(serializer.data)

def delete(self, request, format=None):
try:
id_usuario = request.data.get("id")
usuario = Usuario.objects.get(id=id_usuario)
usuario.ativo = False
usuario.save()
return Response(usuario, status=status.HTTP_200_OK)
except Usuario.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
except Exception as e:
return Response({"message": str(e)}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None):
serializer = UsuarioSerializer(data=request.data)
if serializer.is_valid():
Expand Down

0 comments on commit f1af1e4

Please sign in to comment.