Exemplo n.º 1
0
 def setUp(self):
     config = ConfigManager(os.getcwd() +
                            '/test_adam_config.json').get_config()
     self.service = Service(config)
     self.assertTrue(self.service.setup())
     self.me = "*****@*****.**"
     self.added_groups = []
 def setUp(self):
     config = ConfigManager(os.getcwd() +
                            '/test_adam_config.json').get_config('dev')
     self.service = Service(config)
     self.assertTrue(self.service.setup())
     self.working_project = self.service.new_working_project()
     self.assertIsNotNone(self.working_project)
Exemplo n.º 3
0
 def setUp(self):
     cwd = os.getcwd()
     cwd_str = str(cwd).split("/")
     os.chdir("..")
     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())
Exemplo n.º 4
0
 def setUp(self):
     config = ConfigManager(os.getcwd() + '/test_config.json').get_config()
     self.service = Service(config)
     self.assertTrue(self.service.setup())
     self.working_project = self.service.new_working_project()
     self.assertIsNotNone(self.working_project)
     self.stm_module = StmPropagationModule(
         self.service.get_batches_module())
Exemplo n.º 5
0
class BasicTest(unittest.TestCase):
    """Basic integration test to demonstrate use of service tester.
    
    """
    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())

    def tearDown(self):
        self.service.teardown()

    def test_basic(self):
        print("Hello world")
Exemplo n.º 6
0
class BasicTest(unittest.TestCase):
    """Basic integration test to demonstrate use of service tester.

    """

    def setUp(self):
        config = ConfigManager(os.getcwd() + '/test_config.json').get_config()
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.assertIsNotNone(self.service.new_working_project())

    def tearDown(self):
        self.service.teardown()

    def test_basic(self):
        print("Hello world")
Exemplo n.º 7
0
def service(request):
    # a class may be tagged to run in a different environment
    marker = request.node.get_closest_marker("env")
    env = marker.args[0] if marker else None

    config = ConfigManager().get_config(env)
    service = Service.from_config(config)
    assert service.setup()

    yield service

    service.teardown()
Exemplo n.º 8
0
    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())
Exemplo n.º 9
0
 def setUp(self):
     self.config = ConfigManager().get_config()
     self.config["token"] = ""
     self.service = Service.from_config(self.config)
     self.assertTrue(self.service.setup())
Exemplo n.º 10
0
 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())
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
class RunnableManagerTest(unittest.TestCase):
    def setUp(self):
        config = ConfigManager(os.getcwd() +
                               '/test_adam_config.json').get_config('dev')
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    def new_batch_propagation(self):
        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(10 * 365)

        propagation_params = PropagationParams({
            'start_time':
            now.isoformat() + 'Z',
            'end_time':
            later.isoformat() + 'Z',
            'step_size':
            60 * 60,  # 1 hour.
            'description':
            'Created by test at ' + str(now) + 'Z'
        })

        state_vec = [
            130347560.13690618, -74407287.6018632, -35247598.541470632,
            23.935241263310683, 27.146279819258538, 10.346605942591514
        ]
        opm_params = OpmParams({
            'epoch': now.isoformat() + 'Z',
            'state_vector': state_vec,
            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5,
            'originator': 'Test',
            'object_name': 'TestObj',
            'object_id': 'TestObjId',
        })

        return BatchPropagation(propagation_params, opm_params)

    def test_runnable_manager(self):
        batch_propagations = [self.new_batch_propagation() for i in range(3)]
        batch_propagations_module = BatchPropagations(self.service.rest)

        manager = RunnableManager(batch_propagations_module,
                                  batch_propagations,
                                  self.working_project.get_uuid())
        manager.run()

        for prop in batch_propagations:
            self.assertEqual('COMPLETED',
                             prop.get_runnable_state().get_calc_state())

            self.assertEqual(len(prop.get_final_state_vectors()),
                             len(prop.get_children()))
            for i in range(len(prop.get_final_state_vectors())):
                self.assertEqual(
                    prop.get_final_state_vectors()[i],
                    prop.get_children()[i].get_final_state_vector())
