Expose an endpoint to identify the API/webclient bases for a particular user.

We fall back to guessing based on the realm if the user doesn't have a
profile in our system

(imported from commit 833885168c451074c885b4422d62986855a215f7)
This commit is contained in:
Luke Faraone 2013-10-23 18:27:52 -04:00
parent a1b44986e0
commit 2d787e952b
4 changed files with 42 additions and 3 deletions

View File

@ -14,3 +14,7 @@ class Deployment(models.Model):
base_api_url = models.CharField(max_length=128) base_api_url = models.CharField(max_length=128)
base_site_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}

20
zilencer/tests.py Normal file
View File

@ -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/")

View File

@ -3,4 +3,5 @@ from django.conf.urls import patterns, url, include
urlpatterns = patterns('zilencer.views', urlpatterns = patterns('zilencer.views',
url('^feedback$', 'rest_dispatch', url('^feedback$', 'rest_dispatch',
{'POST': 'submit_feedback'}), {'POST': 'submit_feedback'}),
url('^endpoints$', 'lookup_endpoints_for_user'),
) )

View File

@ -1,12 +1,14 @@
from django.http import HttpResponse from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt, csrf_protect 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.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 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))) 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) internal_send_message("feedback@zulip.com", "stream", "support", subject, content)
return HttpResponse(message['sender_email']) 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)