class AnonymousTest(unittest.TestCase): """Tests anonymous access to API. """ def setUp(self): self.config = ConfigManager(os.getcwd() + '/test_config.json').get_config() self.config.set_token("") self.service = Service(self.config) self.assertTrue(self.service.setup()) def tearDown(self): self.service.teardown() def test_access(self): projects_module = self.service.get_projects_module() permissions_module = self.service.get_permissions_module() groups_module = self.service.get_groups_module() # Only prod has a public project. projects = projects_module.get_projects() if self.config.get_environment() == "prod": self.assertEqual(1, len(projects)) self.assertEqual("public", projects[0].get_name()) else: self.assertEqual(0, len(projects)) print("Skipping check for public objects.") # Can't add a project to public project. public_project = "00000000-0000-0000-0000-000000000001" with (self.assertRaises(RuntimeError)): projects_module.new_project(public_project, "", "") # Can't run a batch in the public project. batches_module = self.service.get_batches_module() dummy_propagation_params = PropagationParams({ 'start_time': 'AAA', 'end_time': 'BBB', 'project_uuid': 'CCC' }) dummy_opm_params = OpmParams({ 'epoch': 'DDD', 'state_vector': [1, 2, 3, 4, 5, 6] }) with (self.assertRaises(RuntimeError)): batches_module.new_batch(dummy_propagation_params, dummy_opm_params) # Anon should have no permissions. permissions = permissions_module.get_my_permissions() self.assertEqual(1, len(permissions)) self.assertEqual(0, len(permissions[""])) # And anon is in no groups. groups = groups_module.get_my_memberships() self.assertEqual(0, len(groups)) # Therefore anon can grant no permissions. with (self.assertRaises(RuntimeError)): permissions_module.grant_user_permission( "*****@*****.**", Permission("READ", "PROJECT", public_project)) # And can add/modify no groups. with (self.assertRaises(RuntimeError)): groups_module.new_group("", "") all_group = "00000000-0000-0000-0000-000000000001" with (self.assertRaises(RuntimeError)): groups_module.add_user_to_group("*****@*****.**", all_group) # Not even allowed to see the members of the magic all group. with (self.assertRaises(RuntimeError)): groups_module.get_group_members(all_group)
class ProjectsTest(unittest.TestCase): """Integration test of project management. """ def setUp(self): self.service = Service() self.assertTrue(self.service.setup_with_test_account()) def delete_children(self, project): projects = self.service.get_projects_module().get_projects() # First topographically sort all children of <project> so that we can delete # them in dependency-order. parent_map = {} for p in projects: parent_map[p.get_uuid()] = p.get_parent() children = [project.get_uuid()] change = 1 while change > 0: change = 0 for p in projects: if not p.get_uuid() in children and p.get_parent() in children: children.append(p.get_uuid()) change = change + 1 children = children[1:] if len(children) > 0: print("Cleaning up " + str(len(children)) + " children of " + project.get_uuid()) for p in reversed(children): self.service.get_projects_module().delete_project(p) def tearDown(self): self.delete_children(self.service.get_working_project()) self.service.teardown() def test_projects(self): parent = self.service.get_working_project() p = self.service.get_projects_module() projects = p.get_projects() self.assertTrue(parent.get_uuid() in [p.get_uuid() for p in projects]) p1 = p.new_project(parent.get_uuid(), 'p1', 'description') self.assertTrue(p1.get_uuid() is not None) self.assertTrue(p1.get_parent() == parent.get_uuid()) self.assertTrue(p1.get_name() == 'p1') self.assertTrue(p1.get_description() == 'description') projects = p.get_projects() self.assertTrue(parent.get_uuid() in [p.get_uuid() for p in projects]) self.assertTrue(p1.get_uuid() in [p.get_uuid() for p in projects]) p2 = p.new_project(p1.get_uuid(), 'p2', 'another description') self.assertTrue(p2.get_uuid() is not None) self.assertTrue(p2.get_parent() == p1.get_uuid()) projects = p.get_projects() self.assertTrue(parent.get_uuid() in [p.get_uuid() for p in projects]) self.assertTrue(p1.get_uuid() in [p.get_uuid() for p in projects]) self.assertTrue(p2.get_uuid() in [p.get_uuid() for p in projects]) # Can't delete the project before its children are deleted. try: p.delete_project(p1.get_uuid()) self.fail("Expected error deleting project that still has children.") except RuntimeError: pass # This is expected p.delete_project(p2.get_uuid()) p.delete_project(p1.get_uuid())
class AnonymousTest(unittest.TestCase): """Tests anonymous access to API. """ def setUp(self): cwd = os.getcwd() #print("Current working dir: ", cwd) cwd_str = str(cwd).split("/") #print(cwd_str) #print("length of cwd_str = ", len(cwd_str)) #matching = [s for s in cwd_str if "travis" in s] #print(matching) #Current working dir: /home/travis/build/RookinsAndBear/TestingTravisCI/adam_home os.chdir("..") #testdir = os.getcwd() #print("Test directory (go up 1 level): ", testdir) #Test directory (go up 1 level): /home/travis/build/RookinsAndBear/TestingTravisCI #print(sys.path) #self.config = ConfigManager(os.getcwd() + '/test_adam_config.json').get_config() #config_env_token = ConfigManager(os.getcwd() + '/test_adam_config.json').get_config() #print(config_env_token) if len(cwd_str) > 4: # TRAVIS CI option if cwd_str[1] == "home" and cwd_str[2] == "travis" and cwd_str[ 3] == "build": self.config = ConfigManager( os.getcwd() + '/adam_home/config/adam_config_template.json' ).get_config() print( "home/travis/build found in root dir - USE JSON TEMPLATE") #self.config = ConfigManager(None).get_config() self.config.set_token("") self.service = Service(self.config) self.assertTrue(self.service.setup()) else: self.config = ConfigManager( os.getcwd() + '/adam_test_config.json').get_config() # next line used for testing Travis output with decrypted json info. #self.config = ConfigManager(os.getcwd() + '/test_adam_config.json').read_config(os.getcwd() + '/test_adam_config.json') self.config.set_token("") self.service = Service(self.config) self.assertTrue(self.service.setup()) else: # PERSONAL WORKSTATION option self.config = ConfigManager(os.getcwd() + '/test_config.json').get_config() # next line used for testing Travis output with decrypted json info. #self.config = ConfigManager(os.getcwd() + '/test_adam_config.json').read_config(os.getcwd() + '/test_adam_config.json') self.config.set_token("") self.service = Service(self.config) self.assertTrue(self.service.setup()) def tearDown(self): self.service.teardown() def test_access(self): projects_module = self.service.get_projects_module() permissions_module = self.service.get_permissions_module() groups_module = self.service.get_groups_module() projects = projects_module.get_projects() self.assertEqual(1, len(projects)) self.assertEqual("public", projects[0].get_name()) # Can't add a project to public project. public_project = "00000000-0000-0000-0000-000000000001" with (self.assertRaises(RuntimeError)): projects_module.new_project(public_project, "", "") # Can't run a batch in the public project. batches_module = self.service.get_batches_module() dummy_propagation_params = PropagationParams({ 'start_time': 'AAA', 'end_time': 'BBB', 'project_uuid': 'CCC' }) dummy_opm_params = OpmParams({ 'epoch': 'DDD', 'state_vector': [1, 2, 3, 4, 5, 6] }) with (self.assertRaises(RuntimeError)): batches_module.new_batch(dummy_propagation_params, dummy_opm_params) # Anon should have no permissions. permissions = permissions_module.get_my_permissions() self.assertEqual(1, len(permissions)) self.assertEqual(0, len(permissions[""])) # And anon is in no groups. groups = groups_module.get_my_memberships() self.assertEqual(0, len(groups)) # Therefore anon can grant no permissions. with (self.assertRaises(RuntimeError)): permissions_module.grant_user_permission( "*****@*****.**", Permission("READ", "PROJECT", public_project)) # And can add/modify no groups. with (self.assertRaises(RuntimeError)): groups_module.new_group("", "") all_group = "00000000-0000-0000-0000-000000000001" with (self.assertRaises(RuntimeError)): groups_module.add_user_to_group("*****@*****.**", all_group) # Not even allowed to see the members of the magic all group. with (self.assertRaises(RuntimeError)): groups_module.get_group_members(all_group)
class PropagatorConfigTest(unittest.TestCase): """Test of propagator config manipulation. """ def setUp(self): config = ConfigManager(os.getcwd() + '/test_config.json').get_config() self.service = Service(config) self.assertTrue(self.service.setup()) def tearDown(self): self.service.teardown() def test_get_public_configs(self): # Config management isn't very common, doesn't merit direct addition to service. configs = PropagatorConfigs(self.service.rest) public_config_1 = configs.get_config( PropagatorConfigs.PUBLIC_CONFIG_ALL_PLANETS_AND_MOON) self.assertEqual("00000000-0000-0000-0000-000000000001", public_config_1.get_uuid()) public_config_2 = configs.get_config( PropagatorConfigs.PUBLIC_CONFIG_SUN_ONLY) self.assertEqual("00000000-0000-0000-0000-000000000002", public_config_2.get_uuid()) public_config_3 = configs.get_config( PropagatorConfigs.PUBLIC_CONFIG_ALL_PLANETS_AND_MOON_AND_ASTEROIDS) self.assertEqual("00000000-0000-0000-0000-000000000003", public_config_3.get_uuid()) def test_config_management(self): # Config management isn't very common, doesn't merit direct addition to service. configs = PropagatorConfigs(self.service.rest) project = self.service.new_working_project() self.assertIsNotNone(project) config = configs.new_config({ 'project': project.get_uuid(), 'description': 'test config' }) self.assertEqual(project.get_uuid(), config.get_project()) my_configs = configs.get_configs() self.assertIn(config.get_uuid(), [c.get_uuid() for c in my_configs]) config_again = configs.get_config(config.get_uuid()) self.assertEqual(config.get_config_json(), config_again.get_config_json()) configs.delete_config(config.get_uuid()) my_configs = configs.get_configs() self.assertNotIn(config.get_uuid(), [c.get_uuid() for c in my_configs]) def test_config_in_use_pins_project(self): # Config management isn't very common, doesn't merit direct addition to service. configs = PropagatorConfigs(self.service.rest) projects = self.service.get_projects_module() project = self.service.new_working_project() project1 = projects.new_project(project.get_uuid(), "", "") self.assertIsNotNone(project1) project2 = projects.new_project(project.get_uuid(), "", "") self.assertIsNotNone(project2) print("Added child projects to working project: " + "[" + project1.get_uuid() + ", " + project2.get_uuid() + "]") config = configs.new_config({ 'project': project1.get_uuid(), 'description': 'test config' }) self.assertEqual(project1.get_uuid(), config.get_project()) batch = Batch( PropagationParams({ 'start_time': '2017-10-04T00:00:00Z', 'end_time': '2017-10-05T00:00:00Z', 'project_uuid': project2.get_uuid(), 'propagator_uuid': config.get_uuid() }), OpmParams({ 'epoch': '2017-10-04T00:00:00Z', 'state_vector': [ 130347560.13690618, -74407287.6018632, -35247598.541470632, 23.935241263310683, 27.146279819258538, 10.346605942591514 ] })) BatchRunManager(self.service.get_batches_module(), [batch]).run() # Attempt to delete the project with the config in it. It should refuse because the # config is still in use by the batch. with self.assertRaises(RuntimeError): projects.delete_project(project1.get_uuid()) # Then delete the batch. After that, the project with the config in it should # delete no problem. self.service.batches.delete_batch(batch.get_uuid()) projects.delete_project(project1.get_uuid()) # Clean up the batch holder project. projects.delete_project(project2.get_uuid())