<html>
<head>
    <script>
        const policyShouldGrantAccess = document.location.hash === "#policyShouldGrantAccess";

        function messageToTop(messagePrefix, fetchData) {
            top.postMessage(messagePrefix + " document.cookie == " + document.cookie +
                (fetchData ? ", cookies seen server-side == " + JSON.stringify(fetchData) : ""), "http://127.0.0.1:8000");
        }

        function checkHasStorageAccess() {
            var promise = document.hasStorageAccess();
            promise.then(
                function (hasAccess) {
                    if (hasAccess && policyShouldGrantAccess)
                        fetch("echo-incoming-cookies-as-json.py", { credentials: "same-origin" }).then(function(response) {
                            return response.json();
                        }).then(function(data) {
                            messageToTop("PASS Has storage access.", data);
                        }).catch(function(error) {
                            console.log(error.message);
                        });
                    else if (!hasAccess && !policyShouldGrantAccess)
                        fetch("echo-incoming-cookies-as-json.py", { credentials: "same-origin" }).then(function(response) {
                            return response.json();
                        }).then(function(data) {
                            messageToTop("PASS No storage access.", data);
                        }).catch(function(error) {
                            console.log(error.message);
                        });
                    else
                        messageToTop("FAIL " +
                            (hasAccess ? "Storage " : "No storage ") +
                            "access when there should " +
                            (policyShouldGrantAccess ? "" : "not ") +
                            "have been access.");
                },
                function (reason) {
                    messageToTop("FAIL document.hasStorageAccess() was rejected. Reason: " + reason);
                }
            );
        }
    </script>
</head>
<body onload="checkHasStorageAccess()">
</body>
</html>