def test_that_config_can_be_written_to_file(self): cfg = UserConfig('name', '/path/to/user/root') cfg['section.one'] = 'first value' cfg['section.two'] = 'second value' with NamedTemporaryFile() as tmp: cfg.write_to(tmp) tmp.seek(0) self.assertEqual('[section]\none = first value\ntwo = second value\n\n', tmp.read())
def test_that_config_can_be_written_to_file(self): cfg = UserConfig('name', '/path/to/user/root') cfg['section.one'] = 'first value' cfg['section.two'] = 'second value' with NamedTemporaryFile() as tmp: cfg.write_to(tmp) tmp.seek(0) self.assertEqual( '[section]\none = first value\ntwo = second value\n\n', tmp.read())
def test_that_config_can_be_read_from_file(self): with NamedTemporaryFile() as tmp: tmp.write('[section]\none = first value\ntwo = second value\n\n') tmp.seek(0) cfg = UserConfig.read_from('name', 'path/to/user/root', tmp) self.assertEqual('first value', cfg['section.one'])
def test_unicode_passwords_dont_cause_type_error(self): cfg = UserConfig('name', None) self.users.config.return_value = cfg auth = Authenticator(self.users, self.provider) auth.add_credentials('name', u'password') self.assertTrue(auth.authenticate('name', u'password'))
def test_authenticate(self): cfg = UserConfig('name', None) cfg['auth.salt'] = '313930303736343935383333373939343331373734343230383436303239383830363831383831' cfg['auth.hashed_password'] = '******' self.users.config.return_value = cfg auth = Authenticator(self.users, self.provider) self.assertFalse(auth.authenticate('name', 'invalid')) self.assertTrue(auth.authenticate('name', 'password'))
def test_reset_agent_data(self): # given user_config = UserConfig('first', None) self.mock_users.config.return_value = user_config self.mock_provider.status.return_value = {'state': 'stopped'} # when r = self.put('https://localhost:4443/agents/first/reset_data', data={}) # then self.assertSuccessJson({'state': 'stopped'}, r) self.mock_provider.reset_data.assert_called_with(user_config)
def test_reset_agent_data_returns_conflict_if_agent_is_running(self): # given user_config = UserConfig('first', None) self.mock_users.config.return_value = user_config self.mock_provider.status.return_value = {'state': 'running'} self.mock_provider.reset_data.side_effect = InstanceAlreadyRunningError # when r = self.put('https://localhost:4443/agents/first/reset_data', data={}) # then self.assertEqual(409, r.status_code)
def test_store_arbitrary_values(self): cfg = UserConfig('name', '/path/to/user/root') cfg['section.value'] = 'some value' cfg['other_section.other_value'] = 'other value' with self.assertRaises(KeyError): cfg['invalid'] = 'some value' with self.assertRaises(KeyError): cfg['section.value.toomuch'] = 'some value' self.assertEqual('some value', cfg['section.value'])
def test_start_agent(self): # given user_config = UserConfig('first', None) self.mock_provider.status.return_value = {'state': 'running'} self.mock_users.config.return_value = user_config payload = {'state': 'running'} # when r = self.put('https://localhost:4443/agents/first/state', data=payload) # then self.assertSuccessJson({'state': 'running'}, r) self.mock_provider.start.assert_called_with(user_config)
def test_add_credentials(self, getrandbits_mock): auth = Authenticator(self.users, self.provider) cfg = UserConfig('name', None) getrandbits_mock.return_value = 190076495833799431774420846029880681881 self.users.config.return_value = cfg auth.add_credentials('username', 'password') expected_salt = '313930303736343935383333373939343331373734343230383436303239383830363831383831' expected_hashed_password = '******' self.assertEqual(expected_salt, cfg['auth.salt']) self.assertEqual(expected_hashed_password, cfg['auth.hashed_password']) self.users.update_config.assert_called_once_with(cfg)
def test_user_can_be_authenticated_and_passes_credentials_to_provider( self): # given user_config = UserConfig('first', None) self.mock_users.config.return_value = user_config self.mock_authenticator.authenticate.return_value = True payload = {'password': '******'} # when r = self.post('https://localhost:4443/agents/first/authenticate', data=payload) # then self.assertEqual(200, r.status_code) self.mock_provider.pass_credentials_to_agent.assert_called_once_with( user_config, 'some password')
def test_authenticate_with_leap_user_not_yet_known_locally( self, srp_mock, which_api_CA_bundle_mock): # given user_config = UserConfig('name', None) which_api_CA_bundle_mock.return_value = 'some bundle' self.users.has_user_config.return_value = False self.users.config.return_value = user_config # when auth = Authenticator(self.users, self.provider) result = auth.authenticate('name', 'password') # then srp_mock.return_value.authenticate.assert_called_once_with( 'name', 'password') self.users.add.assert_called_once_with('name') srp_mock.assert_called_once_with('/1/some/uri', 'some bundle') srp_mock.return_value.authenticate.assert_called_once_with( 'name', 'password') self.users.update_config.assert_called_once_with(user_config) self.assertTrue(result)
def _user_config(self, name): path = join(self.root_path, name) os.makedirs(path) return UserConfig(name, path)
def test_instances_can_be_removed(self): user_config = UserConfig('test', join(self.root_path, 'test')) os.makedirs(join(user_config.path, 'data')) self.provider.remove(user_config)
def test_that_non_started_instance_cannot_be_stopped(self): self.assertRaises(InstanceNotRunningError, self.provider.stop, UserConfig('not-started', None))
def test_config_expects_username_and_root_path(self): cfg = UserConfig('name', '/path/to/user/root') self.assertEqual('name', cfg.username) self.assertEqual('/path/to/user/root', cfg.path)