Exemplo n.º 13
0
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 BatchPropagationTest(unittest.TestCase):
    def setUp(self):
        config = ConfigManager(os.getcwd() +
                               '/test_adam_config.json').get_config('dev')
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    def new_batch_propagation(self):
        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(10 * 365)

        propagation_params = PropagationParams({
            'start_time':
            now.isoformat() + 'Z',
            'end_time':
            later.isoformat() + 'Z',
            'step_size':
            60 * 60,  # 1 hour.
            'description':
            'Created by test at ' + str(now) + 'Z'
        })

        state_vec = [
            130347560.13690618, -74407287.6018632, -35247598.541470632,
            23.935241263310683, 27.146279819258538, 10.346605942591514
        ]
        opm_params = OpmParams({
            'epoch': now.isoformat() + 'Z',
            'state_vector': state_vec,

            # Comment out state_vector and uncomment this to try with keplerian elements instead.
            # 'keplerian_elements': {
            #     'semi_major_axis_km': 3.1307289138037175E8,
            #     'eccentricity': 0.5355029800000188,
            #     'inclination_deg': 23.439676743246295,
            #     'ra_of_asc_node_deg': 359.9942693176405,
            #     'arg_of_pericenter_deg': 328.5584374618295,
            #     'true_anomaly_deg': -127.01778914927144,
            #     'gm': 1.327124400419394E11
            # },
            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5,
            'originator': 'Test',
            'object_name': 'TestObj',
            'object_id': 'TestObjId',

            # Uncomment this to try a hypercube propagation.
            # Lower triangular covariance matrix (21 elements in a list)
            # 'covariance': [
            #     3.331349476038534e-04,
            #     4.618927349220216e-04, 6.782421679971363e-04,
            #     -3.070007847730449e-04, -4.221234189514228e-04, 3.231931992380369e-04,
            #     -3.349365033922630e-07, -4.686084221046758e-07, 2.484949578400095e-07, 4.296022805587290e-10,  # NOQA (we want to keep the visual triangle)
            #     -2.211832501084875e-07, -2.864186892102733e-07, 1.798098699846038e-07, 2.608899201686016e-10, 1.767514756338532e-10,  # NOQA
            #     -3.041346050686871e-07, -4.989496988610662e-07, 3.540310904497689e-07, 1.869263192954590e-10, 1.008862586240695e-10, 6.224444338635500e-10],  # NOQA
            # 'perturbation': 3,
            # 'hypercube': 'FACES',
        })

        return BatchPropagation(propagation_params, opm_params)

    def test_batch_propagation(self):
        batch_propagation = self.new_batch_propagation()
        props = BatchPropagations(self.service.rest)

        props.insert(batch_propagation, self.working_project.get_uuid())
        uuid = batch_propagation.get_uuid()
        self.assertIsNotNone(uuid)
        print(uuid)

        runnable_state = props.get_runnable_state(uuid)
        self.assertIsNotNone(runnable_state)
        while (runnable_state.get_calc_state() != 'COMPLETED'):
            print(runnable_state.get_calc_state())
            runnable_state = props.get_runnable_state(uuid)
            self.assertIsNotNone(runnable_state)
        self.assertEqual('COMPLETED', runnable_state.get_calc_state())
        self.assertIsNone(runnable_state.get_error())

        runnable_state_list = props.get_runnable_states(
            self.working_project.get_uuid())
        self.assertEqual(1, len(runnable_state_list))

        props.update_with_results(batch_propagation)
        self.assertIsNotNone(batch_propagation)

        fresh_batch = props.get(uuid)
        self.assertIsNotNone(fresh_batch)

        children = props.get_children(uuid)
        self.assertEqual(len(batch_propagation.get_final_state_vectors()),
                         len(children))
        for i in range(len(batch_propagation.get_final_state_vectors())):
            self.assertEqual(batch_propagation.get_final_state_vectors()[i],
                             children[i].get_final_state_vector())

        props.delete(uuid)

        self.assertIsNone(props.get(uuid))
        self.assertEqual(0, len(props.get_children(uuid)))
