一.接口鉴权的多种方式
1.cookie鉴权: 携带身份信息请求认证之后的每次请求都携带cookie信息
cookie记录在请求头中
2.token鉴权: 携带身份信息请求认证之后的每次请求都携带token认证信息
可能记录在请求头,可能记录在url参数中
3.auth鉴权: 每次请求携带用户的username和password,并对其信息加密
4.oauth2(选修)鉴权: 携带身份信息请求认证服务端向指定回调地址回传code
通过code获取token之后的请求信息都携带token。
二.cookie鉴权
1.cookie的获取(根据接口文档获取),发送携带cookie的请求直接通过cookies 参数,通过Session()对象
class TestWithSession:
proxy = {"http": "http://127.0.0.1:8888", "https": "https://127.0.0.1:8888"}
req = requests.Session()
def setup_class(self):
url = "http://train-manage.atstudy.com/login"
data = {"username": "199****9999", "password": "a1***56"}
resp = self.req.request("post", url, data=data, proxies=self.proxy)
print(self.req.headers)
def test_get_userinfo(self):
url = "http://train-manage.atstudy.com/api/manage/User/Info"
resp = self.req.request("get", url, proxies=self.proxy)
print(resp.text)
def test_manage_tag(self):
url = "http://train-manage.atstudy.com/api/manage/Tag?type=1"
resp = self.req.request("get", url, proxies=self.proxy)
print(resp.text)
三.token鉴权
1.token的获取(根据接口文档获取),发送携带token的请求(根据接口文档获取)
class TestWithToken:
proxy = {"http": "http://127.0.0.1:8888", "https": "http://127.0.0.1:8888"}
headers = {}
def setup_class(self):
token = self.login().json()["data"]["token"]
print(token)
self.headers["x-litemall-admin-token"] = token
@classmethod
def login(cls):
url = "https://litemall.hogwarts.ceshiren.com/admin/auth/login"
data = {"username": "hogwarts", "password": "test12345", "code": ""}
resp = requests.request("post", url, json=data, proxies=cls.proxy, verify=False)
return resp
def test_get_dashboard(self):
url = "https://litemall.hogwarts.ceshiren.com/admin/dashboard"
resp = requests.request("get", url, headers=self.headers, proxies=self.proxy, verify=False)
print(resp.text)
# print(1)
def test_category_list(self):
url = "https://litemall.hogwarts.ceshiren.com/admin/category/list"
resp = requests.request("get", url, headers=self.headers, proxies=self.proxy, verify=False)
print(resp.text)
四.加密与解密
1.在得到响应后对响应做解密处理:
1.1.如果知道使用的是哪个通用加密算法的话,可以自行解决。
1.2.如果不了解对应的加密算法的话,可以让研发提供加解密的lib
1.3.如果既不是通用加密算法、研发也无法提供加解密的lib的话
1.3.1.可以让加密方提供远程解析服务,这样算法仍然是保密的
2.实战实例
2.1.调用python自带的base64,直接对返回的响应做解密,即可得到解密后的响应
2.1.1.封装对于不同算法的处理方法
class TestEncode:
def test_decode(self):
url = "http://127.0.0.1:9999/demo.txt"
res = requests.request("get",url)
print(res.content)
de_res = base64.b64decode(res.content)
print(json.loads(de_res))
五.数据库操作与断言
1.接口测试验证响应内容:如何在测试过程中验证接口没有Bug?
1.1.通过接口响应值; 通过查询数据库信息辅助验证
2.接口测试数据清理:自动化测试中会产生大量的脏数据,该如何清理?
2.1.调用delete接口删除 自动化使用干净的测试环境,每次自动化测试完成后,还原数据
3.数据库操作注意事项
3.1.直接对数据库操作是非常危险的行为
3.1.1.权限管理严格的公司对数据库权限给的很低
3.1.2.表结构复杂,随便删除数据会影响功能异常,甚至会出现系统异常
4.接口自动化测试常用的数据库操作
4.1.连接与配置,查询数据与断言
5.数据库封装:封装数据库配置,封装SQL查询操作,调用方法执行SQL语句
class Mysql_DB:
@classmethod
def connect(cls):
mysql_info = {
"host": "litemall.hogwarts.ceshiren.com",
"port": 3306,
"user": "test",
"password": "test123456",
"database": "litemall",
"charset": "utf8mb4"
}
conn = pymysql.Connect(**mysql_info)
return conn
@classmethod
def execute(cls, sql):
cnn = cls.connect()
sor = cnn.cursor()
sor.execute(sql)
res = sor.fetchall()
sor.close()
cnn.close()
return res
6.数据库断言
def test_cart_add(self, good_sn='ZZ110120'):
"""测试添加购物车功能"""
with allure.step("获取商品的id"):
ids = self.good.id(good_sn)
with allure.step("获取商品的good_id和product_id"):
good_id = ids[0]
product_id = ids[1]
sql = f"""
SELECT number FROM litemall_cart where goods_sn='{good_sn}' and goods_id='{good_id}'
"""
good_num1 = Mysql.execute(sql)[0][0]
print(good_num1)
with allure.step("添加商品到购物车"):
print(self.cart.add(good_id, product_id))
good_num2 = Mysql.execute(sql)[0][0]
print(good_num2)
assert good_num2 - good_num1 == 1