Skip to content

Commit

Permalink
Add test for OrgCRUDL.Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnewcomer committed Jan 24, 2025
1 parent 09ea8fb commit f39209a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
24 changes: 24 additions & 0 deletions temba/orgs/views/tests/test_orgcrudl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,3 +1293,27 @@ def test_prometheus(self):

self.org.refresh_from_db()
self.assertIsNone(self.org.prometheus_token)

def test_switch(self):

self.login(self.admin)

# hitting switch for an org we don't have access to routes to choose
response = self.client.get(f"{reverse("orgs.org_switch")}?other_org={self.org2.id}&next=/msg")
self.assertRedirect(response, reverse("orgs.org_choose"))

# can't post to it either
response = self.client.post(reverse("orgs.org_switch"), {"other_org": self.org2.id, "next": "/msg"})
self.assertRedirect(response, reverse("orgs.org_choose"))

# now put us in that org and we should see the switch option
self.org2.add_user(self.admin, OrgRole.ADMINISTRATOR)
response = self.client.get(f"{reverse("orgs.org_switch")}?other_org={self.org2.id}&next=/msg")
self.assertContains(
response, f"The page you are requesting belongs to one of your other workspaces, <b>{self.org2.name}</b>"
)

# now switch to it
response = self.client.post(reverse("orgs.org_switch"), {"other_org": self.org2.id, "next": "/msg"})
self.assertRedirect(response, "/msg")
self.assertEqual(self.org2.id, self.client.session["org_id"])
11 changes: 10 additions & 1 deletion temba/orgs/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,16 @@ class Meta:
fields = ("other_org", "next")
title = _("Switch Workspaces")

def pre_process(self, request, *args, **kwargs):
# make sure the other_org is valid
other_org_id = self.request.GET.get("other_org", self.request.POST.get("other_org"))
if other_org_id:
# make sure we have access to that org
if not User.get_orgs_for_request(self.request).filter(id=other_org_id).exists():
return HttpResponseRedirect(reverse("orgs.org_choose"))

return super().pre_process(request, *args, **kwargs)

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["request"] = self.request
Expand All @@ -1724,7 +1734,6 @@ def derive_initial(self):

# valid form means we set our org and redirect to next
def form_valid(self, form):
print("form_valid")
switch_to_org(self.request, form.cleaned_data["other_org"])
success_url = form.cleaned_data["next"] or reverse("orgs.org_start")
return HttpResponseRedirect(success_url)
Expand Down

0 comments on commit f39209a

Please sign in to comment.