Exemplo n.º 15
0
class GroupsTest(unittest.TestCase):
    """Integration test of group management.
    
    """
    def _clean_up(self):
        groups = self.service.get_groups_module()
        my_groups = groups.get_my_memberships()
        if len(my_groups) > 0:
            print("Cleaning up " + str(len(my_groups)) + " groups...")
        for g in my_groups:
            groups.delete_group(g.get_uuid())

    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())
        self.me = "*****@*****.**"
        self._clean_up()

    def tearDown(self):
        self.service.teardown()
        self._clean_up()

    def test_group_management(self):
        groups = self.service.get_groups_module()

        group = groups.new_group("name", "description")
        self.assertTrue(group.get_uuid() is not None)
        self.assertTrue(group.get_name() == "name")
        self.assertTrue(group.get_description() == "description")

        groups.delete_group(group.get_uuid())

    def _get_user_member_ids(self, members):
        return [
            member.get_id() for member in members
            if member.get_type() == "USER"
        ]

    def _get_group_member_ids(self, members):
        return [
            member.get_id() for member in members
            if member.get_type() == "GROUP"
        ]

    def test_membership_management(self):
        groups = self.service.get_groups_module()

        my_memberships = groups.get_my_memberships()
        self.assertTrue(len(my_memberships) == 0)

        g1 = groups.new_group("g1", "")

        # Current structure:
        #   me -> g1

        my_memberships = groups.get_my_memberships()
        self.assertTrue(len(my_memberships) == 1)
        self.assertTrue(
            g1.get_uuid() in [g.get_uuid() for g in my_memberships])

        g1_members = groups.get_group_members(g1.get_uuid())
        self.assertTrue(len(self._get_group_member_ids(g1_members)) == 0)
        self.assertTrue(len(self._get_user_member_ids(g1_members)) == 1)
        self.assertTrue(self.me in self._get_user_member_ids(g1_members))

        groups.add_user_to_group("u1", g1.get_uuid())

        # Current structure:
        #   me -> g1
        #   u1 -> g1

        g1_members = groups.get_group_members(g1.get_uuid())
        self.assertTrue(len(self._get_group_member_ids(g1_members)) == 0)
        self.assertTrue(len(self._get_user_member_ids(g1_members)) == 2)
        self.assertTrue(self.me in self._get_user_member_ids(g1_members))
        self.assertTrue("u1" in self._get_user_member_ids(g1_members))

        g2 = groups.new_group("g2", "")

        # Current structure:
        #   me -> g1
        #   u1 -> g1
        #   me -> g2

        my_memberships = groups.get_my_memberships()
        self.assertTrue(len(my_memberships) == 2)
        self.assertTrue(
            g1.get_uuid() in [g.get_uuid() for g in my_memberships])
        self.assertTrue(
            g2.get_uuid() in [g.get_uuid() for g in my_memberships])

        g2_members = groups.get_group_members(g2.get_uuid())
        self.assertTrue(len(self._get_group_member_ids(g2_members)) == 0)
        self.assertTrue(len(self._get_user_member_ids(g2_members)) == 1)
        self.assertTrue(self.me in self._get_user_member_ids(g2_members))

        groups.add_group_to_group(g1.get_uuid(), g2.get_uuid())

        # Current structure:
        #   me -> g1
        #   u1 -> g1
        #   me -> g2
        #   g1 -> g2

        my_memberships = groups.get_my_memberships()
        self.assertTrue(len(my_memberships) == 2)
        self.assertTrue(
            g1.get_uuid() in [g.get_uuid() for g in my_memberships])
        # Should not be duplicated, even though I'm in this group two ways.
        self.assertTrue(
            g2.get_uuid() in [g.get_uuid() for g in my_memberships])

        g2_members = groups.get_group_members(g2.get_uuid())
        self.assertTrue(len(self._get_group_member_ids(g2_members)) == 1)
        # u1 is now a member of g2 transitively through g1. However, members of groups
        # are not retrieved recursively, so it won't show up in the list of direct
        # members of g2.
        self.assertTrue(len(self._get_user_member_ids(g2_members)) == 1)
        self.assertTrue(self.me in self._get_user_member_ids(g2_members))
        self.assertTrue(
            g1.get_uuid() in self._get_group_member_ids(g2_members))

        groups.delete_group(g2.get_uuid())

        # Current structure:
        #   me -> g1
        #   u1 -> g1

        my_memberships = groups.get_my_memberships()
        self.assertTrue(len(my_memberships) == 1)
        self.assertTrue(
            g1.get_uuid() in [g.get_uuid() for g in my_memberships])

        g1_members = groups.get_group_members(g1.get_uuid())
        self.assertTrue(len(self._get_group_member_ids(g1_members)) == 0)
        self.assertTrue(len(self._get_user_member_ids(g1_members)) == 2)
        self.assertTrue(self.me in self._get_user_member_ids(g1_members))
        self.assertTrue("u1" in self._get_user_member_ids(g1_members))

        groups.delete_group(g1.get_uuid())

        # Current structure:

        my_memberships = groups.get_my_memberships()
        self.assertTrue(len(my_memberships) == 0)
 def setUp(self):
     self.service = Service()
     self.assertTrue(self.service.setup_with_test_account())
Exemplo n.º 17
0
class BatchRunnerTest(unittest.TestCase):
    """Integration test of batch running.

    """

    def setUp(self):
        config = ConfigManager(os.getcwd() + '/test_adam_config.json').get_config()
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    def new_dummy_batch(self, days_to_propagate):
        if (days_to_propagate > 36500):
            print("Server has trouble handling propagation durations longer than 100 years. " +
                  "Try something smaller.")
            return

        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(days_to_propagate)

        propagation_params = PropagationParams({
            'start_time': now.isoformat() + 'Z',
            'end_time': later.isoformat() + 'Z',
            'step_size': 60 * 60,  # 1 hour.
            'project_uuid': self.working_project.get_uuid(),
            'description': 'Created by test at ' + str(now) + 'Z'
        })

        state_vec = [130347560.13690618,
                     -74407287.6018632,
                     -35247598.541470632,
                     23.935241263310683,
                     27.146279819258538,
                     10.346605942591514]
        opm_params = OpmParams({
            'epoch': now.isoformat() + 'Z',
            'state_vector': state_vec,

            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5,

            'originator': 'Test',
            'object_name': 'TestObj',
            'object_id': 'TestObjId',
        })

        return Batch(propagation_params, opm_params)

    def test_small_batch(self):
        num_batches = 3

        duration = 365  # 1 year
        batches = [self.new_dummy_batch(duration) for i in range(num_batches)]

        runner = BatchRunManager(self.service.get_batches_module(), batches)
        runner.run()
        statuses = runner.get_latest_statuses()
        self.assertEqual(0, len(statuses['PENDING']))
        self.assertEqual(0, len(statuses['RUNNING']))
        self.assertEqual(num_batches, len(statuses['COMPLETED']))
        self.assertEqual(0, len(statuses['FAILED']))

        end_state_vec = [-37535741.96415495,
                         492953227.1713997,
                         204483503.94517875,
                         -11.337510170756701,
                         7.185009462698965,
                         3.3597614338244766]
        for batch in batches:
            npt.assert_allclose(end_state_vec,
                                batch.get_results().get_end_state_vector(),
                                rtol=1e-3,
                                atol=0)
