def test_highlighting_report(monkeypatch): monkeypatch.setenv("MYTHX_API_KEY", "foo") submission = SubmissionPipeline({ "test": { "sourcePath": "contracts/SafeMath.sol", "contractName": "SafeMath" } }) submission.reports = { "contracts/SafeMath.sol": DetectedIssuesResponse.from_dict(TEST_REPORT) } submission.generate_highlighting_report() assert submission.highlight_report == { "highlights": { "MythX": { "SafeMath": { "contracts/SafeMath.sol": [[ 0, 23, "yellow", ("SWC-103: A floating pragma is set.\nIt is recommended to make " "a conscious choice on what version of Solidity is used for " 'compilation. Currently multiple versions "^0.5.0" are allowed.' ), ]] } } } } monkeypatch.delenv("MYTHX_API_KEY")
def test_update_report(): client = mock.MagicMock() client.report = lambda x: DetectedIssuesResponse.from_dict(TEST_REPORT) source_to_name = {"contracts/SafeMath.sol": "SafeMath"} highlight_report = { "highlights": { "MythX": { "SafeMath": { "contracts/SafeMath.sol": [] } } } } update_report(client, "foo", highlight_report, {}, source_to_name) assert highlight_report == { "highlights": { "MythX": { "SafeMath": { "contracts/SafeMath.sol": [[ 0, 23, "yellow", 'SWC-103: A floating pragma is set.\nIt is recommended to make a conscious choice on what version of Solidity is used for compilation. Currently multiple versions "^0.5.0" are allowed.', # NOQA: E501 ]] } } } }
def test_stdout_report(monkeypatch): monkeypatch.setenv("MYTHX_API_KEY", "foo") submission = SubmissionPipeline({ "test": { "sourcePath": "contracts/SafeMath.sol", "contractName": "SafeMath" } }) submission.reports = { "contracts/SafeMath.sol": DetectedIssuesResponse.from_dict(TEST_REPORT) } submission.generate_stdout_report() assert submission.stdout_report == {"contracts/SafeMath.sol": {"LOW": 3}} monkeypatch.delenv("MYTHX_API_KEY")
def parse_response(self, resp: dict, model_cls: Type[RESPONSE_MODELS]) -> RESPONSE_MODELS: """Parse the API response into its respective domain model variant. This method takes the raw HTTP response and a class it should deserialize the responsse data into. As each domain model implements the :code:`from_json` method, we simply call it on the raw input data and return the resulting model. If a deserialization or validation error is raised, it is not caught and directly passed on to the user. :param resp: The raw HTTP response JSON payload :param model_cls: The domain model class the data should be deserialized into :return: The domain model holding the response data """ if type(resp) is list and model_cls is DetectedIssuesResponse: m = DetectedIssuesResponse( issue_reports=parse_obj_as(List[IssueReport], resp)) else: m = model_cls(**resp) return self.execute_response_middlewares(m)
def format_detected_issues(resp: DetectedIssuesResponse, inp: AnalysisInputResponse) -> str: """Format an issue report response as compressed JSON.""" return resp.to_json()
], "sourceType": SOURCE_TYPE, "sourceFormat": SOURCE_FORMAT, "sourceList": SOURCE_LIST, "meta": {}, } ISSUE_REPORT_OBJECT = IssueReport( issues=[ISSUE_OBJECT], source_type=SourceType.RAW_BYTECODE, source_format=SourceFormat.EVM_BYZANTIUM_BYTECODE, source_list=SOURCE_LIST, meta_data={}, ) DETECTED_ISSUES_RESPONSE_DICT = {"issueReports": [ISSUE_REPORT_DICT]} DETECTED_ISSUES_RESPONSE_OBJECT = DetectedIssuesResponse( issue_reports=[ISSUE_REPORT_OBJECT] ) def generate_request_dict(req): return { "method": req.method, "payload": req.payload, "params": req.parameters, "headers": req.headers, "url": "https://test.com/" + req.endpoint, }
def test_detected_issues_from_invalid_dict(): with pytest.raises(ValidationError): DetectedIssuesResponse.from_dict({})
def test_detected_issues_from_empty_list(): resp = DetectedIssuesResponse.from_dict([]) assert resp.issue_reports == []
def test_detected_issues_from_list(): resp = DetectedIssuesResponse.from_dict([testdata.ISSUE_REPORT_DICT]) assert_detected_issues(resp)
def test_detected_issues_from_dict(): resp = DetectedIssuesResponse.from_dict( testdata.DETECTED_ISSUES_RESPONSE_DICT) assert_detected_issues(resp)
def test_detected_issues_from_empty_json(): resp = DetectedIssuesResponse.from_json("[]") assert resp.issue_reports == []
def test_detected_issues_from_valid_json(): resp = DetectedIssuesResponse.from_json( # DetectedIssuesResponse, json.dumps(testdata.DETECTED_ISSUES_RESPONSE_DICT)) assert_detected_issues(resp)
def test_detected_issues_from_list(): resp = DetectedIssuesResponse.from_dict([DICT_DATA[0]]) assert_detected_issues(resp)
def test_detected_issues_from_valid_json(): resp = DetectedIssuesResponse.from_json(JSON_DATA) assert_detected_issues(resp)
import pytest from mythx_models.response import ( DetectedIssuesResponse, Issue, IssueReport, Severity, SourceFormat, SourceLocation, SourceType, ) from .common import get_test_case JSON_DATA, DICT_DATA = get_test_case("testdata/detected-issues-response.json") OBJ_DATA = DetectedIssuesResponse.from_json(JSON_DATA) def assert_detected_issues(resp): assert len(resp.issue_reports) == 1 report = resp.issue_reports[0] assert type(report) == IssueReport issue = report.issues[0] assert_issue(issue, DICT_DATA[0]["issues"][0]) def assert_issue(issue: Issue, data: Dict, skip_decoded: bool = False): assert issue.swc_id == data["swcID"] assert issue.swc_title == data["swcTitle"] assert issue.description_short == data["description"]["head"] assert issue.description_long == data["description"]["tail"]