server/tests/oauth-scopes.test.ts

122 lines
5.7 KiB
TypeScript
Raw Permalink Normal View History

import { describe, expect, it } from "bun:test";
import { checkIfOauthIsValid } from "@/oauth";
import { Application } from "@versia/kit/db";
describe("checkIfOauthIsValid", () => {
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes and application.scopes are empty", () => {
const application = new Application({
scopes: "",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes: string[] = [];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes is empty and application.scopes contains write:* or write", () => {
const application = new Application({
scopes: "write:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes: string[] = [];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes is empty and application.scopes contains read:* or read", () => {
const application = new Application({
scopes: "read:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes: string[] = [];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes contains only write: permissions and application.scopes contains write:* or write", () => {
const application = new Application({
scopes: "write:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "write:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes contains only read: permissions and application.scopes contains read:* or read", () => {
const application = new Application({
scopes: "read:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["read:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes contains both write: and read: permissions and application.scopes contains write:* or write and read:* or read", () => {
const application = new Application({
scopes: "write:* read:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return false when routeScopes contains write: permissions but application.scopes does not contain write:* or write", () => {
const application = new Application({
scopes: "read:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "write:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(false);
});
2024-04-07 07:30:49 +02:00
it("should return false when routeScopes contains read: permissions but application.scopes does not contain read:* or read", () => {
const application = new Application({
scopes: "write:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["read:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(false);
});
2024-04-07 07:30:49 +02:00
it("should return false when routeScopes contains both write: and read: permissions but application.scopes does not contain write:* or write and read:* or read", () => {
const application = new Application({
scopes: "",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(false);
});
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes contains a mix of valid and invalid permissions and application.scopes contains all the required permissions", () => {
const application = new Application({
scopes: "write:* read:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "invalid:permission", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return false when routeScopes contains a mix of valid and invalid permissions but application.scopes does not contain all the required permissions", () => {
const application = new Application({
scopes: "write:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "invalid:permission", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(false);
});
2024-04-07 07:30:49 +02:00
it("should return true when routeScopes contains a mix of valid write and read permissions and application.scopes contains all the required permissions", () => {
const application = new Application({
scopes: "write:* read:posts",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(true);
});
2024-04-07 07:30:49 +02:00
it("should return false when routeScopes contains a mix of valid write and read permissions but application.scopes does not contain all the required permissions", () => {
const application = new Application({
scopes: "write:*",
} as typeof Application.$type);
2024-04-07 07:30:49 +02:00
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
2024-04-07 07:30:49 +02:00
expect(result).toBe(false);
});
});