From fa02dfdff44bb9d8e5cbe6534f2342c4368e05bf Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sun, 29 Jan 2017 22:21:13 -0800 Subject: [PATCH] mobile: Add an endpoint for checking compatibility. --- tools/lint-all | 1 + zerver/tests/test_compatibility.py | 12 ++++++++++++ zerver/views/compatibility.py | 14 ++++++++++++++ zproject/urls.py | 4 ++++ 4 files changed, 31 insertions(+) create mode 100644 zerver/tests/test_compatibility.py create mode 100644 zerver/views/compatibility.py diff --git a/tools/lint-all b/tools/lint-all index 36a84837ec..156faf8cd5 100755 --- a/tools/lint-all +++ b/tools/lint-all @@ -368,6 +368,7 @@ def build_custom_checkers(by_lang): ('zerver/middleware.py', 'return json_error(exception.to_json_error_msg(), status=status_code)'), ('zerver/tornado/views.py', 'return json_error(result["message"])'), + ('zerver/views/compatibility.py', 'return json_error("Client is too old")'), ('zerver/views/invite.py', 'return json_error(data=error_data, msg=ret_error)'), ('zerver/views/streams.py', 'return json_error(property_conversion)'), diff --git a/zerver/tests/test_compatibility.py b/zerver/tests/test_compatibility.py new file mode 100644 index 0000000000..72e5a3f98c --- /dev/null +++ b/zerver/tests/test_compatibility.py @@ -0,0 +1,12 @@ +from __future__ import absolute_import +from __future__ import print_function + +from zerver.lib.test_classes import ZulipTestCase + +class CompatibilityTest(ZulipTestCase): + def test_compatibility(self): + # type: () -> None + result = self.client_get("/compatibility", HTTP_USER_AGENT='ZulipMobile/5.0') + self.assert_json_success(result) + result = self.client_get("/compatibility", HTTP_USER_AGENT='ZulipInvalid/5.0') + self.assert_json_error(result, "Client is too old") diff --git a/zerver/views/compatibility.py b/zerver/views/compatibility.py new file mode 100644 index 0000000000..30eed7bfda --- /dev/null +++ b/zerver/views/compatibility.py @@ -0,0 +1,14 @@ +from __future__ import absolute_import + +from django.http import HttpResponse, HttpRequest +from typing import Any, List, Dict, Optional, Text + +from zerver.lib.response import json_error, json_success +from zerver.lib.user_agent import parse_user_agent + +def check_compatibility(request): + # type: (HttpRequest) -> HttpResponse + user_agent = parse_user_agent(request.META["HTTP_USER_AGENT"]) + if user_agent['name'] == "ZulipInvalid": + return json_error("Client is too old") + return json_success() diff --git a/zproject/urls.py b/zproject/urls.py index 52bc6abce0..2148f55d6d 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -18,6 +18,7 @@ from django.contrib.auth.views import (login, password_reset, import zerver.tornado.views import zerver.views import zerver.views.auth +import zerver.views.compatibility import zerver.views.home import zerver.views.registration import zerver.views.zephyr @@ -351,6 +352,9 @@ urls += [ # backends the server allows, to display the proper UI and check for server existence url(r'^api/v1/get_auth_backends', zerver.views.auth.api_get_auth_backends, name='zerver.views.auth.api_get_auth_backends'), + # used by mobile apps to check if they are compatible with the server + url(r'^compatibility$', zerver.views.compatibility.check_compatibility), + # This json format view used by the mobile apps accepts a username # password/pair and returns an API key. url(r'^api/v1/fetch_api_key$', zerver.views.auth.api_fetch_api_key, name='zerver.views.auth.api_fetch_api_key'),