def test_process_not_status(self): """Test that the customer, tenant and user are created.""" mock_request = self.request middleware = IdentityHeaderMiddleware() middleware.process_request(mock_request) self.assertTrue(hasattr(mock_request, 'user')) self.assertEqual(mock_request.user.username, self.user_data['username']) tenant = Tenant.objects.get(schema_name=self.schema_name) self.assertIsNotNone(tenant)
def test_race_condition_user(self): """Test case where another request may create the user in a race condition.""" mock_request = self.request middleware = IdentityHeaderMiddleware() middleware.process_request(mock_request) self.assertTrue(hasattr(mock_request, 'user')) tenant = Tenant.objects.get(schema_name=self.schema_name) self.assertIsNotNone(tenant) user = User.objects.get(username=self.user_data['username']) self.assertIsNotNone(user) IdentityHeaderMiddleware._create_user( username=self.user_data['username'], # pylint: disable=W0212 tenant=tenant, request=mock_request)
def test_process_no_customer(self): """Test that the customer, tenant and user are not created.""" customer = self._create_customer_data() account_id = customer["account_id"] del customer["account_id"] request_context = self._create_request_context(customer, self.user_data, create_customer=False) mock_request = request_context["request"] mock_request.path = "/api/v1/providers/" middleware = IdentityHeaderMiddleware() middleware.process_request(mock_request) self.assertTrue(hasattr(mock_request, "user")) with self.assertRaises(Tenant.DoesNotExist): Tenant.objects.get(schema_name=self.schema_name)
def test_get_tenant_user_not_found(self): """Test that a 401 is returned.""" mock_user = Mock(username='******', system=False) mock_request = Mock(path='/api/v1/providers/', user=mock_user) middleware = IdentityHeaderMiddleware() result = middleware.process_request(mock_request) self.assertIsInstance(result, HttpResponseUnauthorizedRequest)
def test_get_tenant_with_no_user(self): """Test that a 401 is returned.""" request_context = self._create_request_context(self.customer, None, create_customer=False) mock_request = request_context["request"] mock_request.path = "/api/v1/providers/" middleware = IdentityHeaderMiddleware() result = middleware.process_request(mock_request) self.assertIsInstance(result, HttpResponseUnauthorizedRequest)
def test_process_cross_account_request(self): """Test that the process request functions correctly for cross account request.""" middleware = IdentityHeaderMiddleware() # User without redhat email will fail. request_context = self._create_request_context(self.customer, self.user_data, create_customer=False, cross_account=True, is_internal=True) mock_request = request_context["request"] mock_request.path = "/api/v1/providers/" response = middleware.process_request(mock_request) self.assertIsInstance(response, HttpResponseUnauthorizedRequest) # User with is_internal equal to False will fail. self.user_data["email"] = "*****@*****.**" request_context = self._create_request_context(self.customer, self.user_data, create_customer=False, cross_account=True) mock_request = request_context["request"] mock_request.path = "/api/v1/providers/" response = middleware.process_request(mock_request) self.assertIsInstance(response, HttpResponseUnauthorizedRequest) # Success pass if user is internal and with redhat email self.user_data["email"] = "*****@*****.**" request_context = self._create_request_context(self.customer, self.user_data, create_customer=False, cross_account=True, is_internal=True) mock_request = request_context["request"] mock_request.path = "/api/v1/providers/" response = middleware.process_request(mock_request) self.assertEqual(response, None)
def test_process_status(self): """Test that the request gets a user.""" mock_request = Mock(path='/api/v1/status/') middleware = IdentityHeaderMiddleware() middleware.process_request(mock_request) self.assertTrue(hasattr(mock_request, 'user'))
def test_get_tenant_with_no_user(self): """Test that a 401 is returned.""" mock_request = Mock(path='/api/v1/providers/', user=None) middleware = IdentityHeaderMiddleware() result = middleware.process_request(mock_request) self.assertIsInstance(result, HttpResponseUnauthorizedRequest)