def test_verify_admin_privs_ok(self, rqm): ws_ids_ok = {123: {self.user_id: "a"}, "1123": {self.user_id: "a"}} set_up_ok_mocks(rqm, ws_perms=ws_ids_ok) for ws_id in ws_ids_ok: self.assertIsNone( verify_admin_privilege(self.cfg["workspace-url"], self.user_id, self.token, ws_id))
def test_get_static_info_ok(self, rqm): ws_id1 = 123 ws_id2 = 456 save_time = str(int(time.time() * 1000)) ws_ts = '2019-08-26T17:33:56+0000' ws_ts_epoch = 1566840836000 obj_info = [ 1, 'fake_narr', 'KBaseNarrative.Narrative-4.0', ws_ts, 1, 'some_user', 5, 'fake_ws', 'an_md5', 12345, None ] ref_to_info = {f"{ws_id1}/1/1": obj_info, f"{ws_id2}/1/1": obj_info} ws_info_map = { ws_id1: [ ws_id1, 'some_util_test_narrative', self.user_id, ws_ts, 7, 'a', 'r', 'unlocked', { 'cell_count': '1', 'narrative_nice_name': 'Test Exporting 1', 'searchtags': 'narrative', 'is_temporary': 'false', 'narrative': '1' } ], ws_id2: [ ws_id2, 'some_other_util_test_narrative', self.user_id, ws_ts, 7, 'a', 'r', 'unlocked', { 'cell_count': '1', 'narrative_nice_name': 'Test Exporting', 'searchtags': 'narrative', 'is_temporary': 'false', 'narrative': '1', "static_narrative_ver": "1", "static_narrative_saved": save_time, "static_narrative": f"/{ws_id2}/1" } ] } set_up_ok_mocks(rqm, ref_to_info=ref_to_info, ws_info=ws_info_map[ws_id1]) info = get_static_info(self.cfg["workspace-url"], self.token, ws_id1) self.assertEqual(info, {}) set_up_ok_mocks(rqm, ref_to_info=ref_to_info, ws_info=ws_info_map[ws_id2]) info = get_static_info(self.cfg["workspace-url"], self.token, ws_id2) self.assertEqual( info, { "ws_id": ws_id2, "narrative_id": 1, "version": 1, "url": f"/{ws_id2}/1", "static_saved": int(save_time), "narr_saved": ws_ts_epoch })
def test_read_narrative_not_narrative(self, rqm): ref = "43666/3/1" ref_to_file = {ref: "data/43666/report-43666.3.1.json"} set_up_ok_mocks(rqm, ref_to_file=ref_to_file) with self.assertRaises(ValueError) as e: read_narrative( NarrativeRef.parse(ref), Workspace(url=self.cfg["workspace-url"], token=self.token)) self.assertIn( f"Expected a Narrative object with reference {ref}, got a KBaseReport.Report-3.0", str(e.exception))
def test_read_narrative_ok(self, rqm): ref = "43666/1/18" ref_to_file = {ref: "data/43666/narrative-43666.1.18.json"} set_up_ok_mocks(rqm, ref_to_file=ref_to_file) nar = read_narrative( NarrativeRef.parse("43666/1/18"), Workspace(url=self.cfg["workspace-url"], token=self.token)) # spot check that it's loaded and formatted self.assertIsNotNone(nar) self.assertIn("cells", nar) self.assertEqual(len(nar["cells"]), 9)
def test_create_static_narrative_user_not_admin(self, rqm): """ Test case where user doesn't have admin rights on the workspace. """ ws_id = 12345 set_up_ok_mocks(rqm, ws_perms={ws_id: { self.user_id: "n" }}, user_map={self.user_id: "Some User"}) with self.assertRaises(PermissionError) as e: self.service_impl.create_static_narrative( self.ctx, {"narrative_ref": f"{ws_id}/1/1"}) self.assertIn( f"User {self.user_id} does not have admin rights on workspace {ws_id}", str(e.exception))
def test_verify_public_narrative_fail(self, rqm): ws_no_privs = { 123: { self.user_id: "n" }, "456": { self.user_id: "a" }, 789: { self.user_id: "w" } } set_up_ok_mocks(rqm, ws_perms=ws_no_privs) for ws_id in ws_no_privs: with self.assertRaises(PermissionError) as e: verify_public_narrative(self.cfg["workspace-url"], ws_id) self.assertIn( f"Workspace {ws_id} must be publicly readable to make a Static Narrative", str(e.exception))
def test_verify_public_narrative_ok(self, rqm): # all kinda stupid, but valid. ws_perms = { 123: { self.user_id: "a", "*": "r" }, 456: { self.user_id: "n", "*": "w" }, "789": { self.user_id: "r", "*": "a" } } set_up_ok_mocks(rqm, ws_perms=ws_perms) for ws_id in ws_perms: self.assertIsNone( verify_public_narrative(self.cfg["workspace-url"], ws_id))
def test_get_static_info_ok(self, rqm): ws_id = 5 ws_name = "fake_ws" ts_iso = "2019-10-24T21:51:17+0000" ws_meta = { 'cell_count': '1', 'narrative_nice_name': 'Tester', 'searchtags': 'narrative', 'is_temporary': 'false', 'narrative': '1', 'static_narrative_ver': '1', 'static_narrative_saved': '1573170933432', 'static_narrative': '/5/1' } ref_to_file = {} ref_to_info = { "5/1/1": [ 1, "fake_narr", "KBaseNarrative.Narrative-4.0", ts_iso, 1, self.user_id, ws_id, ws_name, "an_md5", 12345, None ] } ws_info = [ 5, ws_name, self.user_id, ts_iso, 1, 'a', 'r', 'unlocked', ws_meta ] set_up_ok_mocks(rqm, ref_to_file=ref_to_file, ref_to_info=ref_to_info, ws_info=ws_info) info = self.service_impl.get_static_narrative_info( self.ctx, {"ws_id": ws_id})[0] std_info = { "ws_id": ws_id, "version": 1, "narrative_id": 1, "url": "/5/1", "static_saved": 1573170933432, "narr_saved": 1571953877000 } self.assertEqual(info, std_info)
def test_create_static_narrative_not_public(self, rqm): """ Test case where Narative isn't public. """ ws_perms = { 123: { self.user_id: "a", "*": "n" }, 456: { self.user_id: "a" } } set_up_ok_mocks(rqm, ws_perms=ws_perms, user_map={self.user_id: "Some User"}) for ws_id in ws_perms: with self.assertRaises(PermissionError) as e: self.service_impl.create_static_narrative( self.ctx, {"narrative_ref": f"{ws_id}/1/1"}) self.assertIn( f"Workspace {ws_id} must be publicly readable to make a Static Narrative", str(e.exception))
def test_exporter_ok(self, rqm): ws_id = 43666 ref_to_file = { "43666/1/21": "data/43666/narrative-43666.1.21.json", "43666/1/18": "data/43666/narrative-43666.1.18.json", "43666/3/1": "data/43666/report-43666.3.1.json", "43666/7/1": "data/43666/report-43666.7.1.json" } ref_to_info = {} ws_info = [ ws_id, 'some_narrative', self.user_id, '2019-08-26T17:33:56+0000', 7, 'a', 'r', 'unlocked', { 'cell_count': '1', 'narrative_nice_name': 'Test Exporting', 'searchtags': 'narrative', 'is_temporary': 'false', 'narrative': '1' } ] user_map = {self.user_id: "Some User"} set_up_ok_mocks(rqm, ref_to_file=ref_to_file, ref_to_info=ref_to_info, ws_info=ws_info, user_map=user_map, ws_obj_info_file="data/43666/objects-43666.json") exporter = NarrativeExporter(self.cfg, self.user_id, self.token) static_path = exporter.export_narrative( NarrativeRef({ "wsid": ws_id, "objid": 1, "ver": 21 }), self.cfg["scratch"]) self.assertEqual(static_path, os.path.join(self.cfg["scratch"], "narrative.html"))
def test_verify_admin_privs_fail(self, rqm): ws_no_privs = { 123: { self.user_id: "n" }, 456: { self.user_id: "w" }, 789: { self.user_id: "r" }, 234: { "some_other_user": "******", "*": "r" } } set_up_ok_mocks(rqm, ws_perms=ws_no_privs) for ws_id in ws_no_privs: with self.assertRaises(PermissionError) as e: verify_admin_privilege(self.cfg["workspace-url"], self.user_id, self.token, ws_id) self.assertIn( f"User {self.user_id} does not have admin rights on workspace {ws_id}", str(e.exception))
def test_create_static_narrative_ok_unit(self, rqm): """ Runs through the create process with a number of narratives. """ ref_to_file = dict() # 1. Add ref -> file for narratives narr_refs = [ "5846/1/19", "25022/1/114", "30530/107/25", "40800/178/1", "43666/1/18", "43666/1/21", "54980/144/1", "47123/1/28" ] ws_to_reports = { "5846": [], "25022": [ "25022/4", "25022/6", "25022/8", "25022/9", "25022/11", "25022/12", "25022/13", "25022/19", "25022/20", "25022/31", "25022/34" ], "30530": [ "30462/16", "30462/73", "30462/80", "30462/82", "30462/96", "30462/105", "30462/106", "30530/108", "30530/109", "30530/110", "30530/111", "30530/112", "30530/113", "30530/114" ], "40800": [ "40589/17/2", "40589/22/3", "40589/30/2", "40589/31", "40589/32", "40589/43", "40589/44", "40589/175" ], "43666": ["43666/3", "43666/7"], "47123": ["47123/4", "47123/5", "47123/6", "47123/7", "47123/8", "47123/9"], "54980": ["24065/141", "24065/143"] } for ref in narr_refs: ws_id = ref.split("/")[0] ref_dots = ref.replace("/", ".") ref_to_file[ref] = f"data/{ws_id}/narrative-{ref_dots}.json" for ws_id in ws_to_reports: for report_ref in ws_to_reports[ws_id]: if len(report_ref.split("/")) == 2: report_ref = report_ref + "/1" ref_dots = report_ref.replace("/", ".") ref_to_file[ report_ref] = f"data/{ws_id}/report-{ref_dots}.json" for narr_ref in narr_refs: ws_id = int(narr_ref.split("/")[0]) ws_info = [ ws_id, 'some_narrative', self.user_id, '2019-08-26T17:33:56+0000', 7, 'a', 'r', 'unlocked', { 'cell_count': '1', 'narrative_nice_name': 'Test Exporting', 'searchtags': 'narrative', 'is_temporary': 'false', 'narrative': '1' } ] ws_perms = { ws_id: { self.user_id: "a", "*": "r", "some_other_user": "******" } } user_map = { self.user_id: "Some User", "some_other_user": "******" } set_up_ok_mocks( rqm, ref_to_file=ref_to_file, ref_to_info={}, ws_info=ws_info, ws_perms=ws_perms, user_map=user_map, ws_obj_info_file=f"data/{ws_id}/objects-{ws_id}.json") output = self.service_impl.create_static_narrative( self.ctx, {"narrative_ref": narr_ref})[0] self.assertEqual(output["static_narrative_url"], f"/{ws_id}/{narr_ref.split('/')[-1]}/")