tests working

This commit is contained in:
simojenki
2022-04-23 14:18:19 +10:00
parent 2997e5ac3b
commit d2f13416f6
8 changed files with 1886 additions and 1040 deletions

286
tests/http.test.ts Normal file
View File

@@ -0,0 +1,286 @@
import {
http2,
} from "../src/http";
// describe("request modifiers", () => {
// describe("baseUrl", () => {
// it.each([
// [
// { data: "bob" },
// "http://example.com",
// { data: "bob", baseURL: "http://example.com" },
// ],
// [
// { baseURL: "http://originalBaseUrl.example.com" },
// "http://example.com",
// { baseURL: "http://example.com" },
// ],
// ])(
// "should apply the baseUrl",
// (requestConfig: any, value: string, expected: any) => {
// expect(baseUrl(value)(requestConfig)).toEqual(expected);
// }
// );
// });
// describe("params", () => {
// it.each([
// [
// { data: "bob" },
// { param1: "value1", param2: "value2" },
// { data: "bob", params: { param1: "value1", param2: "value2" } },
// ],
// [
// { data: "bob", params: { orig1: "origValue1" } },
// {},
// { data: "bob", params: { orig1: "origValue1" } },
// ],
// [
// { data: "bob", params: { orig1: "origValue1" } },
// { param1: "value1", param2: "value2" },
// {
// data: "bob",
// params: { orig1: "origValue1", param1: "value1", param2: "value2" },
// },
// ],
// ])(
// "should apply the params",
// (requestConfig: any, newParams: any, expected: any) => {
// expect(params(newParams)(requestConfig)).toEqual(expected);
// }
// );
// });
// describe("headers", () => {
// it.each([
// [
// { data: "bob" },
// { h1: "value1", h2: "value2" },
// { data: "bob", headers: { h1: "value1", h2: "value2" } },
// ],
// [
// { data: "bob", headers: { orig1: "origValue1" } },
// {},
// { data: "bob", headers: { orig1: "origValue1" } },
// ],
// [
// { data: "bob", headers: { orig1: "origValue1" } },
// { h1: "value1", h2: "value2" },
// {
// data: "bob",
// headers: { orig1: "origValue1", h1: "value1", h2: "value2" },
// },
// ],
// ])(
// "should apply the headers",
// (requestConfig: any, newParams: any, expected: any) => {
// expect(headers(newParams)(requestConfig)).toEqual(expected);
// }
// );
// });
// describe("chain", () => {
// it.each([
// [
// { data: "bob" },
// [params({ param1: "value1", param2: "value2" })],
// { data: "bob", params: { param1: "value1", param2: "value2" } },
// ],
// [
// { data: "bob" },
// [params({ param1: "value1" }), params({ param2: "value2" })],
// { data: "bob", params: { param1: "value1", param2: "value2" } },
// ],
// [{ data: "bob" }, [], { data: "bob" }],
// ])(
// "should apply the chain",
// (requestConfig: any, newParams: RequestModifier[], expected: any) => {
// expect(chain(...newParams)(requestConfig)).toEqual(expected);
// }
// );
// });
// describe("wrapping", () => {
// const mockAxios = jest.fn();
// describe("baseURL", () => {
// const base = http(
// mockAxios,
// baseUrl("http://original.example.com")
// );
// describe("when no baseURL passed in when being invoked", () => {
// it("should use the original value", () => {
// base({})
// expect(mockAxios).toHaveBeenCalledWith({ baseURL: "http://original.example.com" });
// });
// });
// describe("when a new baseURL is passed in when being invoked", () => {
// it("should use the new value", () => {
// base({ baseURL: "http://new.example.com" })
// expect(mockAxios).toHaveBeenCalledWith({ baseURL: "http://new.example.com" });
// });
// });
// });
// describe("params", () => {
// const base = http(
// mockAxios,
// params({ a: "1", b: "2" })
// );
// it("should apply the modified when invoked", () => {
// base({ method: 'get' });
// expect(mockAxios).toHaveBeenCalledWith({ method: 'get', params: { a: "1", b: "2" }});
// });
// describe("wrapping the base", () => {
// const wrapped = http(base, params({ b: "2b", c: "3" }));
// it("should the wrapped values as priority", () => {
// wrapped({ method: 'get', params: { a: "1b", c: "3b", d: "4" } });
// expect(mockAxios).toHaveBeenCalledWith({ method: 'get', params: { a: "1b", b: "2b", c: "3b", d: "4" }});
// });
// });
// });
// });
// });
describe("http2", () => {
const mockAxios = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});
describe.each([
["baseURL"],
["url"],
])('%s', (field) => {
const getValue = (value: string) => {
const thing = {} as any;
thing[field] = value;
return thing;
};
const base = http2(mockAxios, getValue('base'));
describe("using default", () => {
it("should use the default", () => {
base({})
expect(mockAxios).toHaveBeenCalledWith(getValue('base'));
});
});
describe("overriding", () => {
it("should use the override", () => {
base(getValue('override'))
expect(mockAxios).toHaveBeenCalledWith(getValue('override'));
});
});
describe("wrapping", () => {
const firstLayer = http2(base, getValue('level1'));
const secondLayer = http2(firstLayer, getValue('level2'));
describe("when the outter call provides a value", () => {
it("should apply it", () => {
secondLayer(getValue('outter'))
expect(mockAxios).toHaveBeenCalledWith(getValue('outter'));
});
});
describe("when the outter call does not provide a value", () => {
it("should use the second layer", () => {
secondLayer({ })
expect(mockAxios).toHaveBeenCalledWith(getValue('level2'));
});
});
});
});
describe("requestType", () => {
const base = http2(mockAxios, { responseType: 'stream' });
describe("using default", () => {
it("should use the default", () => {
base({})
expect(mockAxios).toHaveBeenCalledWith({ responseType: 'stream' });
});
});
describe("overriding", () => {
it("should use the override", () => {
base({ responseType: 'arraybuffer' })
expect(mockAxios).toHaveBeenCalledWith({ responseType: 'arraybuffer' });
});
});
describe("wrapping", () => {
const firstLayer = http2(base, { responseType: 'arraybuffer' });
const secondLayer = http2(firstLayer, { responseType: 'blob' });
describe("when the outter call provides a value", () => {
it("should apply it", () => {
secondLayer({ responseType: 'text' })
expect(mockAxios).toHaveBeenCalledWith({ responseType: 'text' });
});
});
describe("when the outter call does not provide a value", () => {
it("should use the second layer", () => {
secondLayer({ })
expect(mockAxios).toHaveBeenCalledWith({ responseType: 'blob' });
});
});
});
});
describe.each([
["params"],
["headers"],
])('%s', (field) => {
const getValues = (values: any) => {
const thing = {} as any;
thing[field] = values;
return thing;
}
const base = http2(mockAxios, getValues({ a: 1, b: 2, c: 3, d: 4 }));
describe("using default", () => {
it("should use the default", () => {
base({});
expect(mockAxios).toHaveBeenCalledWith(getValues({ a: 1, b: 2, c: 3, d: 4 }));
});
});
describe("overriding", () => {
it("should use the override", () => {
base(getValues({ b: 22, e: 5 }));
expect(mockAxios).toHaveBeenCalledWith(getValues({ a: 1, b: 22, c: 3, d: 4, e: 5 }));
});
});
describe("wrapping", () => {
const firstLayer = http2(base, getValues({ b: 22 }));
const secondLayer = http2(firstLayer, getValues({ c: 33 }));
describe("when the outter call provides a value", () => {
it("should apply it", () => {
secondLayer(getValues({ a: 11, e: 5 }));
expect(mockAxios).toHaveBeenCalledWith(getValues({ a: 11, b: 22, c: 33, d: 4, e: 5 }));
});
});
describe("when the outter call does not provide a value", () => {
it("should use the second layer", () => {
secondLayer({ });
expect(mockAxios).toHaveBeenCalledWith(getValues({ a: 1, b: 22, c: 33, d: 4 }));
});
});
});
})
});

