【拉勾三期作业贴】企业微信接口实战(一)

  1. 实现部门管理,包括增删改查

https://github.com/niujiama/LilyGithub.git

位置:testAPIs2–test_work_depart.py

import requests

corpid = 'wwc72ee4311e0af881'  # 企业ID
corpsecret = 'c7yr-vMycD18YcUVTb5iRkb1tPxkPOBnqz5UyymmHv8'   # 通讯录页面的Secret
# 获取token信息
def get_token():
    url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}'
    result = requests.get(url).json()
    return result['access_token']

# 创建部门
def test_department_add():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={get_token()}"
    data = {
        "name": "子部门",
        "parentid": 2
    }
    print(requests.post(url, json=data).json())

# 更新部门
def test_department_update():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={get_token()}"
    data = {
        "id": 5,
        "name": "子部门_1"
    }
    print(requests.post(url, json=data).json())

# 删除部门
def test_department_delete():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={get_token()}&id=7"
    res = requests.get(url).json()
    assert res["errcode"] == 0

# 获取部门列表
def test_get_department():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={get_token()}&id=2"
    print(requests.get(url).json())

https://github.com/realtalk1007/hogwarts/tree/master/test_http

from jsonpath import jsonpath
import requests


class TestMenber:
    _de_name = "霍格沃兹研发中心"
    _de_name_en = "Hogwarts"
    _de_id = 999

    def get_url(self, func):
        return f"https://qyapi.weixin.qq.com/cgi-bin/department/{func}?access_token={self.get_token()}"

    def get_token(self):
        corpid = "ww5b005eced8645715"
        corpsecret = "UZu4N8ruskIwKmofJI0s9B5Gr_HQsUxrmQkWC7CpdAE"
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
        token = requests.get(url).json()["access_token"]
        return token

    def test_add(self):
        data = {
            "name": f"{self._de_name}",
            "name_en": f"{self._de_name_en}",
            "parentid": 1,
            "order": 1,
            "id": self._de_id
        }
        result = requests.post(self.get_url("create"), json=data)
        print(result.json())
        assert jsonpath(result.json(), "$.errmsg")[0] == 'created'

    def test_get(self):
        result = requests.get(self.get_url("list"), params={"id": self._de_id})
        print(result.json())
        print(result.url)
        assert jsonpath(result.json(), "$.errmsg")[0] == 'ok'

    def test_update(self):
        data = {
            "id": self._de_id,
            "name": f"{self._de_name}—编辑",
        }
        result = requests.post(self.get_url("update"), json=data)
        print(result.json())
        assert jsonpath(result.json(), "$.errmsg")[0] == 'updated'

    def test_delete(self):
        result = requests.get(self.get_url("delete"), params={"id": self._de_id})
        print(result.json())
        assert jsonpath(result.json(), "$.errmsg")[0] == 'deleted'

import requests
import pytest
from faker import Faker
import json

ip=‘https://qyapi.weixin.qq.com

@pytest.fixture(autouse=True)
def token():
corpid=‘ww9d15d2c2087d997e’
corpsecre=‘tTVu4kSZ47qpvuKD6ng6VE_mxATs73KbdAnm54y6GWU’
route=f’/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecre}’
re=requests.get(ip+route)
return re.json()[‘access_token’]

class TestCase():

 def setup(self):
     self.faker=Faker(locale='zh_CN')
     self.data = {
          "userid": f'{self.faker.random_number(digits=7)}',
          "name": f'{self.faker.name()}',
          "mobile": f'{self.faker.phone_number()}',
          "department": [2],
     }


 def teardown(self):
      pass


 def test_add(self,token):
      print(f'我添加了{self.data["name"]}')
      route=f'/cgi-bin/user/create?access_token={token}'
      res=requests.post(ip+route,json=self.data).json()
      print(json.dumps(res,ensure_ascii=False, sort_keys=True, indent=4,))



 def test_delete(self,token):
     route=f'/cgi-bin/user/simplelist?access_token={token}&department_id=2&fetch_child=0'
     res=requests.get(ip+route).json()
     del_route=f'/cgi-bin/user/delete?access_token={token}&userid={res["userlist"][1]["userid"]}'
     print(f'我删除了{res["userlist"][1]["name"]}')
     print(requests.get(ip + del_route).json())

先这样,有时间了再加上数据驱动
https://github.com/shifangxue/Hogwarts_LG3

https://github.com/tina1tang/huogewozi_study/tree/master/homework_interface_test

https://github.com/Galaxyfanfan/HogwartsAppiumCode/tree/master/WeChatDepJson

https://github.com/shadingyu96/Shadingyu.git

def getToken():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
    result = requests.get(url).json()
    # print(result["access_token"])
    return result["access_token"]


