-
-
Notifications
You must be signed in to change notification settings - Fork 151
[change] Extracted storage logic from Status component #918 #1070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kunalverma2512
wants to merge
2
commits into
openwisp:master
Choose a base branch
from
kunalverma2512:refactor-status-storage-918
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−60
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,59 @@ | ||
| import {localStorage} from "./storage"; | ||
|
|
||
| /** | ||
| * Stores a value in both cookies and localStorage if synchronous | ||
| * captive portal authentication is enabled. | ||
| * | ||
| * In synchronous authentication, submitting the captive portal form | ||
| * triggers a page reload, which resets the component state. | ||
| * Storing the value in cookies ensures it persists across reloads. | ||
| * | ||
| * The value is also saved in localStorage as a fallback in case the browser does not support cookies. | ||
| * | ||
| * @param {boolean} captivePortalSyncAuth - Whether synchronous authentication is enabled. | ||
| * @param {string} key - The key under which the value is stored. | ||
| * @param {boolean} value - The value to store. | ||
| * @param {Cookies} cookies - The cookies instance used to set the cookie. | ||
| */ | ||
| export const storeValue = (captivePortalSyncAuth, key, value, cookies) => { | ||
| if (!captivePortalSyncAuth) { | ||
| return; | ||
| } | ||
| localStorage.setItem(key, value); | ||
| cookies.set(key, value, {path: "/", maxAge: 60}); | ||
| }; | ||
|
|
||
| /** | ||
| * Resolves the correct value by checking cookies, then localStorage, | ||
| * falling back to a default value if neither is found. | ||
| * | ||
| * @param {boolean} captivePortalSyncAuth - Whether synchronization is enabled. | ||
| * @param {string} key - The key to look for in cookies and localStorage. | ||
| * @param {*} fallback - The fallback value if no valid stored value is found. | ||
| * @param {Cookies} cookies - The cookies instance used to get the cookie. | ||
| * @returns {*} - The selected value based on storage or fallback. | ||
| */ | ||
| export const resolveStoredValue = ( | ||
| captivePortalSyncAuth, | ||
| key, | ||
| fallback, | ||
| cookies, | ||
| ) => { | ||
| if (!captivePortalSyncAuth) { | ||
| return fallback; | ||
| } | ||
|
|
||
| const cookieValue = cookies.get(key); | ||
| if (cookieValue !== undefined) { | ||
| localStorage.removeItem(key); | ||
| return cookieValue === true || cookieValue === "true"; | ||
| } | ||
|
|
||
| const localStorageValue = localStorage.getItem(key); | ||
| if (localStorageValue !== null) { | ||
| localStorage.removeItem(key); | ||
| return localStorageValue === "true"; | ||
| } | ||
|
|
||
| return fallback; | ||
| }; | ||
|
kunalverma2512 marked this conversation as resolved.
|
||
This file contains hidden or 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,64 @@ | ||
| import {resolveStoredValue} from "./captive-portal-storage"; | ||
| import {localStorage} from "./storage"; | ||
|
|
||
| // Mock the internal storage dependency | ||
| jest.mock("./storage", () => ({ | ||
| localStorage: { | ||
| getItem: jest.fn(), | ||
| setItem: jest.fn(), | ||
| removeItem: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe("captive-portal-storage utility", () => { | ||
| let mockCookies; | ||
|
|
||
| beforeEach(() => { | ||
| // Clear mocks before each test | ||
| jest.clearAllMocks(); | ||
|
|
||
| mockCookies = { | ||
| get: jest.fn(), | ||
| set: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe("resolveStoredValue", () => { | ||
| const key = "testKey"; | ||
| const fallback = "fallbackValue"; | ||
|
|
||
| it("returns fallback when captivePortalSyncAuth is false", () => { | ||
| const result = resolveStoredValue(false, key, fallback, mockCookies); | ||
| expect(result).toBe(fallback); | ||
| expect(mockCookies.get).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns true and removes localStorage when cookie exists", () => { | ||
| mockCookies.get.mockReturnValue("true"); // Cookie exists | ||
|
|
||
| const result = resolveStoredValue(true, key, fallback, mockCookies); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(localStorage.removeItem).toHaveBeenCalledWith(key); | ||
| }); | ||
|
|
||
| it("returns true and removes localStorage when cookie is undefined but localStorage exists", () => { | ||
| mockCookies.get.mockReturnValue(undefined); // No cookie | ||
| localStorage.getItem.mockReturnValue("true"); // Has localStorage | ||
|
|
||
| const result = resolveStoredValue(true, key, fallback, mockCookies); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(localStorage.removeItem).toHaveBeenCalledWith(key); | ||
| }); | ||
|
|
||
| it("returns fallback when neither cookie nor localStorage is present", () => { | ||
| mockCookies.get.mockReturnValue(undefined); | ||
| localStorage.getItem.mockReturnValue(null); | ||
|
|
||
| const result = resolveStoredValue(true, key, fallback, mockCookies); | ||
|
|
||
| expect(result).toBe(fallback); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.