From 708c63907abeff0908c5b590b5b220312ca692a2 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 28 Mar 2023 19:47:23 +0300 Subject: [PATCH] user-info tests --- src/__tests__/TuitioClient.test.ts | 1 + src/__tests__/TuitioUserInfo.test.ts | 33 ++++++++++++++++++++++++++++ src/types.ts | 8 +++---- src/utils/axios.ts | 2 +- 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/__tests__/TuitioUserInfo.test.ts diff --git a/src/__tests__/TuitioClient.test.ts b/src/__tests__/TuitioClient.test.ts index 21e036b..84582c4 100644 --- a/src/__tests__/TuitioClient.test.ts +++ b/src/__tests__/TuitioClient.test.ts @@ -8,6 +8,7 @@ test("Get url templates", () => { const result = getUrlTemplates("https://test.com/api"); expect(result).toHaveProperty("login"); expect(result).toHaveProperty("logout"); + expect(result).toHaveProperty("userInfo"); expect(result.login).toContain("https://test.com/api"); expect(result.login).toContain("{username}"); expect(result.login).toContain("{password}"); diff --git a/src/__tests__/TuitioUserInfo.test.ts b/src/__tests__/TuitioUserInfo.test.ts new file mode 100644 index 0000000..0da17fc --- /dev/null +++ b/src/__tests__/TuitioUserInfo.test.ts @@ -0,0 +1,33 @@ +/** + * @jest-environment jsdom + */ + +// Copyright (c) 2023 Tudor Stanciu + +import axios from "axios"; +import { TuitioClient } from "../client"; + +jest.mock("axios"); + +test("Tuitio client: successfully obtain data from getUserInfo method", async () => { + (axios.request as jest.Mock).mockResolvedValue({ + data: { + userId: 1, + userName: "tuitio.test", + firstName: "Tuitio", + lastName: "Test", + email: "tuitio.test@lab.com", + profilePictureUrl: "https://domain/cdn/resources/test.jpg", + securityStamp: "mock", + creationDate: "2023-03-08T11:46:31.453", + failedLoginAttempts: 0, + lastLoginDate: "2023-03-27T16:09:26.283" + } + }); + + const client = new TuitioClient("https://test.com/api"); + const userInfoResult = await client.getUserInfo(); + + expect(userInfoResult.userId).toBe(1); + expect(userInfoResult.userName).toBe("tuitio.test"); +}); diff --git a/src/types.ts b/src/types.ts index f0dc4e4..791a000 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,12 +14,12 @@ export type TuitioUserInfoResponse = { userName: string; firstName: string; lastName: string; - email: string; - profilePictureUrl: string; + email?: string; + profilePictureUrl?: string; securityStamp: string; creationDate: Date; - failedLoginAttempts: number; - lastLoginDate: Date; + failedLoginAttempts?: number; + lastLoginDate?: Date; claim?: object; contactOptions?: [TuitioUserContactOption]; }; diff --git a/src/utils/axios.ts b/src/utils/axios.ts index a1275cc..1216957 100644 --- a/src/utils/axios.ts +++ b/src/utils/axios.ts @@ -6,7 +6,7 @@ import { fetch } from "../state"; function getHeaders(): AxiosHeaders { const { token } = fetch(); const headers = new AxiosHeaders({ - "Content-Type": "application/json" + ["Content-Type"]: "application/json" }); if (token) { headers["Authorization"] = `Tuitio ${token}`;