Exemplo n.º 18
0
class TargetedPropagationTest(unittest.TestCase):
    def setUp(self):
        config = ConfigManager(os.getcwd() +
                               '/test_adam_config.json').get_config('dev')
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    def new_targeted_propagation(self, initial_maneuver):
        start = '2013-05-25T00:00:02.000000Z'
        end = '2018-04-25T03:06:14.200000Z'
        propagation_params = PropagationParams({
            'start_time':
            start,
            'end_time':
            end,
            'project_uuid':
            self.working_project.get_uuid(),
            'description':
            'Created by test at ' + start
        })

        state_vec = [
            -1.4914794358536252e+8, 1.0582106861692128e+8,
            6.0492834101479955e+7, -11.2528789273597756, -22.3258231726462242,
            -9.7271222877710155
        ]
        opm_params = OpmParams({
            'epoch': start,
            'state_vector': state_vec,
            'initial_maneuver': initial_maneuver,
        })

        return TargetedPropagation(
            propagation_params, opm_params,
            TargetingParams({
                'target_distance_from_earth': 1.0e4,
                'tolerance': 1.0
            }))

    def test_targeted_propagation(self):
        targeted_propagation = self.new_targeted_propagation([0, 0, 0])

        props = TargetedPropagations(self.service.rest)

        props.insert(targeted_propagation, self.working_project.get_uuid())
        uuid = targeted_propagation.get_uuid()
        self.assertIsNotNone(uuid)

        runnable_state = props.get_runnable_state(uuid)
        self.assertIsNotNone(runnable_state)
        while (runnable_state.get_calc_state() != 'COMPLETED'
               and runnable_state.get_calc_state() != 'FAILED'):
            runnable_state = props.get_runnable_state(uuid)
            self.assertIsNotNone(runnable_state)
        self.assertEqual('COMPLETED', runnable_state.get_calc_state())
        self.assertIsNone(runnable_state.get_error())

        runnable_state_list = props.get_runnable_states(
            self.working_project.get_uuid())
        self.assertEqual(1, len(runnable_state_list))

        props.update_with_results(targeted_propagation)
        self.assertIsNotNone(targeted_propagation.get_ephemeris())
        maneuver = targeted_propagation.get_maneuver()

        fresh_targeted_prop = props.get(uuid)
        self.assertIsNotNone(fresh_targeted_prop)
        self.assertEqual(uuid, fresh_targeted_prop.get_uuid())

        props.delete(uuid)

        self.assertIsNone(props.get(uuid))

        # Create a new propagation with the given maneuver as the initial maneuver.
        # It should report no additional maneuver needed.
        targeted_propagation2 = self.new_targeted_propagation(maneuver)

        RunnableManager(props, [targeted_propagation2],
                        self.working_project.get_uuid()).run()
        self.assertEqual(maneuver[0], targeted_propagation2.get_maneuver()[0])
        self.assertEqual(maneuver[1], targeted_propagation2.get_maneuver()[1])
        self.assertEqual(maneuver[2], targeted_propagation2.get_maneuver()[2])
Exemplo n.º 19
0
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)
Exemplo n.º 20
0
class KeplerianTest(unittest.TestCase):
    """Integration test of using keplerian elements instead of cartesian.

    """
    def setUp(self):
        config = ConfigManager(os.getcwd() + '/test_config.json').get_config()
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    def make_cartesian_and_keplerian_batches(self, start_time_str,
                                             end_time_str):
        keplerian_elements = {
            'semi_major_axis_km': 3.1307289138037175E8,
            'eccentricity': 0.5355029800000188,
            'inclination_deg': 23.439676743246295,
            'ra_of_asc_node_deg': 359.9942693176405,
            'arg_of_pericenter_deg': 328.5584374618295,
            'true_anomaly_deg': -127.01778914927144,
            'gm': 1.327124400419394E11
        }

        cartesian_state_vector = [
            -3.0653634150102222e8,
            -1.1097955684640282e8,
            -4.8129706422527283e7,  # x, y, z
            15.7598552764090590,
            -10.5875673291958420,
            -4.5896734328869746  # dx, dy, dZ
        ]

        propagation_params = PropagationParams({
            'start_time':
            start_time_str,
            'end_time':
            end_time_str,
            'project_uuid':
            self.working_project.get_uuid(),
            'description':
            'Created by test at ' + start_time_str
        })
        opm_params_templ = {
            'epoch': start_time_str,
            # state_vector or keplerian_elements will be set later.
            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5
        }

        cartesian_opm_params = opm_params_templ.copy()
        cartesian_opm_params['state_vector'] = cartesian_state_vector

        keplerian_opm_params = opm_params_templ.copy()
        keplerian_opm_params['keplerian_elements'] = keplerian_elements

        cartesian = Batch(propagation_params, OpmParams(cartesian_opm_params))
        keplerian = Batch(propagation_params, OpmParams(keplerian_opm_params))
        return cartesian, keplerian

    def test_keplerian_and_cartesian(self):
        start = datetime.datetime.now()
        end = start + datetime.timedelta(10)  # 10 days
        start_time_str = start.isoformat() + 'Z'
        end_time_str = end.isoformat() + 'Z'

        cartesian, keplerian = self.make_cartesian_and_keplerian_batches(
            start_time_str, end_time_str)

        BatchRunManager(self.service.get_batches_module(),
                        [cartesian, keplerian]).run()

        cartesian_end_state = cartesian.get_results().get_end_state_vector()
        keplerian_end_state = keplerian.get_results().get_end_state_vector()

        difference = np.subtract(cartesian_end_state, keplerian_end_state)
        npt.assert_allclose(difference[0:3], [0, 0, 0], rtol=0, atol=1e-5)
        npt.assert_allclose(difference[3:6], [0, 0, 0], rtol=0, atol=1e-10)
 def setUp(self):
     self.service = Service()
     self.assertTrue(self.service.setup_with_test_account())
     self.stm_module = StmPropagationModule(
         self.service.get_batches_module())