View File

@@ -17,7 +17,6 @@ import {
import axios from "axios";
jest.mock("axios");
import randomstring from "randomstring";
jest.mock("randomstring");
@@ -27,7 +26,6 @@ import {
import {
aTrack,
} from "./builders";
import { asURLSearchParams } from "../src/utils";
describe("t", () => {
it("should be an md5 of the password and the salt", () => {
@@ -129,7 +127,10 @@ const pingJson = (pingResponse: Partial<PingResponse> = {}) => ({
const PING_OK = pingJson({ status: "ok" });
describe("Subsonic", () => {
const mockAxios = axios as unknown as jest.Mock;
const url = "http://127.0.0.22:4567";
const baseURL = url;
const username = `user1-${uuid()}`;
const password = `pass1-${uuid()}`;
const salt = "saltysalty";
@@ -141,16 +142,12 @@ describe("Subsonic", () => {
);
const mockRandomstring = jest.fn();
const mockGET = jest.fn();
const mockPOST = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
randomstring.generate = mockRandomstring;
axios.get = mockGET;
axios.post = mockPOST;
mockRandomstring.mockReturnValue(salt);
});
@@ -187,7 +184,7 @@ describe("Subsonic", () => {
describe("when the credentials are valid", () => {
describe("when the backend is generic subsonic", () => {
it("should be able to generate a token and then login using it", async () => {
(axios.get as jest.Mock).mockResolvedValue(ok(PING_OK));
(mockAxios as jest.Mock).mockResolvedValue(ok(PING_OK));
const token = await tokenFor({
username,
@@ -200,15 +197,18 @@ describe("Subsonic", () => {
expect(parseToken(token.serviceToken)).toEqual({ username, password, type: PING_OK["subsonic-response"].type })
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: asURLSearchParams(authParamsPlusJson),
expect(mockAxios).toHaveBeenCalledWith({
method: 'get',
baseURL,
url: `/rest/ping.view`,
params: authParamsPlusJson,
headers,
});
});
it("should store the type of the subsonic server on the token", async () => {
const type = "someSubsonicClone";
(axios.get as jest.Mock).mockResolvedValue(ok(pingJson({ type })));
mockAxios.mockResolvedValue(ok(pingJson({ type })));
const token = await tokenFor({
username,
@@ -221,8 +221,11 @@ describe("Subsonic", () => {
expect(parseToken(token.serviceToken)).toEqual({ username, password, type })
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: asURLSearchParams(authParamsPlusJson),
expect(mockAxios).toHaveBeenCalledWith({
method: 'get',
baseURL,
url: `/rest/ping.view`,
params: authParamsPlusJson,
headers,
});
});
@@ -232,8 +235,9 @@ describe("Subsonic", () => {
it("should login to nd and get the nd bearer token", async () => {
const navidromeToken = `nd-${uuid()}`;
(axios.get as jest.Mock).mockResolvedValue(ok(pingJson({ type: "navidrome" })));
(axios.post as jest.Mock).mockResolvedValue(ok({ token: navidromeToken }));
mockAxios
.mockResolvedValueOnce(ok(pingJson({ type: "navidrome" })))
.mockResolvedValueOnce(ok({ token: navidromeToken }));
const token = await tokenFor({
username,
@@ -246,13 +250,21 @@ describe("Subsonic", () => {
expect(parseToken(token.serviceToken)).toEqual({ username, password, type: "navidrome", bearer: navidromeToken })
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: asURLSearchParams(authParamsPlusJson),
expect(mockAxios).toHaveBeenCalledWith({
method: 'get',
baseURL,
url: `/rest/ping.view`,
params: authParamsPlusJson,
headers,
});
expect(axios.post).toHaveBeenCalledWith(`${url}/auth/login`, {
username,
password,
expect(mockAxios).toHaveBeenCalledWith({
method: 'post',
baseURL,
url: `/auth/login`,
data: {
username,
password,
}
});
});
});
@@ -260,7 +272,7 @@ describe("Subsonic", () => {
describe("when the credentials are not valid", () => {
it("should be able to generate a token and then login using it", async () => {
(axios.get as jest.Mock).mockResolvedValue({
mockAxios.mockResolvedValue({
status: 200,
data: error("40", "Wrong username or password"),
});
@@ -276,7 +288,7 @@ describe("Subsonic", () => {
describe("when the backend is generic subsonic", () => {
it("should be able to generate a token and then login using it", async () => {
const type = `subsonic-clone-${uuid()}`;
(axios.get as jest.Mock).mockResolvedValue(ok(pingJson({ type })));
mockAxios.mockResolvedValue(ok(pingJson({ type })));
const credentials = { username, password, type: "foo", bearer: undefined };
const originalToken = asToken(credentials)
@@ -292,8 +304,11 @@ describe("Subsonic", () => {
expect(parseToken(refreshedToken.serviceToken)).toEqual({ username, password, type })
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: asURLSearchParams(authParamsPlusJson),
expect(mockAxios).toHaveBeenCalledWith({
method:'get',
baseURL,
url: `/rest/ping.view`,
params: authParamsPlusJson,
headers,
});
});
@@ -303,8 +318,9 @@ describe("Subsonic", () => {
it("should login to nd and get the nd bearer token", async () => {
const navidromeToken = `nd-${uuid()}`;
(axios.get as jest.Mock).mockResolvedValue(ok(pingJson({ type: "navidrome" })));
(axios.post as jest.Mock).mockResolvedValue(ok({ token: navidromeToken }));
mockAxios
.mockResolvedValueOnce(ok(pingJson({ type: "navidrome" })))
.mockResolvedValueOnce(ok({ token: navidromeToken }));
const credentials = { username, password, type: "navidrome", bearer: undefined };
const originalToken = asToken(credentials)
@@ -320,13 +336,21 @@ describe("Subsonic", () => {
expect(parseToken(refreshedToken.serviceToken)).toEqual({ username, password, type: "navidrome", bearer: navidromeToken })
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: asURLSearchParams(authParamsPlusJson),
expect(mockAxios).toHaveBeenCalledWith({
method: 'get',
baseURL,
url: `/rest/ping.view`,
params: authParamsPlusJson,
headers,
});
expect(axios.post).toHaveBeenCalledWith(`${url}/auth/login`, {
username,
password,
expect(mockAxios).toHaveBeenCalledWith({
method: 'post',
baseURL,
url: `/auth/login`,
data: {
username,
password,
}
});
});
});
@@ -334,7 +358,7 @@ describe("Subsonic", () => {
describe("when the credentials are not valid", () => {
it("should be able to generate a token and then login using it", async () => {
(axios.get as jest.Mock).mockResolvedValue({
mockAxios.mockResolvedValue({
status: 200,
data: error("40", "Wrong username or password"),
});

File diff suppressed because it is too large Load Diff