import json


def vcr_scrub_response(response):
    """Replaces the response session cookie and any user data in the response body with
    fake data. Also replaces response body content that isn't needed for testing."""
    if "Set-Cookie" in response["headers"]:
        response["headers"]["Set-Cookie"] = [
            "JSESSIONID=sessioncookie; Path=/rest; Secure; HttpOnly"
        ]
    try:
        response_json = json.loads(response["body"]["string"])
    except json.decoder.JSONDecodeError:
        pass
    else:
        response_json.pop("introductoryText", None)
        try:
            email = response_json["email"]
            if email is not None:
                response_json["email"] = "user@example.com"
                response_json["fullname"] = "Test User"
        except (KeyError, TypeError):
            pass
        if response_json != json.loads(response["body"]["string"]):
            response["body"]["string"] = json.dumps(response_json)
    return response