作业1
企业微信的部门管理,根据PO封装思想,编写完整的测试用例
- 部门查询
- 部门创建
- 部门更新
- 部分删除
企业微信的部门管理,根据PO封装思想,编写完整的测试用例
至少回复八个汉字
作业11111111
占楼,最少8个字
作业地址:
https://github.com/Tiffanyfei/homework_hgws.git
department.py
import requests
from test_requests.test_wework.api.wework import WeWork
class Department(WeWork):
"""部门管理"""
secret = "G1bKCaizwcnHww7K4RfHEVpYMnzdSa141jGujwQcU1g"
def create(self, name='', parentid=1, **kwargs):
"""创建部门"""
data = {'name': name, 'parentid': parentid}
data.update(kwargs)
url = 'https://qyapi.weixin.qq.com/cgi-bin/department/create'
r = requests.post(
url,
params={'access_token': self.get_token(self.secret)},
json=data
)
self.format(r)
return r.json()
def list(self, **kwargs):
"""获取部门列表"""
params={'access_token': self.get_token(self.secret)}
params.update(kwargs)
url='https://qyapi.weixin.qq.com/cgi-bin/department/list'
r=requests.get(url=url,params=params)
self.format(r)
return r.json()
def update(self,id:int,**kwargs):
"""修改部门"""
data = {'id': id}
data.update(kwargs)
url = 'https://qyapi.weixin.qq.com/cgi-bin/department/update'
r = requests.post(
url,
params={'access_token': self.get_token(self.secret)},
json=data
)
self.format(r)
return r.json()
def delete(self,id:int):
"""删除部门"""
params = {'access_token': self.get_token(self.secret),'id':id}
url = 'https://qyapi.weixin.qq.com/cgi-bin/department/delete'
r = requests.get(url=url, params=params)
self.format(r)
return r.json()
用例test_department.py
from test_requests.test_wework.api.department import Department
class TestDepartment:
@classmethod
def setup(cls):
cls.department = Department()
def test_department_create_1(self):
"""
测试添加部门
:return:
"""
r = self.department.create(name='test1', parentid=1)
assert r['errcode'] == 0
assert r['errmsg'] == "created"
assert r['id'] is not None
def test_department_create_2(self):
"""
测试添加名称重复
:return:
"""
r = self.department.create(name='test1', parentid=1)
assert r['errcode'] != 0
assert 'department existed' in r['errmsg']
def test_department_create_3(self):
"""
测试部门名称为空
:return:
"""
r = self.department.create(name='', parentid=1)
assert r['errcode'] != 0
assert 'name exceed min utf8 words 1' in r['errmsg']
def test_department_create_4(self):
"""
测试添加二级子部门
:return:
"""
r = self.department.create(name='test2', parentid=13)
assert r['errcode'] == 0
assert r['errmsg'] == "created"
assert r['id'] is not None
def test_department_create_5(self):
"""
parentid为空
:return:
"""
r = self.department.create(name='测试开发', parentid=None)
assert r['errcode'] != 0
assert 'parent department not found' in r['errmsg']
def test_department_list_1(self):
"""
测试部门查询
:return:
"""
r = self.department.list()
assert r['errcode'] == 0
assert r['errmsg'] == 'ok'
assert len(r['department']) > 0
def test_department_list_2(self):
"""
测试部门查询
:return:
"""
test_id = 3
r = self.department.list(id=test_id)
assert r['errcode'] == 0
assert r['errmsg'] == 'ok'
assert len(r['department']) > 0
for i in r['department']:
if i['id'] != test_id:
assert i['parentid'] % 10 == test_id
else:
assert i['id'] % 10 == test_id
def test_department_update_1(self):
"""修改部门名称"""
test_id = 14
r = self.department.update(id=test_id, name='testupdate')
assert r['errcode'] == 0
assert r['errmsg'] == 'updated'
def test_department_update_2(self):
"""修改父部门id"""
test_id = 14
r = self.department.update(id=test_id, parentid=5)
assert r['errcode'] == 0
assert r['errmsg'] == 'updated'
def test_department_update_3(self):
"""修改order次序值"""
test_id = 5
r = self.department.update(id=test_id, order=100000001)
assert r['errcode'] == 0
assert r['errmsg'] == 'updated'
def test_department_delete_1(self):
"""删除0成员一级子部门"""
test_id = 14
r = self.department.delete(id=test_id)
assert r['errcode'] == 0
assert r['errmsg'] == 'deleted'
def test_department_delete_2(self):
"""删除0成员二级子部门"""
test_id = 15
r = self.department.delete(id=test_id)
assert r['errcode'] == 0
assert r['errmsg'] == 'deleted'
def test_department_delete_3(self):
"""删除含有子部门的部门"""
test_id = 3
r = self.department.delete(id=test_id)
assert r['errcode'] != 0
assert 'department contains sub-department' in r['errmsg']
def test_department_delete_4(self):
"""删除含有子成员的部门"""
test_id = 5
r = self.department.delete(id=test_id)
assert r['errcode'] != 0
assert 'department contains user' in r['errmsg']
作业:
Github 地址: https://github.com/kyleyaodn/testDemo/tree/master/apiTest/test_weichat
weichat_accesstoken.py
import requests
from apiTest.entity.common_tool import Common_Tools
class WeChat_AccessToken():
"""这是一个调用企业微信API 获取tooken的类"""
token_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
corpid = "ww4d32ffac234f3c2a"
corpsecret = None
token = dict()
currentTime = ""
corpsecret_time = None
@classmethod
def wechat_token(cls, corpsecret=corpsecret) -> dict:
'''
根据传入的corpsercet 生成access token,
将 corpseret 和 accress_token 生成键值对, 存入 token 词典里面. 如果 token[corpsecret]不存在, 就生成一个.
:param corpsecret: 特定工具的token
:return: token [corpsecret: access_token]
'''
format_date = Common_Tools()
if corpsecret not in cls.token.keys():
cls.token = cls.generate_token_time(corpsecret)
else:
# print('corpseret is existing value is: \n ' + cls.token[corpsecret])
cls.currentTime = format_date.format_date()
for key in cls.token.keys():
if corpsecret + "_" in key:
cls.corpsecret_time = key
break
if int(cls.currentTime) - int(cls.token[cls.corpsecret_time]) > 20000:
# 当前时间和存储的 corpsecret_time 相比较, 大于 2小时就 生成新token, 新 token 时间
print("need generate new token")
cls.token = cls.generate_token_time(corpsecret)
return cls.token
@classmethod
def get_access_token(cls, corpsecret) -> str:
'''
根据传入的corpsercet 生成access token
:param corpsecret: 特定工具的token
:return: JSON 格式的response
'''
r = requests.get(cls.token_URL, params={"corpid": cls.corpid, "corpsecret": corpsecret})
return r.json()
@classmethod
def generate_token_time(cls, corpsecret):
format_date = Common_Tools()
response = cls.get_access_token(corpsecret)
cls.token[corpsecret] = response["access_token"]
cls.currentTime = format_date.format_date()
cls.corpsecret_time = corpsecret + '_' + cls.currentTime
print(cls.corpsecret_time)
cls.token[cls.corpsecret_time] = cls.currentTime
return cls.token
weiChat_departments.py
from apiTest.test_weichat.apis.weichat_accesstoken import WeChat_AccessToken
import requests
class WeiChat_Departments(WeChat_AccessToken):
corpsecret = "vMrcjC8gjHVqTJYTGhEtJvvTQN6gNMxcbZi89nuIJ1k"
def get_departments(self, id="") -> str:
'''
根据ID 获取department的有数据, 如果ID 为空返回所有
:param id: department ID
:return: department 的response json
'''
departmentUrl = "https://qyapi.weixin.qq.com/cgi-bin/department/list"
token = self.wechat_token(self.corpsecret)
params = {}
params["access_token"] = token[self.corpsecret]
params["id"] = id
r = requests.get(departmentUrl, params=params)
json_data = r.json()
# self.format(r)
return json_data
def add_department(self, new_company_data) -> str:
'''
新增department
:param new_company_data: 新department的数据, 词典类型
:return: 调用接口返回的response
'''
add_department_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/create"
token = self.wechat_token(self.corpsecret)
r = requests.post(add_department_URL, params={"access_token": token[self.corpsecret]}, json=new_company_data)
# print(r.json())
return r.json()
def update_departments(self, update_data) -> str:
'''
更新通讯录
:param update_data: 需要更新的数据
:return: update 接口返回的response JSON
'''
update_department_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/update"
token = self.wechat_token(self.corpsecret)
r = requests.post(update_department_URL,
params={"access_token": token[self.corpsecret]},
json=update_data)
return r.json()
def delete_departments(self, id) -> str:
'''
删除指定ID department
:param id: 指定的department ID
:return:
'''
delete_department_URL = 'https://qyapi.weixin.qq.com/cgi-bin/department/delete'
token = self.wechat_token(self.corpsecret)
params_data = {}
params_data["access_token"] = token[self.corpsecret]
params_data["id"] = id
r = requests.get(delete_department_URL, params=params_data)
return r.json()
test_get_Departments.py
import pytest
import json
from apiTest.test_weichat.apis.weiChat_departments import WeiChat_Departments
class TestDepartment():
@classmethod
def setup_class(cls):
cls.departments = WeiChat_Departments()
gets_data_ids = [(3, "广州研发中心update", "update"), (2, "name", "for new test"), (0, 'order', 100000000),
('sss', "order", 111)]
@pytest.mark.parametrize("data_id,checkParam,exceptValue", gets_data_ids)
def test_get_departments(self, data_id, checkParam, exceptValue):
r = self.departments.get_departments(data_id)
print(r)
# r = json.dumps(r, indent=2)
# print(r)
assert r['errmsg'] == "ok"
for i in range(0, len(r['department'])):
if r['department'][i][checkParam] == exceptValue:
assert True
break
assert False
add_data = [("", ""), ("广州研发中心4", "2")]
@pytest.mark.parametrize("name,parentid", add_data)
def test_add_department(self, name, parentid):
new_company_data = {
"name": name,
"parentid": parentid
}
r = self.departments.add_department(new_company_data)
print(r)
assert r["errmsg"] == "created"
update_date = [(3, '3 test update'), (4, '4 test update'), ("0", 'test update')]
@pytest.mark.parametrize("id,name", update_date)
def test_update_department(self, id, name):
update_data = {
"id": id,
"name": name
}
r = self.departments.update_departments(update_data)
print(r)
assert r["errmsg"] == "updated"
@pytest.mark.parametrize("id", [4])
def test_delete_department(self, id):
id = id
r = self.departments.delete_departments(id)
assert r["errmsg"] == "deleted"
import json
from pprint import pprint
import requests
class Testtoken():
token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
corpid = "ww9b54ae861e3b4722"
corpsecret = "xePMoZM37XZdKIv7ugyiw2mjeKF7ouAb5Boh0Z0eAnk"
token = None
def setup(self):
r = requests.get(
self.token_url,
params={"corpid": self.corpid, "corpsecret": self.corpsecret}
)
print(r.json())
assert r.json()["errcode"] == 0
self.token = r.json()['access_token']
def test_add(self):
r = requests.post(
"https://qyapi.weixin.qq.com/cgi-bin/department/create",
params={"access_token": self.token},
json={"name": "江西聚网科技","parentid": 2}
)
print(r.json())
assert r.json()["errcode"] == 0
def test_search(self):
r=requests.get("https://qyapi.weixin.qq.com/cgi-bin/department/list",
params={"access_token":self.token,"id":3})
print(r.json())
assert r.json()["errcode"]==0
def test_update(self):
r = requests.post("https://qyapi.weixin.qq.com/cgi-bin/department/update",
params={"access_token": self.token},
json={"id":3,"name":"江西汉氏"})
print(r.json())
assert r.json()["errcode"] == 0