diff --git a/zilencer/models.py b/zilencer/models.py index 313dfec132..90835656c4 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -14,3 +14,7 @@ class Deployment(models.Model): base_api_url = models.CharField(max_length=128) base_site_url = models.CharField(max_length=128) + + @property + def endpoints(self): + return {'base_api_url': self.base_api_url, 'base_site_url': self.base_site_url} diff --git a/zilencer/tests.py b/zilencer/tests.py new file mode 100644 index 0000000000..9c737303eb --- /dev/null +++ b/zilencer/tests.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import + +import ujson + +from django.test import TestCase + +class EndpointDiscoveryTest(TestCase): + def test_staging_user(self): + response = self.client.get("/api/v1/deployments/endpoints", {"email": "lfaraone@zulip.com"}) + data = ujson.loads(response.content) + self.assertEqual(data["result"]["base_site_url"], "https://staging.zulip.com/") + self.assertEqual(data["result"]["base_api_url"], "https://staging.zulip.com/api/") + + def test_prod_user(self): + response = self.client.get("/api/v1/deployments/endpoints", {"email": "lfaraone@mit.edu"}) + data = ujson.loads(response.content) + self.assertEqual(data["result"]["base_site_url"], "https://zulip.com/") + self.assertEqual(data["result"]["base_api_url"], "https://api.zulip.com/") + diff --git a/zilencer/urls/api.py b/zilencer/urls/api.py index f444a1cc4d..4f7e594317 100644 --- a/zilencer/urls/api.py +++ b/zilencer/urls/api.py @@ -3,4 +3,5 @@ from django.conf.urls import patterns, url, include urlpatterns = patterns('zilencer.views', url('^feedback$', 'rest_dispatch', {'POST': 'submit_feedback'}), + url('^endpoints$', 'lookup_endpoints_for_user'), ) diff --git a/zilencer/views.py b/zilencer/views.py index 5152948bd2..f2e6f3faf8 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -1,12 +1,14 @@ from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt, csrf_protect -from zerver.models import get_realm -from zerver.lib.actions import internal_send_message from zerver.decorator import has_request_variables, REQ, json_to_dict +from zerver.lib.actions import internal_send_message +from zerver.lib.response import json_success, json_error, json_response, json_method_not_allowed +from zerver.lib.rest import rest_dispatch as _rest_dispatch +from zerver.models import get_realm, get_user_profile_by_email, email_to_domain, \ + UserProfile, Realm from zilencer.models import Deployment -from zerver.lib.rest import rest_dispatch as _rest_dispatch rest_dispatch = csrf_exempt((lambda request, *args, **kwargs: _rest_dispatch(request, globals(), *args, **kwargs))) @@ -46,3 +48,15 @@ def submit_feedback(request, deployment, message=REQ(converter=json_to_dict)): internal_send_message("feedback@zulip.com", "stream", "support", subject, content) return HttpResponse(message['sender_email']) + +# Requests made to this endpoint are UNAUTHENTICATED +@csrf_exempt +@has_request_variables +def lookup_endpoints_for_user(request, email=REQ()): + try: + return json_response(get_user_profile_by_email(email).realm.deployment.endpoints) + except UserProfile.DoesNotExist: + try: + return json_response(get_realm(email_to_domain(email)).deployment.endpoints) + except AttributeError: + return json_error("Cannot determine endpoint for user.", status=404)