def test_keystone_create_user_and_set_role(self, mock_role_get, mock_project_get, mock_add_user_role): """ Cadastra usuario e seta role para project """ mock_user_create = self.mock_keystone_conn.return_value.users.create fake_user = FakeResource('user_id', name='user_name') fake_role = FakeResource('role_id', name='role_name') fake_project = FakeResource('project_id', name='project_name') mock_user_create.return_value = fake_user mock_role_get.return_value = fake_role mock_project_get.return_value = fake_project keystone = Keystone(self.request, 'project_name') keystone.user_create('name', email='*****@*****.**', password='******', project_id='project_id', enabled=True, role_id='role_id') keystone.conn.users.create.assert_called_with('name', domain=None, email='*****@*****.**', enabled=True, password='******', project='project_id') mock_add_user_role.assert_called_with(fake_user, fake_project, fake_role)
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 setUp(self): self.view = views.DeleteProjectView.as_view() self.request = fake_request() self.request.method = 'POST' self.request.user.is_authenticated = lambda: True post = self.request.POST.copy() post.update({ 'user': '******', 'password': '******'}) self.request.POST = post patch('actionlogger.ActionLogger.log', Mock(return_value=None)).start() # # kwargs = { # 'remote_addr': request.environ.get('REMOTE_ADDR', ''), # 'auth_url': getattr(settings, 'KEYSTONE_URL'), # 'insecure': True, # 'tenant_name': self.tenant_name, # 'username': self.username, # 'password': self.password, # 'timeout': 3, # } fake_project = FakeResource(n='abcdefg', name='fake_project') fake_project.description = 'desc do fake' # Mocking Keystone class used on vault views mixin self.mock_vault_keystone = patch('vault.views.Keystone').start() self.mock_project_get = patch('identity.keystone.Keystone.project_get').start() self.mock_project_get.return_value = fake_project
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_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 self.view(self.request) msgs = [msg for msg in self.request._messages] self.assertGreater(len(msgs), 0) self.assertEqual(msgs[0].message, _('Error when updating user'))
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_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 setUp(self): self.request = fake_request() project_id = 'abcdefghiklmnopq' project_name = 'project_test' project_desc = 'project description' self.project = ProjectFactory(id=project_id, name=project_name, description=project_desc) self.mock_project_create = patch( 'identity.keystone.Keystone.project_create').start() self.mock_project_create.return_value = self.project self.mock_project_update = patch( 'identity.keystone.Keystone.project_update').start() self.mock_project_update.return_value = self.project fake_project = FakeResource(n='abcdefg', name='fake_project') fake_project.description = 'desc do fake' self.mock_project_get = patch( 'identity.keystone.Keystone.project_get').start() self.mock_project_get.return_value = fake_project self.mock_keystone_conn = patch( 'identity.keystone.Keystone._keystone_conn').start() self.group = GroupFactory(id=1) self.area = AreaFactory(id=1)
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_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_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' self.request.META.update({'HTTP_HOST': 'localhost'}) 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 setUp(self): self.request = fake_request() self.project = Project( '123', { u'id': str(uuid4()), u'name': 'project_test', u'description': 'project description', u'domain_id': 'default', u'enabled': True }) self.mock_project_create = patch( 'identity.keystone.Keystone.project_create').start() self.mock_project_create.return_value = self.project self.mock_project_update = patch( 'identity.keystone.Keystone.project_update').start() self.mock_project_update.return_value = self.project fake_project = FakeResource(n='abcdefg', name='fake_project') fake_project.description = 'desc do fake' self.mock_project_get = patch( 'identity.keystone.Keystone.project_get').start() self.mock_project_get.return_value = fake_project self.mock_keystone_conn = patch( 'identity.keystone.Keystone._create_keystone_connection').start() self.group = GroupFactory(id=1)
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.default_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 self.view(self.request) mock_user_update.assert_called_with(user, name='aaa', project=project.id, domain=None, enabled=True, password=None, email=None)
def test_get_user_roles_was_called(self, mock_get_user_roles): user = FakeResource(1) user.username = '******' self.mock_user_list.return_value = [user] mock_get_user_roles.return_value = [] post = self.request.POST.copy() post.update({'project': 1}) self.request.POST = post self.view(self.request) mock_get_user_roles.assert_called_with(user, 1)
def setUp(self): self.request = fake_request() self.user_id = 'abcdefghiklmnopq' self.user_name = 'user_name' self.project = Project( '321', { u'id': str(uuid4()), u'name': 'project_test_delete', u'description': 'project test delete description', u'enabled': True }) self.mock_keystone_conn = patch( 'identity.keystone.Keystone._create_keystone_connection').start() self.mock_project_get_by_name = patch( 'identity.keystone.Keystone.project_get_by_name').start() self.mock_project_get_by_name.return_value = self.project self.mock_find_user_with_u_prefix = patch( 'identity.keystone.Keystone.find_user_with_u_prefix').start() self.mock_find_user_with_u_prefix.return_value = FakeResource( self.user_id, name=self.user_name)
def test_vault_create_project_fail_to_save_group_project_on_db( self, mock_gp_save, mock_user_delete, mock_user_create, mock_project_delete, mock_encrypt_password): fake_user = FakeResource(n=self.project.id, name='u_{}'.format(self.project.name)) mock_user_create.return_value = fake_user # Excecao ao salvar no db mock_gp_save.side_effect = Exception mock_encrypt_password.return_value = b'123456' keystone = Keystone(self.request, project_name='project_name') expected = { 'status': False, 'reason': 'Unable to assign project to group' } computed = keystone.vault_project_create( self.project.name, 2, description=self.project.description) self.assertEqual(computed, expected) mock_project_delete.assert_called_with(self.project.id) mock_user_delete.assert_called_with(fake_user.id)
def test_get_user_roles_was_called(self, mock_get_user_roles): user = FakeResource(1) user.username = '******' patch('identity.keystone.Keystone.user_list', Mock(return_value=[user])).start() mock_get_user_roles.return_value = [] post = self.request.POST.copy() post.update({'project': 1}) self.request.POST = post response = self.view(self.request) mock_get_user_roles.assert_called_with(user, 1)
def test_user_delete_method_was_called(self, mock_user_delete): patch('identity.keystone.Keystone.user_get', Mock(return_value=FakeResource(1, 'user1'))).start() self.view(self.request, user_id=1) mock_user_delete.assert_called_with(1)
def test_get_user_roles_exception(self, mock_get_user_roles): mock_get_user_roles.side_effect = Exception() mock_get_user_roles.return_value = [] user = FakeResource(1) user.username = '******' self.mock_user_list.return_value = [user] post = self.request.POST.copy() post.update({'project': 1}) self.request.POST = post response = self.view(self.request) self.assertEqual(response.status_code, 500)
def setUp(self): patch('identity.keystone.Keystone._create_keystone_connection').start() patch('identity.views.actionlog.log').start() patch('identity.views.log').start() patch('identity.views.Keystone.project_get', return_value=FakeResource(1, 'fake_resource')).start() self.mock_get_context_data = patch( 'identity.views.UpdateProjectView.get_context_data').start() # Mock form validation. It's mocked as a invalid form. form = patch('identity.views.ProjectForm').start() self.form_is_valid = form.return_value.is_valid self.view = views.UpdateProjectView.as_view() self.request = fake_request(method='POST') post = self.request.POST.copy() post.update({ 'name': 'update_teste', 'description': 'update desc', 'group': 1, 'enabled': 'False' }) self.request.POST = post
def test_invove_vault_project_create_when_request_is_valid(self, mock): mock.return_value = { 'status': True, 'user': FakeResource(), 'project': FakeResource() } # Fake the validation of the form self.form.return_value.is_valid.return_value = True response = self.view(self.request) group_id = self.group.id mock.assert_called_with('project-teste', group_id, created_by='vault', description='desc', team_owner_id=group_id, first_team_id=group_id)
def test_user_delete_sucessfull_message(self, mock_user_delete): patch('identity.keystone.Keystone.user_get', Mock(return_value=FakeResource(1, 'user1'))).start() self.view(self.request, user_id=1) msgs = [msg for msg in self.request._messages] self.assertGreater(len(msgs), 0) self.assertEqual(msgs[0].message, _('Successfully deleted 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_user_list(self): patch('identity.keystone.Keystone.user_list', Mock(return_value=[FakeResource(1)])).start() self.request.user.token = FakeToken self.request.META.update({'HTTP_HOST': 'localhost'}) response = self.view(self.request) response.render() self.assertIn(b'<td>FakeResource1</td>', response.content)
def test_user_delete_view_exception(self, mock_user_delete): mock_user_delete.side_effect = Exception() patch('identity.keystone.Keystone.user_get', Mock(return_value=FakeResource(1, 'user1'))).start() self.view(self.request, user_id=1) msgs = [msg for msg in self.request._messages] self.assertGreater(len(msgs), 0) self.assertEqual(msgs[0].message, _('Error when deleting user'))
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_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_show_project_list(self): patch('identity.keystone.Keystone.project_list', Mock(return_value=[FakeResource(1)])).start() self.request.user.is_authenticated = lambda: True self.request.user.is_superuser = True response = self.view(self.request) response.render() self.assertIn('<td>FakeResource1</td>', response.content)
def setUp(self): self.view = CreateUserView.as_view() self.request = fake_request() self.request.META.update({ 'SERVER_NAME': 'globo.com', 'SERVER_PORT': '80' }) self.request.user.is_superuser = True self.request.user.is_authenticated = lambda: True self.request.user.token = FakeToken patch('actionlogger.ActionLogger.log', Mock(return_value=None)).start() patch('identity.keystone.Keystone._keystone_conn', Mock(return_value=None)).start() patch('identity.keystone.Keystone.project_list', Mock(return_value=[FakeResource(1, 'project1')])).start() patch('identity.keystone.Keystone.role_list', Mock(return_value=[FakeResource(1, 'role1')])).start()
def setUp(self): self.view = CreateUserView.as_view() self.request = fake_request() self.request.META.update({ 'SERVER_NAME': 'globo.com', 'SERVER_PORT': '80' }) self.request.user.token = FakeToken patch('actionlogger.actionlogger.ActionLogger.log', Mock(return_value=None)).start() patch('identity.keystone.Keystone._create_keystone_connection').start() patch('identity.keystone.Keystone.project_list', Mock(return_value=[FakeResource(1, 'project1')])).start() patch('identity.keystone.Keystone.role_list', Mock(return_value=[FakeResource(1, 'role1')])).start() # does not connect to the keystone client patch('keystoneclient.v3.client.Client').start()
def test_get_request_invoke_render_with_form_object(self): """ It tests if the view render method is invoked passing the proper form object """ mock_render = patch( 'identity.views.DeleteProjectView.get_context_data').start() fake_resource = FakeResource() self.form.return_value = fake_resource _ = self.view(self.request) computed_form = mock_render.call_args[1].get('form') self.assertEqual(fake_resource, computed_form)
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)
def setUp(self): self.request = fake_request() project_id = 'abcdefghiklmnopq' project_name = 'project_test' project_desc = 'project description' self.project = ProjectFactory(id=project_id, name=project_name, description=project_desc) self.mock_project_create = patch('identity.keystone.Keystone.project_create').start() self.mock_project_create.return_value = self.project self.mock_project_update = patch('identity.keystone.Keystone.project_update').start() self.mock_project_update.return_value = self.project fake_project = FakeResource(n='abcdefg', name='fake_project') fake_project.description = 'desc do fake' self.mock_project_get = patch('identity.keystone.Keystone.project_get').start() self.mock_project_get.return_value = fake_project self.mock_keystone_conn = patch('identity.keystone.Keystone._keystone_conn').start() self.group = GroupFactory(id=1) self.area = AreaFactory(id=1)
def test_vault_create_project(self, mock_gp_create, mock_key_pass, mock_key_user, mock_encrypt_password): mock_key_user.return_value = FakeResource(n=self.project.id, name='u_{}'.format( self.project.name)) mock_key_pass.return_value = 'password' mock_encrypt_password.return_value = b'123456' keystone = Keystone(self.request, project_name='project_name') expected = { 'status': True, 'project': self.mock_project_create.return_value, 'user': mock_key_user.return_value, 'password': mock_key_pass.return_value, } computed = keystone.vault_project_create( self.project.name, self.group.id, description=self.project.description) # Project creation self.mock_project_create.assert_called_with( self.project.name, description=self.project.description, enabled=True) # User creation mock_key_user.assert_called_with( name='u_vault_{}'.format(self.project.name), email='', password='******', enabled=True, domain='default', project_id=self.project.id, role_id='e03a556b664d4a41aaf2c5b4518f33ae') mock_gp_create.assert_called_with(group_id=self.group.id, project_id=self.project.id, owner=1) self.assertEqual(computed, expected)
def test_vault_create_project_forbidden_on_project_create( self, mock_key_user): self.mock_project_create.side_effect = exceptions.Forbidden mock_key_user.return_value = FakeResource(n=self.project.id, name='u_{}'.format( self.project.name)) keystone = Keystone(self.request, project_name='project_name') expected = {'status': False, 'reason': 'Superuser required.'} computed = keystone.vault_project_create( self.project.name, 1, description=self.project.description) self.assertEqual(computed, expected) # Criacao de user nao pode ocorrer quando hover falha na criacao do project self.assertFalse(mock_key_user.called)