def request_api(url, payload=None, method="POST",headers=None,urlparams=None, **kwargs): """ This method is to post/get request and return response in dict format. :param url: request url :param payload: request payload :param method: type of method (POST and GET Supported) :param kwargs: kwargs to pass other params like timeout,auth,cert :return: dict of response """ if method == "POST": resp = requests.post( url=url, data=payload, headers=headers, params=urlparams, **kwargs ) else: resp = requests.get(url=url, headers=headers,params=urlparams, **kwargs) synthassert( resp.status_code == 200, message="status code mismatched", response=resp ) return resp
def test_wtwn_third_level(self, request, record_xml_attribute, device_domain, generate_mind_url, generate_json_headers): depends(request, ["TestWtwn::test_wtwn_second_level[{}]".format(device_domain)]) device_domain_config = self.get_device_config(request, record_xml_attribute, device_domain) rootcaption = device_domain_config.get("rootCaption") for thirdLevelFeedItem in TestWtwn.thirdLevelFeedItems[rootcaption]: time.sleep(1) response = self.base.request_api( url=generate_mind_url, payload=json.dumps( self.base.wtwn_payload( device=device_domain_config.get("deviceType"), feedname=thirdLevelFeedItem, device_domain_config=device_domain_config)), headers=generate_json_headers, urlparams={ "type": "feedItemFind", "bodyId": device_domain_config.get("bodyId") }) try: self.base.assert_feeditemfind_response(response) except ValueError: synthassert(False, message="Decoding JSON from the response failed", response=response)
def test_clipMetadataRemove(self, setup_content, generate_middleMind_url, generate_default_headers, generate_clipMetadataRemove_params): contentId=setup_content if contentId == None or not Testadskip.previousTestPassed: pytest.skip('\nTest Depencency Issue' "\nCouldn't store content object or clipMetadataStore failed") Testadskip.previousTestPassed = False payload = self.payloads.payload_clipMetadataRemove() response = requests.post(generate_middleMind_url, headers=generate_default_headers, params=generate_clipMetadataRemove_params, data=payload) synthassert(response.status_code == 200, message="Status code error\nExpected: '{}'\nActual: '{}'\nReason: '{}'".format( 200, response.status_code, response.reason), response=response) try: json_data = response.json() synthassert("success" == json_data["type"], message="Returned JSON has incorrect data\nExpected: 'success'\nActual: '{}'".format(json_data), response=response) except json.JSONDecodeError: synthassert(False, message="Decoding JSON from the response failed", response=response) except KeyError as e: synthassert(False, message="Missing key while parsing the json response. Details:" + str(e), response=response) Testadskip.previousTestPassed = True
def test_wtwn_second_level(self, request, record_xml_attribute, device_domain, generate_mind_url, generate_json_headers): depends(request, ["TestWtwn::test_wtwn_root_level[{}]".format(device_domain)]) device_domain_config = self.get_device_config(request, record_xml_attribute, device_domain) rootcaption = device_domain_config.get("rootCaption") for feedItem in TestWtwn.feedItems[rootcaption]: response = self.base.request_api( url=generate_mind_url, payload=json.dumps( self.base.wtwn_payload( device=device_domain_config.get("deviceType"), feedname=feedItem, device_domain_config=device_domain_config)), headers=generate_json_headers, urlparams={ "type": "feedItemFind", "bodyId": device_domain_config.get("bodyId") }) try: json_data = response.json() self.base.assert_feeditemfind_response(response) synthassert("items" in json_data, message="Items attribute not present in response", response=response) for item in json_data["items"]: self.base.assert_feedname_in_response(response, item) feedname = item["kernel"]["expandedFeedAction"]["feedName"] if feedname == "/liveTvApps": continue TestWtwn.thirdLevelFeedItems.setdefault( rootcaption, []).append(feedname) except ValueError: synthassert(False, message="Decoding JSON from the response failed", response=response)
def test_clipMetadataSearch(self, setup_content, generate_middleMind_url, generate_clipMetadataSearch_params, generate_default_headers, expected_clipMetadataSearch): contentId=setup_content if contentId == None or not Testadskip.previousTestPassed: pytest.skip('\nTest Depencency Issue' "\nCouldn't store content object or clipMetadataStore failed") payload = self.payloads.payload_clipMetadataSearch(contentId) maxLoops = config["maxLoops"] delay = config["delay"] success = False loops = 0 while not success: response = requests.post(generate_middleMind_url, headers=generate_default_headers, params=generate_clipMetadataSearch_params, data=payload) synthassert(response.status_code == 200, message="Status code error\nExpected: '{}'\nActual: '{}'\nReason: '{}'".format( 200, response.status_code, response.reason), response=response) json_data = response.json() if "clipMetadata" in json_data: success = check_ignore_missing(json_data['clipMetadata'][0], expected_clipMetadataSearch) if success: break else: loops+=1 if loops == maxLoops: break else: time.sleep(delay) synthassert(success, message="Returned JSON has incorrect data\nExpected: '{}'\nActual: '{}'".format( expected_clipMetadataSearch, json_data), response=response)
def assert_feeditemfind_response(response): """ This method validates some fields on response of feedItemFind api :param response: feedItemFind api response as response object :return: None """ json_data = response.json() synthassert( "type" in json_data, message="Type attribute missing in response!", response=response) synthassert( json_data["type"] == "feedItemResults", message="Type attribute value is not feedItemResults in response", response=response) synthassert( "feedItemFindCallId" in json_data, message="feedItemFindCallId attribute missing in response!", response=response) synthassert( json_data["feedItemFindCallId"] != "dummyQueryId", message="feedItemFindCallId attribute value is dummyQueryId", response=response)
def assert_feedname_in_response(response, response_item): """ This method checks the existence of feedname attribute in an item in response passed :param response: feedItemFind api response as response object :param response_item: individual item in response :return: None """ synthassert( "kernel" in response_item, message="Missing kernel attribute in response.", response=response) synthassert( "expandedFeedAction" in response_item["kernel"], message="Missing expandedFeedAction attribute in response.", response=response) synthassert( "feedName" in response_item["kernel"]["expandedFeedAction"], message="Missing feedName attribute in response.", response=response)
def setup_content(generate_middleMind_url, generate_appserver_url, generate_default_headers): response = requests.post(generate_middleMind_url, headers=generate_default_headers, params=generate_collectionStore_params(), data=payload_collectionStore()) try: synthassert( response.status_code == 200, message="collectionStore: Status code is not 200. Status code is " + str(response.status_code)) except AssertionError as e: print(e) collectionId = None contentId = None else: try: json_data = response.json() collectionId = json_data["collectionId"] except (json.JSONDecodeError, KeyError): try: synthassert(False, message="collectionStore error", response=response) except AssertionError as e: print(e) collectionId = None contentId = None if collectionId != None: response = requests.post(generate_middleMind_url, headers=generate_default_headers, params=generate_contentStore_params(), data=payload_contentStore(collectionId)) try: synthassert( response.status_code == 200, message="contentStore: Status code is not 200. Status code is " + str(response.status_code)) except AssertionError as e: print(e) contentId = None else: try: json_data = response.json() contentId = json_data["contentId"] except (json.JSONDecodeError, KeyError): try: synthassert(False, message="contentStore error", response=response) except AssertionError as e: print(e) contentId = None yield contentId # Yield has the same effect as return, but code following it is teardown code. # Teardown of content response = requests.post(generate_appserver_url, headers=generate_default_headers, params=generate_contentRemove_params(), data=payload_contentRemove(contentId)) # Teardown of collection response = requests.post(generate_appserver_url, headers=generate_default_headers, params=generate_collectionRemove_params(), data=payload_collectionRemove(collectionId))