pytest_api/test_case/dmglobal/conftest.py

65 lines
2.0 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
import requests
from tools.cache_control import Cache
@pytest.fixture(scope="session", autouse=True)
def change_env():
"""
获取登录的cookie
:return:
"""
from config.configs import Config
try:
testing_evn = Config().__getattr__("DAV_ENVIRONMENT_SLUG")
if "-" in testing_evn:
list_evn = testing_evn.split("-")
cur_evn = list_evn[0]
else:
cur_evn = testing_evn
except:
cur_evn = Config().__getattr__("CI_ENVIRONMENT_SLUG")
host = str(Config().__getattr__("hostbus_global"))
login_url = host + '/v1/mapping/update'
vin = Cache('vin_global').get_cache()
params = {'vin':vin,'environment':cur_evn}
headers = {"Content-Type": "application/json"}
try:
res = requests.post(url=login_url, json=params, headers=headers).json()
print("切换环境成功,当前环境为%s"%cur_evn)
except Exception as e:
raise e
#
@pytest.fixture(scope="session", autouse=True)
def work_login_init():
"""
获取登录的cookie
:return:
"""
from config.configs import Config
host = str(Config().get_host11())
user = str(Config().__getattr__("dm_global_phone"))
pwd = str(Config().__getattr__("dm_global_password"))
login_url = host + '/auth/oauth/token'
params = {'username': user,
'password': pwd,
'grant_type': 'password',
'client_id': 'client',
'client_secret': 'AlplxNdEhtLVaVenq4A'}
headers = {"Content-Type": "application/json"}
try:
res = requests.post(url=login_url, params=params, headers=headers).json()
print(res)
token = res['data']['accessToken']
ref = res['data']['refreshToken']
print(token)
Cache('login_token').set_caches(token)
Cache('refreshToken').set_caches(ref)
except Exception as e:
raise e
# # work_login_init()
# change_env()