def check_response_attributes(context): """Check mandatory attributes presence for stack analyses v2 response.""" json_data = get_json_data(context) # Validate root level attributes. check_attribute_presence(json_data, 'version') check_attribute_presence(json_data, 'started_at') check_timestamp(json_data.get('started_at')) check_attribute_presence(json_data, 'ended_at') check_timestamp(json_data.get('ended_at')) check_attribute_presence(json_data, 'external_request_id') check_attribute_presence(json_data, 'registration_status') check_attribute_presence(json_data, 'manifest_file_path') check_attribute_presence(json_data, 'manifest_name') check_attribute_presence(json_data, 'ecosystem') check_attribute_presence(json_data, 'unknown_dependencies') check_attribute_presence(json_data, 'license_analysis') check_attribute_presence(json_data, 'recommendation') check_attribute_presence(json_data, 'registration_link') check_attribute_presence(json_data, 'analyzed_dependencies') # Validate recommendation object. recommendation = json_data['recommendation'] check_attribute_presence(recommendation, 'companion') check_attribute_presence(recommendation, 'manifest_file_path') check_attribute_presence(recommendation, 'usage_outliers')
def find_timestamp_value_under_the_path(context, path): """Check if the value (attribute) can be found in the JSON output.""" jsondata = context.response.json() assert jsondata is not None v = get_value_using_path(jsondata, path) assert v is not None check_timestamp(v)
def check_timestamp_under_path(context, path): """Check the timestamp stored in selected attribute. Check if timestamp value can be found in the JSON response object under the given path. """ jsondata = context.response.json() assert jsondata is not None timestamp = get_value_using_path(jsondata, path) check_timestamp(timestamp)
def check_timestamp_attribute(context): """Check the commited at timestamp in the JSON response.""" response = context.response assert response is not None data = response.json() timestamp = check_and_get_attribute(data, "committed_at") # we are not interested much in +100 offsets etc. template = "2000-01-01 10:20:30" assert len(timestamp) >= len(template), "Invalid timestamp %s" % timestamp t = timestamp[0:len(template)] check_timestamp(t)
def check_job_debug_analyses_report(context): """Check the analyses report returned by job API.""" json_data = context.response.json() assert json_data is not None assert "now" in json_data check_timestamp(json_data["now"]) assert "report" in json_data report = json_data["report"] check_all_report_attributes(report)
def check_report_from_to_dates(report): """Check all attributes stored in 'report' node from the stack analysis.""" assert report is not None from_date = check_and_get_attribute(report, "from") to_date = check_and_get_attribute(report, "to") generated_on = check_and_get_attribute(report, "generated_on") # 'generated_on' attribute should contain a proper timestamp check_timestamp(generated_on) # 'from' and 'to' attributes should contain a date in format YYYY-MM-DD check_date(from_date) check_date(to_date)
def check_package_toplevel_file(context, package, ecosystem): """Check the content of package toplevel file.""" data = context.s3_data check_attribute_presence(data, 'id') assert int(data['id']) check_attribute_presence(data, 'package_id') assert int(data['package_id']) check_attribute_presence(data, 'analyses') check_attribute_presence(data, 'started_at') check_timestamp(data['started_at']) check_attribute_presence(data, 'finished_at') check_timestamp(data['finished_at'])
def check_job_debug_analyses_report(context): """Check the analyses report returned by job API.""" json_data = context.response.json() assert json_data is not None assert "now" in json_data check_timestamp(json_data["now"]) assert "report" in json_data report = json_data["report"] attributes = [ "analyses", "analyses_finished", "analyses_finished_unique", "analyses_unfinished", "analyses_unique", "packages", "packages_finished", "versions" ] for attribute in attributes: assert attribute in report assert int(report[attribute]) >= 0
def check_new_timestamps(context): """Check the timestamps for the package analysis.""" data = context.s3_data # print("\n\nCurrent") # print(data['started_at']) # print(data['finished_at']) check_attribute_presence(data, 'started_at') check_timestamp(data['started_at']) check_attribute_presence(data, 'finished_at') check_timestamp(data['finished_at']) remembered_started_at = parse_timestamp(context.job_timestamp_started_at) remembered_finished_at = parse_timestamp(context.job_timestamp_finished_at) current_started_at = parse_timestamp(data['started_at']) current_finished_at = parse_timestamp(data['finished_at']) assert current_started_at > remembered_started_at, \ "Current metadata are not newer: failed on started_at attributes comparison" assert current_finished_at > remembered_finished_at, \ "Current metadata are not newer: failed on finished_at attributes comparison"
def check_component_core_data(context, package, version, ecosystem, version2=None): """Check the component core data read from the AWS S3 database. Expected format (with an example data): { "analyses": [ "security_issues", "metadata", "keywords_tagging", "digests", "source_licenses", "dependency_snapshot" ], "audit": null, "dependents_count": -1, "ecosystem": "pypi", "finished_at": "2017-10-06T13:41:43.450021", "id": 1, "latest_version": "0.2.4", "package": "clojure_py", "package_info": { "dependents_count": -1, "relative_usage": "not used" }, "release": "pypi:clojure_py:0.2.4", "started_at": "2017-10-06T13:39:30.134801", "subtasks": null, "version": "0.2.4" } """ data = context.s3_data started_at = check_and_get_attribute(data, "started_at") check_timestamp(started_at) finished_at = check_and_get_attribute(data, "finished_at") check_timestamp(finished_at) actual_ecosystem = check_and_get_attribute(data, "ecosystem") assert ecosystem == actual_ecosystem, "Ecosystem {e1} differs from expected " \ "ecosystem {e2}".format(e1=actual_ecosystem, e2=ecosystem) actual_package = check_and_get_attribute(data, "package") assert package == actual_package, "Package {p1} differs from expected " \ "package {p2}".format(p1=actual_package, p2=package) actual_version = check_and_get_attribute(data, "version") assert version == actual_version, "Version {v1} differs from expected " \ "version {v2}".format(v1=actual_version, v2=version) actual_release = check_and_get_attribute(data, "release") release = release_string(ecosystem, package, version) assert actual_release == release, "Release string {r1} differs from expected " \ "value {r2}".format(r1=actual_release, r2=release) # the following attributes are expected to be presented for all component toplevel metadata attributes_to_check = [ "id", "analyses", "audit", "dependents_count", "package_info", "subtasks" ] check_attributes_presence(data, attributes_to_check)
def check_timestamp_in_json_response(context, attribute): """Check if the timestamp stored in given attribute is correct.""" timestamp = context.response.json().get(attribute) check_timestamp(timestamp)