import requests
import json
import pytest
import assertions

HOST_URL = "{{ host_url }}"


def _prep_data_for_fuzzing(file, params_list):
    fuzz_values = []
    with open(file, encoding="iso-8859-15") as fobj:
        values = [str(_).replace("\n","") for _ in fobj.readlines()]
    no_of_params = len(params_list)
    for _ in values:
        fuzz_values.append([_]*no_of_params)
    print("Fuzz values for "+str(file)+": "+str(fuzz_values))
    return fuzz_values

def _trigger_requests(req_method, url, header, data, proxies=None):
    print("\n\nRegenerating traffic from CloudVector events....")
    return requests.request(method=req_method, url=url, proxies=proxies, headers=header, data=data, verify=False)

{% for fuzz_info in fuzzing_details %}
@pytest.mark.parametrize("{{ api_info.get("params",[])|join(", ") }}", _prep_data_for_fuzzing("{{ fuzz_info.get("file") }}", {{ api_info.get("params",[]) }}))
def test_{{ api_name }}_for_{{ fuzz_info.get("fuzz_type") }}({{ api_info.get("params",[])|join(", ") }}):
    data = {}
    {% for param in api_info.get("params",[]) %}data["{{ param }}"] = {{ param }}
    {% endfor %}
    headers = {{ api_info.get("header") }}
    headers["cv-fuzzed-event"] = 1
    req = {
             "data": data,
             "headers": headers
          }
    resp = _trigger_requests("{{ api_info.get("method") }}", "{{ api_info.get("url") }}",
                      header=req["headers"],
                      data=json.dumps(data))
    print(resp.status_code)
    print(resp.text)
    assertions.assert_for_negative_scenarios(req,resp)
{% endfor %}





