def test_process_request_user_access_no_permissions(self, get_access_mock): """Test PermissionDenied is not raised for user-access calls""" mock_access = {} username = "******" get_access_mock.return_value = mock_access user_data = self._create_user_data() customer = self._create_customer_data() request_context = self._create_request_context(customer, user_data, create_customer=True, create_tenant=True, is_admin=False) mock_request = request_context["request"] mock_request.path = "/api/v1/user-access/" mock_request.META["QUERY_STRING"] = "" mock_user = Mock(username=username, admin=False, access=None) mock_request = Mock(path="/api/v1/user-access/", user=mock_user) IdentityHeaderMiddleware.create_user( username=username, email=self.user_data["email"], customer=Customer.objects.get( account_id=customer.get("account_id")), request=mock_request, ) middleware = KokuTenantMiddleware() with patch.object(BaseTenantMiddleware, "process_request") as mock_process_request: _ = middleware.process_request(mock_request) mock_process_request.assert_called()
def test_get_tenant_user_not_found(self): """Test that a 401 is returned.""" mock_user = Mock(username="******") mock_request = Mock(path="/api/v1/providers/", user=mock_user) middleware = KokuTenantMiddleware() result = middleware.process_request(mock_request) self.assertIsInstance(result, HttpResponseUnauthorizedRequest)
def test_get_default_tenant(self): """Test that the public tenant is returned.""" mock_request = Mock() del mock_request.user middleware = KokuTenantMiddleware() result = middleware.get_tenant(Tenant, 'localhost', mock_request) self.assertEqual(result.schema_name, 'public')
def test_get_tenant_with_user(self, mock_model): """Test that the customer tenant is returned.""" mock_request = Mock() mock_group = Mock() mock_customer = Mock() mock_group.id = ''.join( [random.choice(string.digits) for _ in range(6)]) mock_user_groups = mock_request.user.return_value.groups.return_value mock_user_groups.first.return_value = mock_group mock_customer.schema_name = self.schema_name mock_model.objects.get.return_value = mock_customer middleware = KokuTenantMiddleware() result = middleware.get_tenant(Tenant, 'localhost', mock_request) self.assertEqual(result.schema_name, self.schema_name)
def test_tenant_caching(self): """Test that the tenant cache is successfully storing and expiring.""" mock_request = self.request_context["request"] middleware = KokuTenantMiddleware() middleware.get_tenant(Tenant, "localhost", mock_request) # Add one item to the cache self.assertEquals(KokuTenantMiddleware.tenant_cache.currsize, 1) middleware.get_tenant(Tenant, "localhost", mock_request) # Call the same tenant self.assertEquals(KokuTenantMiddleware.tenant_cache.currsize, 1) # Size should remain the same self.assertEquals(KokuTenantMiddleware.tenant_cache.currsize, 1) time.sleep(4) # Wait the time greater than the ttl self.assertEqual(KokuTenantMiddleware.tenant_cache.currsize, 0)
def test_get_tenant_with_no_user(self): """Test that a 401 is returned.""" mock_request = Mock(path="/api/v1/providers/", user=None) middleware = KokuTenantMiddleware() result = middleware.process_request(mock_request) self.assertIsInstance(result, HttpResponseUnauthorizedRequest)
def test_get_tenant_with_user(self): """Test that the customer tenant is returned.""" mock_request = self.request_context["request"] middleware = KokuTenantMiddleware() result = middleware.get_tenant(Tenant, "localhost", mock_request) self.assertEqual(result.schema_name, self.schema_name)