Exemplo n.º 22
0
class HypercubeTest(unittest.TestCase):
    """Tests hypercube propagation.

    """

    def setUp(self):
        config = ConfigManager(os.getcwd() + '/test_adam_config.json').get_config()
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    def new_hypercube_batch(self, hypercube):
        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(365 * 10)  # 10 years

        propagation_params = PropagationParams({
            'start_time': now.isoformat() + 'Z',
            'end_time': later.isoformat() + 'Z',
            'step_size': 0,
            'project_uuid': self.working_project.get_uuid(),
            'description': 'Created by test at ' + str(now) + 'Z'
        })

        state_vec = [130347560.13690618,
                     -74407287.6018632,
                     -35247598.541470632,
                     23.935241263310683,
                     27.146279819258538,
                     10.346605942591514]
        opm_params = OpmParams({
            'epoch': now.isoformat() + 'Z',
            'state_vector': state_vec,

            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5,

            'originator': 'Test',
            'object_name': 'TestObj',
            'object_id': 'TestObjId',

            # Lower triangular covariance matrix (21 elements in a list)
            'covariance': [
                3.331349476038534e-04,
                4.618927349220216e-04, 6.782421679971363e-04,
                -3.070007847730449e-04, -4.221234189514228e-04, 3.231931992380369e-04,
                -3.349365033922630e-07, -4.686084221046758e-07, 2.484949578400095e-07, 4.296022805587290e-10,  # NOQA (we want to keep the visual triangle)
                -2.211832501084875e-07, -2.864186892102733e-07, 1.798098699846038e-07, 2.608899201686016e-10, 1.767514756338532e-10,  # NOQA
                -3.041346050686871e-07, -4.989496988610662e-07, 3.540310904497689e-07, 1.869263192954590e-10, 1.008862586240695e-10, 6.224444338635500e-10],  # NOQA
            'perturbation': 3,
            'hypercube': hypercube,
        })

        return Batch(propagation_params, opm_params)

    def test_faces(self):
        batch = self.new_hypercube_batch('FACES')

        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        self.assertEqual('COMPLETED', batch.get_calc_state())

        parts = batch.get_results().get_parts()
        self.assertEqual(13, len(parts))

    def test_corners(self):
        batch = self.new_hypercube_batch('CORNERS')

        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        self.assertEqual('COMPLETED', batch.get_calc_state())

        parts = batch.get_results().get_parts()
        self.assertEqual(65, len(parts))
