2021-02-05 02:18:25 +03:00
|
|
|
"""Tests module."""
|
|
|
|
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
from .repositories import UserRepository, UserNotFoundError
|
|
|
|
from .models import User
|
|
|
|
from .application import app
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def client():
|
|
|
|
yield TestClient(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_list(client):
|
|
|
|
repository_mock = mock.Mock(spec=UserRepository)
|
|
|
|
repository_mock.get_all.return_value = [
|
2021-10-01 02:05:54 +03:00
|
|
|
User(id=1, email="test1@email.com", hashed_password="pwd", is_active=True),
|
|
|
|
User(id=2, email="test2@email.com", hashed_password="pwd", is_active=False),
|
2021-02-05 02:18:25 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
with app.container.user_repository.override(repository_mock):
|
2021-10-01 02:05:54 +03:00
|
|
|
response = client.get("/users")
|
2021-02-05 02:18:25 +03:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
data = response.json()
|
|
|
|
assert data == [
|
2021-10-01 02:05:54 +03:00
|
|
|
{"id": 1, "email": "test1@email.com", "hashed_password": "pwd", "is_active": True},
|
|
|
|
{"id": 2, "email": "test2@email.com", "hashed_password": "pwd", "is_active": False},
|
2021-02-05 02:18:25 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_by_id(client):
|
|
|
|
repository_mock = mock.Mock(spec=UserRepository)
|
|
|
|
repository_mock.get_by_id.return_value = User(
|
|
|
|
id=1,
|
2021-10-01 02:05:54 +03:00
|
|
|
email="xyz@email.com",
|
|
|
|
hashed_password="pwd",
|
2021-02-05 02:18:25 +03:00
|
|
|
is_active=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
with app.container.user_repository.override(repository_mock):
|
2021-10-01 02:05:54 +03:00
|
|
|
response = client.get("/users/1")
|
2021-02-05 02:18:25 +03:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
data = response.json()
|
2021-10-01 02:05:54 +03:00
|
|
|
assert data == {"id": 1, "email": "xyz@email.com", "hashed_password": "pwd", "is_active": True}
|
2021-02-05 02:18:25 +03:00
|
|
|
repository_mock.get_by_id.assert_called_once_with(1)
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_by_id_404(client):
|
|
|
|
repository_mock = mock.Mock(spec=UserRepository)
|
|
|
|
repository_mock.get_by_id.side_effect = UserNotFoundError(1)
|
|
|
|
|
|
|
|
with app.container.user_repository.override(repository_mock):
|
2021-10-01 02:05:54 +03:00
|
|
|
response = client.get("/users/1")
|
2021-02-05 02:18:25 +03:00
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
2021-10-01 02:05:54 +03:00
|
|
|
@mock.patch("webapp.services.uuid4", return_value="xyz")
|
2021-02-05 02:18:25 +03:00
|
|
|
def test_add(_, client):
|
|
|
|
repository_mock = mock.Mock(spec=UserRepository)
|
|
|
|
repository_mock.add.return_value = User(
|
|
|
|
id=1,
|
2021-10-01 02:05:54 +03:00
|
|
|
email="xyz@email.com",
|
|
|
|
hashed_password="pwd",
|
2021-02-05 02:18:25 +03:00
|
|
|
is_active=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
with app.container.user_repository.override(repository_mock):
|
2021-10-01 02:05:54 +03:00
|
|
|
response = client.post("/users")
|
2021-02-05 02:18:25 +03:00
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
data = response.json()
|
2021-10-01 02:05:54 +03:00
|
|
|
assert data == {"id": 1, "email": "xyz@email.com", "hashed_password": "pwd", "is_active": True}
|
|
|
|
repository_mock.add.assert_called_once_with(email="xyz@email.com", password="pwd")
|
2021-02-05 02:18:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_remove(client):
|
|
|
|
repository_mock = mock.Mock(spec=UserRepository)
|
|
|
|
|
|
|
|
with app.container.user_repository.override(repository_mock):
|
2021-10-01 02:05:54 +03:00
|
|
|
response = client.delete("/users/1")
|
2021-02-05 02:18:25 +03:00
|
|
|
|
|
|
|
assert response.status_code == 204
|
|
|
|
repository_mock.delete_by_id.assert_called_once_with(1)
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_404(client):
|
|
|
|
repository_mock = mock.Mock(spec=UserRepository)
|
|
|
|
repository_mock.delete_by_id.side_effect = UserNotFoundError(1)
|
|
|
|
|
|
|
|
with app.container.user_repository.override(repository_mock):
|
2021-10-01 02:05:54 +03:00
|
|
|
response = client.delete("/users/1")
|
2021-02-05 02:18:25 +03:00
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_status(client):
|
2021-10-01 02:05:54 +03:00
|
|
|
response = client.get("/status")
|
2021-02-05 02:18:25 +03:00
|
|
|
assert response.status_code == 200
|
|
|
|
data = response.json()
|
2021-10-01 02:05:54 +03:00
|
|
|
assert data == {"status": "OK"}
|