2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-11-14 07:10:23 -05:00
|
|
|
import {
|
2021-02-24 22:33:09 -05:00
|
|
|
assert,
|
2020-11-14 07:10:23 -05:00
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2020-11-14 07:10:23 -05:00
|
|
|
assertThrows,
|
|
|
|
unitTest,
|
|
|
|
} from "./test_util.ts";
|
2019-10-27 11:22:53 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest(async function permissionInvalidName() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-07-13 12:23:24 -04:00
|
|
|
await Deno.permissions.query({ name: "foo" as any });
|
2021-02-24 22:33:09 -05:00
|
|
|
}, TypeError);
|
2019-10-27 11:22:53 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest(async function permissionNetInvalidHost() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-12-30 17:35:28 -05:00
|
|
|
await Deno.permissions.query({ name: "net", host: ":" });
|
2020-08-18 11:05:51 -04:00
|
|
|
}, URIError);
|
2019-10-27 11:22:53 -04:00
|
|
|
});
|
2020-09-19 17:30:59 -04:00
|
|
|
|
2021-02-24 22:33:09 -05:00
|
|
|
unitTest(async function permissionQueryReturnsEventTarget() {
|
|
|
|
const status = await Deno.permissions.query({ name: "hrtime" });
|
|
|
|
assert(["granted", "denied", "prompt"].includes(status.state));
|
|
|
|
let called = false;
|
|
|
|
status.addEventListener("change", () => {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
status.dispatchEvent(new Event("change"));
|
|
|
|
assert(called);
|
|
|
|
assert(status === (await Deno.permissions.query({ name: "hrtime" })));
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function permissionQueryForReadReturnsSameStatus() {
|
|
|
|
const status1 = await Deno.permissions.query({
|
|
|
|
name: "read",
|
|
|
|
path: ".",
|
|
|
|
});
|
|
|
|
const status2 = await Deno.permissions.query({
|
|
|
|
name: "read",
|
|
|
|
path: ".",
|
|
|
|
});
|
|
|
|
assert(status1 === status2);
|
|
|
|
});
|
|
|
|
|
2020-09-19 17:30:59 -04:00
|
|
|
unitTest(function permissionsIllegalConstructor() {
|
|
|
|
assertThrows(() => new Deno.Permissions(), TypeError, "Illegal constructor.");
|
2021-02-24 22:33:09 -05:00
|
|
|
assertEquals(Deno.Permissions.length, 0);
|
2020-09-19 17:30:59 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function permissionStatusIllegalConstructor() {
|
|
|
|
assertThrows(
|
|
|
|
() => new Deno.PermissionStatus(),
|
|
|
|
TypeError,
|
|
|
|
"Illegal constructor.",
|
|
|
|
);
|
2020-11-14 07:10:23 -05:00
|
|
|
assertEquals(Deno.PermissionStatus.length, 0);
|
2020-09-19 17:30:59 -04:00
|
|
|
});
|
2021-08-06 09:04:00 -04:00
|
|
|
|
|
|
|
unitTest(async function permissionURL() {
|
|
|
|
await Deno.permissions.query({
|
|
|
|
name: "read",
|
|
|
|
path: new URL(".", import.meta.url),
|
|
|
|
});
|
|
|
|
await Deno.permissions.query({
|
|
|
|
name: "write",
|
|
|
|
path: new URL(".", import.meta.url),
|
|
|
|
});
|
|
|
|
await Deno.permissions.query({
|
|
|
|
name: "run",
|
|
|
|
command: new URL(".", import.meta.url),
|
|
|
|
});
|
|
|
|
});
|