class ReferenceFrameTest(unittest.TestCase):
    """Integration test of using different reference frames.

    """
    def setUp(self):
        config = ConfigManager(os.getcwd() +
                               '/test_config.json').get_config('dev')
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    # This test and the following test are exactly the same propagation, but are done
    # in two different reference frames.
    def test_icrf(self):
        start_time_str = "2000-01-01T11:58:55.816Z"
        end_time_str = "2009-07-21T21:55:08.813Z"

        sun_icrf_state_vec = [
            -306536341.5010222, -110979556.84640282, -48129706.42252728,
            15.75985527640906, -10.587567329195842, -4.589673432886975
        ]

        propagation_params = PropagationParams({
            'start_time':
            start_time_str,
            'end_time':
            end_time_str,
            'step_size':
            86400,
            'project_uuid':
            self.working_project.get_uuid(),
            'description':
            'Created by test at ' + start_time_str
        })
        opm_params = OpmParams({
            'epoch': start_time_str,
            'state_vector': sun_icrf_state_vec,
            'center_name': 'SUN',
            'ref_frame': 'ICRF',
        })
        batch = Batch(propagation_params, opm_params)
        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        end_state = batch.get_results().get_end_state_vector()
        expected_end_state = [
            73978163.61069362, -121822760.05571477, -52811158.83249758,
            31.71000343989318, 29.9657246374751, .6754531613947713
        ]

        difference = np.subtract(expected_end_state, end_state)
        print("Difference is %s" % difference)
        print("End state: %s" % end_state)

        npt.assert_allclose(difference[0:3], [0, 0, 0], rtol=0, atol=.02)
        npt.assert_allclose(difference[3:6], [0, 0, 0], rtol=0, atol=.00002)

        ephem = batch.get_results().get_parts()[-1].get_ephemeris()
        self.assertTrue("ICRF" in ephem)

    def test_sun_ememe(self):
        start_time_str = "2000-01-01T11:58:55.816Z"
        end_time_str = "2009-07-21T21:55:08.813Z"

        sun_ememe_state_vec = [
            -306536346.18024945, -120966638.54521248, -12981.069369263947,
            15.759854830195243, -11.539570959741736, 0.0005481049628786039
        ]

        propagation_params = PropagationParams({
            'start_time':
            start_time_str,
            'end_time':
            end_time_str,
            'step_size':
            86400,
            'project_uuid':
            self.working_project.get_uuid(),
            'description':
            'Created by test at ' + start_time_str
        })
        opm_params = OpmParams({
            'epoch': start_time_str,
            'state_vector': sun_ememe_state_vec,
            'center_name': 'SUN',
            'ref_frame': 'EMEME2000',
        })

        batch = Batch(propagation_params, opm_params)
        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        end_state = batch.get_results().get_end_state_vector()
        # The output state is expected to be in ICRF.
        expected_end_state = [
            73978163.61069362, -121822760.05571477, -52811158.83249758,
            31.71000343989318, 29.9657246374751, .6754531613947713
        ]
        # These values are in EMEME. The resulting ephemeris is not expected to match these values.
        # expected_end_state = [73978158.47632701, -132777272.5255892, 5015.073123970032,
        #                       31.710003506237434, 27.761693311026138, -11.299967713192564]

        difference = np.subtract(expected_end_state, end_state)
        print("Difference is %s" % difference)
        print("End state: %s" % end_state)

        npt.assert_allclose(difference[0:3], [0, 0, 0], rtol=0, atol=.02)
        npt.assert_allclose(difference[3:6], [0, 0, 0], rtol=0, atol=.00002)

        # The returned ephemeris will be in Sun-centered ICRF, not EMEME. My best guess is that
        # the ephemeris file doesn't support all reference frames, so if it encounters one that
        # isn't supported, it'll choose a similar one.
        ephem = batch.get_results().get_parts()[-1].get_ephemeris()
        self.assertTrue("ICRF" in ephem)
        self.assertFalse("EMEME" in ephem)
class BackwardsPropagationTest(unittest.TestCase):
    """Integration test of backwards propagation.

    """
    def setUp(self):
        config = ConfigManager(os.getcwd() + '/test_config.json').get_config()
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)

    def tearDown(self):
        self.service.teardown()

    def make_batch(self, state_vec, start_time, end_time):
        start_time_str = start_time.isoformat() + 'Z'
        end_time_str = end_time.isoformat() + 'Z'

        propagation_params = PropagationParams({
            'start_time':
            start_time_str,
            'end_time':
            end_time_str,
            'step_size':
            0,
            'project_uuid':
            self.working_project.get_uuid(),
            'description':
            'Created by test at ' + start_time_str
        })
        opm_params = OpmParams({
            'epoch': start_time_str,
            'state_vector': state_vec,
            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5,
            'originator': 'Test',
            'object_name': 'TestObj',
            'object_id': 'TestObjId',
        })

        return Batch(propagation_params, opm_params)

    def test_backwards_and_forwards(self):
        now = datetime.datetime.now()
        later = now + datetime.timedelta(10 * 365)  # 10 years

        state_vec = [
            130347560.13690618, -74407287.6018632, -35247598.541470632,
            23.935241263310683, 27.146279819258538, 10.346605942591514
        ]

        print("Starting at %s" % (state_vec))

        print("Propagating forward from %s to %s" % (now, later))
        batch = self.make_batch(state_vec, now, later)
        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        forward_end_state = batch.get_results().get_end_state_vector()
        print("Final state at %s" % forward_end_state)

        print("Propagating backward from %s to %s" % (later, now))
        batch = self.make_batch(forward_end_state, later, now)
        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        backwards_end_state = batch.get_results().get_end_state_vector()
        print("Final state at %s" % backwards_end_state)

        difference = np.subtract(state_vec, backwards_end_state)
        print("Difference is %s" % difference)

        npt.assert_allclose(difference[0:3], [0, 0, 0], rtol=0, atol=1e-3)
        npt.assert_allclose(difference[3:6], [0, 0, 0], rtol=0, atol=1e-10)
Exemplo n.º 25
0
 def setUp(self):
     config = ConfigManager(os.getcwd() +
                            '/test_adam_config.json').get_config()
     self.service = Service(config)
     self.assertTrue(self.service.setup())
Exemplo n.º 26
0
 def setUp(self):
     self.service = Service()
     self.assertTrue(self.service.setup_with_test_account())
     self.me = "*****@*****.**"
     self._clean_up()
