refactor mock server:

1, remove authentication switcher;
2, change authentication method: from verify request data's md5 to get token at the beginning.
This commit is contained in:
debugtalk
2017-07-22 15:14:52 +08:00
parent 8c58e87fd8
commit f94f57d102
25 changed files with 587 additions and 759 deletions

View File

@@ -1,4 +1,5 @@
import hashlib
import hmac
import json
import random
import string
@@ -13,37 +14,34 @@ except NameError:
PYTHON_VERSION = 3
import urllib.parse as urllib
SECRET_KEY = "DebugTalk"
def gen_random_string(str_len):
return ''.join(
random.choice(string.ascii_letters + string.digits) for _ in range(str_len))
random_char_list = []
for _ in range(str_len):
random_char = random.choice(string.ascii_letters + string.digits)
random_char_list.append(random_char)
random_string = ''.join(random_char_list)
return random_string
gen_random_string_lambda = lambda str_len: ''.join(
random.choice(string.ascii_letters + string.digits) for _ in range(str_len))
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):
args = [handle_req_data(item) for item in args]
return hashlib.md5("".join(args).encode('utf-8')).hexdigest()
def handle_req_data(data):
if PYTHON_VERSION == 3 and isinstance(data, bytes):
# In Python3, convert bytes to str
data = data.decode('utf-8')
if not data:
return data
if isinstance(data, str):
# check if data in str can be converted to dict
try:
data = json.loads(data)
except ValueError:
pass
if isinstance(data, dict):
# sort data in dict with keys, then convert to str
data = json.dumps(data, sort_keys=True)
return data
def gen_urlencode_str(**kargs):
urlencoded_str = ""
quote_times = int(kargs.pop("quote_times", 1))

View File

@@ -1,42 +0,0 @@
- config:
name: "create user testsets."
import_module_functions:
- tests.data.custom_functions
variable_binds:
- TOKEN: debugtalk
- test:
name: create user which does not exist
variable_binds:
- json: {"name": "user", "password": "123456"}
- random: ${gen_random_string(5)}
- authorization: ${gen_md5($TOKEN, $json, $random)}
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
headers:
Content-Type: application/json
authorization: $authorization
random: $random
json: $json
validators:
- {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true}
- test:
name: create user which does not exist
variable_binds:
- json: {"name": "user", "password": "123456"}
- random: ${gen_random_string(5)}
- authorization: ${gen_md5($TOKEN, $json, $random)}
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
headers:
Content-Type: application/json
authorization: $authorization
random: $random
json: $json
validators:
- {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false}

View File

@@ -1,52 +0,0 @@
- test:
name: create user which does not exist
requires:
- random
- string
- hashlib
function_binds:
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds:
- TOKEN: debugtalk
- random: ${gen_random_string(5)}
- data: '{"name": "user", "password": "123456"}'
- authorization: ${gen_md5($TOKEN, $data, $random)}
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
headers:
Content-Type: application/json
authorization: $authorization
random: $random
data: $data
validators:
- {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true}
- test:
name: create user which does exist
requires:
- random
- string
- hashlib
function_binds:
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds:
- TOKEN: debugtalk
- random: ${gen_random_string(5)}
- data: '{"name": "user", "password": "123456"}'
- authorization: ${gen_md5($TOKEN, $data, $random)}
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
headers:
Content-Type: application/json
authorization: $authorization
random: $random
data: $data
validators:
- {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false}

View File

@@ -1,50 +0,0 @@
- config:
name: "create user testsets."
requires:
- random
- string
- hashlib
function_binds:
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds:
- TOKEN: debugtalk
request:
base_url: http://127.0.0.1:5000
- test:
name: create user which does not exist
variable_binds:
- data: '{"name": "user", "password": "123456"}'
- random: ${gen_random_string(5)}
- authorization: ${gen_md5($TOKEN, $data, $random)}
request:
url: /api/users/1000
method: POST
headers:
Content-Type: application/json
authorization: $authorization
random: $random
data: $data
validators:
- {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true}
- test:
name: create user which does exist
variable_binds:
- data: '{"name": "user", "password": "123456"}'
- random: ${gen_random_string(5)}
- authorization: ${gen_md5($TOKEN, $data, $random)}
- expected_status_code: 500
request:
url: /api/users/1000
method: POST
headers:
Content-Type: application/json
authorization: $authorization
random: $random
data: $data
validators:
- {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false}

View File

