Finish rewrite of everything with Prisma

This commit is contained in:
Jesse Wierzbinski 2023-11-11 15:37:14 -10:00
parent 5eed8374cd
commit dc0ec47543
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
47 changed files with 1283 additions and 1036 deletions

View file

@ -1,71 +1,63 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getConfig } from "@config";
import { Token } from "@prisma/client";
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { AppDataSource } from "~database/datasource";
import { ApplicationAction } from "~database/entities/Application";
import { EmojiAction } from "~database/entities/Emoji";
import { Token, TokenType } from "~database/entities/Token";
import { UserAction } from "~database/entities/User";
import { client } from "~database/datasource";
import { TokenType } from "~database/entities/Token";
import { UserWithRelations, createNewLocalUser } from "~database/entities/User";
import { APIEmoji } from "~types/entities/emoji";
import { APIInstance } from "~types/entities/instance";
const config = getConfig();
let token: Token;
let user: UserAction;
let user2: UserAction;
let user: UserWithRelations;
describe("API Tests", () => {
beforeAll(async () => {
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
// Initialize test user
user = await UserAction.createNewLocal({
user = await createNewLocalUser({
email: "test@test.com",
username: "test",
password: "test",
display_name: "",
});
// Initialize second test user
user2 = await UserAction.createNewLocal({
email: "test2@test.com",
username: "test2",
password: "test2",
display_name: "",
token = await client.token.create({
data: {
access_token: "test",
application: {
create: {
client_id: "test",
name: "Test Application",
redirect_uris: "https://example.com",
scopes: "read write",
secret: "test",
website: "https://example.com",
vapid_key: null,
},
},
code: "test",
scope: "read write",
token_type: TokenType.BEARER,
user: {
connect: {
id: user.id,
},
},
},
});
const app = new ApplicationAction();
app.name = "Test Application";
app.website = "https://example.com";
app.client_id = "test";
app.redirect_uris = "https://example.com";
app.scopes = "read write";
app.secret = "test";
app.vapid_key = null;
await app.save();
// Initialize test token
token = new Token();
token.access_token = "test";
token.application = app;
token.code = "test";
token.scope = "read write";
token.token_type = TokenType.BEARER;
token.user = user;
token = await token.save();
});
afterAll(async () => {
await user.remove();
await user2.remove();
await AppDataSource.destroy();
await client.user.deleteMany({
where: {
username: {
in: ["test", "test2"],
},
},
});
});
describe("GET /api/v1/instance", () => {
@ -106,15 +98,15 @@ describe("API Tests", () => {
describe("GET /api/v1/custom_emojis", () => {
beforeAll(async () => {
const emoji = new EmojiAction();
emoji.instance = null;
emoji.url = "https://example.com/test.png";
emoji.content_type = "image/png";
emoji.shortcode = "test";
emoji.visible_in_picker = true;
await emoji.save();
await client.emoji.create({
data: {
instanceId: null,
url: "https://example.com/test.png",
content_type: "image/png",
shortcode: "test",
visible_in_picker: true,
},
});
});
test("should return an array of at least one custom emoji", async () => {
const response = await fetch(
@ -139,7 +131,11 @@ describe("API Tests", () => {
expect(emojis[0].url).toBe("https://example.com/test.png");
});
afterAll(async () => {
await EmojiAction.delete({ shortcode: "test" });
await client.emoji.deleteMany({
where: {
shortcode: "test",
},
});
});
});
});

View file

@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getConfig } from "@config";
import { Token } from "@prisma/client";
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { AppDataSource } from "~database/datasource";
import { ApplicationAction } from "~database/entities/Application";
import { Token, TokenType } from "~database/entities/Token";
import { UserAction } from "~database/entities/User";
import { client } from "~database/datasource";
import { TokenType } from "~database/entities/Token";
import { UserWithRelations, createNewLocalUser } from "~database/entities/User";
import { APIAccount } from "~types/entities/account";
import { APIRelationship } from "~types/entities/relationship";
import { APIStatus } from "~types/entities/status";
@ -13,59 +13,59 @@ import { APIStatus } from "~types/entities/status";
const config = getConfig();
let token: Token;
let user: UserAction;
let user2: UserAction;
let user: UserWithRelations;
let user2: UserWithRelations;
describe("API Tests", () => {
beforeAll(async () => {
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
// Initialize test user
user = await UserAction.createNewLocal({
user = await createNewLocalUser({
email: "test@test.com",
username: "test",
password: "test",
display_name: "",
});
// Initialize second test user
user2 = await UserAction.createNewLocal({
user2 = await createNewLocalUser({
email: "test2@test.com",
username: "test2",
password: "test2",
display_name: "",
});
const app = new ApplicationAction();
app.name = "Test Application";
app.website = "https://example.com";
app.client_id = "test";
app.redirect_uris = "https://example.com";
app.scopes = "read write";
app.secret = "test";
app.vapid_key = null;
await app.save();
// Initialize test token
token = new Token();
token.access_token = "test";
token.application = app;
token.code = "test";
token.scope = "read write";
token.token_type = TokenType.BEARER;
token.user = user;
token = await token.save();
token = await client.token.create({
data: {
access_token: "test",
application: {
create: {
client_id: "test",
name: "Test Application",
redirect_uris: "https://example.com",
scopes: "read write",
secret: "test",
website: "https://example.com",
vapid_key: null,
},
},
code: "test",
scope: "read write",
token_type: TokenType.BEARER,
user: {
connect: {
id: user.id,
},
},
},
});
});
afterAll(async () => {
await user.remove();
await user2.remove();
await AppDataSource.destroy();
await client.user.deleteMany({
where: {
username: {
in: ["test", "test2"],
},
},
});
});
describe("POST /api/v1/accounts/:id", () => {

View file

@ -1,72 +1,65 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getConfig } from "@config";
import { Token } from "@prisma/client";
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { AppDataSource } from "~database/datasource";
import { ApplicationAction } from "~database/entities/Application";
import { Token, TokenType } from "~database/entities/Token";
import { UserAction } from "~database/entities/User";
import { client } from "~database/datasource";
import { TokenType } from "~database/entities/Token";
import { UserWithRelations, createNewLocalUser } from "~database/entities/User";
import { APIAccount } from "~types/entities/account";
import { APIContext } from "~types/entities/context";
import { APIStatus } from "~types/entities/status";
const config = getConfig();
let token: Token;
let user: UserAction;
let user2: UserAction;
let user: UserWithRelations;
let status: APIStatus | null = null;
let status2: APIStatus | null = null;
describe("API Tests", () => {
beforeAll(async () => {
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
// Initialize test user
user = await UserAction.createNewLocal({
user = await createNewLocalUser({
email: "test@test.com",
username: "test",
password: "test",
display_name: "",
});
// Initialize second test user
user2 = await UserAction.createNewLocal({
email: "test2@test.com",
username: "test2",
password: "test2",
display_name: "",
token = await client.token.create({
data: {
access_token: "test",
application: {
create: {
client_id: "test",
name: "Test Application",
redirect_uris: "https://example.com",
scopes: "read write",
secret: "test",
website: "https://example.com",
vapid_key: null,
},
},
code: "test",
scope: "read write",
token_type: TokenType.BEARER,
user: {
connect: {
id: user.id,
},
},
},
});
const app = new ApplicationAction();
app.name = "Test Application";
app.website = "https://example.com";
app.client_id = "test";
app.redirect_uris = "https://example.com";
app.scopes = "read write";
app.secret = "test";
app.vapid_key = null;
await app.save();
// Initialize test token
token = new Token();
token.access_token = "test";
token.application = app;
token.code = "test";
token.scope = "read write";
token.token_type = TokenType.BEARER;
token.user = user;
token = await token.save();
});
afterAll(async () => {
await user.remove();
await user2.remove();
await AppDataSource.destroy();
await client.user.deleteMany({
where: {
username: {
in: ["test", "test2"],
},
},
});
});
describe("POST /api/v1/statuses", () => {
@ -322,7 +315,7 @@ describe("API Tests", () => {
"application/json"
);
const users = (await response.json()) as UserAction[];
const users = (await response.json()) as APIAccount[];
expect(users.length).toBe(1);
expect(users[0].id).toBe(user.id);

View file

@ -1,9 +1,8 @@
import { getConfig } from "@config";
import { Application, Token } from "@prisma/client";
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { AppDataSource } from "~database/datasource";
import { ApplicationAction } from "~database/entities/Application";
import { Token } from "~database/entities/Token";
import { UserAction, userRelations } from "~database/entities/User";
import { client } from "~database/datasource";
import { createNewLocalUser } from "~database/entities/User";
const config = getConfig();
@ -13,10 +12,8 @@ let code: string;
let token: Token;
beforeAll(async () => {
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
// Initialize test user
await UserAction.createNewLocal({
// Init test user
await createNewLocalUser({
email: "test@test.com",
username: "test",
password: "test",
@ -139,7 +136,7 @@ describe("GET /api/v1/apps/verify_credentials", () => {
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
const credentials = (await response.json()) as Partial<ApplicationAction>;
const credentials = (await response.json()) as Partial<Application>;
expect(credentials.name).toBe("Test Application");
expect(credentials.website).toBe("https://example.com");
@ -150,31 +147,9 @@ describe("GET /api/v1/apps/verify_credentials", () => {
afterAll(async () => {
// Clean up user
const user = await UserAction.findOne({
await client.user.delete({
where: {
username: "test",
},
relations: userRelations,
});
// Clean up tokens
const tokens = await Token.findBy({
user: {
username: "test",
},
});
const applications = await ApplicationAction.findBy({
client_id,
secret: client_secret,
});
await Promise.all(tokens.map(async token => await token.remove()));
await Promise.all(
applications.map(async application => await application.remove())
);
if (user) await user.remove();
await AppDataSource.destroy();
});