def test_getDep():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={getToken()}&id=1"
    r = requests.get(url).json()
    print(r)
    assert r["errmsg"] == "ok"


def test_createDep():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={getToken()}"
    depData = {
        "name": "测试一部",
        "name_en": "CSYB",
        "parentid": 2,
        "order": 2,
        "id": 3
    }
    r = requests.post(url, json=depData).json()
    print(r)
    assert r["errmsg"] == "created"


def test_updateDep():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={getToken()}"
    depData = {
        "id": 3,
        "name": "测试二部",
        "name_en": "CSEB",
        "parentid": 2,
        "order": 2
    }
    r = requests.post(url, json=depData).json()
    print(r)
    assert r["errmsg"] == "updated"


def test_delDep():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={getToken()}&id=3"
    r = requests.get(url).json()
    print(r)
    assert r["errmsg"] == "deleted"

https://github.com/JuliaZxr/hogwarts_lg3_Yuki.git

‘’’
作业:实现部门管理,包括增删改查

‘’’

import requests

def get_token():
corpid=“wwbebf98fa66fd7a94”
corpsecret=“ZX7myPtY825h4G1qZeZlmUEVSDtx2ZAiG64CU0oJOvE”
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
r = requests.get(url)
result = r.json()
return result[“access_token”]

#获取部门
#access_token用来传密钥
def test_get_depart():
token = get_token() #先获取身份信息
departid=“3”
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={token}&id={departid}" #看接口文档传参数
r = requests.get(url)
print(r.json())

#创建部门
def test_add_depart():
url=f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={get_token()}"
data={
“name”: “北京研发中心”,
“parentid”: 3,
“id”: 7
}
r = requests.post(url, json=data)
print(r.json())

#删除部门
def test_del_depart():
url=f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={get_token()}&id=7"
r = requests.get(url)
print(r.json())

#更新部门
def test_update_depart():
url=f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={get_token()}"
data={
“id”:7,
“name”:“大数据测试部门”
}
r = requests.post(url,json=data)
print(r.json())

https://github.com/qblslion/qccatni/tree/master/python_practice/python_commit_11_api

  • 企业微信接口测试作业(部门管理)

···
“”"
@description: 测试企业微信部门管理接口
@author: 29263
@date: 2020-09-25 10:10
“”"
import shelve
import requests
import pytest

@pytest.fixture()
def prepare():
    """
    从数据库读取access_token,判读是否有效,否则进行更新
    :return:
    """
    corpid = "ww8ec10aac089b8d7d"
    corpsecret = "JDViKkbe3hntOoM4E7YgInDc9AkbIrSPDwCtb0BtKmQ"
    token_file = "wework"
    with shelve.open(token_file, "c") as conn:
        global access_token
        access_token = conn.get("access_token")
        if access_token is None:
            data = requests.get(f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}").json()
            if data["errcode"] == 0:
                conn["access_token"] = data["access_token"]
                access_token = data["access_token"]
            else:
                raise Exception("获取token失败")


@pytest.mark.usefixtures("prepare")
class TestDept:

    url = ""  # 请求路径

    @pytest.mark.run(order=1)
    @pytest.mark.parametrize("ID", [4])
    def test_dept_creat(self, ID):
        """
        新增部门信息
        :return:
        """
        self.url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={access_token}"
        dept = {
            "name": "销售部",
            "name_en": "SALE",
            "parentid": 1,
            "order": 1,
            "id": ID
        }
        data = requests.post(self.url, json=dept).json()
        assert data["errcode"] == 0

    @pytest.mark.run(order=2)
    @pytest.mark.parametrize("ID", [4])
    def test_dept_update(self, ID):
        """
        更新部门信息
        :return:
        """
        self.url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={access_token}"
        dept = {
            "id": ID,
            "name": "新销售部",
            # "name_en": "SALE",
            # "parentid": 1,
            # "order": 1,
        }
        data = requests.post(self.url, json=dept).json()
        assert data["errcode"] == 0

    @pytest.mark.run(order=3)
    @pytest.mark.parametrize("ID", [4])
    def test_dept_get(self, ID):
        """
        获取部门信息
        :return:
        """
        self.url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={access_token}&id={ID}"
        data = requests.get(self.url).json()
        print(data)
        assert data["errcode"] == 0

    @pytest.mark.run(order=4)
    @pytest.mark.parametrize("ID", [4])
    def test_dept_deleteByID(self, ID):
        """
        根据ID删除部门信息
        :return:
        """
        self.url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={access_token}&id={ID}"
        data = requests.get(self.url).json()
        assert data["errcode"] == 0

···

https://github.com/Lei666000/huogewozi02

import requests

