def test_user_update_method_was_called(self, mock_user_update): patch('identity.keystone.Keystone.project_list', Mock(return_value=[FakeResource(1, 'project1')])).start() patch('identity.keystone.Keystone.project_get', Mock(return_value=1)).start() user = FakeResource(1, 'user1') user.to_dict = lambda: {'name': user.name} user.project_id = 1 patch('identity.keystone.Keystone.user_get', Mock(return_value=user)).start() project = FakeResource(1, 'project1') project.to_dict = lambda: {'name': project.name} patch('identity.keystone.Keystone.project_get', Mock(return_value=project)).start() self.request.method = 'POST' post = self.request.POST.copy() post.update({'id': 1, 'name': 'aaa', 'project': 1}) self.request.POST = post response = self.view(self.request) mock_user_update.assert_called_with(user, name='aaa', project=project, domain=None, enabled=True, password=None, email=None)
def test_update_validating_name_field_blank(self, mock_gp, mock_ap): mock_gp.return_value = GroupProjectsFactory.build(group_id=1, project_id=1) mock_ap.return_value = AreaProjectsFactory.build(area_id=1, project_id=1) project = FakeResource(1, 'project1') project.to_dict = lambda: {'name': project.name} project.default_project_id = 1 patch('identity.keystone.Keystone.project_get', Mock(return_value=project)).start() self.request.method = 'POST' post = self.request.POST.copy() post.update({ 'name': '', 'id': 1, 'description': 'description', 'groups': 1, 'areas': 1}) self.request.POST = post response = self.view(self.request) response.render() self.assertIn(_('This field is required'), response.content.decode('UTF-8'))
def test_validating_name_field_non_alphanumeric(self, mock_gp, mock_ap): mock_gp.return_value = GroupProjectsFactory.build(group_id=1, project_id=1) mock_ap.return_value = AreaProjectsFactory.build(area_id=1, project_id=1) project = FakeResource(1, 'project1') project.to_dict = lambda: { 'name': project.name, 'description': project.description } patch('identity.keystone.Keystone.project_get', Mock(return_value=project)).start() self.request.method = 'POST' post = self.request.POST.copy() post.update({ 'name': 'valor inválido', 'id': 1, 'description': 'description'}) self.request.POST = post response = self.view(self.request) response.render() self.assertIn(_('Project Name must be an alphanumeric.'), response.content.decode('UTF-8'))
def test_validating_email_field(self): user = FakeResource(1, 'user1') user.to_dict = lambda: {'name': user.name} user.default_project_id = 1 user.project_id = 1 patch('identity.keystone.Keystone.user_get', Mock(return_value=user)).start() self.request.method = 'POST' self.request.META.update({'HTTP_HOST': 'localhost'}) post = self.request.POST.copy() post.update({ 'name': 'aaa', 'enabled': True, 'id': 1, 'project': 1, 'role': 1, 'password': '******', 'password_confirm': 'aaa', 'email': 'a.a.net' }) self.request.POST = post response = self.view(self.request) response.render() self.assertIn(_('Enter a valid email address'), response.content.decode('UTF-8'))
def test_update_user_change_password_exception(self, mock_user_update): mock_user_update.side_effect = Exception() patch('identity.keystone.Keystone.project_list', Mock(return_value=[FakeResource(1, 'project1')])).start() patch('identity.keystone.Keystone.project_get', Mock(return_value=1)).start() user = FakeResource(1, 'user1') user.to_dict = lambda: {'name': user.name} user.default_project_id = 1 patch('identity.keystone.Keystone.user_get', Mock(return_value=user)).start() self.request.method = 'POST' post = self.request.POST.copy() post.update({ 'id': 1, 'name': 'aaa', 'project': 1, 'password': '******', 'password_confirm': 'globo', }) self.request.POST = post response = self.view(self.request) msgs = [msg for msg in self.request._messages] self.assertIn(_('Passwords did not match'), response.rendered_content) self.assertEqual(len(msgs), 0)
def test_update_user_view_exception(self, mock_user_update): mock_user_update.side_effect = Exception() patch('identity.keystone.Keystone.project_list', Mock(return_value=[FakeResource(1, 'project1')])).start() patch('identity.keystone.Keystone.project_get', Mock(return_value=1)).start() user = FakeResource(1, 'user1') user.to_dict = lambda: {'name': user.name} user.default_project_id = 1 patch('identity.keystone.Keystone.user_get', Mock(return_value=user)).start() self.request.method = 'POST' post = self.request.POST.copy() post.update({'id': 1, 'name': 'aaa', 'project': 1}) self.request.POST = post response = self.view(self.request) msgs = [msg for msg in self.request._messages] self.assertGreater(len(msgs), 0) self.assertEqual(msgs[0].message, _('Error when update user'))
def test_vault_update_project_method_was_called(self, mock): project = FakeResource(1, 'project1') project.to_dict = lambda: {'name': project.name} project.default_project_id = 1 patch('identity.keystone.Keystone.project_get', Mock(return_value=project)).start() self.request.method = 'POST' post = self.request.POST.copy() post.update({'name': 'bbb', 'description': 'desc', 'enabled': True, 'areas': 1, 'groups': 1}) self.request.POST = post _ = self.view(self.request) mock.assert_called_with(project.id, project.name, 1, 1, description='desc', enabled=True)
def test_show_project_list(self): """ We only check if the project_list view is being called with the right list of projects on the context. """ fake_project = FakeResource(1) # This mocks is faking keystone retrieving a defined list of # projects patch('identity.views.Keystone.project_list', Mock(return_value=[fake_project])).start() render_mock = patch( 'identity.views.ListProjectView.render_to_response').start() response = self.view(self.request) render_args = render_mock.call_args[0][0] computed = render_args['projects'][0] self.assertEqual(computed, fake_project.to_dict())
def test_validating_email_field(self): user = FakeResource(1, 'user1') user.to_dict = lambda: {'name': user.name} user.default_project_id = 1 user.project_id = 1 patch('identity.keystone.Keystone.user_get', Mock(return_value=user)).start() self.request.method = 'POST' post = self.request.POST.copy() post.update({'name': 'aaa', 'enabled': True, 'id': 1, 'project': 1, 'role': 1, 'password': '******', 'password_confirm': 'aaa', 'email': 'a.a.net'}) self.request.POST = post response = self.view(self.request) response.render() self.assertIn(_('Enter a valid email address'), response.content.decode('UTF-8'))
def test_initial_data_loaded(self, mock_ap, mock_gp): group_id = 123 area_id = 456 mock_gp.return_value = GroupProjectsFactory(id=1, group_id=group_id) mock_ap.return_value = AreaProjectsFactory(id=2, area_id=area_id) project = FakeResource(1, 'project1') project.default_project_id = 1 project.description = 'description' project.to_dict = lambda: {'name': project.name, 'description': project.description} patch('identity.keystone.Keystone.project_get', Mock(return_value=project)).start() response = self.view(self.request, project_id='project_id') computed_form = response.context_data['form'] self.assertEqual(computed_form.initial['name'], project.name) self.assertEqual(computed_form.initial['description'], project.description) self.assertEqual(computed_form.initial['groups'], group_id) self.assertEqual(computed_form.initial['areas'], area_id)