Exemplo n.º 27
0
class StmPropagationModuleTest(unittest.TestCase):
    """Basic integration test to demonstrate use of service tester.

    """
    def setUp(self):
        config = ConfigManager(os.getcwd() + '/test_config.json').get_config()
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.working_project = self.service.new_working_project()
        self.assertIsNotNone(self.working_project)
        self.stm_module = StmPropagationModule(
            self.service.get_batches_module())

    def tearDown(self):
        self.service.teardown()

    def test_basic_stm(self):
        state_vec = [
            130347560.13690618, -74407287.6018632, -35247598.541470632,
            23.935241263310683, 27.146279819258538, 10.346605942591514
        ]

        start_time = datetime.datetime(2017, 10, 4, 0, 0, 0, 123456)
        end_time = datetime.datetime(2018, 10, 4, 0, 0, 0, 123456)

        propagation_params = PropagationParams({
            'start_time':
            start_time.isoformat() + 'Z',
            'end_time':
            end_time.isoformat() + 'Z',
            'project_uuid':
            self.working_project.get_uuid(),
            'description':
            'Created by test at ' + start_time.isoformat() + 'Z'
        })

        opm_params = OpmParams({
            'epoch': start_time.isoformat() + 'Z',
            'state_vector': state_vec,
        })

        end_state, stm = self.stm_module.run_stm_propagation(
            propagation_params, opm_params)

        # Taken from printed output of ../state_stm_propagation.py
        expected_end_state = np.array([
            -37523497.931654416, 492950622.8491298, 204482176.63445434,
            -11.336957217854795, 7.18499733419028, 3.3597496059480085
        ])
        expected_stm = np.matrix(
            [[
                9.70874844e+00, -1.21563565e+00, -9.26967637e-01,
                5.34214567e+07, 1.64329953e+07, 5.30094892e+06
            ],
             [
                 7.11171945e+00, -3.24202476e+00, -5.93038128e-01,
                 3.90278376e+07, 3.82420496e+07, 9.62761631e+06
             ],
             [
                 2.50503331e+00, -3.00334152e-01, -2.62144498e+00,
                 1.46131045e+07, 1.04218322e+07, 1.53347450e+07
             ],
             [
                 2.14264136e-07, -2.82295666e-08, -2.23357566e-08,
                 1.33259336e+00, 6.98930318e-01, 2.41824966e-01
             ],
             [
                 4.69172199e-07, -2.03571494e-07, -6.32223023e-08,
                 2.52995851e+00, 2.04570983e+00, 7.47014439e-01
             ],
             [
                 1.82661672e-07, -4.57388872e-08, -1.15455121e-07,
                 9.96459361e-01, 8.11376173e-01, 3.16765622e-01
             ]])

        npt.assert_allclose(expected_end_state,
                            np.array(end_state),
                            rtol=1e-8,
                            atol=0)
        npt.assert_allclose(expected_stm.getA(), stm.getA(), rtol=1e-8, atol=0)
