2018-10-16 06:37:21 +09:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
import * as assert from 'assert';
|
2023-06-28 13:37:13 +09:00
|
|
|
import { signup, api, startServer, successfulApiCall, failedApiCall, uploadFile, waitFire, connectStream } from '../utils.js';
|
2023-03-03 11:13:12 +09:00
|
|
|
import type { INestApplicationContext } from '@nestjs/common';
|
2023-06-25 08:34:18 +09:00
|
|
|
import type * as misskey from 'misskey-js';
|
2023-06-28 13:37:13 +09:00
|
|
|
import { IncomingMessage } from 'http';
|
2018-10-16 06:37:21 +09:00
|
|
|
|
|
|
|
describe('API', () => {
|
2023-03-12 20:57:01 +09:00
|
|
|
let app: INestApplicationContext;
|
2023-06-25 08:34:18 +09:00
|
|
|
let alice: misskey.entities.MeSignup;
|
|
|
|
let bob: misskey.entities.MeSignup;
|
|
|
|
let carol: misskey.entities.MeSignup;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
beforeAll(async () => {
|
2023-03-12 20:57:01 +09:00
|
|
|
app = await startServer();
|
2022-02-19 14:05:32 +09:00
|
|
|
alice = await signup({ username: 'alice' });
|
|
|
|
bob = await signup({ username: 'bob' });
|
|
|
|
carol = await signup({ username: 'carol' });
|
2023-03-03 11:13:12 +09:00
|
|
|
}, 1000 * 60 * 2);
|
2018-10-16 06:37:21 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
afterAll(async () => {
|
2023-03-12 20:57:01 +09:00
|
|
|
await app.close();
|
2019-01-23 13:35:22 +09:00
|
|
|
});
|
|
|
|
|
2022-02-19 14:05:32 +09:00
|
|
|
describe('General validation', () => {
|
2023-03-03 11:13:12 +09:00
|
|
|
test('wrong type', async () => {
|
|
|
|
const res = await api('/test', {
|
2022-02-19 14:05:32 +09:00
|
|
|
required: true,
|
|
|
|
string: 42,
|
2018-10-16 06:37:21 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
assert.strictEqual(res.status, 400);
|
2023-03-03 11:13:12 +09:00
|
|
|
});
|
2018-10-16 06:37:21 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
test('missing require param', async () => {
|
|
|
|
const res = await api('/test', {
|
2022-02-19 14:05:32 +09:00
|
|
|
string: 'a',
|
2018-10-16 06:37:21 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
assert.strictEqual(res.status, 400);
|
2023-03-03 11:13:12 +09:00
|
|
|
});
|
2018-10-16 06:37:21 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
test('invalid misskey:id (empty string)', async () => {
|
|
|
|
const res = await api('/test', {
|
2022-02-19 14:05:32 +09:00
|
|
|
required: true,
|
|
|
|
id: '',
|
2018-10-16 06:37:21 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
assert.strictEqual(res.status, 400);
|
2023-03-03 11:13:12 +09:00
|
|
|
});
|
2018-10-16 06:37:21 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
test('valid misskey:id', async () => {
|
|
|
|
const res = await api('/test', {
|
2022-02-19 14:05:32 +09:00
|
|
|
required: true,
|
|
|
|
id: '8wvhjghbxu',
|
2018-10-16 06:37:21 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
assert.strictEqual(res.status, 200);
|
2023-03-03 11:13:12 +09:00
|
|
|
});
|
2018-10-16 08:54:36 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
test('default value', async () => {
|
|
|
|
const res = await api('/test', {
|
2022-02-19 14:05:32 +09:00
|
|
|
required: true,
|
|
|
|
string: 'a',
|
2018-10-16 09:45:36 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
assert.strictEqual(res.status, 200);
|
2022-02-19 14:05:32 +09:00
|
|
|
assert.strictEqual(res.body.default, 'hello');
|
2023-03-03 11:13:12 +09:00
|
|
|
});
|
2018-10-16 09:45:36 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
test('can set null even if it has default value', async () => {
|
|
|
|
const res = await api('/test', {
|
2022-02-19 14:05:32 +09:00
|
|
|
required: true,
|
|
|
|
nullableDefault: null,
|
2018-10-16 09:45:36 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
assert.strictEqual(res.status, 200);
|
2022-02-19 14:05:32 +09:00
|
|
|
assert.strictEqual(res.body.nullableDefault, null);
|
2023-03-03 11:13:12 +09:00
|
|
|
});
|
2018-10-16 10:18:47 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
test('cannot set undefined if it has default value', async () => {
|
|
|
|
const res = await api('/test', {
|
2022-02-19 14:05:32 +09:00
|
|
|
required: true,
|
|
|
|
nullableDefault: undefined,
|
2018-10-16 10:18:47 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
assert.strictEqual(res.status, 200);
|
2022-02-19 14:05:32 +09:00
|
|
|
assert.strictEqual(res.body.nullableDefault, 'hello');
|
2023-03-03 11:13:12 +09:00
|
|
|
});
|
2019-01-25 10:58:39 +09:00
|
|
|
});
|
2023-06-26 10:09:12 +09:00
|
|
|
|
|
|
|
test('管理者専用のAPIのアクセス制限', async () => {
|
|
|
|
// aliceは管理者、APIを使える
|
|
|
|
await successfulApiCall({
|
|
|
|
endpoint: '/admin/get-index-stats',
|
|
|
|
parameters: {},
|
|
|
|
user: alice,
|
|
|
|
});
|
|
|
|
|
|
|
|
// bobは一般ユーザーだからダメ
|
|
|
|
await failedApiCall({
|
|
|
|
endpoint: '/admin/get-index-stats',
|
|
|
|
parameters: {},
|
|
|
|
user: bob,
|
|
|
|
}, {
|
|
|
|
status: 403,
|
|
|
|
code: 'ROLE_PERMISSION_DENIED',
|
|
|
|
id: 'c3d38592-54c0-429d-be96-5636b0431a61',
|
|
|
|
});
|
|
|
|
|
|
|
|
// publicアクセスももちろんダメ
|
|
|
|
await failedApiCall({
|
|
|
|
endpoint: '/admin/get-index-stats',
|
|
|
|
parameters: {},
|
|
|
|
user: undefined,
|
|
|
|
}, {
|
|
|
|
status: 401,
|
|
|
|
code: 'CREDENTIAL_REQUIRED',
|
|
|
|
id: '1384574d-a912-4b81-8601-c7b1c4085df1',
|
|
|
|
});
|
|
|
|
|
|
|
|
// ごまがしもダメ
|
|
|
|
await failedApiCall({
|
|
|
|
endpoint: '/admin/get-index-stats',
|
|
|
|
parameters: {},
|
|
|
|
user: { token: 'tsukawasete' },
|
|
|
|
}, {
|
|
|
|
status: 401,
|
|
|
|
code: 'AUTHENTICATION_FAILED',
|
|
|
|
id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14',
|
|
|
|
});
|
|
|
|
});
|
2023-06-28 13:37:13 +09:00
|
|
|
|
|
|
|
describe('Authentication header', () => {
|
|
|
|
test('一般リクエスト', async () => {
|
|
|
|
await successfulApiCall({
|
|
|
|
endpoint: '/admin/get-index-stats',
|
|
|
|
parameters: {},
|
|
|
|
user: {
|
|
|
|
token: alice.token,
|
|
|
|
bearer: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('multipartリクエスト', async () => {
|
|
|
|
const result = await uploadFile({
|
|
|
|
token: alice.token,
|
|
|
|
bearer: true,
|
|
|
|
});
|
|
|
|
assert.strictEqual(result.status, 200);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('streaming', async () => {
|
|
|
|
const fired = await waitFire(
|
|
|
|
{
|
|
|
|
token: alice.token,
|
|
|
|
bearer: true,
|
|
|
|
},
|
|
|
|
'homeTimeline',
|
|
|
|
() => api('notes/create', { text: 'foo' }, alice),
|
|
|
|
msg => msg.type === 'note' && msg.body.text === 'foo',
|
|
|
|
);
|
|
|
|
assert.strictEqual(fired, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('tokenエラー応答でWWW-Authenticate headerを送る', () => {
|
|
|
|
describe('invalid_token', () => {
|
|
|
|
test('一般リクエスト', async () => {
|
|
|
|
const result = await api('/admin/get-index-stats', {}, {
|
|
|
|
token: 'syuilo',
|
|
|
|
bearer: true,
|
|
|
|
});
|
|
|
|
assert.strictEqual(result.status, 401);
|
|
|
|
assert.ok(result.headers.get('WWW-Authenticate')?.startsWith('Bearer realm="Misskey", error="invalid_token", error_description'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('multipartリクエスト', async () => {
|
|
|
|
const result = await uploadFile({
|
|
|
|
token: 'syuilo',
|
|
|
|
bearer: true,
|
|
|
|
});
|
|
|
|
assert.strictEqual(result.status, 401);
|
|
|
|
assert.ok(result.headers.get('WWW-Authenticate')?.startsWith('Bearer realm="Misskey", error="invalid_token", error_description'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('streaming', async () => {
|
|
|
|
await assert.rejects(connectStream(
|
|
|
|
{
|
|
|
|
token: 'syuilo',
|
|
|
|
bearer: true,
|
|
|
|
},
|
|
|
|
'homeTimeline',
|
|
|
|
() => { },
|
|
|
|
), (err: IncomingMessage) => {
|
|
|
|
assert.strictEqual(err.statusCode, 401);
|
|
|
|
assert.ok(err.headers['www-authenticate']?.startsWith('Bearer realm="Misskey", error="invalid_token", error_description'));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('tokenがないとrealmだけおくる', () => {
|
|
|
|
test('一般リクエスト', async () => {
|
|
|
|
const result = await api('/admin/get-index-stats', {});
|
|
|
|
assert.strictEqual(result.status, 401);
|
|
|
|
assert.strictEqual(result.headers.get('WWW-Authenticate'), 'Bearer realm="Misskey"');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('multipartリクエスト', async () => {
|
|
|
|
const result = await uploadFile();
|
|
|
|
assert.strictEqual(result.status, 401);
|
|
|
|
assert.strictEqual(result.headers.get('WWW-Authenticate'), 'Bearer realm="Misskey"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('invalid_request', async () => {
|
|
|
|
const result = await api('/notes/create', { text: true }, {
|
|
|
|
token: alice.token,
|
|
|
|
bearer: true,
|
|
|
|
});
|
|
|
|
assert.strictEqual(result.status, 400);
|
|
|
|
assert.ok(result.headers.get('WWW-Authenticate')?.startsWith('Bearer realm="Misskey", error="invalid_request", error_description'));
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: insufficient_scope test (authテストが全然なくて書けない)
|
|
|
|
});
|
2018-10-16 06:37:21 +09:00
|
|
|
});
|