refactor: 🏷️ Move all types that represent ORM abstractions to ORM class static properties

This commit is contained in:
Jesse Wierzbinski 2024-11-04 14:58:17 +01:00
parent ca31830fb3
commit 02c3c9d0bf
No known key found for this signature in database
17 changed files with 143 additions and 136 deletions

View file

@ -1,11 +1,11 @@
import { describe, expect, it } from "bun:test";
import { checkIfOauthIsValid } from "@/oauth";
import { Application } from "@versia/kit/db";
import type { ApplicationType } from "~/classes/database/application";
describe("checkIfOauthIsValid", () => {
it("should return true when routeScopes and application.scopes are empty", () => {
const application = new Application({ scopes: "" } as ApplicationType);
const application = new Application({
scopes: "",
} as typeof Application.$type);
const routeScopes: string[] = [];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -14,7 +14,7 @@ describe("checkIfOauthIsValid", () => {
it("should return true when routeScopes is empty and application.scopes contains write:* or write", () => {
const application = new Application({
scopes: "write:*",
} as ApplicationType);
} as typeof Application.$type);
const routeScopes: string[] = [];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -23,7 +23,7 @@ describe("checkIfOauthIsValid", () => {
it("should return true when routeScopes is empty and application.scopes contains read:* or read", () => {
const application = new Application({
scopes: "read:*",
} as ApplicationType);
} as typeof Application.$type);
const routeScopes: string[] = [];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -32,7 +32,7 @@ describe("checkIfOauthIsValid", () => {
it("should return true when routeScopes contains only write: permissions and application.scopes contains write:* or write", () => {
const application = new Application({
scopes: "write:*",
} as ApplicationType);
} as typeof Application.$type);
const routeScopes = ["write:users", "write:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -41,7 +41,7 @@ describe("checkIfOauthIsValid", () => {
it("should return true when routeScopes contains only read: permissions and application.scopes contains read:* or read", () => {
const application = new Application({
scopes: "read:*",
} as ApplicationType);
} as typeof Application.$type);
const routeScopes = ["read:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -50,7 +50,7 @@ describe("checkIfOauthIsValid", () => {
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 ApplicationType);
} as typeof Application.$type);
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -59,7 +59,7 @@ describe("checkIfOauthIsValid", () => {
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 ApplicationType);
} as typeof Application.$type);
const routeScopes = ["write:users", "write:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(false);
@ -68,14 +68,16 @@ describe("checkIfOauthIsValid", () => {
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 ApplicationType);
} as typeof Application.$type);
const routeScopes = ["read:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(false);
});
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 ApplicationType);
const application = new Application({
scopes: "",
} as typeof Application.$type);
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(false);
@ -84,7 +86,7 @@ describe("checkIfOauthIsValid", () => {
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 ApplicationType);
} as typeof Application.$type);
const routeScopes = ["write:users", "invalid:permission", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -93,7 +95,7 @@ describe("checkIfOauthIsValid", () => {
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 ApplicationType);
} as typeof Application.$type);
const routeScopes = ["write:users", "invalid:permission", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(false);
@ -102,7 +104,7 @@ describe("checkIfOauthIsValid", () => {
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 ApplicationType);
} as typeof Application.$type);
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(true);
@ -111,7 +113,7 @@ describe("checkIfOauthIsValid", () => {
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 ApplicationType);
} as typeof Application.$type);
const routeScopes = ["write:users", "read:posts"];
const result = checkIfOauthIsValid(application, routeScopes);
expect(result).toBe(false);

View file

@ -3,9 +3,8 @@ import { randomString } from "@/math";
import { Note, Token, User, db } from "@versia/kit/db";
import { Notes, Users } from "@versia/kit/tables";
import { solveChallenge } from "altcha-lib";
import { asc, inArray, like } from "drizzle-orm";
import { type InferSelectModel, asc, inArray, like } from "drizzle-orm";
import { appFactory } from "~/app";
import type { Status } from "~/classes/functions/status";
import { searchManager } from "~/classes/search/search-manager";
import { setupDatabase } from "~/drizzle/db";
import { config } from "~/packages/config-manager";
@ -92,7 +91,7 @@ export const getTestUsers = async (
export const getTestStatuses = async (
count: number,
user: User,
partial?: Partial<Status>,
partial?: Partial<InferSelectModel<typeof Notes>>,
): Promise<Note["data"][]> => {
const statuses: Note[] = [];