2023-07-26 22:31:52 -07:00
|
|
|
/*
|
2024-02-13 07:59:27 -08:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-26 22:31:52 -07:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2018-10-15 14:37:21 -07:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2019-04-07 05:50:36 -07:00
|
|
|
import * as assert from 'assert';
|
2023-07-26 22:31:52 -07:00
|
|
|
import { IncomingMessage } from 'http';
|
2024-01-08 00:43:52 -08:00
|
|
|
import {
|
|
|
|
api,
|
|
|
|
connectStream,
|
|
|
|
createAppToken,
|
|
|
|
failedApiCall,
|
|
|
|
relativeFetch,
|
|
|
|
signup,
|
|
|
|
successfulApiCall,
|
|
|
|
uploadFile,
|
|
|
|
waitFire,
|
|
|
|
} from '../utils.js';
|
2023-06-24 16:34:18 -07:00
|
|
|
import type * as misskey from 'misskey-js';
|
2018-10-15 14:37:21 -07:00
|
|
|
|
|
|
|
describe('API', () => {
|
2024-01-02 20:41:28 -08:00
|
|
|
let alice: misskey.entities.SignupResponse;
|
|
|
|
let bob: misskey.entities.SignupResponse;
|
2019-04-07 05:50:36 -07:00
|
|
|
|
2022-09-17 11:27:08 -07:00
|
|
|
beforeAll(async () => {
|
2022-02-18 21:05:32 -08:00
|
|
|
alice = await signup({ username: 'alice' });
|
|
|
|
bob = await signup({ username: 'bob' });
|
2023-03-02 18:13:12 -08:00
|
|
|
}, 1000 * 60 * 2);
|
2018-10-15 14:37:21 -07:00
|
|
|
|
2022-02-18 21:05:32 -08:00
|
|
|
describe('General validation', () => {
|
2023-03-02 18:13:12 -08:00
|
|
|
test('wrong type', async () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const res = await api('test', {
|
2022-02-18 21:05:32 -08:00
|
|
|
required: true,
|
2024-03-03 03:15:35 -08:00
|
|
|
// @ts-expect-error string must be string
|
2022-02-18 21:05:32 -08:00
|
|
|
string: 42,
|
2018-10-15 14:37:21 -07:00
|
|
|
});
|
2019-04-07 05:50:36 -07:00
|
|
|
assert.strictEqual(res.status, 400);
|
2023-03-02 18:13:12 -08:00
|
|
|
});
|
2018-10-15 14:37:21 -07:00
|
|
|
|
2023-03-02 18:13:12 -08:00
|
|
|
test('missing require param', async () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
// @ts-expect-error required is required
|
|
|
|
const res = await api('test', {
|
2022-02-18 21:05:32 -08:00
|
|
|
string: 'a',
|
2018-10-15 14:37:21 -07:00
|
|
|
});
|
2019-04-07 05:50:36 -07:00
|
|
|
assert.strictEqual(res.status, 400);
|
2023-03-02 18:13:12 -08:00
|
|
|
});
|
2018-10-15 14:37:21 -07:00
|
|
|
|
2023-03-02 18:13:12 -08:00
|
|
|
test('invalid misskey:id (empty string)', async () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const res = await api('test', {
|
2022-02-18 21:05:32 -08:00
|
|
|
required: true,
|
|
|
|
id: '',
|
2018-10-15 14:37:21 -07:00
|
|
|
});
|
2019-04-07 05:50:36 -07:00
|
|
|
assert.strictEqual(res.status, 400);
|
2023-03-02 18:13:12 -08:00
|
|
|
});
|
2018-10-15 14:37:21 -07:00
|
|
|
|
2023-03-02 18:13:12 -08:00
|
|
|
test('valid misskey:id', async () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const res = await api('test', {
|
2022-02-18 21:05:32 -08:00
|
|
|
required: true,
|
|
|
|
id: '8wvhjghbxu',
|
2018-10-15 14:37:21 -07:00
|
|
|
});
|
2019-04-07 05:50:36 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2023-03-02 18:13:12 -08:00
|
|
|
});
|
2018-10-15 16:54:36 -07:00
|
|
|
|
2023-03-02 18:13:12 -08:00
|
|
|
test('default value', async () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const res = await api('test', {
|
2022-02-18 21:05:32 -08:00
|
|
|
required: true,
|
|
|
|
string: 'a',
|
2018-10-15 17:45:36 -07:00
|
|
|
});
|
2019-04-07 05:50:36 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2022-02-18 21:05:32 -08:00
|
|
|
assert.strictEqual(res.body.default, 'hello');
|
2023-03-02 18:13:12 -08:00
|
|
|
});
|
2018-10-15 17:45:36 -07:00
|
|
|
|
2023-03-02 18:13:12 -08:00
|
|
|
test('can set null even if it has default value', async () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const res = await api('test', {
|
2022-02-18 21:05:32 -08:00
|
|
|
required: true,
|
|
|
|
nullableDefault: null,
|
2018-10-15 17:45:36 -07:00
|
|
|
});
|
2019-04-07 05:50:36 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2022-02-18 21:05:32 -08:00
|
|
|
assert.strictEqual(res.body.nullableDefault, null);
|
2023-03-02 18:13:12 -08:00
|
|
|
});
|
2018-10-15 18:18:47 -07:00
|
|
|
|
2023-03-02 18:13:12 -08:00
|
|
|
test('cannot set undefined if it has default value', async () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const res = await api('test', {
|
2022-02-18 21:05:32 -08:00
|
|
|
required: true,
|
|
|
|
nullableDefault: undefined,
|
2018-10-15 18:18:47 -07:00
|
|
|
});
|
2019-04-07 05:50:36 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2022-02-18 21:05:32 -08:00
|
|
|
assert.strictEqual(res.body.nullableDefault, 'hello');
|
2023-03-02 18:13:12 -08:00
|
|
|
});
|
2019-01-24 17:58:39 -08:00
|
|
|
});
|
2023-06-25 18:09:12 -07:00
|
|
|
|
|
|
|
test('管理者専用のAPIのアクセス制限', async () => {
|
2023-12-26 22:08:59 -08:00
|
|
|
const application = await createAppToken(alice, ['read:account']);
|
|
|
|
const application2 = await createAppToken(alice, ['read:admin:index-stats']);
|
|
|
|
const application3 = await createAppToken(bob, []);
|
|
|
|
const application4 = await createAppToken(bob, ['read:admin:index-stats']);
|
|
|
|
|
2023-06-25 18:09:12 -07:00
|
|
|
// aliceは管理者、APIを使える
|
|
|
|
await successfulApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-06-25 18:09:12 -07:00
|
|
|
parameters: {},
|
|
|
|
user: alice,
|
|
|
|
});
|
|
|
|
|
|
|
|
// bobは一般ユーザーだからダメ
|
|
|
|
await failedApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-06-25 18:09:12 -07:00
|
|
|
parameters: {},
|
|
|
|
user: bob,
|
|
|
|
}, {
|
|
|
|
status: 403,
|
|
|
|
code: 'ROLE_PERMISSION_DENIED',
|
|
|
|
id: 'c3d38592-54c0-429d-be96-5636b0431a61',
|
|
|
|
});
|
|
|
|
|
|
|
|
// publicアクセスももちろんダメ
|
|
|
|
await failedApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-06-25 18:09:12 -07:00
|
|
|
parameters: {},
|
|
|
|
user: undefined,
|
|
|
|
}, {
|
|
|
|
status: 401,
|
|
|
|
code: 'CREDENTIAL_REQUIRED',
|
|
|
|
id: '1384574d-a912-4b81-8601-c7b1c4085df1',
|
|
|
|
});
|
|
|
|
|
|
|
|
// ごまがしもダメ
|
|
|
|
await failedApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-06-25 18:09:12 -07:00
|
|
|
parameters: {},
|
|
|
|
user: { token: 'tsukawasete' },
|
|
|
|
}, {
|
|
|
|
status: 401,
|
|
|
|
code: 'AUTHENTICATION_FAILED',
|
|
|
|
id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14',
|
|
|
|
});
|
2023-12-26 22:08:59 -08:00
|
|
|
|
|
|
|
await successfulApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-12-26 22:08:59 -08:00
|
|
|
parameters: {},
|
|
|
|
user: { token: application2 },
|
|
|
|
});
|
|
|
|
|
|
|
|
await failedApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-12-26 22:08:59 -08:00
|
|
|
parameters: {},
|
|
|
|
user: { token: application },
|
|
|
|
}, {
|
|
|
|
status: 403,
|
|
|
|
code: 'PERMISSION_DENIED',
|
|
|
|
id: '1370e5b7-d4eb-4566-bb1d-7748ee6a1838',
|
|
|
|
});
|
|
|
|
|
|
|
|
await failedApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-12-26 22:08:59 -08:00
|
|
|
parameters: {},
|
|
|
|
user: { token: application3 },
|
|
|
|
}, {
|
|
|
|
status: 403,
|
|
|
|
code: 'ROLE_PERMISSION_DENIED',
|
|
|
|
id: 'c3d38592-54c0-429d-be96-5636b0431a61',
|
|
|
|
});
|
|
|
|
|
|
|
|
await failedApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-12-26 22:08:59 -08:00
|
|
|
parameters: {},
|
|
|
|
user: { token: application4 },
|
|
|
|
}, {
|
|
|
|
status: 403,
|
|
|
|
code: 'ROLE_PERMISSION_DENIED',
|
|
|
|
id: 'c3d38592-54c0-429d-be96-5636b0431a61',
|
|
|
|
});
|
2023-06-25 18:09:12 -07:00
|
|
|
});
|
2023-06-27 21:37:13 -07:00
|
|
|
|
|
|
|
describe('Authentication header', () => {
|
|
|
|
test('一般リクエスト', async () => {
|
|
|
|
await successfulApiCall({
|
2024-03-03 03:15:35 -08:00
|
|
|
endpoint: 'admin/get-index-stats',
|
2023-06-27 21:37:13 -07:00
|
|
|
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 () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const result = await api('admin/get-index-stats', {}, {
|
2023-06-27 21:37:13 -07:00
|
|
|
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 () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
const result = await api('admin/get-index-stats', {});
|
2023-06-27 21:37:13 -07:00
|
|
|
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 () => {
|
2024-03-03 03:15:35 -08:00
|
|
|
// @ts-expect-error text must be string
|
|
|
|
const result = await api('notes/create', { text: true }, {
|
2023-06-27 21:37:13 -07:00
|
|
|
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'));
|
|
|
|
});
|
|
|
|
|
2023-07-27 02:51:58 -07:00
|
|
|
describe('invalid bearer format', () => {
|
|
|
|
test('No preceding bearer', async () => {
|
|
|
|
const result = await relativeFetch('api/notes/create', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: alice.token,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ text: 'test' }),
|
|
|
|
});
|
|
|
|
assert.strictEqual(result.status, 401);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Lowercase bearer', async () => {
|
|
|
|
const result = await relativeFetch('api/notes/create', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: `bearer ${alice.token}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ text: 'test' }),
|
|
|
|
});
|
|
|
|
assert.strictEqual(result.status, 401);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('No space after bearer', async () => {
|
|
|
|
const result = await relativeFetch('api/notes/create', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer${alice.token}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ text: 'test' }),
|
|
|
|
});
|
|
|
|
assert.strictEqual(result.status, 401);
|
|
|
|
});
|
|
|
|
});
|
2023-06-27 21:37:13 -07:00
|
|
|
});
|
2018-10-15 14:37:21 -07:00
|
|
|
});
|