1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-25 20:18:07 +09:00
hotomoe/test/api.ts

213 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-05-23 12:19:39 +09:00
import { APIClient, isAPIError } from '../src/api';
2021-05-16 11:11:05 +09:00
import { enableFetchMocks } from 'jest-fetch-mock';
enableFetchMocks();
function getFetchCall(call: any[]) {
const { body, method } = call[1];
if (body != null && typeof body != 'string') {
throw new Error('invalid body');
}
return {
url: call[0],
method: method,
body: JSON.parse(body as any)
};
}
2021-05-14 13:49:40 +09:00
describe('API', () => {
test('success', async () => {
2021-05-16 11:11:05 +09:00
fetchMock.resetMocks();
fetchMock.mockResponse(async (req) => {
const body = await req.json();
if (req.method == 'POST' && req.url == 'https://misskey.test/api/i') {
2021-05-23 12:15:28 +09:00
if (body.i === 'TOKEN') {
return JSON.stringify({ id: 'foo' });
} else {
2021-05-16 11:11:05 +09:00
return { status: 400 };
2021-05-14 13:49:40 +09:00
}
2021-05-16 11:11:05 +09:00
} else {
return { status: 404 };
}
});
2021-05-14 13:49:40 +09:00
2021-05-23 12:15:28 +09:00
const cli = new APIClient({
origin: 'https://misskey.test',
credential: 'TOKEN',
});
const res = await cli.request('i');
2021-05-14 13:49:40 +09:00
expect(res).toEqual({
id: 'foo'
});
2021-05-16 11:11:05 +09:00
expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
2021-05-14 13:49:40 +09:00
url: 'https://misskey.test/api/i',
2021-05-16 11:11:05 +09:00
method: 'POST',
2021-05-14 13:49:40 +09:00
body: { i: 'TOKEN' }
2021-05-16 11:11:05 +09:00
});
2021-05-14 13:49:40 +09:00
});
2021-05-17 23:55:13 +09:00
2021-06-25 09:58:35 +09:00
test('with params', async () => {
fetchMock.resetMocks();
fetchMock.mockResponse(async (req) => {
const body = await req.json();
if (req.method == 'POST' && req.url == 'https://misskey.test/api/notes/show') {
if (body.i === 'TOKEN' && body.noteId === 'aaaaa') {
return JSON.stringify({ id: 'foo' });
} else {
return { status: 400 };
}
} else {
return { status: 404 };
}
});
const cli = new APIClient({
origin: 'https://misskey.test',
credential: 'TOKEN',
});
const res = await cli.request('notes/show', { noteId: 'aaaaa' });
expect(res).toEqual({
id: 'foo'
});
expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
url: 'https://misskey.test/api/notes/show',
method: 'POST',
body: { i: 'TOKEN', noteId: 'aaaaa' }
});
});
test('204 No Content で null が返る', async () => {
fetchMock.resetMocks();
fetchMock.mockResponse(async (req) => {
if (req.method == 'POST' && req.url == 'https://misskey.test/api/reset-password') {
return { status: 204 };
} else {
return { status: 404 };
}
});
const cli = new APIClient({
origin: 'https://misskey.test',
credential: 'TOKEN',
});
const res = await cli.request('reset-password', { token: 'aaa', password: 'aaa' });
expect(res).toEqual(null);
expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
url: 'https://misskey.test/api/reset-password',
method: 'POST',
body: { i: 'TOKEN', token: 'aaa', password: 'aaa' }
});
});
2021-05-23 12:32:58 +09:00
test('インスタンスの credential が指定されていても引数で credential が null ならば null としてリクエストされる', async () => {
fetchMock.resetMocks();
fetchMock.mockResponse(async (req) => {
const body = await req.json();
if (req.method == 'POST' && req.url == 'https://misskey.test/api/i') {
if (typeof body.i === 'string') {
return JSON.stringify({ id: 'foo' });
} else {
return {
status: 401,
body: JSON.stringify({
error: {
message: 'Credential required.',
code: 'CREDENTIAL_REQUIRED',
id: '1384574d-a912-4b81-8601-c7b1c4085df1',
}
})
};
}
} else {
return { status: 404 };
}
});
try {
const cli = new APIClient({
origin: 'https://misskey.test',
credential: 'TOKEN',
});
await cli.request('i', {}, null);
} catch (e) {
expect(isAPIError(e)).toEqual(true);
}
});
2021-05-23 12:22:25 +09:00
test('api error', async () => {
2021-05-19 14:01:04 +09:00
fetchMock.resetMocks();
fetchMock.mockResponse(async (req) => {
return {
status: 500,
body: JSON.stringify({
2021-05-23 12:19:39 +09:00
error: {
message: 'Internal error occurred. Please contact us if the error persists.',
code: 'INTERNAL_ERROR',
id: '5d37dbcb-891e-41ca-a3d6-e690c97775ac',
kind: 'server',
},
2021-05-19 14:01:04 +09:00
})
};
});
try {
2021-05-23 12:15:28 +09:00
const cli = new APIClient({
origin: 'https://misskey.test',
credential: 'TOKEN',
});
await cli.request('i');
2022-01-01 21:52:46 +09:00
} catch (e: any) {
2021-05-23 12:19:39 +09:00
expect(isAPIError(e)).toEqual(true);
2021-05-19 14:01:04 +09:00
expect(e.id).toEqual('5d37dbcb-891e-41ca-a3d6-e690c97775ac');
}
});
2021-05-17 23:55:13 +09:00
2021-05-23 12:22:25 +09:00
test('network error', async () => {
fetchMock.resetMocks();
fetchMock.mockAbort();
2021-05-23 12:15:28 +09:00
2021-05-23 12:22:25 +09:00
try {
const cli = new APIClient({
origin: 'https://misskey.test',
credential: 'TOKEN',
});
await cli.request('i');
} catch (e) {
expect(isAPIError(e)).toEqual(false);
}
});
test('json parse error', async () => {
fetchMock.resetMocks();
fetchMock.mockResponse(async (req) => {
return {
status: 500,
body: '<html>I AM NOT JSON</html>'
};
});
try {
const cli = new APIClient({
origin: 'https://misskey.test',
credential: 'TOKEN',
});
await cli.request('i');
} catch (e) {
expect(isAPIError(e)).toEqual(false);
}
});
2021-05-14 13:49:40 +09:00
});