Exemplo n.º 28
0
class PermissionsTest(unittest.TestCase):
    """Integration test of permissions management.

    """
    def _clean_up(self):
        # Clean up all the groups added as part of this test. This should
        # automatically clean up any permissions associated with them.
        groups = self.service.get_groups_module()
        if len(self.added_groups) > 0:
            print("Cleaning up " + str(len(self.added_groups)) + " groups...")
        for g in self.added_groups:
            try:
                groups.delete_group(g.get_uuid())
            except RuntimeError:
                pass  # No big deal. Probably it was already deleted.

    def setUp(self):
        config = ConfigManager(os.getcwd() +
                               '/test_adam_config.json').get_config()
        self.service = Service(config)
        self.assertTrue(self.service.setup())
        self.me = "*****@*****.**"
        self.added_groups = []

    def tearDown(self):
        self.service.teardown()
        self._clean_up()

    def test_permission_management(self):
        permissions = self.service.get_permissions_module()

        # Create three groups to grant permission to.
        groups = self.service.get_groups_module()
        group1 = groups.new_group("name1", "description1")
        self.added_groups.append(group1)
        group2 = groups.new_group("name2", "description2")
        self.added_groups.append(group2)
        group3 = groups.new_group("name3", "description3")
        self.added_groups.append(group3)

        # All permissions lists should be empty.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {group1.get_uuid(): []})
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): []})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # I should have permission to all three groups.
        pms = permissions.get_my_permissions()
        self.assertTrue(
            Permission('ADMIN', 'GROUP', group1.get_uuid()) in pms[''])
        self.assertTrue(
            Permission('ADMIN', 'GROUP', group2.get_uuid()) in pms[''])
        self.assertTrue(
            Permission('ADMIN', 'GROUP', group3.get_uuid()) in pms[''])

        # Add WRITE permission for group1 on group2.
        pm1 = Permission('WRITE', 'GROUP', group2.get_uuid())
        permissions.grant_group_permission(group1.get_uuid(), pm1)

        # group1 should have that permissions listed. Other groups should be unaffected.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {group1.get_uuid(): [pm1]})
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): []})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # Add READ permission for group2 on group3.
        pm2 = Permission('READ', 'GROUP', group3.get_uuid())
        permissions.grant_group_permission(group2.get_uuid(), pm2)

        # That should show up in group2's list.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {group1.get_uuid(): [pm1]})
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): [pm2]})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # Add group1 as a member of group2. Now pm2 should show up transitively in
        # group1's list as well.
        groups.add_group_to_group(group1.get_uuid(), group2.get_uuid())
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {
            group1.get_uuid(): [pm1],
            group2.get_uuid(): [pm2]
        })
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): [pm2]})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # Add group2 to group3 and add ADMIN permission for group3 on group1
        # (yes, cycles are OK).
        pm3 = Permission('ADMIN', 'GROUP', group1.get_uuid())
        permissions.grant_group_permission(group3.get_uuid(), pm3)
        groups.add_group_to_group(group2.get_uuid(), group3.get_uuid())

        # pm3 should show up for everybody.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(
            pms, {
                group1.get_uuid(): [pm1],
                group2.get_uuid(): [pm2],
                group3.get_uuid(): [pm3]
            })
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {
            group2.get_uuid(): [pm2],
            group3.get_uuid(): [pm3]
        })
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): [pm3]})

        # All of these transitive permissions should show up in my permissions.
        pms = permissions.get_my_permissions()
        self.assertEqual(pms[group1.get_uuid()], [pm1])
        self.assertEqual(pms[group2.get_uuid()], [pm2])
        self.assertEqual(pms[group3.get_uuid()], [pm3])

        # Remove pm3, which should be removed from everybody's lists. The lists should
        # still include an item for group3 because membership hasn't changed, just with
        # no permissions directly granted.
        permissions.revoke_group_permission(group3.get_uuid(), pm3)
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(
            pms, {
                group1.get_uuid(): [pm1],
                group2.get_uuid(): [pm2],
                group3.get_uuid(): []
            })
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {
            group2.get_uuid(): [pm2],
            group3.get_uuid(): []
        })
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})
        pms = permissions.get_my_permissions()
        self.assertEqual(pms[group1.get_uuid()], [pm1])
        self.assertEqual(pms[group2.get_uuid()], [pm2])
        self.assertEqual(pms[group3.get_uuid()], [])

        # Delete group1. All permissions (pm1) involving group1 should no longer exist.
        groups.delete_group(group1.get_uuid())
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {
            group2.get_uuid(): [pm2],
            group3.get_uuid(): []
        })  # It's still in group3
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})
        pms = permissions.get_my_permissions()
        self.assertFalse(group1.get_uuid() in pms)
        self.assertEqual(pms[group2.get_uuid()], [pm2])
        self.assertEqual(pms[group3.get_uuid()], [])

        # Delete group2. This should remove pm2.
        groups.delete_group(group2.get_uuid())
        pms = permissions.get_my_permissions()
        self.assertFalse(group1.get_uuid() in pms)
        self.assertFalse(group2.get_uuid() in pms)
        self.assertEqual(pms[group3.get_uuid()], [])

        # Now grant and revoke a user permission, just to show it doesn't die.
        permissions.grant_user_permission(
            "u1", Permission('READ', 'GROUP', group3.get_uuid()))
        permissions.revoke_user_permission(
            "u1", Permission('READ', 'GROUP', group3.get_uuid()))

        groups.delete_group(group3.get_uuid())

    def test_permission_mismanagement(self):
        permissions = self.service.get_permissions_module()

        # Target object does not exist.
        with self.assertRaises(RuntimeError):
            permissions.grant_user_permission(
                'u1', Permission('READ', 'GROUP', 'wat this is not a group'))

        groups = self.service.get_groups_module()
        group1 = groups.new_group("name1", "description1")
        self.added_groups.append(group1)

        # Recipient of permission does not exist.
        with self.assertRaises(RuntimeError):
            permissions.grant_group_permission(
                'g1', Permission('READ', 'GROUP', group1.get_uuid()))

        # Target does not exist.
        with self.assertRaises(RuntimeError):
            permissions.grant_group_permission(
                group1.get_uuid(),
                Permission('READ', 'GROUP', 'wat this is not a group'))

        # Finally this works.
        permissions.grant_group_permission(
            group1.get_uuid(), Permission('READ', 'GROUP', group1.get_uuid()))

        # Unable to check authorization (target does not exist).
        with self.assertRaises(RuntimeError):
            permissions.revoke_user_permission(
                "u1", Permission('READ', 'GROUP', 'wat this is not a group'))
        with self.assertRaises(RuntimeError):
            permissions.revoke_group_permission(
                group1.get_uuid(),
                Permission('READ', 'GROUP', 'wat this is not a group'))

        # But this is fine. Deleting something that doesn't exist.
        permissions.revoke_group_permission(
            'not a group', Permission('READ', 'GROUP', group1.get_uuid()))

        # Can't inspect nonexistent things either.
        with self.assertRaises(RuntimeError):
            permissions.get_group_permissions('not a group')

        # Not permitted to inspect other users.
        with self.assertRaises(RuntimeError):
            permissions.get_my_permissions(
                user_superuser_only="some other user")

        groups.delete_group(group1.get_uuid())
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())