2021-03-12 06:10:15 -08:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import * as assert from 'assert';
|
2023-03-02 18:13:12 -08:00
|
|
|
import { startServer, signup, post, api, simpleGet } from '../utils.js';
|
|
|
|
import type { INestApplicationContext } from '@nestjs/common';
|
2021-03-12 06:10:15 -08:00
|
|
|
|
|
|
|
// Request Accept
|
|
|
|
const ONLY_AP = 'application/activity+json';
|
|
|
|
const PREFER_AP = 'application/activity+json, */*';
|
|
|
|
const PREFER_HTML = 'text/html, */*';
|
|
|
|
const UNSPECIFIED = '*/*';
|
|
|
|
|
2023-02-11 23:40:12 -08:00
|
|
|
// Response Content-Type
|
2021-03-12 06:10:15 -08:00
|
|
|
const AP = 'application/activity+json; charset=utf-8';
|
|
|
|
const HTML = 'text/html; charset=utf-8';
|
2023-03-09 09:37:44 -08:00
|
|
|
const JSON_UTF8 = 'application/json; charset=utf-8';
|
2021-03-12 06:10:15 -08:00
|
|
|
|
|
|
|
describe('Fetch resource', () => {
|
2023-03-12 04:57:01 -07:00
|
|
|
let app: INestApplicationContext;
|
2021-03-12 06:10:15 -08:00
|
|
|
|
|
|
|
let alice: any;
|
|
|
|
let alicesPost: any;
|
|
|
|
|
2022-09-17 11:27:08 -07:00
|
|
|
beforeAll(async () => {
|
2023-03-12 04:57:01 -07:00
|
|
|
app = await startServer();
|
2021-03-12 06:10:15 -08:00
|
|
|
alice = await signup({ username: 'alice' });
|
|
|
|
alicesPost = await post(alice, {
|
2022-05-21 06:21:41 -07:00
|
|
|
text: 'test',
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
2023-03-02 18:13:12 -08:00
|
|
|
}, 1000 * 60 * 2);
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2022-09-17 11:27:08 -07:00
|
|
|
afterAll(async () => {
|
2023-03-12 04:57:01 -07:00
|
|
|
await app.close();
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Common', () => {
|
2023-02-02 01:18:25 -08:00
|
|
|
test('meta', async () => {
|
2023-03-02 18:13:12 -08:00
|
|
|
const res = await api('/meta', {
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(res.status, 200);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('GET root', async () => {
|
2021-03-13 06:22:54 -08:00
|
|
|
const res = await simpleGet('/');
|
2021-03-12 06:10:15 -08:00
|
|
|
assert.strictEqual(res.status, 200);
|
2021-03-13 06:22:54 -08:00
|
|
|
assert.strictEqual(res.type, HTML);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('GET docs', async () => {
|
2021-03-13 06:22:54 -08:00
|
|
|
const res = await simpleGet('/docs/ja-JP/about');
|
2021-03-12 06:10:15 -08:00
|
|
|
assert.strictEqual(res.status, 200);
|
2021-03-13 06:22:54 -08:00
|
|
|
assert.strictEqual(res.type, HTML);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-03-09 09:37:44 -08:00
|
|
|
test('GET api-doc', async () => {
|
2021-03-13 06:22:54 -08:00
|
|
|
const res = await simpleGet('/api-doc');
|
2023-03-09 09:37:44 -08:00
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
// fastify-static gives charset=UTF-8 instead of utf-8 and that's okay
|
|
|
|
assert.strictEqual(res.type?.toLowerCase(), HTML);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-03-09 09:37:44 -08:00
|
|
|
test('GET api.json', async () => {
|
2021-03-13 06:22:54 -08:00
|
|
|
const res = await simpleGet('/api.json');
|
2023-03-09 09:37:44 -08:00
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, JSON_UTF8);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 07:18:46 -08:00
|
|
|
|
2023-03-02 18:13:12 -08:00
|
|
|
test('GET api/foo (存在しない)', async () => {
|
|
|
|
const res = await simpleGet('/api/foo');
|
|
|
|
assert.strictEqual(res.status, 404);
|
|
|
|
assert.strictEqual(res.body.error.code, 'UNKNOWN_API_ENDPOINT');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-05-05 03:02:30 -07:00
|
|
|
|
2023-03-09 09:37:44 -08:00
|
|
|
test('GET api-console (client page)', async () => {
|
|
|
|
const res = await simpleGet('/api-console');
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
|
|
|
});
|
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('GET favicon.ico', async () => {
|
2021-03-13 06:22:54 -08:00
|
|
|
const res = await simpleGet('/favicon.ico');
|
2021-03-12 07:18:46 -08:00
|
|
|
assert.strictEqual(res.status, 200);
|
2023-03-02 18:13:12 -08:00
|
|
|
assert.strictEqual(res.type, 'image/vnd.microsoft.icon');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 20:21:33 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('GET apple-touch-icon.png', async () => {
|
2021-03-13 06:22:54 -08:00
|
|
|
const res = await simpleGet('/apple-touch-icon.png');
|
2021-03-12 20:21:33 -08:00
|
|
|
assert.strictEqual(res.status, 200);
|
2021-03-13 06:22:54 -08:00
|
|
|
assert.strictEqual(res.type, 'image/png');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-10-24 05:10:45 -07:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('GET twemoji svg', async () => {
|
2021-10-24 05:10:45 -07:00
|
|
|
const res = await simpleGet('/twemoji/2764.svg');
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, 'image/svg+xml');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-10-24 05:10:45 -07:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('GET twemoji svg with hyphen', async () => {
|
2021-10-24 05:10:45 -07:00
|
|
|
const res = await simpleGet('/twemoji/2764-fe0f-200d-1f525.svg');
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, 'image/svg+xml');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('/@:username', () => {
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Only AP => AP', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, ONLY_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Prefer AP => AP', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, PREFER_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Prefer HTML => HTML', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, PREFER_HTML);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Unspecified => HTML', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('/users/:id', () => {
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Only AP => AP', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, ONLY_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Prefer AP => AP', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, PREFER_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Prefer HTML => Redirect to /@:username', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, PREFER_HTML);
|
|
|
|
assert.strictEqual(res.status, 302);
|
|
|
|
assert.strictEqual(res.location, `/@${alice.username}`);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Undecided => HTML', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 302);
|
|
|
|
assert.strictEqual(res.location, `/@${alice.username}`);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('/notes/:id', () => {
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Only AP => AP', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, ONLY_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Prefer AP => AP', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, PREFER_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Prefer HTML => HTML', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, PREFER_HTML);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('Unspecified => HTML', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Feeds', () => {
|
2023-02-02 01:18:25 -08:00
|
|
|
test('RSS', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/@${alice.username}.rss`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, 'application/rss+xml; charset=utf-8');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('ATOM', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/@${alice.username}.atom`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, 'application/atom+xml; charset=utf-8');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
|
2023-02-02 01:18:25 -08:00
|
|
|
test('JSON', async () => {
|
2021-03-12 06:10:15 -08:00
|
|
|
const res = await simpleGet(`/@${alice.username}.json`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, 'application/json; charset=utf-8');
|
2022-09-17 11:27:08 -07:00
|
|
|
});
|
2021-03-12 06:10:15 -08:00
|
|
|
});
|
|
|
|
});
|