"""Helper utils for integration tests.""" from random import random import re from tests.integration import config from tests.integration.consts import api def validate_response_status(response, expect_status=api.STATUS_OK): """Validate response for expected status and return body as json.""" assert response.status_code == expect_status, \ 'Expected status_code to be {}, was {}, body: {}'.format( expect_status, response.status_code, response.json()) return response.json() def assert_has_keys(source_object, keys_list): """Validate that key exists in object. Args: source_object (dict): object that must contain target keys keys_list (iterable): list of keys to compare """ for key in keys_list: assert key in source_object, \ 'Expected {} key to exist in {}'.format(key, source_object) def pattern_matched(pattern, string): """Validate regexp pattern matching.""" return re.match(pattern, string) def omit_keys(source_object, keys): """Return source_object without keys.""" return {k: value for k, value in source_object.items() if k not in keys} def asset_url(filename): """Return full output url for filename.""" return 'https://{}/{}'.format(config.OUTPUT_PODCAST_CDN, filename) def random_slug(): """Generate a random slug for a new podcast.""" return 'api-tests-slug-{}'.format(str(int(random() * 10000000000000 + 1)))