2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2020-04-01 22:11:26 +02:00
|
|
|
|
2021-06-04 10:19:50 +02:00
|
|
|
from zerver.lib.compatibility import find_mobile_os, is_outdated_desktop_app, version_lt
|
2017-01-30 07:21:13 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-03-25 02:00:28 +01:00
|
|
|
|
2018-12-04 23:22:05 +01:00
|
|
|
|
|
|
|
class VersionTest(ZulipTestCase):
|
2021-02-12 08:19:30 +01:00
|
|
|
data = [
|
|
|
|
case.split()
|
2021-02-12 08:20:45 +01:00
|
|
|
for case in """
|
2018-12-04 23:22:05 +01:00
|
|
|
1.2.3 < 1.2.4
|
|
|
|
1.2.3 = 1.2.3
|
|
|
|
1.4.1 > 1.2.3
|
|
|
|
1.002a = 1.2a
|
2018-12-05 00:00:56 +01:00
|
|
|
1.2 < 1.2.3
|
2018-12-04 23:22:05 +01:00
|
|
|
1.2.3 ? 1.2-dev
|
|
|
|
1.2-dev ? 1.2a
|
|
|
|
1.2a ? 1.2rc3
|
|
|
|
1.2rc3 ? 1.2
|
|
|
|
1.2 ? 1.2-g0f1e2d3c4
|
|
|
|
10.1 > 1.2
|
|
|
|
0.17.18 < 16.2.96
|
|
|
|
9.10.11 < 16.2.96
|
|
|
|
15.1.95 < 16.2.96
|
|
|
|
16.2.96 = 16.2.96
|
|
|
|
20.0.103 > 16.2.96
|
2021-02-12 08:20:45 +01:00
|
|
|
""".strip().split(
|
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
] + [
|
2021-02-12 08:20:45 +01:00
|
|
|
["", "?", "1"],
|
|
|
|
["", "?", "a"],
|
2018-12-05 00:06:46 +01:00
|
|
|
]
|
2018-12-04 23:22:05 +01:00
|
|
|
|
|
|
|
def test_version_lt(self) -> None:
|
|
|
|
for ver1, cmp, ver2 in self.data:
|
2021-02-12 08:20:45 +01:00
|
|
|
msg = f"expected {ver1} {cmp} {ver2}"
|
|
|
|
if cmp == "<":
|
2018-12-04 23:22:05 +01:00
|
|
|
self.assertTrue(version_lt(ver1, ver2), msg=msg)
|
|
|
|
self.assertFalse(version_lt(ver2, ver1), msg=msg)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif cmp == "=":
|
2018-12-04 23:22:05 +01:00
|
|
|
self.assertFalse(version_lt(ver1, ver2), msg=msg)
|
|
|
|
self.assertFalse(version_lt(ver2, ver1), msg=msg)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif cmp == ">":
|
2018-12-04 23:22:05 +01:00
|
|
|
self.assertFalse(version_lt(ver1, ver2), msg=msg)
|
|
|
|
self.assertTrue(version_lt(ver2, ver1), msg=msg)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif cmp == "?":
|
2018-12-04 23:22:05 +01:00
|
|
|
self.assertIsNone(version_lt(ver1, ver2), msg=msg)
|
|
|
|
self.assertIsNone(version_lt(ver2, ver1), msg=msg)
|
|
|
|
else:
|
|
|
|
assert False # nocoverage
|
2017-01-30 07:21:13 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
mobile_os_data = [
|
|
|
|
case.split(None, 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
for case in """
|
2018-12-05 00:46:52 +01:00
|
|
|
android ZulipMobile/1.2.3 (Android 4.5)
|
|
|
|
ios ZulipMobile/1.2.3 (iPhone OS 2.1)
|
|
|
|
ios ZulipMobile/1.2.3 (iOS 6)
|
|
|
|
None ZulipMobile/1.2.3 (Windows 8)
|
2021-02-12 08:20:45 +01:00
|
|
|
""".strip().split(
|
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
]
|
2018-12-05 00:46:52 +01:00
|
|
|
|
|
|
|
def test_find_mobile_os(self) -> None:
|
|
|
|
for expected_, user_agent in self.mobile_os_data:
|
2021-02-12 08:20:45 +01:00
|
|
|
expected = None if expected_ == "None" else expected_
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(find_mobile_os(user_agent), expected, msg=user_agent)
|
2018-12-05 00:46:52 +01:00
|
|
|
|
|
|
|
|
2017-01-30 07:21:13 +01:00
|
|
|
class CompatibilityTest(ZulipTestCase):
|
2021-02-12 08:19:30 +01:00
|
|
|
data = [
|
|
|
|
case.split(None, 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
for case in """
|
2018-12-05 01:07:58 +01:00
|
|
|
old ZulipInvalid/5.0
|
|
|
|
ok ZulipMobile/5.0
|
|
|
|
ok ZulipMobile/5.0 (iOS 11)
|
|
|
|
ok ZulipMobile/5.0 (Androidish 9)
|
|
|
|
old ZulipMobile/5.0 (Android 9)
|
|
|
|
old ZulipMobile/15.1.95 (Android 9)
|
|
|
|
old ZulipMobile/16.1.94 (Android 9)
|
|
|
|
ok ZulipMobile/16.2.96 (Android 9)
|
|
|
|
ok ZulipMobile/20.0.103 (Android 9)
|
|
|
|
|
|
|
|
ok ZulipMobile/0.7.1.1 (iOS 11.4)
|
|
|
|
old ZulipMobile/1.0.13 (Android 9)
|
|
|
|
ok ZulipMobile/17.1.98 (iOS 12.0)
|
|
|
|
ok ZulipMobile/19.2.102 (Android 6.0)
|
|
|
|
ok ZulipMobile/1 CFNetwork/974.2.1 Darwin/18.0.0
|
|
|
|
ok ZulipMobile/20.0.103 (Android 6.0.1)
|
|
|
|
ok ZulipMobile/20.0.103 (iOS 12.1)
|
2021-02-12 08:20:45 +01:00
|
|
|
""".strip().split(
|
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
if case
|
|
|
|
]
|
2018-12-05 00:17:02 +01:00
|
|
|
|
2018-12-10 22:42:28 +01:00
|
|
|
def test_compatibility_without_user_agent(self) -> None:
|
2020-02-25 02:53:12 +01:00
|
|
|
result = self.client_get("/compatibility", skip_user_agent=True)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "User-Agent header missing from request")
|
2018-12-10 22:42:28 +01:00
|
|
|
|
2018-12-05 01:07:58 +01:00
|
|
|
def test_compatibility(self) -> None:
|
|
|
|
for expected, user_agent in self.data:
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_get("/compatibility", HTTP_USER_AGENT=user_agent)
|
2021-02-12 08:20:45 +01:00
|
|
|
if expected == "ok":
|
2018-12-05 01:07:58 +01:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif expected == "old":
|
2018-12-05 01:07:58 +01:00
|
|
|
self.assert_json_error(result, "Client is too old")
|
|
|
|
else:
|
|
|
|
assert False # nocoverage
|
2020-02-28 09:55:29 +01:00
|
|
|
|
2021-06-04 10:19:50 +02:00
|
|
|
@mock.patch("zerver.lib.compatibility.DESKTOP_MINIMUM_VERSION", "5.0.0")
|
|
|
|
@mock.patch("zerver.lib.compatibility.DESKTOP_WARNING_VERSION", "5.2.0")
|
2020-02-28 09:55:29 +01:00
|
|
|
def test_insecure_desktop_app(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(is_outdated_desktop_app("ZulipDesktop/0.5.2 (Mac)"), (True, True, True))
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
is_outdated_desktop_app(
|
2021-02-12 08:20:45 +01:00
|
|
|
"ZulipElectron/2.3.82 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Zulip/2.3.82 Chrome/61.0.3163.100 Electron/2.0.9 Safari/537.36"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(True, True, True),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
is_outdated_desktop_app(
|
2021-02-12 08:20:45 +01:00
|
|
|
"ZulipElectron/4.0.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Zulip/4.0.3 Chrome/66.0.3359.181 Electron/3.1.10 Safari/537.36"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(True, True, False),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
is_outdated_desktop_app(
|
2021-02-12 08:20:45 +01:00
|
|
|
"ZulipElectron/4.0.3 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Zulip/4.0.3 Chrome/66.0.3359.181 Electron/3.1.10 Safari/537.36"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(True, True, False),
|
|
|
|
)
|
2020-04-01 22:11:26 +02:00
|
|
|
|
|
|
|
# Verify what happens if DESKTOP_MINIMUM_VERSION < v < DESKTOP_WARNING_VERSION
|
2021-06-04 10:19:50 +02:00
|
|
|
with mock.patch("zerver.lib.compatibility.DESKTOP_MINIMUM_VERSION", "4.0.3"):
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
is_outdated_desktop_app(
|
2021-02-12 08:20:45 +01:00
|
|
|
"ZulipElectron/4.0.3 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Zulip/4.0.3 Chrome/66.0.3359.181 Electron/3.1.10 Safari/537.36"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(True, False, False),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
is_outdated_desktop_app(
|
2021-02-12 08:20:45 +01:00
|
|
|
"ZulipElectron/5.2.0 Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Zulip/5.2.0 Chrome/80.0.3987.165 Electron/8.2.5 Safari/537.36"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(False, False, False),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
is_outdated_desktop_app(
|
2021-02-12 08:20:45 +01:00
|
|
|
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(False, False, False),
|
|
|
|
)
|
2020-02-28 15:54:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(is_outdated_desktop_app(""), (False, False, False))
|