refactor(database): ♻️ Move Token to its own ORM abstraction, optimize familiar_followers route

This commit is contained in:
Jesse Wierzbinski 2024-11-03 17:45:21 +01:00
parent 962c159ddd
commit 845041e4db
No known key found for this signature in database
55 changed files with 694 additions and 504 deletions

View file

@ -15,7 +15,7 @@ describe("API Tests", () => {
const response = await fakeRequest("/api/v1/statuses", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "multipart/form-data",
},
body: formData,
@ -42,7 +42,7 @@ describe("API Tests", () => {
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
),

View file

@ -34,7 +34,7 @@ describe("API Tests", () => {
{
method: "PATCH",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
body: getFormData({
display_name: "New Display Name",
@ -59,7 +59,7 @@ describe("API Tests", () => {
"/api/v1/accounts/verify_credentials",
{
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -106,7 +106,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
@ -132,7 +132,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
@ -156,7 +156,7 @@ describe("API Tests", () => {
const response = await fakeRequest("/api/v1/blocks", {
method: "GET",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
});
@ -179,7 +179,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
@ -205,7 +205,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
@ -231,7 +231,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
@ -257,7 +257,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ comment: "This is a new note" }),
@ -283,7 +283,7 @@ describe("API Tests", () => {
{
method: "GET",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -314,7 +314,7 @@ describe("API Tests", () => {
const response = await fakeRequest("/api/v1/profile/avatar", {
method: "DELETE",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
});
@ -335,7 +335,7 @@ describe("API Tests", () => {
const response = await fakeRequest("/api/v1/profile/header", {
method: "DELETE",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
});
@ -358,7 +358,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
@ -371,13 +371,13 @@ describe("API Tests", () => {
);
});
test("should return an array of objects with id and accounts properties, where id is a string and accounts is an array of APIAccount objects", async () => {
test("should return no familiar followers", async () => {
const response = await fakeRequest(
`/api/v1/accounts/familiar_followers?id[]=${user2.id}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -393,7 +393,9 @@ describe("API Tests", () => {
}[];
expect(Array.isArray(familiarFollowers)).toBe(true);
expect(familiarFollowers.length).toBe(0);
expect(familiarFollowers.length).toBe(1);
expect(familiarFollowers[0].id).toBe(user2.id);
expect(familiarFollowers[0].accounts).toBeArrayOfSize(0);
});
});
});

View file

@ -29,7 +29,7 @@ describe("API Tests", () => {
const response = await fakeRequest("/api/v2/media", {
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
body: formData,
});
@ -52,7 +52,7 @@ describe("API Tests", () => {
const response = await fakeRequest("/api/v1/statuses", {
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
@ -94,7 +94,7 @@ describe("API Tests", () => {
const response = await fakeRequest("/api/v1/statuses", {
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
body: new URLSearchParams({
status: "This is a reply!",
@ -139,7 +139,7 @@ describe("API Tests", () => {
`/api/v1/statuses/${status?.id}`,
{
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -184,7 +184,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -209,7 +209,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -232,7 +232,7 @@ describe("API Tests", () => {
`/api/v1/statuses/${status?.id}/context`,
{
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -259,7 +259,7 @@ describe("API Tests", () => {
{
method: "GET",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -289,7 +289,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -306,7 +306,7 @@ describe("API Tests", () => {
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);
@ -330,7 +330,7 @@ describe("API Tests", () => {
{
method: "DELETE",
headers: {
Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.data.accessToken}`,
},
},
);

View file

@ -1,12 +1,11 @@
import { generateChallenge } from "@/challenges";
import { randomString } from "@/math";
import { Note, User, db } from "@versia/kit/db";
import { Notes, Tokens, Users } from "@versia/kit/tables";
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 { appFactory } from "~/app";
import type { Status } from "~/classes/functions/status";
import type { Token } from "~/classes/functions/token";
import { searchManager } from "~/classes/search/search-manager";
import { setupDatabase } from "~/drizzle/db";
import { config } from "~/packages/config-manager";
@ -60,23 +59,24 @@ export const getTestUsers = async (
users.push(user);
}
const tokens = await db
.insert(Tokens)
.values(
users.map((u) => ({
accessToken: randomString(32, "hex"),
tokenType: "bearer",
userId: u.id,
applicationId: null,
code: randomString(32, "hex"),
scope: "read write follow push",
})),
)
.returning();
const tokens = await Token.insertMany(
users.map((u) => ({
accessToken: randomString(32, "hex"),
tokenType: "bearer",
userId: u.id,
applicationId: null,
code: randomString(32, "hex"),
scope: "read write follow push",
})),
);
return {
users,
tokens,
// Order tokens in the same order as users
// The first token belongs to the first user, the second token belongs to the second user, etc.
tokens: users.map(
(u) => tokens.find((t) => t.data.userId === u.id) as Token,
),
passwords,
deleteUsers: async (): Promise<void> => {
await db.delete(Users).where(