110 lines
3.1 KiB
Python
110 lines
3.1 KiB
Python
import pytest
|
|
import json
|
|
from flask import Blueprint
|
|
from faker import Faker
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
from models.user import User, db
|
|
from flasgger.utils import swag_from
|
|
from flask_jwt_extended import create_access_token
|
|
from dotenv import load_dotenv
|
|
from sqlalchemy import text
|
|
from extensions import db, jwt
|
|
from flask import Flask
|
|
|
|
# Carrega as variáveis do .env
|
|
load_dotenv()
|
|
|
|
user_tests_bp = Blueprint('user_tests', __name__, url_prefix='/users_test')
|
|
fake = Faker()
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
db.init_app(app)
|
|
#blueprints
|
|
from routes.user import user_bp
|
|
app.register_blueprint(user_bp)
|
|
|
|
return app
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
app = create_app()
|
|
app.config["TESTING"] = True
|
|
|
|
with app.app_context():
|
|
yield app
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
"""Configuração do cliente de teste do Flask."""
|
|
return app.test_client()
|
|
|
|
'''@pytest.fixture
|
|
def access_token():
|
|
"""Cria um usuário de teste e retorna um token JWT válido."""
|
|
test_user = User(
|
|
username="test_user",
|
|
email="test@example.com",
|
|
phone="123456789",
|
|
password="securepassword",
|
|
birth_date="1990-01-01",
|
|
address = [
|
|
{
|
|
"street": "Rua Exemplo, 123",
|
|
"city": "São Paulo",
|
|
"state": "SP",
|
|
"zip_code": "01000-000",
|
|
"country": "Brasil",
|
|
"address_type_id": 1,
|
|
}]
|
|
|
|
)
|
|
|
|
|
|
|
|
db.session.add(test_user)
|
|
db.session.add(addresses_to_create)
|
|
db.session.commit()
|
|
|
|
return create_access_token(identity=test_user.id) # Gera um token JWT válido
|
|
'''
|
|
def test_create_user(client, access_token):
|
|
"""Testa a criação de usuário usando dados fictícios da Factory."""
|
|
user_data = {
|
|
"username": fake.user_name(),
|
|
"email": fake.email(),
|
|
"phone": fake.phone_number(),
|
|
"password": "securepassword",
|
|
"birth_date": str(fake.date_of_birth(minimum_age=18, maximum_age=80)),
|
|
"address": { # Adicionando os dados de endereço
|
|
"street": fake.street_address(),
|
|
"city": fake.city(),
|
|
"state": fake.state(),
|
|
"zip_code": fake.zipcode(),
|
|
"type": "residencial"
|
|
}
|
|
}
|
|
|
|
response = client.post(
|
|
"/users/",
|
|
data=json.dumps(user_data),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {access_token}"
|
|
}
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
assert "Usuário criado com sucesso" in response.get_json()["message"]
|
|
|
|
# Confirma se o usuário foi salvo no banco
|
|
created_user = User.query.filter_by(username=user_data["username"]).first()
|
|
assert created_user is not None
|
|
assert created_user.email == user_data["email"]
|