2023-09-22 00:42:59 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
import { getConfig } from "@config";
|
|
|
|
|
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
|
|
|
import { AppDataSource } from "~database/datasource";
|
|
|
|
|
import { Application } from "~database/entities/Application";
|
2023-10-08 22:20:42 +02:00
|
|
|
import { Emoji } from "~database/entities/Emoji";
|
2023-09-22 00:42:59 +02:00
|
|
|
import { Token, TokenType } from "~database/entities/Token";
|
|
|
|
|
import { User } from "~database/entities/User";
|
2023-10-08 22:20:42 +02:00
|
|
|
import { APIEmoji } from "~types/entities/emoji";
|
2023-10-01 05:24:58 +02:00
|
|
|
import { APIInstance } from "~types/entities/instance";
|
2023-09-22 00:42:59 +02:00
|
|
|
|
|
|
|
|
const config = getConfig();
|
|
|
|
|
|
|
|
|
|
let token: Token;
|
|
|
|
|
let user: User;
|
2023-09-22 05:18:05 +02:00
|
|
|
let user2: User;
|
2023-09-22 00:42:59 +02:00
|
|
|
|
2023-10-18 02:57:47 +02:00
|
|
|
describe("API Tests", () => {
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
|
|
|
|
|
|
|
|
|
|
// Initialize test user
|
|
|
|
|
user = await User.createNewLocal({
|
|
|
|
|
email: "test@test.com",
|
|
|
|
|
username: "test",
|
|
|
|
|
password: "test",
|
|
|
|
|
display_name: "",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Initialize second test user
|
|
|
|
|
user2 = await User.createNewLocal({
|
|
|
|
|
email: "test2@test.com",
|
|
|
|
|
username: "test2",
|
|
|
|
|
password: "test2",
|
|
|
|
|
display_name: "",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const app = new Application();
|
|
|
|
|
|
|
|
|
|
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();
|
2023-09-22 05:18:05 +02:00
|
|
|
});
|
|
|
|
|
|
2023-10-19 21:53:59 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await user.remove();
|
|
|
|
|
await user2.remove();
|
|
|
|
|
|
|
|
|
|
await AppDataSource.destroy();
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-18 02:57:47 +02:00
|
|
|
describe("GET /api/v1/instance", () => {
|
|
|
|
|
test("should return an APIInstance object", async () => {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${config.http.base_url}/api/v1/instance`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-10-08 22:20:42 +02:00
|
|
|
|
2023-10-18 02:57:47 +02:00
|
|
|
expect(response.status).toBe(200);
|
|
|
|
|
expect(response.headers.get("content-type")).toBe(
|
|
|
|
|
"application/json"
|
|
|
|
|
);
|
2023-10-08 22:20:42 +02:00
|
|
|
|
2023-10-19 21:53:59 +02:00
|
|
|
const instance = (await response.json()) as APIInstance;
|
2023-10-18 02:57:47 +02:00
|
|
|
|
|
|
|
|
expect(instance.uri).toBe(new URL(config.http.base_url).hostname);
|
|
|
|
|
expect(instance.title).toBeDefined();
|
|
|
|
|
expect(instance.description).toBeDefined();
|
|
|
|
|
expect(instance.email).toBeDefined();
|
|
|
|
|
expect(instance.version).toBeDefined();
|
|
|
|
|
expect(instance.urls).toBeDefined();
|
|
|
|
|
expect(instance.stats).toBeDefined();
|
|
|
|
|
expect(instance.thumbnail).toBeDefined();
|
|
|
|
|
expect(instance.languages).toBeDefined();
|
|
|
|
|
// Not implemented yet
|
|
|
|
|
// expect(instance.contact_account).toBeDefined();
|
|
|
|
|
expect(instance.rules).toBeDefined();
|
|
|
|
|
expect(instance.approval_required).toBeDefined();
|
|
|
|
|
expect(instance.max_toot_chars).toBeDefined();
|
|
|
|
|
});
|
2023-10-08 22:20:42 +02:00
|
|
|
});
|
|
|
|
|
|
2023-10-18 02:57:47 +02:00
|
|
|
describe("GET /api/v1/custom_emojis", () => {
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
const emoji = new Emoji();
|
|
|
|
|
|
|
|
|
|
emoji.instance = null;
|
2023-11-05 00:59:55 +01:00
|
|
|
emoji.url = "https://example.com/test.png";
|
|
|
|
|
emoji.content_type = "image/png";
|
2023-10-18 02:57:47 +02:00
|
|
|
emoji.shortcode = "test";
|
|
|
|
|
emoji.visible_in_picker = true;
|
|
|
|
|
|
|
|
|
|
await emoji.save();
|
|
|
|
|
});
|
|
|
|
|
test("should return an array of at least one custom emoji", async () => {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${config.http.base_url}/api/v1/custom_emojis`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token.access_token}`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
|
expect(response.headers.get("content-type")).toBe(
|
|
|
|
|
"application/json"
|
|
|
|
|
);
|
2023-10-08 22:20:42 +02:00
|
|
|
|
2023-10-19 21:53:59 +02:00
|
|
|
const emojis = (await response.json()) as APIEmoji[];
|
2023-10-08 22:20:42 +02:00
|
|
|
|
2023-10-18 02:57:47 +02:00
|
|
|
expect(emojis.length).toBeGreaterThan(0);
|
|
|
|
|
expect(emojis[0].shortcode).toBe("test");
|
2023-11-05 00:59:55 +01:00
|
|
|
expect(emojis[0].url).toBe("https://example.com/test.png");
|
2023-10-18 02:57:47 +02:00
|
|
|
});
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
await Emoji.delete({ shortcode: "test" });
|
|
|
|
|
});
|
2023-10-08 22:20:42 +02:00
|
|
|
});
|
2023-09-22 00:42:59 +02:00
|
|
|
});
|