From 5475c5cedb7cce5e3736e0a9576e5c3ee15b5ea2 Mon Sep 17 00:00:00 2001 From: invinciblycool Date: Sat, 16 Sep 2017 11:54:17 +0530 Subject: [PATCH] tests: Replace test_user_agent_parsing with a normal test. Previously, this was its own separate test script; now it's a normal part of the test suite. Tweaked by tabbott to use a proper test method. Fixes #6327. --- tools/test_user_agent_parsing.py | 35 ------------------- {tools => zerver/fixtures}/user_agents_unique | 0 zerver/lib/user_agent.py | 4 +-- zerver/tests/test_decorators.py | 27 ++++++++++++++ 4 files changed, 29 insertions(+), 37 deletions(-) delete mode 100755 tools/test_user_agent_parsing.py rename {tools => zerver/fixtures}/user_agents_unique (100%) diff --git a/tools/test_user_agent_parsing.py b/tools/test_user_agent_parsing.py deleted file mode 100755 index d7042974ab..0000000000 --- a/tools/test_user_agent_parsing.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 -from __future__ import print_function -import re -from collections import defaultdict -import os -import sys -from typing import Dict - -sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) - -from zerver.lib.user_agent import parse_user_agent - -user_agents_parsed = defaultdict(int) # type: Dict[str, int] -user_agents_path = os.path.join(os.path.dirname(__file__), "user_agents_unique") -parse_errors = 0 -for line in open(user_agents_path).readlines(): - line = line.strip() - match = re.match('^(?P[0-9]+) "(?P.*)"$', line) - if match is None: - print(line) - continue - groupdict = match.groupdict() - count = groupdict["count"] - user_agent = groupdict["user_agent"] - ret = parse_user_agent(user_agent) - if ret is None: - print("parse error", line) - parse_errors += 1 - continue - user_agents_parsed[ret["name"]] += int(count) - -for key in user_agents_parsed: - print(" ", key, user_agents_parsed[key]) - -print("%s parse errors!" % (parse_errors,)) diff --git a/tools/user_agents_unique b/zerver/fixtures/user_agents_unique similarity index 100% rename from tools/user_agents_unique rename to zerver/fixtures/user_agents_unique diff --git a/zerver/lib/user_agent.py b/zerver/lib/user_agent.py index a0c7b3fc35..a82ed3743a 100644 --- a/zerver/lib/user_agent.py +++ b/zerver/lib/user_agent.py @@ -2,8 +2,8 @@ import re from typing import Optional, Dict # Warning: If you change this parsing, please test using -# tools/test_user_agent_parsing.py -# And extend tools/user_agents_unique with any new test cases +# zerver/tests/test_decorators.py +# And extend zerver/fixtures/user_agents_unique with any new test cases def parse_user_agent(user_agent): # type: (str) -> Optional[Dict[str, str]] match = re.match("^(?P[^/ ]*[^0-9/(]*)(/(?P[^/ ]*))?([ /].*)?$", user_agent) diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py index 7bf3a6eb60..b7450984ca 100644 --- a/zerver/tests/test_decorators.py +++ b/zerver/tests/test_decorators.py @@ -1,5 +1,8 @@ # -*- coding: utf-8 -*- import mock +import re +import os +from collections import defaultdict from typing import Any, Dict, Iterable, List, Optional, Text, Tuple from django.test import TestCase @@ -19,6 +22,7 @@ from zerver.lib.test_classes import ( WebhookTestCase, ) from zerver.lib.response import json_response +from zerver.lib.user_agent import parse_user_agent from zerver.lib.request import \ REQ, has_request_variables, RequestVariableMissingError, \ RequestVariableConversionError, JsonableError @@ -1259,3 +1263,26 @@ class RestAPITest(ZulipTestCase): HTTP_ACCEPT='text/html') self.assertEqual(result.status_code, 302) self.assertTrue(result["Location"].endswith("/login/?next=/json/users")) + +class TestUserAgentParsing(ZulipTestCase): + def test_user_agent_parsing(self): + # type: () -> None + """Test for our user agent parsing logic, using a large data set.""" + user_agents_parsed = defaultdict(int) # type: Dict[str, int] + user_agents_path = os.path.join(settings.DEPLOY_ROOT, "zerver/fixtures/user_agents_unique") + parse_errors = [] + for line in open(user_agents_path).readlines(): + line = line.strip() + match = re.match('^(?P[0-9]+) "(?P.*)"$', line) + self.assertIsNotNone(match) + groupdict = match.groupdict() + count = groupdict["count"] + user_agent = groupdict["user_agent"] + ret = parse_user_agent(user_agent) + self.assertIsNotNone(ret) + if ret is None: # nocoverage + parse_errors.append(line) + continue + user_agents_parsed[ret["name"]] += int(count) + + self.assertEqual(len(parse_errors), 0)