account logout method fix

master
Tudor Stanciu 2023-03-17 18:26:30 +02:00
parent 1caa46972b
commit 3767a8eeb4
4 changed files with 34 additions and 11 deletions

View File

@ -48,8 +48,8 @@ test("Tuitio client fake authentication", async () => {
const loginResult = await client.login("user", "pass"); const loginResult = await client.login("user", "pass");
expect(loginResult.result.token).toBe("mock-user-pass"); expect(loginResult.result?.token).toBe("mock-user-pass");
expect(loginResult.result.expiresIn).toBe(600000); expect(loginResult.result?.expiresIn).toBe(600000);
expect(loginResult.error).toBeNull; expect(loginResult.error).toBeNull;
spy.mockRestore(); spy.mockRestore();

View File

@ -23,8 +23,8 @@ test("Tuitio client: successfully account login", async () => {
const client = new TuitioClient("https://test.com/api"); const client = new TuitioClient("https://test.com/api");
const loginResult = await client.login("user", "pass"); const loginResult = await client.login("user", "pass");
expect(loginResult.result.token).toBe("token-mock"); expect(loginResult.result?.token).toBe("token-mock");
expect(loginResult.result.expiresIn).toBe(600000); expect(loginResult.result?.expiresIn).toBe(600000);
expect(loginResult.error).toBeNull(); expect(loginResult.error).toBeNull();
const storage = fetch(); const storage = fetch();

View File

@ -6,9 +6,27 @@
import axios from "axios"; import axios from "axios";
import { TuitioClient, fetch } from "../client"; import { TuitioClient, fetch } from "../client";
import { localStorage } from "@flare/js-utils";
import { storageKeys } from "../constants";
const { setItem } = localStorage;
jest.mock("axios"); jest.mock("axios");
test("Tuitio client: error if account logout is called before login", async () => {
(axios.request as jest.Mock).mockResolvedValue({
data: {
result: { userId: 0, userName: "tuitio.user", logoutDate: new Date() },
error: null
}
});
const client = new TuitioClient("https://test.com/api");
const logoutResult = await client.logout();
expect(logoutResult.result).toBeNull();
expect(logoutResult.error).toBe("UNAUTHENTICATED");
});
test("Tuitio client: successfully account logout", async () => { test("Tuitio client: successfully account logout", async () => {
(axios.request as jest.Mock).mockResolvedValue({ (axios.request as jest.Mock).mockResolvedValue({
data: { data: {
@ -18,10 +36,13 @@ test("Tuitio client: successfully account logout", async () => {
}); });
const client = new TuitioClient("https://test.com/api"); const client = new TuitioClient("https://test.com/api");
const logoutResult = await client.logout("token-mock"); const authData = { token: "mocked-token", validUntil: new Date(), userName: "tuitio.user" };
setItem(storageKeys.AUTH_DATA, authData);
expect(logoutResult.result.userId).toBe(0); const logoutResult = await client.logout();
expect(logoutResult.result.userName).toBe("tuitio.user");
expect(logoutResult.result?.userId).toBe(0);
expect(logoutResult.result?.userName).toBe("tuitio.user");
expect(logoutResult.error).toBeNull(); expect(logoutResult.error).toBeNull();
const storage = fetch(); const storage = fetch();

View File

@ -25,8 +25,8 @@ async function request(url: string, method: string) {
} }
} }
export type TuitioLoginResult = { result: { token: string; expiresIn: number }; error: string | null }; export type TuitioLoginResult = { result: { token: string; expiresIn: number } | null; error: string | null };
export type TuitioLogoutResult = { result: { userId: number; userName: string; logoutDate: Date }; error: string }; export type TuitioLogoutResult = { result: { userId: number; userName: string; logoutDate: Date } | null; error: string };
class TuitioClient { class TuitioClient {
tuitioUrl: string; tuitioUrl: string;
@ -56,8 +56,10 @@ class TuitioClient {
return response; return response;
} }
async logout(token: string): Promise<TuitioLogoutResult> { async logout(): Promise<TuitioLogoutResult> {
const url = this.urlTemplates.logout.replace("{token}", token); const authData = getItem(storageKeys.AUTH_DATA);
if (!authData) return { result: null, error: "UNAUTHENTICATED" } as TuitioLogoutResult;
const url = this.urlTemplates.logout.replace("{token}", authData.token);
const response = await request(url, "post"); const response = await request(url, "post");
if (!response.error) { if (!response.error) {
removeItem(storageKeys.AUTH_DATA); removeItem(storageKeys.AUTH_DATA);