def empty_project_data(): return { "uuid": f"project-{uuidlib.uuid4()}", "name": "Empty name", "description": "some description of an empty project", "prjOwner": "I'm the empty project owner, hi!", "creationDate": now_str(), "lastChangeDate": now_str(), "thumbnail": "", "workbench": {}, }
def test_time_utils(): snapshot0 = now_str() time.sleep(0.5) snapshot1 = now_str() now0 = to_datetime(snapshot0) now1 = to_datetime(snapshot1) assert now0 < now1 # tests biyective now_time = datetime.utcnow() snapshot = now_time.strftime(DATETIME_FORMAT) assert now_time == datetime.strptime(snapshot, DATETIME_FORMAT)
async def test_new_project( client, logged_user, expected, computational_system_mock, storage_subsystem_mock, ): # POST /v0/projects url = client.app.router["create_projects"].url_for() assert str(url) == API_PREFIX + "/projects" # Pre-defined fields imposed by required properties in schema default_project = { "uuid": "0000000-invalid-uuid", "name": "Minimal name", "description": "this description should not change", "prjOwner": "me but I will be removed anyway", "creationDate": now_str(), "lastChangeDate": now_str(), "thumbnail": "", "accessRights": {"12": "some rights"}, "workbench": {}, "tags": [], } resp = await client.post(url, json=default_project) data, error = await assert_status(resp, expected) if not error: new_project = data # updated fields assert default_project["uuid"] != new_project["uuid"] assert default_project["prjOwner"] != logged_user["email"] assert new_project["prjOwner"] == logged_user["email"] assert to_datetime(default_project["creationDate"]) < to_datetime( new_project["creationDate"] ) # invariant fields for key in new_project.keys(): if key not in ("uuid", "prjOwner", "creationDate", "lastChangeDate"): assert default_project[key] == new_project[key] # TODO: validate response using OAS? # FIXME: cannot delete user until project is deleted. See cascade or too coupled?? # i.e. removing a user, removes all its projects!! # asyncpg.exceptions.ForeignKeyViolationError: update or delete on table "users" # violates foreign key constraint "user_to_projects_user_id_fkey" on table "user_to_projects" await delete_all_projects(client.app)
async def _new_project( client, expected_response: Type[web.HTTPException], logged_user: Dict[str, str], primary_group: Dict[str, str], *, project: Optional[Dict] = None, from_template: Optional[Dict] = None, ) -> Dict: # POST /v0/projects url = client.app.router["create_projects"].url_for() assert str(url) == f"{API_PREFIX}/projects" if from_template: url = url.with_query(from_template=from_template["uuid"]) # Pre-defined fields imposed by required properties in schema project_data = {} expected_data = {} if from_template: # access rights are replaced expected_data = deepcopy(from_template) expected_data["accessRights"] = {} if not from_template or project: project_data = { "uuid": "0000000-invalid-uuid", "name": "Minimal name", "description": "this description should not change", "prjOwner": "me but I will be removed anyway", "creationDate": now_str(), "lastChangeDate": now_str(), "thumbnail": "", "accessRights": {}, "workbench": {}, "tags": [], "classifiers": [], "ui": {}, "dev": {}, "quality": {}, } if project: project_data.update(project) for key in project_data: expected_data[key] = project_data[key] if ( key in OVERRIDABLE_DOCUMENT_KEYS and not project_data[key] and from_template ): expected_data[key] = from_template[key] resp = await client.post(url, json=project_data) new_project, error = await assert_status(resp, expected_response) if not error: # has project state assert not ProjectState( **new_project.pop("state") ).locked.value, "Newly created projects should be unlocked" # updated fields assert expected_data["uuid"] != new_project["uuid"] assert ( new_project["prjOwner"] == logged_user["email"] ) # the project owner is assigned the user id e-mail assert to_datetime(expected_data["creationDate"]) < to_datetime( new_project["creationDate"] ) assert to_datetime(expected_data["lastChangeDate"]) < to_datetime( new_project["lastChangeDate"] ) # the access rights are set to use the logged user primary group + whatever was inside the project expected_data["accessRights"].update( {str(primary_group["gid"]): {"read": True, "write": True, "delete": True}} ) assert new_project["accessRights"] == expected_data["accessRights"] # invariant fields modified_fields = [ "uuid", "prjOwner", "creationDate", "lastChangeDate", "accessRights", "workbench" if from_template else None, "ui" if from_template else None, ] for key in new_project.keys(): if key not in modified_fields: assert expected_data[key] == new_project[key] return new_project