2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-10-27 11:22:53 -04:00
|
|
|
import { test, testPerm, assert, assertEquals } from "./test_util.ts";
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
const knownPermissions: Deno.PermissionName[] = [
|
2019-03-07 21:56:56 -05:00
|
|
|
"run",
|
|
|
|
"read",
|
|
|
|
"write",
|
|
|
|
"net",
|
2019-04-08 16:22:40 -04:00
|
|
|
"env",
|
2019-12-05 15:30:20 -05:00
|
|
|
"plugin",
|
2019-05-23 12:28:29 -04:00
|
|
|
"hrtime"
|
2019-03-07 21:56:56 -05:00
|
|
|
];
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2019-12-05 15:30:20 -05:00
|
|
|
function genFunc(grant: Deno.PermissionName): () => Promise<void> {
|
|
|
|
const gen: () => Promise<void> = async function Granted(): Promise<void> {
|
2019-10-27 11:22:53 -04:00
|
|
|
const status0 = await Deno.permissions.query({ name: grant });
|
|
|
|
assert(status0 != null);
|
|
|
|
assertEquals(status0.state, "granted");
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
const status1 = await Deno.permissions.revoke({ name: grant });
|
|
|
|
assert(status1 != null);
|
|
|
|
assertEquals(status1.state, "prompt");
|
2019-12-05 15:30:20 -05:00
|
|
|
};
|
|
|
|
// Properly name these generated functions.
|
|
|
|
Object.defineProperty(gen, "name", { value: grant + "Granted" });
|
|
|
|
return gen;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const grant of knownPermissions) {
|
|
|
|
testPerm({ [grant]: true }, genFunc(grant));
|
2019-03-04 11:04:19 -05:00
|
|
|
}
|
2019-10-27 11:22:53 -04:00
|
|
|
|
|
|
|
test(async function permissionInvalidName(): Promise<void> {
|
2020-02-21 10:36:13 -05:00
|
|
|
let thrown = false;
|
2019-10-27 11:22:53 -04:00
|
|
|
try {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
await Deno.permissions.query({ name: "foo" as any });
|
|
|
|
} catch (e) {
|
2020-02-21 10:36:13 -05:00
|
|
|
thrown = true;
|
|
|
|
assert(e instanceof Error);
|
|
|
|
} finally {
|
|
|
|
assert(thrown);
|
2019-10-27 11:22:53 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test(async function permissionNetInvalidUrl(): Promise<void> {
|
2020-02-21 10:36:13 -05:00
|
|
|
let thrown = false;
|
2019-10-27 11:22:53 -04:00
|
|
|
try {
|
|
|
|
await Deno.permissions.query({ name: "net", url: ":" });
|
|
|
|
} catch (e) {
|
2020-02-21 10:36:13 -05:00
|
|
|
thrown = true;
|
|
|
|
assert(e instanceof URIError);
|
|
|
|
} finally {
|
|
|
|
assert(thrown);
|
2019-10-27 11:22:53 -04:00
|
|
|
}
|
|
|
|
});
|