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_account_transfer_consent(self): params = get_connected_params() params.settings = { 'share_account_to': [{ 'role_id': ent_env.role1_id, 'public_key': vault_env.encoded_public_key }], 'must_perform_account_share_by': datetime.datetime.now().timestamp() } cmd = utils.CheckEnforcementsCommand() with mock.patch('builtins.print'), mock.patch( 'keepercommander.api.accept_account_transfer_consent' ) as m_transfer: m_transfer.return_value = True cmd.execute(params) m_transfer.assert_called() self.assertNotIn('share_account_to', params.settings) self.assertNotIn('must_perform_account_share_by', params.settings) m_transfer.reset() m_transfer.return_value = False cmd.execute(params) m_transfer.assert_called() self.assertNotIn('share_account_to', params.settings) self.assertNotIn('must_perform_account_share_by', params.settings)
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(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_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_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 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_export_import(self): params_export = get_synced_params() cmd_export = commands.RecordExportCommand() param_import = get_connected_params() cmd_import = commands.RecordImportCommand() json_text = '' def mock_write(text): nonlocal json_text json_text += text def mock_read(): nonlocal json_text return json_text with mock.patch('keepercommander.api.sync_down'), mock.patch('builtins.open', mock.mock_open()) as m_open: m_open.return_value.write = mock_write cmd_export.execute(params_export, format='json', name='json') with mock.patch('keepercommander.api.sync_down'), mock.patch('builtins.open', mock.mock_open()) as m_open: m_open.return_value.read = mock_read self.communicate_mock.side_effect = None self.communicate_mock.return_value = { 'result': 'success', 'result_code': '', 'message': '' } with mock.patch('os.path.isfile', return_value=True): cmd_import.execute(param_import, format='json', name='json')
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)
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_decline_account_transfer_consent(self): params = get_connected_params() share_account_to = [{ 'role_id': 123456789, 'public_key': vault_env.encoded_public_key }] with mock.patch('builtins.print'), mock.patch('builtins.input', return_value='decline'): self.assertFalse( api.accept_account_transfer_consent(params, share_account_to))
def test_full_sync(self): params = get_connected_params() self.communicate_mock.side_effect = None self.communicate_mock.return_value = get_sync_down_response() sync_down(params) self.assertEqual(len(params.record_cache), 3) self.assertEqual(len(params.shared_folder_cache), 1) self.assertEqual(len(params.team_cache), 1) self.assert_key_unencrypted(params)
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)
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 test_decline_account_transfer_consent(self): params = get_connected_params() params.settings = { 'must_perform_account_share_by': '1632370067000', 'share_account_to': [{ 'role_id': 123456789, 'public_key': vault_env.encoded_public_key }] } with mock.patch('builtins.print'), mock.patch('builtins.input', return_value='decline'): self.assertFalse(api.accept_account_transfer_consent(params))
def test_accept_account_transfer_consent(self): params = get_connected_params() share_account_to = [{ 'role_id': 123456789, 'public_key': vault_env.encoded_public_key }] with mock.patch('builtins.print'), mock.patch('builtins.input', return_value='accept'): KeeperApiHelper.communicate_expect(['share_account']) self.assertTrue( api.accept_account_transfer_consent(params, share_account_to)) self.assertTrue(KeeperApiHelper.is_expect_empty())
def test_accept_account_transfer_consent(self): params = get_connected_params() params.settings = { 'must_perform_account_share_by': '1632370067000', 'share_account_to': [{ 'role_id': 123456789, 'public_key': vault_env.encoded_public_key }] } with mock.patch('builtins.print'), mock.patch('builtins.input', return_value='accept'): KeeperApiHelper.communicate_expect(['share_account']) self.assertTrue(api.accept_account_transfer_consent(params)) self.assertTrue(KeeperApiHelper.is_expect_empty())
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 test_change_password(self): params = get_connected_params() with mock.patch('keepercommander.rest_api.get_new_user_params' ) as m_params, mock.patch( 'builtins.print'), mock.patch( 'getpass.getpass') as m_getpass: user_params = APIRequest_pb2.NewUserMinimumParams() user_params.minimumIterations = 1000 user_params.passwordMatchRegex.extend( ['^(?=(.*[A-Z]){1,}).*$', '^(?=(.*[0-9]){2,}).*$', '.{6,}']) user_params.passwordMatchDescription.extend([ 'Contains at least 1 uppercase character(s)', 'Contains at least 2 digit(s)', 'At least 6 character(s)' ]) m_params.return_value = user_params m_getpass.return_value = '1New2Password3' KeeperApiHelper.communicate_expect(['change_master_password']) self.assertTrue(api.change_master_password(params)) self.assertTrue(KeeperApiHelper.is_expect_empty())
def test_enterprise_invite(self): params = get_connected_params() params.enforcements = {'enterprise_invited': 'Test Enterprise'} cmd = utils.CheckEnforcementsCommand() with mock.patch('builtins.print'), mock.patch( 'keepercommander.commands.utils.user_choice' ) as m_choice, mock.patch('builtins.input') as m_input: # accept, enter invite code def accept_enterprise_invite(rq): self.assertEqual(rq['command'], 'accept_enterprise_invite') self.assertEqual(rq['verification_code'], TestRegister.enterpriseInviteCode) m_choice.return_value = 'Accept' m_input.return_value = TestRegister.enterpriseInviteCode KeeperApiHelper.communicate_expect([accept_enterprise_invite]) cmd.execute(params) self.assertTrue(KeeperApiHelper.is_expect_empty()) # accept, skip invite code m_choice.return_value = 'Accept' m_input.return_value = '' cmd.execute(params) # decline m_choice.return_value = 'Decline' m_input.side_effect = KeyboardInterrupt() KeeperApiHelper.communicate_expect(['decline_enterprise_invite']) cmd.execute(params) self.assertTrue(KeeperApiHelper.is_expect_empty()) # ignore m_choice.return_value = 'Ignore' cmd.execute(params)
def test_change_weak_password(self): params = get_connected_params() with mock.patch('keepercommander.rest_api.get_new_user_params' ) as m_params, mock.patch( 'builtins.print'), mock.patch( 'getpass.getpass') as m_getpass: user_params = APIRequest_pb2.NewUserMinimumParams() user_params.minimumIterations = 1000 user_params.passwordMatchRegex.extend( ['^(?=(.*[A-Z]){1,}).*$', '^(?=(.*[0-9]){2,}).*$', '.{6,}']) user_params.passwordMatchDescription.extend([ 'Contains at least 1 uppercase character(s)', 'Contains at least 2 digit(s)', 'At least 6 character(s)' ]) m_params.return_value = user_params m_getpass.side_effect = [ 'NewPassword', 'NewPassword', '', Exception() ] with self.assertLogs(): self.assertFalse(api.change_master_password(params))
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)
def test_refresh_session_token(self): params = get_connected_params() login(params) self.assertEqual(params.session_token, vault_env.session_token) self.assertEqual(params.data_key, vault_env.data_key)