| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  | from typing import Generator | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  | from pytest import fixture | 
					
						
							|  |  |  | from starlette.testclient import TestClient | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from tests import utils | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  | from tests.utils import api_routes | 
					
						
							| 
									
										
										
										
											2022-02-08 14:55:18 -09:00
										 |  |  | from tests.utils.factories import random_string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 18:13:38 -08:00
										 |  |  | def build_unique_user(group: str, api_client: TestClient) -> utils.TestUser: | 
					
						
							| 
									
										
										
										
											2022-02-08 14:55:18 -09:00
										 |  |  |     group = group or random_string(12) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     registration = utils.user_registration_factory() | 
					
						
							|  |  |  |     response = api_client.post("/api/users/register", json=registration.dict(by_alias=True)) | 
					
						
							|  |  |  |     assert response.status_code == 201 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     form_data = {"username": registration.username, "password": registration.password} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |     token = utils.login(form_data, api_client) | 
					
						
							| 
									
										
										
										
											2022-02-08 14:55:18 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     user_data = api_client.get(api_routes.users_self, headers=token).json() | 
					
						
							|  |  |  |     assert token is not None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return utils.TestUser( | 
					
						
							|  |  |  |         _group_id=user_data.get("groupId"), | 
					
						
							|  |  |  |         user_id=user_data.get("id"), | 
					
						
							|  |  |  |         email=user_data.get("email"), | 
					
						
							| 
									
										
										
										
											2022-03-15 23:28:42 +01:00
										 |  |  |         username=user_data.get("username"), | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |         password=registration.password, | 
					
						
							| 
									
										
										
										
											2022-02-08 14:55:18 -09:00
										 |  |  |         token=token, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @fixture(scope="module") | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  | def g2_user(admin_token, api_client: TestClient): | 
					
						
							| 
									
										
										
										
											2022-02-08 14:55:18 -09:00
										 |  |  |     group = random_string(12) | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |     # Create the user | 
					
						
							|  |  |  |     create_data = { | 
					
						
							|  |  |  |         "fullName": utils.random_string(), | 
					
						
							|  |  |  |         "username": utils.random_string(), | 
					
						
							|  |  |  |         "email": utils.random_email(), | 
					
						
							|  |  |  |         "password": "useruser", | 
					
						
							| 
									
										
										
										
											2022-02-08 14:55:18 -09:00
										 |  |  |         "group": group, | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |         "admin": False, | 
					
						
							|  |  |  |         "tokens": [], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |     response = api_client.post(api_routes.admin_groups, json={"name": group}, headers=admin_token) | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |     response = api_client.post(api_routes.users, json=create_data, headers=admin_token) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert response.status_code == 201 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Log in as this user | 
					
						
							|  |  |  |     form_data = {"username": create_data["email"], "password": "useruser"} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |     token = utils.login(form_data, api_client) | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     self_response = api_client.get(api_routes.users_self, headers=token) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert self_response.status_code == 200 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     user_id = json.loads(self_response.text).get("id") | 
					
						
							|  |  |  |     group_id = json.loads(self_response.text).get("groupId") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |         yield utils.TestUser( | 
					
						
							|  |  |  |             user_id=user_id, | 
					
						
							|  |  |  |             _group_id=group_id, | 
					
						
							|  |  |  |             token=token, | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |             email=create_data["email"],  # type: ignore | 
					
						
							|  |  |  |             username=create_data.get("username"),  # type: ignore | 
					
						
							|  |  |  |             password=create_data.get("password"),  # type: ignore | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |     finally: | 
					
						
							|  |  |  |         # TODO: Delete User after test | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @fixture(scope="module") | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  | def unique_user(api_client: TestClient): | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |     registration = utils.user_registration_factory() | 
					
						
							|  |  |  |     response = api_client.post("/api/users/register", json=registration.dict(by_alias=True)) | 
					
						
							|  |  |  |     assert response.status_code == 201 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     form_data = {"username": registration.username, "password": registration.password} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |     token = utils.login(form_data, api_client) | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     user_data = api_client.get(api_routes.users_self, headers=token).json() | 
					
						
							|  |  |  |     assert token is not None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         yield utils.TestUser( | 
					
						
							| 
									
										
										
										
											2021-12-04 14:18:46 -09:00
										 |  |  |             _group_id=user_data.get("groupId"), | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |             user_id=user_data.get("id"), | 
					
						
							|  |  |  |             email=user_data.get("email"), | 
					
						
							| 
									
										
										
										
											2022-03-15 23:28:42 +01:00
										 |  |  |             username=user_data.get("username"), | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |             password=registration.password, | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |             token=token, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         # TODO: Delete User after test | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  | @fixture(scope="module") | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  | def user_tuple(admin_token, api_client: TestClient) -> Generator[list[utils.TestUser], None, None]: | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |     group_name = utils.random_string() | 
					
						
							|  |  |  |     # Create the user | 
					
						
							|  |  |  |     create_data_1 = { | 
					
						
							|  |  |  |         "fullName": utils.random_string(), | 
					
						
							|  |  |  |         "username": utils.random_string(), | 
					
						
							|  |  |  |         "email": utils.random_email(), | 
					
						
							|  |  |  |         "password": "useruser", | 
					
						
							|  |  |  |         "group": group_name, | 
					
						
							|  |  |  |         "admin": False, | 
					
						
							|  |  |  |         "tokens": [], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     create_data_2 = { | 
					
						
							|  |  |  |         "fullName": utils.random_string(), | 
					
						
							|  |  |  |         "username": utils.random_string(), | 
					
						
							|  |  |  |         "email": utils.random_email(), | 
					
						
							|  |  |  |         "password": "useruser", | 
					
						
							|  |  |  |         "group": group_name, | 
					
						
							|  |  |  |         "admin": False, | 
					
						
							|  |  |  |         "tokens": [], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |     api_client.post(api_routes.admin_groups, json={"name": group_name}, headers=admin_token) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |     users_out = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for usr in [create_data_1, create_data_2]: | 
					
						
							|  |  |  |         response = api_client.post(api_routes.users, json=usr, headers=admin_token) | 
					
						
							|  |  |  |         assert response.status_code == 201 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Log in as this user | 
					
						
							|  |  |  |         form_data = {"username": usr["email"], "password": "useruser"} | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |         token = utils.login(form_data, api_client) | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |         response = api_client.get(api_routes.users_self, headers=token) | 
					
						
							|  |  |  |         assert response.status_code == 200 | 
					
						
							|  |  |  |         user_data = json.loads(response.text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         users_out.append( | 
					
						
							|  |  |  |             utils.TestUser( | 
					
						
							|  |  |  |                 _group_id=user_data.get("groupId"), | 
					
						
							|  |  |  |                 user_id=user_data.get("id"), | 
					
						
							| 
									
										
										
										
											2022-03-15 23:28:42 +01:00
										 |  |  |                 username=user_data.get("username"), | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |                 email=user_data.get("email"), | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |                 password="useruser", | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |                 token=token, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         yield users_out | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  | @fixture(scope="session") | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  | def user_token(admin_token, api_client: TestClient): | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |     # Create the user | 
					
						
							|  |  |  |     create_data = { | 
					
						
							|  |  |  |         "fullName": utils.random_string(), | 
					
						
							|  |  |  |         "username": utils.random_string(), | 
					
						
							|  |  |  |         "email": utils.random_email(), | 
					
						
							|  |  |  |         "password": "useruser", | 
					
						
							|  |  |  |         "group": "Home", | 
					
						
							|  |  |  |         "admin": False, | 
					
						
							|  |  |  |         "tokens": [], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = api_client.post(api_routes.users, json=create_data, headers=admin_token) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert response.status_code == 201 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Log in as this user | 
					
						
							|  |  |  |     form_data = {"username": create_data["email"], "password": "useruser"} | 
					
						
							| 
									
										
										
										
											2022-10-18 14:49:41 -08:00
										 |  |  |     return utils.login(form_data, api_client) |