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.
This commit is contained in:
invinciblycool 2017-09-16 11:54:17 +05:30 committed by Tim Abbott
parent e8820f4456
commit 5475c5cedb
4 changed files with 29 additions and 37 deletions

View File

@ -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<count>[0-9]+) "(?P<user_agent>.*)"$', 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,))

View File

@ -2,8 +2,8 @@ import re
from typing import Optional, Dict from typing import Optional, Dict
# Warning: If you change this parsing, please test using # Warning: If you change this parsing, please test using
# tools/test_user_agent_parsing.py # zerver/tests/test_decorators.py
# And extend tools/user_agents_unique with any new test cases # And extend zerver/fixtures/user_agents_unique with any new test cases
def parse_user_agent(user_agent): def parse_user_agent(user_agent):
# type: (str) -> Optional[Dict[str, str]] # type: (str) -> Optional[Dict[str, str]]
match = re.match("^(?P<name>[^/ ]*[^0-9/(]*)(/(?P<version>[^/ ]*))?([ /].*)?$", user_agent) match = re.match("^(?P<name>[^/ ]*[^0-9/(]*)(/(?P<version>[^/ ]*))?([ /].*)?$", user_agent)

View File

@ -1,5 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import mock import mock
import re
import os
from collections import defaultdict
from typing import Any, Dict, Iterable, List, Optional, Text, Tuple from typing import Any, Dict, Iterable, List, Optional, Text, Tuple
from django.test import TestCase from django.test import TestCase
@ -19,6 +22,7 @@ from zerver.lib.test_classes import (
WebhookTestCase, WebhookTestCase,
) )
from zerver.lib.response import json_response from zerver.lib.response import json_response
from zerver.lib.user_agent import parse_user_agent
from zerver.lib.request import \ from zerver.lib.request import \
REQ, has_request_variables, RequestVariableMissingError, \ REQ, has_request_variables, RequestVariableMissingError, \
RequestVariableConversionError, JsonableError RequestVariableConversionError, JsonableError
@ -1259,3 +1263,26 @@ class RestAPITest(ZulipTestCase):
HTTP_ACCEPT='text/html') HTTP_ACCEPT='text/html')
self.assertEqual(result.status_code, 302) self.assertEqual(result.status_code, 302)
self.assertTrue(result["Location"].endswith("/login/?next=/json/users")) 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<count>[0-9]+) "(?P<user_agent>.*)"$', 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)