secret = “TWe_VHV7KXkdNUYL3R3cFP00PbsKcuvVcvnRgKvQjto”
corpid = “wweb3d2aeb84039a81”

def get_token():
#获取验证信息
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}"
r = requests.get(url=url).json()
return r[“access_token”]

def test_incre_depart():
#添加部门
data={
“name”:“测试开发研发部”,
“parentid”: 1,
“id”: 2
}
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={get_token()}"
result = requests.post(url = url, json = data).json()
print(result)

def test_update_depart():
#更新部门信息
data = {
“id”: 1,
“name”: “测试开发部”
}
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={get_token()}"
result = requests.post(url=url,json=data).json()
print(result)

def test_del_depart():
#删除部门
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={get_token()}&id=2"
result = requests.get(url=url).json()
print(result)

def test_getlist_derpart():
#获取部门列表
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={get_token()}&id=ID"
result = requests.get(url=url).json()
print(result)

https://github.com/linpeijie-python/linpeijiecode/tree/master/APItest/department

import requests
corpid = "ww7498486774626c7d"
corpsecret = "KfBc78vzqdYYCvFC4Wh-rJO59bCAEh43S51TnopIZQo"


def get_token():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
    result = requests.get(url).json()
    return result["access_token"]

def test_add():
    token = get_token()
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={token}"
    data = {
       "name": "测试组",
       "parentid": 1
    }
    print(requests.post(url, json=data).json())

def test_updata():
    tokoen = get_token()
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={tokoen}"
    data = {
        "id": 2,
        "name": "更新测试组"
    }
    print(requests.post(url, json=data).json())

def test_delete():
    token = get_token()
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={token}&id={2}"
    print(requests.get(url).json())

def test_get_apartment():
    token = get_token()
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={token}&id={1}"
    print(requests.get(url).json())

import requests

corpid=“wwe653983e4c732493”
corpsecret=“T72_Vgw9TaNS-FLDU2gJlw6AteerMXsuMval9kGNZbc”

‘’‘获取token’‘’
def get_token():
url=f" https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"

req=requests.get(url).json()
token=req["access_token"]
return token

def get_ip():

url=f’https://qyapi.weixin.qq.com/cgi-bin/get_api_domain_ip?access_token={get_token()}

print(requests.get(url).json())

‘’‘创建成员’‘’
def create_user():
url=f’https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={get_token()}
body=‘’'{
“userid”: “jinmuyan”,
“name”: “金木研”,
“mobile”:13399999999,
“department”:[1]

}'''
print(requests.post(url=url, data=body.encode('utf-8')).json())

‘’‘读取成员’‘’
def get_user(userid):
url=f’https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={get_token()}&userid={userid}
print(requests.get(url).json())

‘’‘更新成员’‘’

def update_user():
url=f’https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={get_token()}
body=‘’‘{
“userid”: “jinmuyan”,
“name”: “金木”,
“mobile”:13399999999,
“department”:[1]
}’‘’
print(requests.post(url=url, data=body.encode(‘utf-8’)).json())

‘’‘删除成员’‘’

def delete_user(userid):
url=f’https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={get_token()}&userid={userid}
print(requests.get(url=url).json())

https://github.com/len-zhang/api_test_framework.git

-- coding: UTF-8 --

“”"
获取access_token
请求方式:GET(HTTPS)
请求URL:https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
“”"
import requests

corpid=‘ww5fcfd4d37ecb1852’
corpsecret=‘Y0gDaB1xqsLzQKyVyEzxp59cBVWzd_iNUWR8WaUM6Lc’
def get_token():
url=f’https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}
r=requests.get(url)
result=r.json()
token = result[‘access_token’]
return token
“”"
创建部门
请求方式:POST(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN
“”"

def test_creatdepartment():
token=get_token()
url=f’https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={token}
data={
“name”: “广州研发中心”,
“name_en”: “RDGZ”,
“parentid”: 1,
“order”: 1,
“id”: 4
}
r=requests.post(url,json=data)
print(r.json())

“”"
更新部门
请求方式:POST(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=ACCESS_TOKEN
“”"
def test_updatedepartment():
token=get_token()
url=f’https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={token}
data={
“id”: 4,
“name”: “北京研发中心”,
“name_en”: “RDGZ”,
“parentid”: 1,
“order”: 1
}
r=requests.post(url,json=data)
print(r.json())

“”"
获取部门列表
请求方式:GET(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID
“”"
def test_getdepartmentlist():
token = get_token()
url = f’https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={token}
r =requests.get(url)
print(r.json())

“”"
删除部门
请求方式:GET(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=ID
“”"
def test_deletedepartment():
token = get_token()
url = f’https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={token}&id=4
r=requests.get(url)
print(r.json())