def test_iterate_endpoints_uri_methods(config): fuzzer = Fuzzer( config_obj=config, domain=domain, global_timeout=True, timeout=5, methods=["GET", "POST"], uri="/multiple", ) n_times = 1 expected_n_summaries = _get_n_expected_summaries( fuzzer.model_obj["endpoints"], n_times, fuzzer.uri, fuzzer.methods) summaries = fuzzer.iterate_endpoints() assert ( len(summaries) == expected_n_summaries ), f"should only iterate {expected_n_summaries} times over all endpoints with methods {fuzzer.methods}" for summary in summaries: assert ( summary.method in fuzzer.methods ), f"expected iteration {json.dumps(summary)} to contain one of methods {fuzzer.methods}" placeholder = "{otherId}" original_uri = "/" + placeholder expected_constant = "shoop" expected_uri = "/" + expected_constant fuzzer = Fuzzer( config_obj=config, domain=domain, constants={placeholder: expected_constant}, uri=original_uri, ) summaries = fuzzer.iterate_endpoints() assert expected_uri in json.dumps([ str(summary) for summary in summaries ]), ( f"should find a request with uri {original_uri} that was changed to {expected_uri} after injecting {expected_constant} " "as a constant")
def test_slack_error_throttle(config, mocker): mock_summary = Summary( method="GET", headers={"X-fuzzeREST-State": 0}, body={}, delay=0, timestamp=1, url="http://nowhere", ) mock_summary.status_code = 200 mocker.patch.object(request, "send_request", return_value=mock_summary) fuzzer = Fuzzer( config_obj=config, domain=domain, global_timeout=True, timeout=5, uri="/query/string", ) expected_errors = fuzzer.slack_errors + 1 fuzzer.last_hour = time.localtime().tm_hour fuzzer.iterate_endpoints() assert fuzzer.slack_errors == expected_errors, "should increment by 1" fuzzer.slack_errors = config.slack_errors_per_hour expected_errors = fuzzer.slack_errors fuzzer.last_hour = time.localtime().tm_hour fuzzer.iterate_endpoints() assert fuzzer.slack_errors == expected_errors, ( "should match because errors per hour limit was reached", ) fuzzer.last_hour += 1 fuzzer.iterate_endpoints() expected_errors = 1 assert fuzzer.slack_errors == expected_errors, ( "should reset to 0 and increment to 1 because hour changed", )
def test_get_states_from_file(config): expected_states = [234, 812, 1, 999909, 234, 22222893428923498, 9] states = Fuzzer.get_states_from_file(config.states_file) assert states == expected_states, ("states should have loaded from " + config.states_file)
def fuzzer(config): return Fuzzer(config_obj=config, domain=domain)
def test_evaluate_expectations(expectations, success): assert (Fuzzer.evaluate_expectations( expectations, Summary(method="GET", headers={}, body={}, timestamp=2, url=""), ) is success)
def test_init_uri(config, uri, expect_exception): if expect_exception: with pytest.raises(expect_exception): Fuzzer(config_obj=config, domain="default", uri=uri) else: Fuzzer(config_obj=config, domain="default", uri=uri)
def test_init_domain(config, domain_name, expect_exception): if expect_exception: with pytest.raises(expect_exception): Fuzzer(config_obj=config, domain=domain_name) else: Fuzzer(config_obj=config, domain=domain_name)
def test_init_logger(config, uri, methods, expected_file_name): assert (expected_file_name in Fuzzer(config_obj=config, domain=domain, methods=methods, uri=uri).log_file_name)