|
15 | 15 | from simcore_postgres_database.models.comp_pipeline import StateType, comp_pipeline |
16 | 16 | from simcore_postgres_database.models.comp_tasks import comp_tasks |
17 | 17 | from simcore_postgres_database.models.projects import ProjectType, projects |
| 18 | +from simcore_postgres_database.models.services import services_access_rights |
18 | 19 | from simcore_postgres_database.models.users import UserRole, UserStatus, users |
19 | 20 | from simcore_postgres_database.utils_projects_nodes import ( |
20 | 21 | ProjectNodeCreate, |
@@ -183,3 +184,74 @@ def creator(project_id: ProjectID, **task_kwargs) -> dict[str, Any]: |
183 | 184 | conn.execute( |
184 | 185 | comp_tasks.delete().where(comp_tasks.c.task_id.in_(created_task_ids)) |
185 | 186 | ) |
| 187 | + |
| 188 | + |
| 189 | +@pytest.fixture |
| 190 | +def grant_service_access_rights( |
| 191 | + postgres_db: sa.engine.Engine, |
| 192 | +) -> Iterator[Callable[..., dict[str, Any]]]: |
| 193 | + """Fixture to grant access rights on a service for a given group. |
| 194 | +
|
| 195 | + Creates a row in the services_access_rights table with the provided parameters and cleans up after the test. |
| 196 | + """ |
| 197 | + created_entries: list[tuple[str, str, int, str]] = [] |
| 198 | + |
| 199 | + def creator( |
| 200 | + *, |
| 201 | + service_key: str, |
| 202 | + service_version: str, |
| 203 | + group_id: int = 1, |
| 204 | + product_name: str = "osparc", |
| 205 | + execute_access: bool = True, |
| 206 | + write_access: bool = False, |
| 207 | + ) -> dict[str, Any]: |
| 208 | + values = { |
| 209 | + "key": service_key, |
| 210 | + "version": service_version, |
| 211 | + "gid": group_id, |
| 212 | + "product_name": product_name, |
| 213 | + "execute_access": execute_access, |
| 214 | + "write_access": write_access, |
| 215 | + } |
| 216 | + |
| 217 | + # Directly use SQLAlchemy to insert and retrieve the row |
| 218 | + with postgres_db.begin() as conn: |
| 219 | + # Insert the row |
| 220 | + conn.execute(services_access_rights.insert().values(**values)) |
| 221 | + |
| 222 | + # Retrieve the inserted row |
| 223 | + result = conn.execute( |
| 224 | + sa.select(services_access_rights).where( |
| 225 | + sa.and_( |
| 226 | + services_access_rights.c.key == service_key, |
| 227 | + services_access_rights.c.version == service_version, |
| 228 | + services_access_rights.c.gid == group_id, |
| 229 | + services_access_rights.c.product_name == product_name, |
| 230 | + ) |
| 231 | + ) |
| 232 | + ) |
| 233 | + row = result.one() |
| 234 | + |
| 235 | + # Track the entry for cleanup |
| 236 | + created_entries.append( |
| 237 | + (service_key, service_version, group_id, product_name) |
| 238 | + ) |
| 239 | + |
| 240 | + # Convert row to dict |
| 241 | + return dict(row._asdict()) |
| 242 | + |
| 243 | + yield creator |
| 244 | + |
| 245 | + # Cleanup all created entries |
| 246 | + with postgres_db.begin() as conn: |
| 247 | + for key, version, gid, product in created_entries: |
| 248 | + conn.execute( |
| 249 | + services_access_rights.delete().where( |
| 250 | + sa.and_( |
| 251 | + services_access_rights.c.key == key, |
| 252 | + services_access_rights.c.version == version, |
| 253 | + services_access_rights.c.gid == gid, |
| 254 | + services_access_rights.c.product_name == product, |
| 255 | + ) |
| 256 | + ) |
| 257 | + ) |
0 commit comments