summaryrefslogtreecommitdiff
path: root/cli/js/permissions_test.ts
blob: 22e8494e9fb6bd385fbd6281c7dfa377a96261a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEquals } from "./test_util.ts";

const knownPermissions: Deno.PermissionName[] = [
  "run",
  "read",
  "write",
  "net",
  "env",
  "plugin",
  "hrtime"
];

function genFunc(grant: Deno.PermissionName): () => Promise<void> {
  const gen: () => Promise<void> = async function Granted(): Promise<void> {
    const status0 = await Deno.permissions.query({ name: grant });
    assert(status0 != null);
    assertEquals(status0.state, "granted");

    const status1 = await Deno.permissions.revoke({ name: grant });
    assert(status1 != null);
    assertEquals(status1.state, "prompt");
  };
  // Properly name these generated functions.
  Object.defineProperty(gen, "name", { value: grant + "Granted" });
  return gen;
}

for (const grant of knownPermissions) {
  testPerm({ [grant]: true }, genFunc(grant));
}

test(async function permissionInvalidName(): Promise<void> {
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    await Deno.permissions.query({ name: "foo" as any });
  } catch (e) {
    assert(e.name === "TypeError");
  }
});

test(async function permissionNetInvalidUrl(): Promise<void> {
  try {
    // Invalid url causes TypeError.
    await Deno.permissions.query({ name: "net", url: ":" });
  } catch (e) {
    assert(e.name === "TypeError");
  }
});