Tuitio client authentication test

master
Tudor Stanciu 2023-02-11 03:05:48 +02:00
parent 2eb3c83d0e
commit 413594b6ed
2 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1,25 @@
/**
* @jest-environment jsdom
*/
import axios from "axios";
import { TuitioClient } from "../client";
jest.mock("axios");
test("Tuitio client authentication", async () => {
(axios.request as jest.Mock).mockResolvedValue({
data: {
token: { raw: "mock-user-pass", validFrom: new Date("10/02/2023"), validUntil: new Date("11/02/2023") },
status: "_MOCK_"
}
});
const client = new TuitioClient("https://test.com/api");
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_");
});

View File

@ -26,7 +26,7 @@ test("Tuitio client initialization", () => {
expect(client.baseUrl).toBe("https://test.com/api"); expect(client.baseUrl).toBe("https://test.com/api");
}); });
test("Tuitio client authentication", async () => { test("Tuitio client fake authentication", async () => {
const client = new TuitioClient("https://test.com/api"); const client = new TuitioClient("https://test.com/api");
const spy = jest.spyOn(client, "authenticate").mockImplementation(async (userName, password) => { 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 token = <TuitioToken>{ raw: `mock-${userName}-${password}`, validFrom: new Date("10/02/2023"), validUntil: new Date("11/02/2023") };