|
@@ -1,3 +1,5 @@
|
|
|
+from urllib.parse import urlparse
|
|
|
+
|
|
|
from django.conf import settings
|
|
|
from django.conf.urls import url
|
|
|
from django.test import RequestFactory, override_settings
|
|
@@ -22,6 +24,30 @@ class CustomerDomainMiddlewareTest(TestCase):
|
|
|
assert request.session == {"activeorg": "test"}
|
|
|
assert response == request
|
|
|
|
|
|
+ def test_recycles_last_active_org(self):
|
|
|
+ self.create_organization(name="test")
|
|
|
+
|
|
|
+ request = RequestFactory().get("/organizations/test/issues/")
|
|
|
+ request.subdomain = "does-not-exist"
|
|
|
+ request.session = {"activeorg": "test"}
|
|
|
+ response = CustomerDomainMiddleware(lambda request: request)(request)
|
|
|
+
|
|
|
+ assert request.session == {"activeorg": "test"}
|
|
|
+ assert response.status_code == 302
|
|
|
+ assert response["Location"] == "http://test.testserver/organizations/test/issues/"
|
|
|
+
|
|
|
+ def test_recycles_last_active_org_path_mismatch(self):
|
|
|
+ self.create_organization(name="test")
|
|
|
+
|
|
|
+ request = RequestFactory().get("/organizations/albertos-apples/issues/")
|
|
|
+ request.subdomain = "does-not-exist"
|
|
|
+ request.session = {"activeorg": "test"}
|
|
|
+ response = CustomerDomainMiddleware(lambda request: request)(request)
|
|
|
+
|
|
|
+ assert request.session == {"activeorg": "test"}
|
|
|
+ assert response.status_code == 302
|
|
|
+ assert response["Location"] == "http://test.testserver/organizations/test/issues/"
|
|
|
+
|
|
|
def test_removes_active_organization(self):
|
|
|
request = RequestFactory().get("/")
|
|
|
request.subdomain = "does-not-exist"
|
|
@@ -216,7 +242,7 @@ class End2EndTest(APITestCase):
|
|
|
assert "activeorg" in self.client.session
|
|
|
assert self.client.session["activeorg"] == "albertos-apples"
|
|
|
|
|
|
- # Redirect response
|
|
|
+ # Redirect response for org slug path mismatch
|
|
|
response = self.client.get(
|
|
|
reverse("org-events-endpoint", kwargs={"organization_slug": "some-org"}),
|
|
|
HTTP_HOST="albertos-apples.testserver",
|
|
@@ -232,6 +258,30 @@ class End2EndTest(APITestCase):
|
|
|
assert "activeorg" in self.client.session
|
|
|
assert self.client.session["activeorg"] == "albertos-apples"
|
|
|
|
|
|
+ # Redirect response for subdomain and path mismatch
|
|
|
+ response = self.client.get(
|
|
|
+ reverse("org-events-endpoint", kwargs={"organization_slug": "some-org"}),
|
|
|
+ HTTP_HOST="does-not-exist.testserver",
|
|
|
+ )
|
|
|
+ assert response.status_code == 302
|
|
|
+ assert (
|
|
|
+ response["Location"] == "http://albertos-apples.testserver/api/0/albertos-apples/"
|
|
|
+ )
|
|
|
+
|
|
|
+ parsed = urlparse(response["Location"])
|
|
|
+ response = self.client.get(
|
|
|
+ parsed.path,
|
|
|
+ HTTP_HOST=parsed.netloc,
|
|
|
+ )
|
|
|
+ assert response.status_code == 200
|
|
|
+ assert response.data == {
|
|
|
+ "organization_slug": "albertos-apples",
|
|
|
+ "subdomain": "albertos-apples",
|
|
|
+ "activeorg": "albertos-apples",
|
|
|
+ }
|
|
|
+ assert "activeorg" in self.client.session
|
|
|
+ assert self.client.session["activeorg"] == "albertos-apples"
|
|
|
+
|
|
|
def test_without_middleware(self):
|
|
|
self.create_organization(name="albertos-apples")
|
|
|
|