-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from software-architecture-fiap/test/CARD-22
Testes Unitários
- Loading branch information
Showing
6 changed files
with
552 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.