Tuitio client authentication test

master
Tudor Stanciu 2023-02-11 02:21:48 +02:00
parent 8b28ec10eb
commit 2eb3c83d0e
1 changed files with 21 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import { getUrlTemplates } from "../config";
import { combineUrls } from "../utils";
import { TuitioClient } from "../client";
import type { TuitioAuthenticationResult, TuitioToken } from "../client";
test("Combine urls with trailing slash", () => {
const result = combineUrls("https://test.com/api/", "/test");
@ -21,6 +22,24 @@ test("Get url templates", () => {
});
test("Tuitio client initialization", () => {
const result = new TuitioClient("https://test.com/api");
expect(result.baseUrl).toBe("https://test.com/api");
const client = new TuitioClient("https://test.com/api");
expect(client.baseUrl).toBe("https://test.com/api");
});
test("Tuitio client authentication", async () => {
const client = new TuitioClient("https://test.com/api");
const spy = jest.spyOn(client, "authenticate").mockImplementation(async (userName, password) => {
const token = <TuitioToken>{ raw: `mock-${userName}-${password}`, validFrom: new Date("10/02/2023"), validUntil: new Date("11/02/2023") };
const authResult = <TuitioAuthenticationResult>{ token, status: "_MOCK_" };
return authResult;
});
const result = await client.authenticate("user", "pass");
expect(result.token.raw).toBe("mock-user-pass");
expect(result.token.validFrom).toStrictEqual(new Date("10/02/2023"));
expect(result.token.validUntil).toStrictEqual(new Date("11/02/2023"));
expect(result.status).toBe("_MOCK_");
spy.mockRestore();
});