Skip to content

Commit

Permalink
fix: fix tests not passing
Browse files Browse the repository at this point in the history
  • Loading branch information
jjgancfer committed Mar 5, 2024
1 parent d6beb38 commit 072f943
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 12 additions & 6 deletions webapp/src/components/auth/AuthUtils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import axios, { HttpStatusCode } from "axios";

export function isUserLogged() {
return localStorage.getItem("authData") !== null;
return getLoginData().jwtToken !== null;
}

export function saveToken(requestAnswer) {
axios.defaults.headers.common["Authorization"] = "Bearer " + requestAnswer.data.token;
localStorage.setItem("authData", {
"jwtToken": requestAnswer.data.token,
"refreshToken": requestAnswer.data.refresh_Token,
"receivedOnUTC": Date.now()
});
sessionStorage.setItem("jwtToken", requestAnswer.data.token);
sessionStorage.setItem("jwtRefreshToken", requestAnswer.data.refresh_Token);
sessionStorage.setItem("jwtReceptionMillis", Date.now().toString());
}

export function getLoginData() {
return {
"jwtToken": sessionStorage.getItem("jwtToken"),
"jwtRefreshToken": sessionStorage.getItem("jwtRefreshToken"),
"jwtReceptionDate": new Date(sessionStorage.getItem("jwtReceptionMillis"))
};
}

export async function login(loginData, onSuccess, onError) {
Expand Down
8 changes: 4 additions & 4 deletions webapp/src/tests/AuthUtils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MockAdapter from "axios-mock-adapter";
import axios, { HttpStatusCode } from "axios";
import { isUserLogged, login } from "components/auth/AuthUtils";
import { isUserLogged, login} from "components/auth/AuthUtils";

const mockAxios = new MockAdapter(axios);

Expand Down Expand Up @@ -35,15 +35,15 @@ describe("Auth Utils tests", () => {

//Check the user is now logged in
expect(isUserLogged()).toBe(true);
expect(localStorage.getItem("authData").jwtToken).toBe("token");
expect(localStorage.getItem("authData").jwtRefreshToken).toBe("refreshToken");
expect(sessionStorage.getItem("jwtToken")).toBe("token");
expect(sessionStorage.getItem("jwtRefreshToken")).toBe("refreshToken");
});
});

describe("when the user is authenticated", () => {

beforeAll(() => {
localStorage.setItem("authData", {
sessionStorage.setItem("authData", {
"token": "testToken"
});
})
Expand Down

0 comments on commit 072f943

Please sign in to comment.