@@ -0,0 +1,74 @@
[
{
"test": {
"name": "get token",
"request": {
"url": "http://127.0.0.1:5000/api/get-token",
"method": "POST",
"headers": {
"content-type": "application/json",
"user_agent": "iOS/10.3",
"device_sn": "HZfFBh6tU59EdXJ",
"os_platform": "ios",
"app_version": "2.8.6"
},
"json": {
"sign": "f1219719911caae89ccc301679857ebfda115ca2"
}
},
"extract_binds": [
{
"token": "content.token"
}
],
"validators": [
{"check": "status_code", "comparator": "eq", "expected": 200},
{"check": "content.token", "comparator": "len_eq", "expected": 16}
]
}
},
{
"test": {
"name": "create user which does not exist",
"request": {
"url": "http://127.0.0.1:5000/api/users/2000",
"method": "POST",
"headers": {
"content-type": "application/json",
"device_sn": "HZfFBh6tU59EdXJ",
"token": "$token"
},
"json": {
"name": "user1",
"password": "123456"
}
},
"validators": [
{"check": "status_code", "comparator": "eq", "expected": 201},
{"check": "content.success", "comparator": "eq", "expected": true}
]
}
},
{
"test": {
"name": "create user which existed",
"request": {
"url": "http://127.0.0.1:5000/api/users/2000",
"method": "POST",
"headers": {
"content-type": "application/json",
"device_sn": "HZfFBh6tU59EdXJ",
"token": "$token"
},
"json": {
"name": "user1",
"password": "123456"
}
},
"validators": [
{"check": "status_code", "comparator": "eq", "expected": 500},
{"check": "content.success", "comparator": "eq", "expected": false}
]
}
}
]

View File

@@ -1,3 +1,22 @@
- test:
name: get token
request:
url: http://127.0.0.1:5000/api/get-token
method: POST
headers:
Content-Type: application/json
user_agent: 'iOS/10.3'
device_sn: 'HZfFBh6tU59EdXJ'
os_platform: 'ios'
app_version: '2.8.6'
json:
sign: f1219719911caae89ccc301679857ebfda115ca2
extract_binds:
- token: content.token
validators:
- {"check": "status_code", "comparator": "eq", "expected": 200}
- {"check": "content.token", "comparator": "len_eq", "expected": 16}
- test:
name: create user which does not exist
request:
@@ -5,8 +24,8 @@
method: POST
headers:
Content-Type: application/json
authorization: a83de0ff8d2e896dbd8efb81ba14e17d
random: A2dEx
device_sn: 'HZfFBh6tU59EdXJ'
token: $token
json:
name: "user1"
password: "123456"
@@ -21,8 +40,8 @@
method: POST
headers:
Content-Type: application/json
authorization: a83de0ff8d2e896dbd8efb81ba14e17d
random: A2dEx
device_sn: 'HZfFBh6tU59EdXJ'
token: $token
json:
name: "user1"
password: "123456"

View File

@@ -0,0 +1,64 @@
- config:
name: "create user testsets."
import_module_functions:
- tests.data.custom_functions
variable_binds:
- user_agent: 'iOS/10.3'
- device_sn: ${gen_random_string(15)}
- os_platform: 'ios'
- app_version: '2.8.6'
- sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)}
request:
base_url: http://127.0.0.1:5000
headers:
Content-Type: application/json
device_sn: $device_sn
- test:
name: get token
request:
url: /api/get-token
method: POST
headers:
user_agent: $user_agent
device_sn: $device_sn
os_platform: $os_platform
app_version: $app_version
json:
sign: $sign
extract_binds:
- token: content.token
validators:
- {"check": "status_code", "comparator": "eq", "expected": 200}
- {"check": "content.token", "comparator": "len_eq", "expected": 16}
- test:
name: create user which does not exist
variable_binds:
- user_name: "user1"
- user_password: "123456"
request:
url: /api/users/1000
method: POST
headers:
token: $token
json:
name: $user_name
password: $user_password
validators:
- {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true}
- test:
name: create user which does not exist
request:
url: /api/users/1000
method: POST
headers:
token: $token
json:
name: "user1"
password: "123456"
validators:
- {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false}

View File

