-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #433: save validation progress in cookies
- Loading branch information
Showing
8 changed files
with
151 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Cookies from "js-cookie"; | ||
import { VERIFIED_LABEL_DATA, VERIFIED_LABEL_DATA_WITH_ID } from "../constants"; | ||
import { | ||
getAuthHeader, | ||
getLabelDataFromCookies, | ||
setLabelDataInCookies, | ||
} from "../cookies"; | ||
|
||
jest.mock("js-cookie"); | ||
|
||
describe("getAuthHeader", () => { | ||
it("should return the correct Authorization header", () => { | ||
(Cookies.get as jest.Mock).mockReturnValue(btoa("testUser:password")); | ||
const authHeader = getAuthHeader(); | ||
expect(authHeader).toBe("Basic dGVzdFVzZXI6cGFzc3dvcmQ6"); | ||
}); | ||
|
||
it("should return empty Authorization header if token is missing", () => { | ||
(Cookies.get as jest.Mock).mockReturnValue(undefined); | ||
const authHeader = getAuthHeader(); | ||
expect(authHeader).toBe("Basic Og=="); | ||
}); | ||
}); | ||
|
||
describe("setLabelDataInCookies", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should set label data with an inspectionId", () => { | ||
setLabelDataInCookies(VERIFIED_LABEL_DATA_WITH_ID); | ||
expect(Cookies.set).toHaveBeenCalledWith( | ||
"labelData_1234", | ||
JSON.stringify(VERIFIED_LABEL_DATA_WITH_ID), | ||
{ expires: 1 }, | ||
); | ||
}); | ||
|
||
it("should set label data with default name if no inspectionId", () => { | ||
setLabelDataInCookies(VERIFIED_LABEL_DATA); | ||
expect(Cookies.set).toHaveBeenCalledWith( | ||
"labelData_no_id", | ||
JSON.stringify(VERIFIED_LABEL_DATA), | ||
{ expires: 1 }, | ||
); | ||
}); | ||
}); | ||
|
||
describe("getLabelDataFromCookies", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should retrieve label data if cookie exists", () => { | ||
(Cookies.get as jest.Mock).mockReturnValue( | ||
JSON.stringify(VERIFIED_LABEL_DATA_WITH_ID), | ||
); | ||
const result = getLabelDataFromCookies("1234"); | ||
expect(result).toEqual(VERIFIED_LABEL_DATA_WITH_ID); | ||
}); | ||
|
||
it("should return null if cookie does not exist", () => { | ||
(Cookies.get as jest.Mock).mockReturnValue(undefined); | ||
const result = getLabelDataFromCookies("1234"); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it("should retrieve label data with default name if no inspectionId", () => { | ||
(Cookies.get as jest.Mock).mockReturnValue( | ||
JSON.stringify(VERIFIED_LABEL_DATA), | ||
); | ||
const result = getLabelDataFromCookies(null); | ||
expect(result).toEqual(VERIFIED_LABEL_DATA); | ||
}); | ||
|
||
it("should retrieve label data with default name if inspectionId is an empty string", () => { | ||
(Cookies.get as jest.Mock).mockReturnValue( | ||
JSON.stringify(VERIFIED_LABEL_DATA), | ||
); | ||
const result = getLabelDataFromCookies(""); | ||
expect(result).toEqual(VERIFIED_LABEL_DATA); | ||
}); | ||
|
||
it("should retrieve label data with default name if inspectionId is undefined", () => { | ||
(Cookies.get as jest.Mock).mockReturnValue( | ||
JSON.stringify(VERIFIED_LABEL_DATA), | ||
); | ||
const result = getLabelDataFromCookies(undefined); | ||
expect(result).toEqual(VERIFIED_LABEL_DATA); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { LabelData } from "@/types/types"; | ||
import Cookies from "js-cookie"; | ||
|
||
const COOKIE_PREFIX = "labelData_"; | ||
|
||
const getCookieName = (inspectionId?: string | null) => | ||
COOKIE_PREFIX + (inspectionId || "no_id"); | ||
|
||
export const getAuthHeader = () => { | ||
return "Basic " + btoa(`${atob(Cookies.get("token") ?? "")}:`); | ||
}; | ||
|
||
export const setLabelDataInCookies = (labelData: LabelData) => { | ||
const cookieName = getCookieName(labelData.inspectionId); | ||
|
||
console.debug(`[Cookies] Setting label data for:`, cookieName); | ||
|
||
Cookies.set(cookieName, JSON.stringify(labelData), { expires: 1 }); | ||
}; | ||
|
||
export const getLabelDataFromCookies = ( | ||
inspectionId: string | null | undefined, | ||
): LabelData | null => { | ||
const cookieName = getCookieName(inspectionId); | ||
|
||
const data = Cookies.get(cookieName); | ||
if (!data) return null; | ||
|
||
console.debug(`[Cookies] Retrieved label data for:`, cookieName); | ||
return JSON.parse(data); | ||
}; |