From 77fad7a16ee7917fc71fe3a7897a36cdacbdb091 Mon Sep 17 00:00:00 2001 From: Darren Worrall Date: Fri, 2 Oct 2015 12:23:26 +0100 Subject: [PATCH] Add an api endpoint to fetch GOOGLE_CLIENT_ID Further to #102, this provides an endpoint suitable for mobile apps to consume the GOOGLE_CLIENT_ID if configured. --- zerver/test_signup.py | 19 +++++++++++++++++++ zerver/views/__init__.py | 6 ++++++ zproject/urls.py | 3 +++ 3 files changed, 28 insertions(+) diff --git a/zerver/test_signup.py b/zerver/test_signup.py index 8e06dc2087..5dc1070f55 100644 --- a/zerver/test_signup.py +++ b/zerver/test_signup.py @@ -83,6 +83,25 @@ class PublicURLTest(TestCase): for status_code, url_set in post_urls.iteritems(): self.fetch("post", url_set, status_code) + def test_get_gcid_when_not_configured(self): + with self.settings(GOOGLE_CLIENT_ID=None): + resp = self.client.get("/api/v1/fetch_google_client_id") + self.assertEquals(400, resp.status_code, + msg="Expected 400, received %d for GET /api/v1/fetch_google_client_id" % resp.status_code, + ) + data = ujson.loads(resp.content) + self.assertEqual('error', data['result']) + + def test_get_gcid_when_configured(self): + with self.settings(GOOGLE_CLIENT_ID="ABCD"): + resp = self.client.get("/api/v1/fetch_google_client_id") + self.assertEquals(200, resp.status_code, + msg="Expected 200, received %d for GET /api/v1/fetch_google_client_id" % resp.status_code, + ) + data = ujson.loads(resp.content) + self.assertEqual('success', data['result']) + self.assertEqual('ABCD', data['google_client_id']) + class LoginTest(AuthedTestCase): """ Logging in, registration, and logging out. diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 4428f2e7b7..5ad06a2a3f 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -1881,6 +1881,12 @@ def json_fetch_api_key(request, user_profile, password=REQ(default='')): return json_error("Your username or password is incorrect.") return json_success({"api_key": user_profile.api_key}) +@csrf_exempt +def api_fetch_google_client_id(request): + if not settings.GOOGLE_CLIENT_ID: + return json_error("GOOGLE_CLIENT_ID is not configured", status=400) + return json_success({"google_client_id": settings.GOOGLE_CLIENT_ID}) + def get_status_list(requesting_user_profile): return {'presences': get_status_dict(requesting_user_profile), 'server_timestamp': time.time()} diff --git a/zproject/urls.py b/zproject/urls.py index 434017ef19..eb3fa1889d 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -153,6 +153,9 @@ urlpatterns += patterns('zerver.views', # password/pair and returns an API key. url(r'^api/v1/fetch_api_key$', 'api_fetch_api_key'), + # Used to present the GOOGLE_CLIENT_ID to mobile apps + url(r'^api/v1/fetch_google_client_id$', 'api_fetch_google_client_id'), + # These are integration-specific web hook callbacks url(r'^api/v1/external/beanstalk$' , 'webhooks.api_beanstalk_webhook'), url(r'^api/v1/external/github$', 'webhooks.api_github_landing'),