diff --git a/src/tupan/usuarios/serializer.py b/src/tupan/usuarios/serializer.py index 84ce7b8..ad2bbf2 100644 --- a/src/tupan/usuarios/serializer.py +++ b/src/tupan/usuarios/serializer.py @@ -4,7 +4,7 @@ class UsuarioSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Usuario - fields = ['email', 'password', 'ativo', 'criacao', 'alterado'] + fields = ['id', 'email', 'password', 'ativo', 'criacao', 'alterado'] extra_kwargs = {'password': {'write_only': True}} def create(self, validated_data): diff --git a/src/tupan/usuarios/tests.py b/src/tupan/usuarios/tests.py index 88df74c..7f249bc 100644 --- a/src/tupan/usuarios/tests.py +++ b/src/tupan/usuarios/tests.py @@ -26,4 +26,11 @@ def test_listagem_usuarios(self): usuarios = Usuario.objects.all() assert len(usuarios) == 2 assert user1 in usuarios - assert user2 in usuarios \ No newline at end of file + assert user2 in usuarios + + @pytest.mark.django_db + def test_inativar_usuario(self): + user1 = Usuario.objects.create_user(email="wellingtonll.faria@gmail.com", password="123123") + user1.ativo = False + user1.save() + assert user1.ativo == False \ No newline at end of file diff --git a/src/tupan/usuarios/views.py b/src/tupan/usuarios/views.py index e64829c..8e71a3f 100644 --- a/src/tupan/usuarios/views.py +++ b/src/tupan/usuarios/views.py @@ -10,7 +10,7 @@ class UsuarioList(APIView): Lista, cria, atualiza e deleta os usuários. """ def get(self, request, format=None): - usuarios = Usuario.objects.all() + usuarios = Usuario.objects.all().filter(ativo=True) serializer = UsuarioSerializer(usuarios, many=True) return Response(serializer.data) @@ -20,14 +20,16 @@ def delete(self, request, format=None): usuario = Usuario.objects.get(id=id_usuario) usuario.ativo = False usuario.save() - return Response(usuario, status=status.HTTP_200_OK) + serializer = UsuarioSerializer(usuario) + return Response(serializer.data, 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(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)