From 2eb3c83d0eed049354365c9dc3f58afdd7bda7ca Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 11 Feb 2023 02:21:48 +0200 Subject: [PATCH] Tuitio client authentication test --- src/__tests__/TuitioClient.test.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/__tests__/TuitioClient.test.ts b/src/__tests__/TuitioClient.test.ts index d736877..de6fd88 100644 --- a/src/__tests__/TuitioClient.test.ts +++ b/src/__tests__/TuitioClient.test.ts @@ -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 = { raw: `mock-${userName}-${password}`, validFrom: new Date("10/02/2023"), validUntil: new Date("11/02/2023") }; + const authResult = { 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(); });