@@ -0,0 +1,74 @@
- config:
name: "create user testsets."
requires:
- random
- string
- hashlib
- hmac
function_binds:
gen_random_string_lambda: "lambda str_len: ''.join(
random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
get_sign_lambda: "lambda *args: hmac.new(
'DebugTalk'.encode('ascii'),
''.join(args).encode('ascii'),
hashlib.sha1).hexdigest()"
variable_binds:
- user_agent: 'iOS/10.3'
- device_sn: ${gen_random_string_lambda(15)}
- os_platform: 'ios'
- app_version: '2.8.6'
- sign: ${get_sign_lambda($user_agent, $device_sn, $os_platform, $app_version)}
request:
base_url: http://127.0.0.1:5000
headers:
Content-Type: application/json
device_sn: $device_sn
- test:
name: get token
request:
url: /api/get-token
method: POST
headers:
user_agent: $user_agent
device_sn: $device_sn
os_platform: $os_platform
app_version: $app_version
json:
sign: $sign
extract_binds:
- token: content.token
validators:
- {"check": "status_code", "comparator": "eq", "expected": 200}
- {"check": "content.token", "comparator": "len_eq", "expected": 16}
- test:
name: create user which does not exist
variable_binds:
- user_name: "user1"
- user_password: "123456"
request:
url: /api/users/1000
method: POST
headers:
token: $token
json:
name: $user_name
password: $user_password
validators:
- {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true}
- test:
name: create user which does not exist
request:
url: /api/users/1000
method: POST
headers:
token: $token
json:
name: "user1"
password: "123456"
validators:
- {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false}

View File

@@ -0,0 +1,62 @@
- config:
name: "create user testsets."
variable_binds:
- user_agent: 'iOS/10.3'
- device_sn: 'HZfFBh6tU59EdXJ'
- os_platform: 'ios'
- app_version: '2.8.6'
request:
base_url: http://127.0.0.1:5000
headers:
Content-Type: application/json
device_sn: $device_sn
- test:
name: get token
request:
url: /api/get-token
method: POST
headers:
Content-Type: application/json
user_agent: $user_agent
device_sn: $device_sn
os_platform: $os_platform
app_version: $app_version
json:
sign: f1219719911caae89ccc301679857ebfda115ca2
extract_binds:
- token: content.token
validators:
- {"check": "status_code", "comparator": "eq", "expected": 200}
- {"check": "content.token", "comparator": "len_eq", "expected": 16}
- test:
name: create user which does not exist
variable_binds:
- user_name: "user1"
- user_password: "123456"
request:
url: /api/users/1000
method: POST
headers:
token: $token
json:
name: $user_name
password: $user_password
validators:
- {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true}
- test:
name: create user which does not exist
request:
url: /api/users/1000
method: POST
headers:
token: $token
json:
name: "user1"
password: "123456"
validators:
- {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false}

View File

@@ -1,46 +0,0 @@
[
{
"test": {
"name": "create user which does not exist",
"request": {
"url": "http://127.0.0.1:5000/api/users/1000",
"method": "POST",
"headers": {
"content-type": "application/json",
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
"random": "A2dEx"
},
"json": {
"name": "user1",
"password": "123456"
}
},
"validators": [
{"check": "status_code", "comparator": "eq", "expected": 201},
{"check": "content.success", "comparator": "eq", "expected": true}
]
}
},
{
"test": {
"name": "create user which existed",
"request": {
"url": "http://127.0.0.1:5000/api/users/1000",
"method": "POST",
"headers": {
"content-type": "application/json",
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
"random": "A2dEx"
},
"json": {
"name": "user1",
"password": "123456"
}
},
"validators": [
{"check": "status_code", "comparator": "eq", "expected": 500},
{"check": "content.success", "comparator": "eq", "expected": false}
]
}
}
]

View File

@@ -1,43 +0,0 @@
[
{
"test": {
"name": "create user which does not exist",
"request": {
"url": "http://127.0.0.1:5000/api/users/1000",
"method": "POST",
"headers": {
"content-type": "application/json"
},
"cookies": {},
"json": {
"name": "user1",
"password": "123456"
}
},
"validators": [
{"check": "status_code", "comparator": "eq", "expected": 201},
{"check": "content.success", "comparator": "eq", "expected": true}
]
}
},
{
"test": {
"name": "create user which existed",
"request": {
"url": "http://127.0.0.1:5000/api/users/1000",
"method": "POST",
"headers": {
"content-type": "application/json"
},
"json": {
"name": "user1",
"password": "123456"
}
},
"validators": [
{"check": "status_code", "comparator": "eq", "expected": 500},
{"check": "content.success", "comparator": "eq", "expected": false}
]
}
}
]

View File

@@ -1,29 +0,0 @@
- test:
name: create user which does not exist
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
headers:
Content-Type: application/json
json:
name: user1
password: 123456
validators:
- {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true}
- {"check": "headers.content-type", "comparator": "eq", "expected": "application/json"}
- test:
name: create user which existed
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
headers:
Content-Type: application/json
json:
name: user1
password: 123456
validators:
- {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false}
- {"check": "headers.content-type", "comparator": "eq", "expected": "application/json"}