def test_postUsingGet_notFound(apiBaseUrl, postEndpoint, apiCode): url = makeUrl(apiBaseUrl, postEndpoint, apiCode) resp = requests.get(url) apitest.verifyResponse(resp, isJson=False, respCodeRange=(404, 404), respEmptyOk=True)
def apiStatus(getStatusUrl, setStatusUrl): # read, remember current status resp = requests.get(getStatusUrl) jresp = apitest.verifyResponse(resp, isJson=True, respCodeRange=(200, 299)) beforeStatus = jresp['Status'] restoreData = { 'Status': beforeStatus, 'CreatedBy': anytest.ID, 'Reason': 'Restore after test' } yield beforeStatus setStatus(setStatusUrl, restoreData, 'Restore status back')
def test_getStatus(getStatusUrl): resp = requests.get(getStatusUrl) jresp = apitest.verifyResponse(resp, isJson=True, respCodeRange=(200, 299)) expectedFields = [ 'Status', 'CreatedAt', 'CreatedBy', 'Reason', # comes from CosmosDB 'DataSetType', '_ts', ] print(f"\ngot status={jresp}") assert set(expectedFields) == set(jresp.keys()), f'resp={resp.text}'
def test_setStatusToggle(apiStatus, getStatusUrl, setStatusUrl): newStatus = not apiStatus creator = anytest.ID statusData = {'Status': newStatus, 'CreatedBy': creator, 'Reason': 'Test'} jresp = setStatus(setStatusUrl, statusData, 'Change status') # verify new status in response assert jresp['Status'] is newStatus, f'resp={jresp}' assert jresp['CreatedBy'] == creator, f'resp={jresp}' # verify new status in a separate API call respGet = requests.get(getStatusUrl) jrespGet = apitest.verifyResponse(respGet, isJson=True, respCodeRange=(200, 299)) assert jrespGet['Status'] is newStatus, f'respGet={jrespGet}' assert jrespGet['CreatedBy'] == creator, f'respGet={jrespGet}'
def test_postInvalidJson(apiBaseUrl, postEndpoint, apiCode, req): url = makeUrl(apiBaseUrl, postEndpoint, apiCode) resp = requests.post(url, json=req) apitest.verifyResponse(resp, isJson=True, respCodeRange=(400, 400))
def test_POST(config, apiUrl, req): resp = requests.post(apiUrl, req) apitest.verifyResponse(resp, isJson=False, respCodeRange=(200, 299))
def test_GET(config, apiUrl): resp = requests.get(apiUrl) apitest.verifyResponse(resp, isJson=True, respCodeRange=(200, 299), respEmptyOk=True)
def setStatus(setStatusUrl, statusData, msg='Set status'): print(f'\n{msg}: request={statusData}') resp = requests.post(setStatusUrl, json=statusData) # print(f"got resp={resp.status_code}: {resp.text}") return apitest.verifyResponse(resp, isJson=True, respCodeRange=(200, 299))