| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | 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, | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 15:39:23 -05:00
										 |  |  | @pytest.mark.parametrize("test_case_type", all_cases) | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | def test_multitenant_cases_get_all( | 
					
						
							|  |  |  |     api_client: TestClient, | 
					
						
							|  |  |  |     multitenants: MultiTenant, | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |     unfiltered_database: AllRepositories, | 
					
						
							| 
									
										
										
										
											2023-08-21 15:39:23 -05:00
										 |  |  |     test_case_type: type[ABCMultiTenantTestCase], | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | ): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |     test_case = test_case_type(unfiltered_database, api_client) | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with test_case: | 
					
						
							|  |  |  |         expected_ids = test_case.seed_action(user1.group_id) | 
					
						
							| 
									
										
										
										
											2023-08-21 15:39:23 -05:00
										 |  |  |         expected_results: list = [ | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  |             (user1.token, expected_ids), | 
					
						
							|  |  |  |             (user2.token, []), | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for token, item_ids in expected_results: | 
					
						
							|  |  |  |             response = test_case.get_all(token) | 
					
						
							|  |  |  |             assert response.status_code == 200 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |             data = response.json()["items"] | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |             assert len(data) == len(item_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if len(data) > 0: | 
					
						
							|  |  |  |                 for item in data: | 
					
						
							|  |  |  |                     assert item["id"] in item_ids | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 15:39:23 -05:00
										 |  |  | @pytest.mark.parametrize("test_case_type", all_cases) | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | def test_multitenant_cases_same_named_resources( | 
					
						
							|  |  |  |     api_client: TestClient, | 
					
						
							|  |  |  |     multitenants: MultiTenant, | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |     unfiltered_database: AllRepositories, | 
					
						
							| 
									
										
										
										
											2023-08-21 15:39:23 -05:00
										 |  |  |     test_case_type: type[ABCMultiTenantTestCase], | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | ): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     This test is used to ensure that the same resource can be created with the same values in different tenants. | 
					
						
							| 
									
										
										
										
											2024-08-12 17:09:30 +02:00
										 |  |  |     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. | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     user1 = multitenants.user_one | 
					
						
							|  |  |  |     user2 = multitenants.user_two | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |     test_case = test_case_type(unfiltered_database, api_client) | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |             data = response.json()["items"] | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |             assert len(data) == len(item_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if len(data) > 0: | 
					
						
							|  |  |  |                 for item in data: | 
					
						
							|  |  |  |                     assert item["id"] in item_ids |