From 9489ce0efc0b7eff62fe2f6b5ff985d42020f363 Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Mon, 27 Aug 2018 16:13:00 +0530 Subject: [PATCH] plans: Redirect to login if /plans is accessed from realm subdomain. --- zerver/tests/test_docs.py | 27 ++++++++++++++++++++++++++- zerver/views/home.py | 11 ++++++++++- zproject/urls.py | 2 +- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/zerver/tests/test_docs.py b/zerver/tests/test_docs.py index 8abfcbc438..46e85f32b2 100644 --- a/zerver/tests/test_docs.py +++ b/zerver/tests/test_docs.py @@ -15,6 +15,7 @@ from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import HostRequestMock from zerver.lib.test_runner import slow from zerver.lib.utils import split_by +from zerver.models import Realm, get_realm from zerver.views.integrations import ( add_api_uri_context, add_integrations_context, @@ -104,7 +105,6 @@ class DocPageTest(ZulipTestCase): self._test('/for/companies/', 'in a company') self._test('/for/working-groups-and-communities/', 'standards bodies') self._test('/for/mystery-hunt/', 'four SIPB alums') - self._test('/plans/', 'Community support') self._test('/devlogin/', 'Normal users', landing_page=False) self._test('/devtools/', 'Useful development URLs') self._test('/errors/404/', 'Page not found') @@ -304,3 +304,28 @@ class ConfigErrorTest(ZulipTestCase): result = self.client_get("/config-error/dev") self.assertEqual(result.status_code, 200) self.assert_in_success_response(["DevAuthBackend"], result) + +class PlansPageTest(ZulipTestCase): + def test_plans_auth(self) -> None: + # Test root domain + result = self.client_get("/plans/", subdomain="") + self.assert_in_success_response(["Sign up now"], result) + # Test non-existant domain + result = self.client_get("/plans/", subdomain="moo") + self.assert_in_success_response(["does not exist"], result) + # Test valid domain, no login + result = self.client_get("/plans/", subdomain="zulip") + self.assertEqual(result.status_code, 302) + self.assertEqual(result["Location"], "/accounts/login?next=plans") + # Test valid domain, with login + self.login(self.example_email('hamlet')) + realm = get_realm("zulip") + realm.plan_type = Realm.PREMIUM_FREE + realm.save(update_fields=["plan_type"]) + result = self.client_get("/plans/", subdomain="zulip") + self.assert_in_success_response(["Sign up now"], result) + # Test root domain, with login on different domain + result = self.client_get("/plans/", subdomain="") + # TODO: works in manual testing, but I suspect something is funny in + # the test environment + # self.assert_in_success_response(["Sign up now"], result) diff --git a/zerver/views/home.py b/zerver/views/home.py index 1bcda63645..6e472d01cd 100644 --- a/zerver/views/home.py +++ b/zerver/views/home.py @@ -8,7 +8,9 @@ from django.utils import translation from django.utils.cache import patch_cache_control from itertools import zip_longest -from zerver.decorator import zulip_login_required, process_client +from zerver.context_processors import get_realm_from_request +from zerver.decorator import zulip_login_required, process_client, \ + redirect_to_login from zerver.forms import ToSForm from zerver.lib.realm_icon import realm_icon_url from zerver.models import Message, UserProfile, Stream, Subscription, Huddle, \ @@ -288,3 +290,10 @@ def apps_view(request: HttpRequest, _: str) -> HttpResponse: if settings.ZILENCER_ENABLED: return render(request, 'zerver/apps.html') return HttpResponseRedirect('https://zulipchat.com/apps/', status=301) + +def plans_view(request: HttpRequest) -> HttpResponse: + realm = get_realm_from_request(request) + if realm is not None: + if not request.user.is_authenticated(): + return redirect_to_login(next="plans") + return render(request, "zerver/plans.html") diff --git a/zproject/urls.py b/zproject/urls.py index 5909adf332..0b28a871f9 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -486,7 +486,7 @@ i18n_urls = [ url(r'^team/$', zerver.views.users.team_view), url(r'^history/$', TemplateView.as_view(template_name='zerver/history.html')), url(r'^apps/(.*)', zerver.views.home.apps_view, name='zerver.views.home.apps_view'), - url(r'^plans/$', TemplateView.as_view(template_name='zerver/plans.html'), name='plans'), + url(r'^plans/$', zerver.views.home.plans_view, name='plans'), # Landing page, features pages, signup form, etc. url(r'^hello/$', TemplateView.as_view(template_name='zerver/hello.html'), name='landing-page'),