From 52aef260384ab88644c5fa497f492316d8862e85 Mon Sep 17 00:00:00 2001 From: floraachy Date: Mon, 20 Apr 2020 17:12:24 +0800 Subject: [PATCH] commit jenkins use --- .../unitTest/class03212020/README.txt | 3 +- .../class03212020/demo1/requestSendParam.py | 55 +++--- .../class03212020/demo2/requestsRequest.py | 24 +++ .../unitTest/class03262020/operateMySql.py | 29 ++- .../unitTest/class04022020/regUse.py | 57 +++--- .../unitTest/class04072020/handle_data.py | 33 +++- .../unitTest/class04142020/01mock.py | 29 +++ .../unitTest/class04142020/02mock.py | 7 + .../unitTest/class04142020/02mock测试.py | 35 ++++ .../class04142020/03mock请求数据.py | 168 ++++++++++++++++++ .../04cookie_session鉴权的接口.py | 37 ++++ ..._session鉴权的接口之session对象.py | 35 ++++ ...鉴权的方式 怎么去做自动化.py | 30 ++++ .../unitTest/class04142020/README.txt | 5 + .../unitTest/class04142020/handle_sign.py | 87 +++++++++ .../unitTest/class04142020/接口断言.py | 78 ++++++++ 16 files changed, 636 insertions(+), 76 deletions(-) create mode 100644 python27Class/unitTest/class03212020/demo2/requestsRequest.py create mode 100644 python27Class/unitTest/class04142020/01mock.py create mode 100644 python27Class/unitTest/class04142020/02mock.py create mode 100644 python27Class/unitTest/class04142020/02mock测试.py create mode 100644 python27Class/unitTest/class04142020/03mock请求数据.py create mode 100644 python27Class/unitTest/class04142020/04cookie_session鉴权的接口.py create mode 100644 python27Class/unitTest/class04142020/04cookie_session鉴权的接口之session对象.py create mode 100644 python27Class/unitTest/class04142020/05前程贷V3版鉴权的方式 怎么去做自动化.py create mode 100644 python27Class/unitTest/class04142020/README.txt create mode 100644 python27Class/unitTest/class04142020/handle_sign.py create mode 100644 python27Class/unitTest/class04142020/接口断言.py diff --git a/python27Class/unitTest/class03212020/README.txt b/python27Class/unitTest/class03212020/README.txt index c7de9d3..601b4c2 100644 --- a/python27Class/unitTest/class03212020/README.txt +++ b/python27Class/unitTest/class03212020/README.txt @@ -10,4 +10,5 @@ downloadFiles.py -- 关于请求内容获取和文件下载 安装:pip install jsonpath jsonpathPractice.py ---请求返回数据的提取:jsonpath模块 -login_recharge.py ---登录充值案例演示 \ No newline at end of file +login_recharge.py ---登录充值案例演示 +requestsRequest.py ---直接使用request方法定义请求的类型 \ No newline at end of file diff --git a/python27Class/unitTest/class03212020/demo1/requestSendParam.py b/python27Class/unitTest/class03212020/demo1/requestSendParam.py index d50d80c..a97db4d 100644 --- a/python27Class/unitTest/class03212020/demo1/requestSendParam.py +++ b/python27Class/unitTest/class03212020/demo1/requestSendParam.py @@ -11,30 +11,29 @@ Time: 2020/3/21 9:58 import requests # ----------------------------------post------------------------------------------------------ -# url = 'http://httpbin.org/post' -# -# data = { -# 'mobile_phone': '18774970056', -# 'pwd': '12345678' -# } +url = 'http://httpbin.org/post' -# 参数类型为 "Content-Type": "application/json", 发送请求的时候使用json来传递参数 -# response = requests.post(url=url, json=data) +data = { + 'mobile_phone': '18774970056', + 'pwd': '12345678' +} -# 参数类型为"Content-Type": "application/x-www-form-urlencoded", 发送请求时使用data来传递参数 -# response = requests.post(url=url, data=data) +#参数类型为 "Content-Type": "application/json", 发送请求的时候使用json来传递参数 +response1 = requests.post(url=url, json=data) + +#参数类型为"Content-Type": "application/x-www-form-urlencoded", 发送请求时使用data来传递参数 +response2 = requests.post(url=url, data=data) + +#参数类型为 "Content-Type": "multipart/form-data", 发送请求的时候使用files来传递参数 +# 文件上传传递参数 +file = { # 文件名(后台接收到的服务器看到的名字,可以自定义), 打开的文件句柄,文件类型 + # pic需要具体看接口里面上传文件需要的参数名是什么 + 'pic': ('picture.jpg', open('picture.jpg', 'rb'), 'image/jpg') +} +#注意:如果文件上传中还有其他参数,其他参数是不能放在file里面 +response3 = requests.post(url=url, files=file) +response4 = requests.post(url=url, files=file, data=data) -# 参数类型为 "Content-Type": "multipart/form-data", 发送请求的时候使用files来传递参数 -# # 文件上传传递参数 -# file = { # 文件名(后台接收到的服务器看到的名字,可以自定义), 打开的文件句柄,文件类型 -# # pic需要具体看接口里面上传文件需要的参数名是什么 -# 'pic': ('picture.jpg', open('picture.jpg', 'rb'), 'image/jpg') -# } -# 注意:如果文件上传中还有其他参数,其他参数是不能放在file里面 -# response = requests.post(url=url, files=file) -# response = requests.post(url=url, files=file, data=data) -# -# print(response.text) # ----------------------------------get------------------------------------------------------ url = 'http://httpbin.org/get' @@ -43,11 +42,11 @@ response = requests.get(url=url) print(response.text) -# get请求参数,方式一:放到URL地址后面 -# url1 = 'http://httpbin.org/get?name=musen&age=18' -# response1 = requests.get(url=url1) -# # 打印返回内容 -# print(response1.text) +#get请求参数,方式一:放到URL地址后面 +url1 = 'http://httpbin.org/get?name=musen&age=18' +response5 = requests.get(url=url1) +# 打印返回内容 +print(response5.text) # get请求参数,方式二:使用params来进行传递 @@ -56,5 +55,5 @@ data2 = { 'name': 'flora', 'age': 19 } -response2 = requests.get(url=url, params=data2) -print(response2.text) \ No newline at end of file +response6 = requests.get(url=url, params=data2) +print(response6.text) \ No newline at end of file diff --git a/python27Class/unitTest/class03212020/demo2/requestsRequest.py b/python27Class/unitTest/class03212020/demo2/requestsRequest.py new file mode 100644 index 0000000..c131533 --- /dev/null +++ b/python27Class/unitTest/class03212020/demo2/requestsRequest.py @@ -0,0 +1,24 @@ +""" +================================= +Author: Flora Chen +Time: 2020/4/13 9:38 +-_- -_- -_- -_- -_- -_- -_- -_- +================================= +""" + +from requests import request + +# 请求参数 +method = 'post' +url = 'http://api.lemonban.com/futureloan/member/login' +headers = { + 'X-lemonban-Media-Type': 'lemonban.v2' +} + +data = { + 'mobile_phone': '18774970056', + 'pwd': '12345678' +} + +# 直接使用request方法定义请求的类型 +request(method=method, url=url, json=data, headers=headers) \ No newline at end of file diff --git a/python27Class/unitTest/class03262020/operateMySql.py b/python27Class/unitTest/class03262020/operateMySql.py index bcf1673..d1d0e57 100644 --- a/python27Class/unitTest/class03262020/operateMySql.py +++ b/python27Class/unitTest/class03262020/operateMySql.py @@ -11,16 +11,24 @@ import pymysql """ 接口base_url: http://api.lemonban.com/futureloan MySQL数据库连接信息: - 主机:120.78.128.25 + 主机:120.78.128.****** port:3306 - 用户:future - 密码:123456 + 用户:futu****** + 密码:123****** """ # 第一步:连接数据库 -conn = pymysql.connect(host='120.78.128.25', +conn1 = pymysql.connect(host='120.78.128.******', port=3306, - user='future', - password='123456', + user='futu******', + password='123******', + charset='utf8', + cursorclass=pymysql.cursors) + + +conn = pymysql.connect(host='120.78.128.******', + port=3306, + user='futu******', + password='123******', charset='utf8', cursorclass=pymysql.cursors.DictCursor)# 加上这个返回的就是字典 @@ -62,13 +70,4 @@ pymysql操作数据库, 默认是开启了事务 所以在执行增删改的相关操作之后一定要提交事务才会生效 连接对象.commit() - -2052976,2053131,2053207,2053209,2053220,2053130,2038001,2038000,2037999,2037620,2037619,2053284,2053301,2048577,2053317,2053323,2052877,2052857,2052846,2052844,2053045,2053357,2048575,2048576 - - -7,6,9,14,9,7,20,2,6,4,32,7,3,13,7,7,19,26,7,6,2,3,10,8 - - -810500,809502,810623,914941,914947,914936 - """ \ No newline at end of file diff --git a/python27Class/unitTest/class04022020/regUse.py b/python27Class/unitTest/class04022020/regUse.py index 3329e8f..959b75e 100644 --- a/python27Class/unitTest/class04022020/regUse.py +++ b/python27Class/unitTest/class04022020/regUse.py @@ -11,43 +11,40 @@ import re from python27Class.unitTest.class04022020.handle_config import conf -user = 'python' -pwd = 'lemonban' -name = 'flora' -age = 18 - -data = "{'user': '#user#', 'pwd': '#pwd#', 'name': '#name#', 'age': #age#}" -# 用replace方法需要多次替换 (缺点:每次只能替换一个数据,效率太低) -data = data.replace('#user#', user) -data = data.replace('#pwd#', pwd) -data = data.replace('#name#', name) -data = data.replace('#age#', str(age)) -print(data) - -# 使用正则替换 -data = "{'user': '#user#', 'pwd': '#pwd#', 'name': '#name#', 'age': #age#}" -# rule = re.search('#(.*?)#', data) -# print(rule) # 输出结果: -# -# # 获取匹配到的数据 -# key = rule.group() -# print(key) # 输出结果:#user# -# -# # 获取匹配规则中()里面的内容, 1表示第一个()里的内容 -# item = rule.group(1) -# print(item) # 输出结果:user -# -# value = conf.get('REG', item) -# data = data.replace(key, value) -# print(data) - test = { 'user': 'python', 'name' : 'flora', 'pwd' : 'lemonban', 'age' : 18 } +data = "{'user': '#user#', 'pwd': '#pwd#', 'name': '#name#', 'age': #age#}" +# 使用replace方法替换 (缺点:需要每一个单独进行替换,代码太多) + +# 用replace方法需要多次替换 (缺点:每次只能替换一个数据,效率太低) +data = data.replace('#user#', test['user']) +data = data.replace('#pwd#', test['pwd']) +data = data.replace('#name#', test['name']) +data = data.replace('#age#', str(test['age'])) +print(data) + +# 使用正则替换 +rule = re.search('#(.*?)#', data) +print(rule) # 输出结果: + +# 获取匹配到的数据 +key = rule.group() +print(key) # 输出结果:#user# + +# 获取匹配规则中()里面的内容, 1表示第一个()里的内容 +item = rule.group(1) +print(item) # 输出结果:user + +value = conf.get('REG', item) +data = data.replace(key, value) +print(data) + +# 封装成方法 def replace_data(data): while re.search('#(.*?)#', data): rule = re.search('#(.*?)#', data) diff --git a/python27Class/unitTest/class04072020/handle_data.py b/python27Class/unitTest/class04072020/handle_data.py index 0869de4..77e5133 100644 --- a/python27Class/unitTest/class04072020/handle_data.py +++ b/python27Class/unitTest/class04072020/handle_data.py @@ -5,13 +5,42 @@ Time: 2020/4/8 9:42 -_- -_- -_- -_- -_- -_- -_- -_- ================================= """ +import re + class EnvData: """ - 定义一个类,用来保存用例执行过程中,提取出来的数据 - (当成环境变量的容器)。 + 定义一个类,用来保存用例执行过程中,提取出来的数据。 + (当成环境变量的容器) """ pass + +def replace_data(data): + while re.search('#(.*?)#', data): + rule = re.search('#(.*?)#', data) + # 获取匹配到的数据 + key = rule.group() + + # 获取匹配规则中()里面的内容, 1表示第一个()里的内容 + item = rule.group(1) + + # 用字典中的值来替换 + # value = str(test[item]) + + # 用配置文件中的值来替换(获取配置文件中对应的值) + # value = conf.get('REG', item) + try: + # 获取配置文件中对应的值 + value = conf.get('TEST', item) + except: + # 去EnvData这个类里面获取 + value = getattr(EnvData, item) + + data = data.replace(key, value) + return data + + + data = {"member_id": 188, "mobile_phone": "13567895555"} token = "dfgfdgfdgdf dfgfdg dfgfdg dyjyui" diff --git a/python27Class/unitTest/class04142020/01mock.py b/python27Class/unitTest/class04142020/01mock.py new file mode 100644 index 0000000..a0ad0f2 --- /dev/null +++ b/python27Class/unitTest/class04142020/01mock.py @@ -0,0 +1,29 @@ +""" +================================= +Author: Flora Chen +Time: 2020/4/20 16:30 +-_- -_- -_- -_- -_- -_- -_- -_- +================================= +""" +import unittest +from requests import request +from unittest.mock import Mock + +""" +get_sign("musen) +使用用户忙和时间进行加密得到一个加密的签名数据 +""" +get_sign = Mock(return_value="sdfsSDGDF234dfg4353fdg45645fghfg435") + +class TestLogin(unittest.TestCase): + def test_login(self): + data = { + "user": "flora", + "pwd": "sdfgdgfdg", + "sign": get_sign("musen", 100) # 这里可以传任意参数 + } + + url = "http://127.0.0.1:8080/login" + print(data) + + response = request(method="post", url=url, json=data) \ No newline at end of file diff --git a/python27Class/unitTest/class04142020/02mock.py b/python27Class/unitTest/class04142020/02mock.py new file mode 100644 index 0000000..ead1855 --- /dev/null +++ b/python27Class/unitTest/class04142020/02mock.py @@ -0,0 +1,7 @@ +""" +================================= +Author: Flora Chen +Time: 2020/4/20 16:41 +-_- -_- -_- -_- -_- -_- -_- -_- +================================= +""" \ No newline at end of file diff --git a/python27Class/unitTest/class04142020/02mock测试.py b/python27Class/unitTest/class04142020/02mock测试.py new file mode 100644 index 0000000..611055f --- /dev/null +++ b/python27Class/unitTest/class04142020/02mock测试.py @@ -0,0 +1,35 @@ +""" +============================ +Author:柠檬班-木森 +Time:2020/4/14 20:33 +E-mail:3247119728@qq.com +Company:湖南零檬信息技术有限公司 +============================ +""" + +import unittest +from requests import request +from unittest.mock import Mock + +""" + + +gen_sign("musen") +使用用户名,和时间进行加密得到一个加密的签名数据 + +""" +gen_sign = Mock(return_value="yJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJf") + + + +class TestLogin(unittest.TestCase): + + def test_login(self): + data = { + "user": "musne", + "pwd": "ldjfss", + "sign": gen_sign("musen") + } + url = "http://127.0.0.1:8000/login" + + reponse = request(url=url,json=data,method="post") diff --git a/python27Class/unitTest/class04142020/03mock请求数据.py b/python27Class/unitTest/class04142020/03mock请求数据.py new file mode 100644 index 0000000..93bae73 --- /dev/null +++ b/python27Class/unitTest/class04142020/03mock请求数据.py @@ -0,0 +1,168 @@ +""" +============================ +Author:柠檬班-木森 +Time:2020/4/14 20:45 +E-mail:3247119728@qq.com +Company:湖南零檬信息技术有限公司 +============================ +""" +import unittest +from unittest import mock + +url = "https://docs.open.alipay.com/api_1/alipay.trade.pay" +# 账号密码都正确,正常 +data = {'user': 'python01', 'pay_pwd': 123456,'money':88.88} + +res_data = { + "alipay_trade_pay_response": { + "code": "10000", + "msg": "Success", + "trade_no": "2013112011001004330000121536", + "out_trade_no": "6823789339978248", + "buyer_logon_id": "159****5620", + "settle_amount": "88.88", + "pay_currency": "CNY", + "pay_amount": "580.04", + "settle_trans_rate": "1", + "trans_pay_rate": "6.5261", + "total_amount": 120.88, + "trans_currency": "USD", + "settle_currency": "USD", + "receipt_amount": "88.88", + "buyer_pay_amount": 8.88, + "point_amount": 8.12, + "invoice_amount": 12.5, + "gmt_payment": "2014-11-27 15:45:57", + "fund_bill_list": [ + { + "fund_channel": "ALIPAYACCOUNT", + "bank_code": "CEB", + "amount": 10, + "real_amount": 11.21 + } + ], + "card_balance": 98.23, + "store_name": "证大五道口店", + "buyer_user_id": "2088101117955611", + "discount_goods_detail": "[{\"goods_id\":\"STANDARD1026181538\",\"goods_name\":\"雪碧\",\"discount_amount\":\"100.00\",\"voucher_id\":\"2015102600073002039000002D5O\"}]", + "voucher_detail_list": [ + { + "id": "2015102600073002039000002D5O", + "name": "XX超市5折优惠", + "type": "ALIPAY_FIX_VOUCHER", + "amount": 10, + "merchant_contribute": 9, + "other_contribute": 1, + "memo": "学生专用优惠", + "template_id": "20171030000730015359000EMZP0", + "purchase_buyer_contribute": 2.01, + "purchase_merchant_contribute": 1.03, + "purchase_ant_contribute": 0.82 + } + ], + "advance_amount": "88.8", + "auth_trade_pay_mode": "CREDIT_PREAUTH_PAY", + "charge_amount": "8.88", + "charge_flags": "bluesea_1", + "settlement_id": "2018101610032004620239146945", + "business_params": "{\"data\":\"123\"}", + "buyer_user_type": "PRIVATE", + "mdiscount_amount": "88.88", + "discount_amount": "88.88", + "buyer_user_name": "菜鸟网络有限公司" + }, + "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE" +} +# 创建一个mock对象 +request = mock.Mock(return_value=res_data) + +# 调用mock对象 +response = request(url=url,data=data) +print(response) + + + +class TestPay(unittest.TestCase): + + @classmethod + def setUpClass(cls): + """请求支付结果获取支付接口返回的数据""" + url = "https://docs.open.alipay.com/api_1/alipay.trade.pay" + # 账号密码都正确,正常 + data = {'user': 'python01', 'pay_pwd': 123456, 'money': 88.88} + + res_data = { + "alipay_trade_pay_response": { + "code": "10000", + "msg": "Success", + "trade_no": "2013112011001004330000121536", + "out_trade_no": "6823789339978248", + "buyer_logon_id": "159****5620", + "settle_amount": "88.88", + "pay_currency": "CNY", + "pay_amount": "580.04", + "settle_trans_rate": "1", + "trans_pay_rate": "6.5261", + "total_amount": 120.88, + "trans_currency": "USD", + "settle_currency": "USD", + "receipt_amount": "88.88", + "buyer_pay_amount": 8.88, + "point_amount": 8.12, + "invoice_amount": 12.5, + "gmt_payment": "2014-11-27 15:45:57", + "fund_bill_list": [ + { + "fund_channel": "ALIPAYACCOUNT", + "bank_code": "CEB", + "amount": 10, + "real_amount": 11.21 + } + ], + "card_balance": 98.23, + "store_name": "证大五道口店", + "buyer_user_id": "2088101117955611", + "discount_goods_detail": "[{\"goods_id\":\"STANDARD1026181538\",\"goods_name\":\"雪碧\",\"discount_amount\":\"100.00\",\"voucher_id\":\"2015102600073002039000002D5O\"}]", + "voucher_detail_list": [ + { + "id": "2015102600073002039000002D5O", + "name": "XX超市5折优惠", + "type": "ALIPAY_FIX_VOUCHER", + "amount": 10, + "merchant_contribute": 9, + "other_contribute": 1, + "memo": "学生专用优惠", + "template_id": "20171030000730015359000EMZP0", + "purchase_buyer_contribute": 2.01, + "purchase_merchant_contribute": 1.03, + "purchase_ant_contribute": 0.82 + } + ], + "advance_amount": "88.8", + "auth_trade_pay_mode": "CREDIT_PREAUTH_PAY", + "charge_amount": "8.88", + "charge_flags": "bluesea_1", + "settlement_id": "2018101610032004620239146945", + "business_params": "{\"data\":\"123\"}", + "buyer_user_type": "PRIVATE", + "mdiscount_amount": "88.88", + "discount_amount": "88.88", + "buyer_user_name": "菜鸟网络有限公司" + }, + "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE" + } + # 创建一个mock对象 + request= mock.Mock(return_value=res_data) + + # 调用mock对象 + cls.response = request(url=url, data=data) + + + + + + def test_case01(self): + """测试用例中需要用到前置条件支付接口返回的数据""" + + # 获取前置条件中支付返回的数据 + pay_res_data = self.response \ No newline at end of file diff --git a/python27Class/unitTest/class04142020/04cookie_session鉴权的接口.py b/python27Class/unitTest/class04142020/04cookie_session鉴权的接口.py new file mode 100644 index 0000000..3963ef4 --- /dev/null +++ b/python27Class/unitTest/class04142020/04cookie_session鉴权的接口.py @@ -0,0 +1,37 @@ +""" +============================ +Author:柠檬班-木森 +Time:2020/4/14 21:10 +E-mail:3247119728@qq.com +Company:湖南零檬信息技术有限公司 +============================ +""" +import requests +from requests import request + +# 登录 +login_url = "http://test.lemonban.com/futureloan/mvc/api/member/login" +login_data = { + "mobilephone": "13367899876", + "pwd": "lemonban" +} +response = requests.post(url=login_url, data=login_data) + +# print(response.text) +print(response.headers) +# print(response.json()) +# # 获取http请求的状态码 +# # print(response.status_code) +# # print(response.text) +# # print(response.json()) +print("-*"*50) + +# 调用充值的接口 + +recharge_url = "http://test.lemonban.com/futureloan/mvc/api/member/recharge" +recharge_data = { + "mobilephone": "13367899876", + "amount": 2000 +} +response2 = requests.post(url=recharge_url,data=recharge_data) +print(response2.text) \ No newline at end of file diff --git a/python27Class/unitTest/class04142020/04cookie_session鉴权的接口之session对象.py b/python27Class/unitTest/class04142020/04cookie_session鉴权的接口之session对象.py new file mode 100644 index 0000000..4d78cbd --- /dev/null +++ b/python27Class/unitTest/class04142020/04cookie_session鉴权的接口之session对象.py @@ -0,0 +1,35 @@ +""" +============================ +Author:柠檬班-木森 +Time:2020/4/14 21:10 +E-mail:3247119728@qq.com +Company:湖南零檬信息技术有限公司 +============================ +""" +from requests.sessions import Session + +# 创建一个会话对象 +se = Session() + + +# 登录 +login_url = "http://test.lemonban.com/futureloan/mvc/api/member/login" +login_data = { + "mobilephone": "13367899876", + "pwd": "lemonban" +} +response = se.post(url=login_url, data=login_data) + +print(response.text) +print(response.headers) + +print("-*"*50) + +# 调用充值的接口 +recharge_url = "http://test.lemonban.com/futureloan/mvc/api/member/recharge" +recharge_data = { + "mobilephone": "13367899876", + "amount": 2000 +} +response2 = se.post(url=recharge_url,data=recharge_data) +print(response2.text) \ No newline at end of file diff --git a/python27Class/unitTest/class04142020/05前程贷V3版鉴权的方式 怎么去做自动化.py b/python27Class/unitTest/class04142020/05前程贷V3版鉴权的方式 怎么去做自动化.py new file mode 100644 index 0000000..9a5b4de --- /dev/null +++ b/python27Class/unitTest/class04142020/05前程贷V3版鉴权的方式 怎么去做自动化.py @@ -0,0 +1,30 @@ +""" +============================ +Author:柠檬班-木森 +Time:2020/4/14 21:37 +E-mail:3247119728@qq.com +Company:湖南零檬信息技术有限公司 +============================ +""" + +import time +from common.handle_sign import HandleSign + +token = "q2weretryfgyihfdsadsfdgfhjhgfdasfgdhgjhdsfgfgjhgf" + +# # 获取当前的时间戳 +# s = int(time.time()) +# # token前50位和时间戳拼接 +# msg = token[:50]+str(s) +# +# sign = HandleSign.to_encrypt(msg=msg) +# print(sign) + +data ={"name":1111,"pwd":2222} + +sign_data = HandleSign.generate_sign(token) + +print(sign_data) + +data.update(sign_data) +print(data) \ No newline at end of file diff --git a/python27Class/unitTest/class04142020/README.txt b/python27Class/unitTest/class04142020/README.txt new file mode 100644 index 0000000..85edf42 --- /dev/null +++ b/python27Class/unitTest/class04142020/README.txt @@ -0,0 +1,5 @@ +Mock测试 +Mock测试是在测试过程中,对于一些不容易构造/获取的对象,创建一个mock对象来模拟对象的行为。 +单元测试/接口测试中猜测是对象依赖其他对象,这些对象的构造复杂,耗时或者根本无法构建(未交付)就会使用到mock测试。 +from unittest.mock import Mock + diff --git a/python27Class/unitTest/class04142020/handle_sign.py b/python27Class/unitTest/class04142020/handle_sign.py new file mode 100644 index 0000000..a00df50 --- /dev/null +++ b/python27Class/unitTest/class04142020/handle_sign.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +""" +------------------------------------------------- + @Time : 2019/11/25 15:17 + @Auth : 可优 + @File : handle_sign.py + @IDE : PyCharm + @Motto: ABC(Always Be Coding) + @Email: keyou100@qq.com + @Company: 湖南省零檬信息技术有限公司 + @Copyright: 柠檬班 +------------------------------------------------- +""" +import base64 +from time import time + +# 需要安装rsa模块, pip install rsa +import rsa + + +class HandleSign: + server_pub = """ + -----BEGIN PUBLIC KEY----- + MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQENQujkLfZfc5Tu9Z1LprzedE + O3F7gs+7bzrgPsMl29LX8UoPYvIG8C604CprBQ4FkfnJpnhWu2lvUB0WZyLq6sBr + tuPorOc42+gLnFfyhJAwdZB6SqWfDg7bW+jNe5Ki1DtU7z8uF6Gx+blEMGo8Dg+S + kKlZFc8Br7SHtbL2tQIDAQAB + -----END PUBLIC KEY----- + """ + + @classmethod + def to_encrypt(cls, msg, pub_key=None): + """ + 非对称加密 + :param msg: 待加密字符串或者字节 + :param pub_key: 公钥 + :return: 密文 + """ + if isinstance(msg, str): # 如果msg为字符串, 则转化为字节类型 + msg = msg.encode('utf-8') + elif isinstance(msg, bytes): # 如果msg为字节类型, 则无需处理 + pass + else: # 否则抛出异常 + raise TypeError('msg必须为字符串或者字节类型!') + + if not pub_key: # 如果pub_key为空, 则使用全局公钥 + pub_key = cls.server_pub.encode("utf-8") + elif isinstance(pub_key, str): # 如果pub_key为字符串, 则转化为字节类型 + pub_key = pub_key.encode('utf-8') + elif isinstance(pub_key, bytes): # 如果msg为字节类型, 则无需处理 + pass + else: # 否则抛出异常 + raise TypeError('pub_key必须为None、字符串或者字节类型!') + + public_key_obj = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key) # 创建 PublicKey 对象 + + cryto_msg = rsa.encrypt(msg, public_key_obj) # 生成加密文本 + cipher_base64 = base64.b64encode(cryto_msg) # 将加密文本转化为 base64 编码 + + return cipher_base64.decode() # 将字节类型的 base64 编码转化为字符串类型 + + @classmethod + def generate_sign(cls, token, timestamp=None): + """ + 生成sign + :param timestamp: 当前秒级时间戳, 为int类型 + :param token: token, 为str类型 + :return: 时间戳和sign组成的字典 + """ + timestamp = timestamp or int(time()) # 获取当前的时间戳 + prefix_50_token = token[:50] # 获取token前50位 + message = prefix_50_token + str(timestamp) # 将token前50位与时间戳字符串进行拼接 + sign = cls.to_encrypt(message) # 生成sign + + return {"timestamp": timestamp, "sign": sign} + + +if __name__ == '__main__': + pass + # my_token = "eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjI2NSwiZXhwIjoxNTc0NjY3MjMzfQ.ftrNcidmk_zxwl0wzdhE5_39bsGlILoSSoTCy043fjhbjhCFG4FwCnOj4iy5svbDlSbgCJM3qRa1zsXJLJmH4A" + # cryto_info = HandleSign.generate_sign(my_token) + # print(cryto_info) + # s = "1234567" + # + # res = HandleSign.to_encrypt(s) + + diff --git a/python27Class/unitTest/class04142020/接口断言.py b/python27Class/unitTest/class04142020/接口断言.py new file mode 100644 index 0000000..6b59d18 --- /dev/null +++ b/python27Class/unitTest/class04142020/接口断言.py @@ -0,0 +1,78 @@ +""" +============================ +Author:柠檬班-木森 +Time:2020/4/14 20:04 +E-mail:3247119728@qq.com +Company:湖南零檬信息技术有限公司 +============================ +""" + +# 邮箱已注册 +expected = { + "email": [ + "此邮箱已注册" + ] +} + +res = { + "email": [ + "此邮箱已注册" + ] +} + +# assert expected == res + +# 注册成功的情况 +expected1 = { + "username": "musenww01" +} + +# 实际结果 +res1 = { + "id": 1659, + "username": "musenww01", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxNjU5LCJ1c2VybmFtZSI6Im11c2Vud3cwMSIsImV4cCI6MTU4Njk1MjMzNSwiZW1haWwiOiJtdXNlbjA4ODJAcXEuY29tIn0.ACuGW5kukoQjlv1K6UE-4er3l7vL57wOvdpgiTrxGi4" +} + + +# assert expected1 in res1 + + +def assert_dict(expected, res): + """ + 自定义 用来对连个字典进行成员运算断言的方法 + :param expected: 预期结果 + :param res: 实际结果 + :return: + """ + for key in expected: + # 判断键是否存在,键对应的值也相等 + if key in res.keys() and expected[key] == res[key]: + # 这个键对应的值是否一致,断言通过 + pass + else: + raise AssertionError("断言不通过") + + +# +# assert_dict(expected1, res1) +# assert_dict(expected, res) + +li = [1, 2, 3,555] +li2 = [12, 3, 4, 521, 1, 2, 3] +# assert li in li2 + +def list_in(list1,list2): + """ + :param list1: 预期 + :param list2: 实际 + :return: + """ + for li in list1: + if li in list2: + pass + else: + raise AssertionError("{} not in{},断言不通过".format(list1,list2)) + + +list_in(li,li2) \ No newline at end of file