diff --git a/httprunner/utils.py b/httprunner/utils.py index 02a5b50f..e785faac 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -1,34 +1,16 @@ # encoding: utf-8 import copy -import hashlib -import hmac import io import itertools import json import os.path -import random import string from datetime import datetime from httprunner import exceptions, logger from httprunner.compat import OrderedDict, basestring, is_py2 -SECRET_KEY = "DebugTalk" - - -def gen_random_string(str_len): - return ''.join( - random.choice(string.ascii_letters + string.digits) for _ in range(str_len)) - -def gen_md5(*str_args): - return hashlib.md5("".join(str_args).encode('utf-8')).hexdigest() - -def get_sign(*args): - content = ''.join(args).encode('ascii') - sign_key = SECRET_KEY.encode('ascii') - sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest() - return sign def remove_prefix(text, prefix): """ remove prefix from text diff --git a/tests/api_server.py b/tests/api_server.py index 02f2e05d..187c2227 100644 --- a/tests/api_server.py +++ b/tests/api_server.py @@ -1,9 +1,23 @@ import hashlib +import hmac import json from functools import wraps -from httprunner import utils from flask import Flask, make_response, request +from httprunner.built_in import gen_random_string + +try: + from httpbin import app as httpbin_app + HTTPBIN_HOST = "127.0.0.1" + HTTPBIN_PORT = 3458 +except ImportError: + httpbin_app = None + HTTPBIN_HOST = "httpbin.org" + HTTPBIN_PORT = 80 + +FLASK_APP_PORT = 5000 +HTTPBIN_SERVER = "http://{}:{}".format(HTTPBIN_HOST, HTTPBIN_PORT) +SECRET_KEY = "DebugTalk" app = Flask(__name__) @@ -31,6 +45,17 @@ data structure: """ token_dict = {} + +def get_sign(*args): + content = ''.join(args).encode('ascii') + sign_key = SECRET_KEY.encode('ascii') + sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest() + return sign + +def gen_md5(*args): + return hashlib.md5("".join(args).encode('utf-8')).hexdigest() + + def validate_request(func): @wraps(func) @@ -74,7 +99,7 @@ def get_token(): data = request.get_json() sign = data.get('sign', "") - expected_sign = utils.get_sign(user_agent, device_sn, os_platform, app_version) + expected_sign = get_sign(user_agent, device_sn, os_platform, app_version) if expected_sign != sign: result = { @@ -83,7 +108,7 @@ def get_token(): } response = make_response(json.dumps(result), 403) else: - token = utils.gen_random_string(16) + token = gen_random_string(16) token_dict[device_sn] = token result = { diff --git a/tests/base.py b/tests/base.py index 745bd222..8c6c5abc 100644 --- a/tests/base.py +++ b/tests/base.py @@ -3,26 +3,17 @@ import time import unittest import requests -from httprunner import utils +from tests.api_server import FLASK_APP_PORT, HTTPBIN_HOST, HTTPBIN_PORT from tests.api_server import app as flask_app - -try: - from httpbin import app as httpbin_app - HTTPBIN_HOST = "127.0.0.1" - HTTPBIN_PORT = 3458 -except ImportError: - HTTPBIN_HOST = "httpbin.org" - HTTPBIN_PORT = 80 - -FLASK_APP_PORT = 5000 -HTTPBIN_SERVER = "http://{}:{}".format(HTTPBIN_HOST, HTTPBIN_PORT) +from tests.api_server import gen_md5, gen_random_string, get_sign, httpbin_app def run_flask(): flask_app.run(port=FLASK_APP_PORT) + def run_httpbin(): - if HTTPBIN_HOST == "127.0.0.1": + if httpbin_app: httpbin_app.run(host=HTTPBIN_HOST, port=HTTPBIN_PORT) @@ -59,7 +50,7 @@ class ApiServerUnittest(unittest.TestCase): 'app_version': app_version } data = { - 'sign': utils.get_sign(user_agent, device_sn, os_platform, app_version) + 'sign': get_sign(user_agent, device_sn, os_platform, app_version) } resp = self.api_client.post(url, json=data, headers=headers) @@ -71,7 +62,7 @@ class ApiServerUnittest(unittest.TestCase): def get_authenticated_headers(self): user_agent = 'iOS/10.3' - device_sn = utils.gen_random_string(15) + device_sn = gen_random_string(15) os_platform = 'ios' app_version = '2.8.6' diff --git a/tests/debugtalk.py b/tests/debugtalk.py index 1e965360..db12b87e 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -6,30 +6,21 @@ import random import string import time -from tests.base import HTTPBIN_SERVER +from tests.api_server import HTTPBIN_SERVER, SECRET_KEY, gen_md5, get_sign try: import urllib except NameError: import urllib.parse as urllib -SECRET_KEY = "DebugTalk" BASE_URL = "http://127.0.0.1:5000" -def get_sign(*args): - content = ''.join(args).encode('ascii') - sign_key = SECRET_KEY.encode('ascii') - sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest() - return sign get_sign_lambda = lambda *args: hmac.new( 'DebugTalk'.encode('ascii'), ''.join(args).encode('ascii'), hashlib.sha1).hexdigest() -def gen_md5(*args): - return hashlib.md5("".join(args).encode('utf-8')).hexdigest() - def sum_status_code(status_code, expect_sum): """ sum status code digits e.g. 400 => 4, 201 => 3 @@ -62,8 +53,6 @@ def get_account(): {"username": "user2", "password": "222222"} ] -SECRET_KEY = "DebugTalk" - def gen_random_string(str_len): random_char_list = [] for _ in range(str_len): diff --git a/tests/test_context.py b/tests/test_context.py index d28351a9..bdf1e4a9 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -4,7 +4,6 @@ import unittest import requests from httprunner import context, exceptions, loader, parser, response, runner -from httprunner.utils import gen_md5 from tests.base import ApiServerUnittest @@ -120,6 +119,7 @@ class TestContext(ApiServerUnittest): {"authorization": "${gen_md5($TOKEN, $data, $random)}"} ] from tests import debugtalk + from tests.debugtalk import gen_md5 self.context.import_module_items(debugtalk) self.context.bind_variables(variables) context_variables = self.context.testcase_variables_mapping diff --git a/tests/test_httprunner.py b/tests/test_httprunner.py index 010328ff..9c5002e6 100644 --- a/tests/test_httprunner.py +++ b/tests/test_httprunner.py @@ -2,7 +2,8 @@ import os import shutil from httprunner import HttpRunner -from tests.base import HTTPBIN_SERVER, ApiServerUnittest +from tests.api_server import HTTPBIN_SERVER +from tests.base import ApiServerUnittest class TestHttpRunner(ApiServerUnittest): diff --git a/tests/test_loader.py b/tests/test_loader.py index 867e2723..fb491d5f 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -197,17 +197,17 @@ class TestModuleLoader(unittest.TestCase): from httprunner import utils module_mapping = loader.load_python_module(utils) - gen_md5 = loader.get_module_item(module_mapping, "functions", "gen_md5") - self.assertTrue(validator.is_function(("gen_md5", gen_md5))) - self.assertEqual(gen_md5("abc"), "900150983cd24fb0d6963f7d28e17f72") + get_uniform_comparator = loader.get_module_item( + module_mapping, "functions", "get_uniform_comparator") + self.assertTrue(validator.is_function(("get_uniform_comparator", get_uniform_comparator))) + self.assertEqual(get_uniform_comparator("=="), "equals") with self.assertRaises(exceptions.FunctionNotFound): loader.get_module_item(module_mapping, "functions", "gen_md4") def test_get_module_item_variables(self): - from httprunner import utils - module_mapping = loader.load_python_module(utils) - + from tests import debugtalk + module_mapping = loader.load_python_module(debugtalk) SECRET_KEY = loader.get_module_item(module_mapping, "variables", "SECRET_KEY") self.assertTrue(validator.is_variable(("SECRET_KEY", SECRET_KEY))) diff --git a/tests/test_response.py b/tests/test_response.py index cb1ce474..efc8492b 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -1,7 +1,8 @@ import requests from httprunner import built_in, exceptions, loader, response from httprunner.compat import basestring, bytes -from tests.base import HTTPBIN_SERVER, ApiServerUnittest +from tests.api_server import HTTPBIN_SERVER +from tests.base import ApiServerUnittest class TestResponse(ApiServerUnittest): diff --git a/tests/test_runner.py b/tests/test_runner.py index d6f7029b..82df9a48 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -3,7 +3,8 @@ import time from httprunner import HttpRunner, exceptions, loader, runner from httprunner.utils import deep_update_dict -from tests.base import HTTPBIN_SERVER, ApiServerUnittest +from tests.api_server import HTTPBIN_SERVER +from tests.base import ApiServerUnittest class TestRunner(ApiServerUnittest):