mealie/tests/multitenant_tests/test_multitenant_cases.py
Hayden 3c2744a3da
improve developer tooling (backend) (#1051)
* add basic pre-commit file

* add flake8

* add isort

* add pep585-upgrade (typing upgrades)

* use namespace for import

* add mypy

* update ci for backend

* flake8 scope

* fix version format

* update makefile

* disable strict option (temporary)

* fix mypy issues

* upgrade type hints (pre-commit)

* add vscode typing check

* add types to dev deps

* remote container draft

* update setup script

* update compose version

* run setup on create

* dev containers update

* remove unused pages

* update setup tips

* expose ports

* Update pre-commit to include flask8-print (#1053)

* Add in flake8-print to pre-commit

* pin version of flake8-print

* formatting

* update getting strated docs

* add mypy to pre-commit

* purge .mypy_cache on clean

* drop mypy

Co-authored-by: zackbcom <zackbcom@users.noreply.github.com>
2022-03-15 15:01:56 -08:00

94 lines
2.9 KiB
Python

import pytest
from fastapi.testclient import TestClient
from mealie.repos.repository_factory import AllRepositories
from tests.fixtures.fixture_multitenant import MultiTenant
from tests.multitenant_tests.case_abc import ABCMultiTenantTestCase
from tests.multitenant_tests.case_categories import CategoryTestCase
from tests.multitenant_tests.case_foods import FoodsTestCase
from tests.multitenant_tests.case_tags import TagsTestCase
from tests.multitenant_tests.case_tools import ToolsTestCase
from tests.multitenant_tests.case_units import UnitsTestCase
all_cases = [
UnitsTestCase,
FoodsTestCase,
ToolsTestCase,
TagsTestCase,
CategoryTestCase,
]
@pytest.mark.parametrize("test_case", all_cases)
def test_multitenant_cases_get_all(
api_client: TestClient,
multitenants: MultiTenant,
database: AllRepositories,
test_case: type[ABCMultiTenantTestCase],
):
"""
This test will run all the multitenant test cases and validate that they return only the data for their group.
When requesting all resources.
"""
user1 = multitenants.user_one
user2 = multitenants.user_two
test_case = test_case(database, api_client)
with test_case:
expected_ids = test_case.seed_action(user1.group_id)
expected_results = [
(user1.token, expected_ids),
(user2.token, []),
]
for token, item_ids in expected_results:
response = test_case.get_all(token)
assert response.status_code == 200
data = response.json()
assert len(data) == len(item_ids)
if len(data) > 0:
for item in data:
assert item["id"] in item_ids
@pytest.mark.parametrize("test_case", all_cases)
def test_multitenant_cases_same_named_resources(
api_client: TestClient,
multitenants: MultiTenant,
database: AllRepositories,
test_case: type[ABCMultiTenantTestCase],
):
"""
This test is used to ensure that the same resource can be created with the same values in different tenants.
i.e. the same category can exist in multiple groups. This is important to validate that the compound unique constraints
are operating in SQLAlchemy correctly.
"""
user1 = multitenants.user_one
user2 = multitenants.user_two
test_case = test_case(database, api_client)
with test_case:
expected_ids, expected_ids2 = test_case.seed_multi(user1.group_id, user2.group_id)
expected_results = [
(user1.token, expected_ids),
(user2.token, expected_ids2),
]
for token, item_ids in expected_results:
response = test_case.get_all(token)
assert response.status_code == 200
data = response.json()
assert len(data) == len(item_ids)
if len(data) > 0:
for item in data:
assert item["id"] in item_ids