def test_enterprise_team(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseTeamCommand()
        with mock.patch('builtins.print'):
            cmd.execute(params, team=[ent_env.team1_uid])

        TestEnterprise.expected_commands = ['team_add']
        cmd.execute(params,
                    add=True,
                    restrict_edit='on',
                    node=str(ent_env.node1_id),
                    team=['Team 3'])
        self.assertEqual(len(TestEnterprise.expected_commands), 0)

        with mock.patch('keepercommander.commands.enterprise.user_choice'
                        ) as mock_choice:
            TestEnterprise.expected_commands = ['team_delete']
            mock_choice.return_value = 'y'
            cmd.execute(params, delete=True, team=['Team 1'])
            self.assertEqual(len(TestEnterprise.expected_commands), 0)

            with mock.patch('builtins.print'):
                mock_choice.return_value = 'n'
                cmd.execute(params, delete=True, team=[ent_env.team1_uid])
                self.assertEqual(len(TestEnterprise.expected_commands), 0)

        with self.assertLogs(level=logging.WARNING):
            cmd.execute(params, delete=True, team=['Unknown Team'])
            self.assertEqual(len(TestEnterprise.expected_commands), 0)
 def test_get_enterprise(self):
     params = get_connected_params()
     api.query_enterprise(params)
     self.assertIsNotNone(params.enterprise)
     self.assertEqual(params.enterprise['unencrypted_tree_key'],
                      ent_env.tree_key)
     self.assertEqual(len(params.enterprise['nodes']), 2)
    def test_enterprise_wrong_user(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseUserCommand()
        with self.assertLogs(level=logging.WARNING):
            cmd.execute(params, lock=True, email='*****@*****.**')
 def test_get_enterprise_public_key(self):
     TestEnterprise.use_data_key = False
     params = get_connected_params()
     api.query_enterprise(params)
     self.assertIsNotNone(params.enterprise)
     self.assertEqual(params.enterprise['unencrypted_tree_key'], ent_env.tree_key)
     self.assertEqual(len(params.enterprise['nodes']), 2)
    def wipe_out_data(cls):
        params = cls.params  # type: KeeperParams
        managed_roles = set()
        if 'managed_nodes' in params.enterprise:
            for mn in params.enterprise['managed_nodes']:
                managed_roles.add(mn['role_id'])

        if 'role_users' in params.enterprise:
            for ru in params.enterprise['role_users']:
                if ru['role_id'] not in managed_roles:
                    request = {
                        'command': 'role_user_remove',
                        'role_id': ru['role_id'],
                        'enterprise_user_id': ru['enterprise_user_id']
                    }
                    api.communicate(params, request)

        if 'users' in params.enterprise:
            for user in params.enterprise['users']:
                if user['status'] == 'invited':
                    request = {
                        'command': 'enterprise_user_delete',
                        'enterprise_user_id': user['enterprise_user_id']
                    }
                    api.communicate(params, request)

        if 'teams' in params.enterprise:
            for team in params.enterprise['teams']:
                request = {
                    'command': 'team_delete',
                    'team_uid': team['team_uid']
                }
                api.communicate(params, request)
        api.query_enterprise(params)
    def test_enterprise_info_command(self):
        params = get_connected_params()
        api.query_enterprise(params)

        with mock.patch('builtins.print'):
            cmd = enterprise.EnterpriseInfoCommand()
            cmd.execute(params, verbose=True)
    def test_enterprise_role(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseRoleCommand()
        with mock.patch('builtins.print'):
            cmd.execute(params, role=[ent_env.role1_name])

        with mock.patch('keepercommander.commands.enterprise.user_choice'
                        ) as mock_choice:
            mock_choice.return_value = 'y'
            TestEnterprise.expected_commands = ['role_user_add']
            cmd.execute(params,
                        add_user=[ent_env.user2_email],
                        role=[ent_env.role1_id])
            self.assertEqual(len(TestEnterprise.expected_commands), 0)

        TestEnterprise.expected_commands = ['role_user_remove']
        cmd.execute(params,
                    remove_user=[ent_env.user2_email],
                    role=[ent_env.role1_name])
        self.assertEqual(len(TestEnterprise.expected_commands), 0)

        with self.assertLogs(level=logging.WARNING):
            cmd.execute(params,
                        add_user=[ent_env.user2_email],
                        verbose=True,
                        role=['Invalid'])
            with mock.patch('builtins.print'):
                cmd.execute(params,
                            add_user=['*****@*****.**'],
                            verbose=True,
                            role=[ent_env.role1_name])
    def test_enterprise_delete_user(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseUserCommand()
        TestEnterprise.expected_commands = ['enterprise_user_delete']
        cmd.execute(params, delete=True, force=True, email=ent_env.user2_email)
        self.assertEqual(len(TestEnterprise.expected_commands), 0)
    def test_enterprise_add_user(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseUserCommand()
        TestEnterprise.expected_commands = ['enterprise_user_add']
        cmd.execute(params, add=True, email='*****@*****.**')
        self.assertEqual(len(TestEnterprise.expected_commands), 0)
Beispiel #10
0
    def test_enterprise_lock_user(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseUserCommand()
        TestEnterprise.expected_commands = ['enterprise_user_lock', 'enterprise_user_lock']
        cmd.execute(params, unlock=True, email=[ent_env.user2_email])
        cmd.execute(params, lock=True, email=[ent_env.user2_email])
        self.assertEqual(len(TestEnterprise.expected_commands), 0)
    def test_enterprise_user_team(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseUserCommand()
        TestEnterprise.expected_commands = ['team_enterprise_user_add']
        cmd.execute(params, add_team=[ent_env.team2_uid], email=ent_env.user2_email)
        self.assertEqual(len(TestEnterprise.expected_commands), 0)

        TestEnterprise.expected_commands = ['team_enterprise_user_remove']
        cmd.execute(params, remove_team=[ent_env.team2_uid], email=ent_env.user2_email)
        self.assertEqual(len(TestEnterprise.expected_commands), 0)
Beispiel #12
0
    def test_enterprise_user_update(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseUserCommand()
        TestEnterprise.expected_commands = ['enterprise_user_update']
        cmd.execute(params, node='Root node', email=[ent_env.user2_email])
        self.assertEqual(len(TestEnterprise.expected_commands), 0)

        TestEnterprise.expected_commands = ['enterprise_user_update']
        cmd.execute(params, node='{0}'.format(ent_env.node1_id), email=[ent_env.user2_email])
        self.assertEqual(len(TestEnterprise.expected_commands), 0)
    def tearDownClass(cls):
        params = cls.params
        api.query_enterprise(params)
        for user in params.enterprise['users']:
            if user['status'] == 'actove' and user['lock'] != 0:
                request = {
                    'command': 'enterprise_user_lock',
                    'enterprise_user_id': user['enterprise_user_id'],
                    'lock': 'unlocked'
                }
                api.communicate(params, request)

        cli.do_command(params, 'logout')
    def test_enterprise_expire_password(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterpriseUserCommand()
        TestEnterprise.expected_commands = ['set_master_password_expire']
        cmd.execute(params, expire=True, force=True, email=ent_env.user2_email)
        self.assertEqual(len(TestEnterprise.expected_commands), 0)

        with mock.patch('keepercommander.commands.enterprise.user_choice') as mock_choice:
            TestEnterprise.expected_commands = ['set_master_password_expire']
            mock_choice.return_value = 'y'
            cmd.execute(params, expire=True, email=ent_env.user2_email)
            self.assertEqual(len(TestEnterprise.expected_commands), 0)
            mock_choice.return_value = 'n'
            cmd.execute(params, expire=True, email=ent_env.user2_email)
    def wipe_out_data(cls):
        params = cls.params    # type: KeeperParams
        managed_roles = set()
        for mn in params.enterprise['managed_nodes']:
            managed_roles.add(mn['role_id'])

        for ru in params.enterprise['role_users']:
            if ru['role_id'] not in managed_roles:
                request = {
                    'command': 'role_user_remove',
                    'role_id': ru['role_id'],
                    'enterprise_user_id': ru['enterprise_user_id']
                }
                api.communicate(params, request)

        for user in params.enterprise['users']:
            if user['username'] in ['*****@*****.**', '*****@*****.**']:
                if user['lock'] != 0:
                    request = {
                        'command': 'enterprise_user_lock',
                        'enterprise_user_id': user['enterprise_user_id'],
                        'lock': 'unlocked'
                    }
                    api.communicate(params, request)
            else:
                request = {
                    'command': 'enterprise_user_delete',
                    'enterprise_user_id': user['enterprise_user_id']
                }
                api.communicate(params, request)

        if 'teams' in params.enterprise:
            for team in params.enterprise['teams']:
                request = {
                    'command': 'team_delete',
                    'team_uid': team['team_uid']
                }
                api.communicate(params, request)
        api.query_enterprise(params)
Beispiel #16
0
slh = ScriptLogHandler()
slh.setLevel(logging.WARNING)
logging.root.addHandler(slh)

# Create parameters
my_params = KeeperParams()
my_params.user = ''  # Keeper account name. 'config.json' file, 'user' property
my_params.password = ''  # Keeper account password.
my_params.device_token = ''  # Device Token. 'config.json' file, 'device_token' property
my_params.device_private_key = ''  # Device Key. 'config.json' file, 'private_key' property

# Login to Keeper
api.login(my_params)

# Load the Enterprise configuration state
api.query_enterprise(my_params)

# Create team/user mapping
if 'users' in my_params.enterprise and 'team_users' in my_params.enterprise:
    # params.enterprise['users'] is a list of all users in enterprise
    # params.enterprise['team_users'] is a list of team <-> user pairs

    # map user ID to active user email.
    user_lookup = {
        u['enterprise_user_id']: u['username']
        for u in my_params.enterprise['users'] if u['status'] == 'active'
    }

    # team_users. key is team_uid, value is set of emails
    team_users = {}
    for tu in my_params.enterprise['team_users']:
    def test_enterprise_push_command(self):
        params = get_connected_params()
        api.query_enterprise(params)

        cmd = enterprise.EnterprisePushCommand()

        template_body = '''
[
    {
        "title": "Record For ${user_name}",
        "login": "******",
        "password": "******",
        "login_url": "https://keepersecurity.com",
        "notes": "notes",
        "custom_fields": {
            "key1": "value1",
            "key2": "${user_email}"
        }
    },
    {
        "title": "Empty record"
    }

]'''
        templates = json.loads(template_body)
        values = {
            'user_name': api.generate_record_uid(),
            'generate_password': api.generate_record_uid(),
            'user_email': api.generate_record_uid()
        }
        cmd.enumerate_and_substitute_dict_fields(templates[0], values)
        cmd.enumerate_and_substitute_dict_fields(templates[1], values)
        self.assertEqual(templates[0]['title'],
                         'Record For {0}'.format(values['user_name']))
        self.assertEqual(templates[0]['password'], values['generate_password'])
        self.assertEqual(templates[0]['custom_fields']['key2'],
                         values['user_email'])
        self.assertEqual(templates[1]['title'], 'Empty record')

        with self.assertRaises(CommandError):
            cmd.execute(params, file='template.json')

        with self.assertRaises(CommandError):
            cmd.execute(params, user=[ent_env.user2_email])

        def get_public_keys(_params, emails):
            for email in emails:
                emails[email] = vault_env.public_key

        with mock.patch('builtins.open', mock.mock_open(read_data=template_body)), \
                mock.patch('os.path.abspath', return_value='template.json'), \
                mock.patch('os.path.isfile', return_value=True), \
                mock.patch('keepercommander.commands.enterprise.EnterpriseCommand.get_public_keys') as m_pk:

            m_pk.side_effect = get_public_keys

            TestEnterprise.expected_commands = ['execute']
            cmd.execute(params,
                        file='template.json',
                        team=[ent_env.team1_name])
            self.assertEqual(len(TestEnterprise.expected_commands), 0)

            TestEnterprise.expected_commands = ['execute']
            cmd.execute(params,
                        file='template.json',
                        user=[ent_env.user2_email])
            self.assertEqual(len(TestEnterprise.expected_commands), 0)