def test_principal_cleanup_princpal_exists(self, mock_request): """Test that we can run a principal clean up on a tenant with an existing principal.""" with tenant_context(self.tenant): self.principal = Principal(username='******') self.principal.save() try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg='clean_tenant_principals encountered an exception') with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 1)
def test_principal_cleanup_princpal_error(self, mock_request): """Test that we can handle a principal clean up with an unexpected error from proxy.""" with tenant_context(self.tenant): self.principal = Principal(username='******') self.principal.save() try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg='clean_tenant_principals encountered an exception') with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 1)
def test_principal_cleanup_principal_not_in_group(self, mock_request): """Test that we can run a principal clean up on a tenant with a principal not in a group.""" with tenant_context(self.tenant): self.principal = Principal(username="******") self.principal.save() try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg="clean_tenant_principals encountered an exception") with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 0)
def remove_principals(self, group, principals): """Process list of principals and remove them from the group.""" for username in principals: try: principal = Principal.objects.get(username=username) except Principal.DoesNotExist: principal = Principal(username=username) principal.save() group.principals.remove(principal) group.save() return group
def add_principals(self, group, principals): """Process list of principals and add them to the group.""" for item in principals: username = item['username'] try: principal = Principal.objects.get(username=username) except Principal.DoesNotExist: principal = Principal(username=username) principal.save() group.principals.add(principal) group.save() return group
class PrincipalCleanerTests(IdentityRequest): """Test the principal cleaner functions.""" def setUp(self): """Set up the principal cleaner tests.""" super().setUp() with tenant_context(self.tenant): self.group = Group(name='groupA') self.group.save() def test_principal_cleanup_none(self): """Test that we can run a principal clean up on a tenant with no principals.""" try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg='clean_tenant_principals encountered an exception') with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 0) @patch('management.principal.proxy.PrincipalProxy._request_principals', return_value={ 'status_code': status.HTTP_200_OK, 'data': [] }) def test_principal_cleanup_princpal_in_group(self, mock_request): """Test that we can run a principal clean up on a tenant with a principal in a group.""" with tenant_context(self.tenant): self.principal = Principal(username='******') self.principal.save() self.group.principals.add(self.principal) self.group.save() try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg='clean_tenant_principals encountered an exception') with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 0) @patch('management.principal.proxy.PrincipalProxy._request_principals', return_value={ 'status_code': status.HTTP_200_OK, 'data': [] }) def test_principal_cleanup_princpal_not_in_group(self, mock_request): """Test that we can run a principal clean up on a tenant with a principal not in a group.""" with tenant_context(self.tenant): self.principal = Principal(username='******') self.principal.save() try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg='clean_tenant_principals encountered an exception') with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 0) @patch('management.principal.proxy.PrincipalProxy._request_principals', return_value={ 'status_code': status.HTTP_200_OK, 'data': [{ 'username': '******' }] }) def test_principal_cleanup_princpal_exists(self, mock_request): """Test that we can run a principal clean up on a tenant with an existing principal.""" with tenant_context(self.tenant): self.principal = Principal(username='******') self.principal.save() try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg='clean_tenant_principals encountered an exception') with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 1) @patch('management.principal.proxy.PrincipalProxy._request_principals', return_value={'status_code': status.HTTP_504_GATEWAY_TIMEOUT}) def test_principal_cleanup_princpal_error(self, mock_request): """Test that we can handle a principal clean up with an unexpected error from proxy.""" with tenant_context(self.tenant): self.principal = Principal(username='******') self.principal.save() try: clean_tenant_principals(self.tenant) except Exception: self.fail(msg='clean_tenant_principals encountered an exception') with tenant_context(self.tenant): self.assertEqual(Principal.objects.count(), 1)