summaryrefslogtreecommitdiff
path: root/tests/unit/permissions_test.ts
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-02-10 13:22:13 -0700
committerGitHub <noreply@github.com>2024-02-10 20:22:13 +0000
commitf5e46c9bf2f50d66a953fa133161fc829cecff06 (patch)
tree8faf2f5831c1c7b11d842cd9908d141082c869a5 /tests/unit/permissions_test.ts
parentd2477f780630a812bfd65e3987b70c0d309385bb (diff)
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests -> tests, and updates of relative paths for files. This is the first step towards aggregate all of the integration test files under tests/, which will lead to a set of integration tests that can run without the CLI binary being built. While we could leave these tests under `cli`, it would require us to keep a more complex directory structure for the various test runners. In addition, we have a lot of complexity to ignore various test files in the `cli` project itself (cargo publish exclusion rules, autotests = false, etc). And finally, the `tests/` folder will eventually house the `test_ffi`, `test_napi` and other testing code, reducing the size of the root repo directory. For easier review, the extremely large and noisy "move" is in the first commit (with no changes -- just a move), while the remainder of the changes to actual files is in the second commit.
Diffstat (limited to 'tests/unit/permissions_test.ts')
-rw-r--r--tests/unit/permissions_test.ts202
1 files changed, 202 insertions, 0 deletions
diff --git a/tests/unit/permissions_test.ts b/tests/unit/permissions_test.ts
new file mode 100644
index 000000000..4dab0696a
--- /dev/null
+++ b/tests/unit/permissions_test.ts
@@ -0,0 +1,202 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+import {
+ assert,
+ assertEquals,
+ assertRejects,
+ assertThrows,
+} from "./test_util.ts";
+
+Deno.test(async function permissionInvalidName() {
+ await assertRejects(async () => {
+ // deno-lint-ignore no-explicit-any
+ await Deno.permissions.query({ name: "foo" as any });
+ }, TypeError);
+});
+
+Deno.test(function permissionInvalidNameSync() {
+ assertThrows(() => {
+ // deno-lint-ignore no-explicit-any
+ Deno.permissions.querySync({ name: "foo" as any });
+ }, TypeError);
+});
+
+Deno.test(async function permissionNetInvalidHost() {
+ await assertRejects(async () => {
+ await Deno.permissions.query({ name: "net", host: ":" });
+ }, URIError);
+});
+
+Deno.test(function permissionNetInvalidHostSync() {
+ assertThrows(() => {
+ Deno.permissions.querySync({ name: "net", host: ":" });
+ }, URIError);
+});
+
+Deno.test(async function permissionSysValidKind() {
+ await Deno.permissions.query({ name: "sys", kind: "loadavg" });
+ await Deno.permissions.query({ name: "sys", kind: "osRelease" });
+ await Deno.permissions.query({ name: "sys", kind: "osUptime" });
+ await Deno.permissions.query({ name: "sys", kind: "networkInterfaces" });
+ await Deno.permissions.query({ name: "sys", kind: "systemMemoryInfo" });
+ await Deno.permissions.query({ name: "sys", kind: "hostname" });
+ await Deno.permissions.query({ name: "sys", kind: "uid" });
+ await Deno.permissions.query({ name: "sys", kind: "gid" });
+ await Deno.permissions.query({ name: "sys", kind: "cpus" });
+});
+
+Deno.test(function permissionSysValidKindSync() {
+ Deno.permissions.querySync({ name: "sys", kind: "loadavg" });
+ Deno.permissions.querySync({ name: "sys", kind: "osRelease" });
+ Deno.permissions.querySync({ name: "sys", kind: "networkInterfaces" });
+ Deno.permissions.querySync({ name: "sys", kind: "systemMemoryInfo" });
+ Deno.permissions.querySync({ name: "sys", kind: "hostname" });
+ Deno.permissions.querySync({ name: "sys", kind: "uid" });
+ Deno.permissions.querySync({ name: "sys", kind: "gid" });
+ Deno.permissions.querySync({ name: "sys", kind: "cpus" });
+});
+
+Deno.test(async function permissionSysInvalidKind() {
+ await assertRejects(async () => {
+ // deno-lint-ignore no-explicit-any
+ await Deno.permissions.query({ name: "sys", kind: "abc" as any });
+ }, TypeError);
+});
+
+Deno.test(function permissionSysInvalidKindSync() {
+ assertThrows(() => {
+ // deno-lint-ignore no-explicit-any
+ Deno.permissions.querySync({ name: "sys", kind: "abc" as any });
+ }, TypeError);
+});
+
+Deno.test(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" })));
+});
+
+Deno.test(function permissionQueryReturnsEventTargetSync() {
+ const status = Deno.permissions.querySync({ 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 === Deno.permissions.querySync({ name: "hrtime" }));
+});
+
+Deno.test(async function permissionQueryForReadReturnsSameStatus() {
+ const status1 = await Deno.permissions.query({
+ name: "read",
+ path: ".",
+ });
+ const status2 = await Deno.permissions.query({
+ name: "read",
+ path: ".",
+ });
+ assert(status1 === status2);
+});
+
+Deno.test(function permissionQueryForReadReturnsSameStatusSync() {
+ const status1 = Deno.permissions.querySync({
+ name: "read",
+ path: ".",
+ });
+ const status2 = Deno.permissions.querySync({
+ name: "read",
+ path: ".",
+ });
+ assert(status1 === status2);
+});
+
+Deno.test(function permissionsIllegalConstructor() {
+ assertThrows(() => new Deno.Permissions(), TypeError, "Illegal constructor.");
+ assertEquals(Deno.Permissions.length, 0);
+});
+
+Deno.test(function permissionStatusIllegalConstructor() {
+ assertThrows(
+ () => new Deno.PermissionStatus(),
+ TypeError,
+ "Illegal constructor.",
+ );
+ assertEquals(Deno.PermissionStatus.length, 0);
+});
+
+// Regression test for https://github.com/denoland/deno/issues/17020
+Deno.test(async function permissionURL() {
+ const path = new URL(".", import.meta.url);
+
+ await Deno.permissions.query({ name: "read", path });
+ await Deno.permissions.query({ name: "write", path });
+ await Deno.permissions.query({ name: "ffi", path });
+ await Deno.permissions.query({ name: "run", command: path });
+});
+
+Deno.test(function permissionURLSync() {
+ Deno.permissions.querySync({
+ name: "read",
+ path: new URL(".", import.meta.url),
+ });
+ Deno.permissions.querySync({
+ name: "write",
+ path: new URL(".", import.meta.url),
+ });
+ Deno.permissions.querySync({
+ name: "run",
+ command: new URL(".", import.meta.url),
+ });
+});
+
+Deno.test(async function permissionDescriptorValidation() {
+ for (const value of [undefined, null, {}]) {
+ for (const method of ["query", "request", "revoke"]) {
+ await assertRejects(
+ async () => {
+ // deno-lint-ignore no-explicit-any
+ await (Deno.permissions as any)[method](value as any);
+ },
+ TypeError,
+ '"undefined" is not a valid permission name',
+ );
+ }
+ }
+});
+
+Deno.test(function permissionDescriptorValidationSync() {
+ for (const value of [undefined, null, {}]) {
+ for (const method of ["querySync", "revokeSync", "requestSync"]) {
+ assertThrows(
+ () => {
+ // deno-lint-ignore no-explicit-any
+ (Deno.permissions as any)[method](value as any);
+ },
+ TypeError,
+ '"undefined" is not a valid permission name',
+ );
+ }
+ }
+});
+
+// Regression test for https://github.com/denoland/deno/issues/15894.
+Deno.test(async function permissionStatusObjectsNotEqual() {
+ assert(
+ await Deno.permissions.query({ name: "env", variable: "A" }) !=
+ await Deno.permissions.query({ name: "env", variable: "B" }),
+ );
+});
+
+Deno.test(function permissionStatusObjectsNotEqualSync() {
+ assert(
+ Deno.permissions.querySync({ name: "env", variable: "A" }) !=
+ Deno.permissions.querySync({ name: "env", variable: "B" }),
+ );
+});