Skip to content

Commit

Permalink
Merge pull request #25 from software-architecture-fiap/test/CARD-22
Browse files Browse the repository at this point in the history
Testes Unitários
  • Loading branch information
rikemorais authored Sep 29, 2024
2 parents 24ba856 + da13273 commit 3bb4f44
Show file tree
Hide file tree
Showing 6 changed files with 552 additions and 452 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ jobs:
- name: Copiando os Arquivos do Repositório
uses: actions/checkout@v3

- name: Instalando o Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Instalando o Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Instalando o Poetry
run: pipx install poetry

- name: Instalando Dependências
run: poetry install
- name: Construindo e Subindo os Containers
run: docker-compose up -d --build

- name: Executando os Testes
run: poetry run task test
run: docker-compose run tests

- name: Derrubando os Containers
run: docker-compose down
20 changes: 18 additions & 2 deletions app/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
def test_always_passes():
assert True
from fastapi import status
from fastapi.testclient import TestClient

from app.main import app

client = TestClient(app)


def test_read_root():
response = client.get('/')
assert response.status_code == status.HTTP_200_OK
assert response.json() == {'status': 'Operational'}


def test_redoc():
response = client.get('/redoc')
assert response.status_code == status.HTTP_200_OK
assert 'ReDoc' in response.text
36 changes: 36 additions & 0 deletions app/tests/test_route_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from types import SimpleNamespace

from fastapi import status
from fastapi.testclient import TestClient

from app.main import app

client = TestClient(app)


def test_auth_success(mocker):
mock_user = SimpleNamespace(id=1, email='[email protected]', hashed_password='$2b$12$125as3fd45gdas5')

mocker.patch('app.services.repository.get_user_by_email', return_value=mock_user)
mocker.patch('app.services.security.verify_password', return_value=True)
mocker.patch('app.services.security.create_access_token', return_value='mock_access_token')

form_data = {'username': '[email protected]', 'password': 'valid_password'}

response = client.post('/token', data=form_data)

assert response.status_code == status.HTTP_200_OK
print('Response JSON:', response.json())
assert response.json() == {'access_token': 'bearer mock_access_token', 'customer_id': mock_user.id}


def test_auth_invalid_credentials(mocker):
mock_user = None
mocker.patch('app.services.repository.get_user_by_email', return_value=mock_user)

form_data = {'username': '[email protected]', 'password': 'wrong_password'}

response = client.post('/token', data=form_data)

assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json() == {'detail': 'Nome de usuário ou senha incorretos'}
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ services:
DATABASE_URL: postgresql://postgres:localhost%401988@db:5432/challenge
volumes:
- logs:/app/logs
command: poetry run uvicorn app.main:app --host 0.0.0.0 --port 2000 --reload

tests:
build: .
depends_on:
- db
environment:
DATABASE_URL: postgresql://postgres:localhost%401988@db:5432/challenge
volumes:
- logs:/app/logs
command: poetry run pytest -vv

volumes:
db-data:
Expand Down
Loading

0 comments on commit 3bb4f44

Please sign in to comment.