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