-
Notifications
You must be signed in to change notification settings - Fork 56
refactor(spx-gui): remove jwt-decode and reduce cached username dependencies #3224
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
cn0809
wants to merge
6
commits into
goplus:dev
Choose a base branch
from
cn0809:dev
base: dev
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06c559e
remove jwt-based username parsing from sign-in flow
cn0809 de37b29
reduce cached username dependencies
cn0809 3cc3b81
harden signed-in user sync and cache scoping
cn0809 b91ba64
decouple user-scoped storage from cached usernames
cn0809 bf91f2f
remove local cached username
cn0809 0590eb4
restore cached username for user-scoped state
cn0809 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,126 @@ | ||
| import { ref, shallowRef } from 'vue' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { withSetup } from '@/utils/test' | ||
|
|
||
| const exchangeForAccessToken = vi.fn() | ||
| const refreshAccessToken = vi.fn() | ||
| const signinRedirect = vi.fn() | ||
| const getSignedInUser = vi.fn() | ||
| const clientGet = vi.fn() | ||
| const useVueQueryMock = vi.fn() | ||
|
|
||
| class MockCasdoorSdk { | ||
| exchangeForAccessToken() { | ||
| return exchangeForAccessToken() | ||
| } | ||
|
|
||
| refreshAccessToken(...args: unknown[]) { | ||
| return refreshAccessToken(...args) | ||
| } | ||
|
|
||
| signin_redirect(...args: unknown[]) { | ||
| return signinRedirect(...args) | ||
| } | ||
| } | ||
|
|
||
| vi.mock('casdoor-js-sdk', () => ({ | ||
| default: MockCasdoorSdk | ||
| })) | ||
|
|
||
| const Client = vi.fn(function MockClient(this: { get: typeof clientGet; setTokenProvider: ReturnType<typeof vi.fn> }) { | ||
| this.get = clientGet | ||
| this.setTokenProvider = vi.fn() | ||
| }) | ||
|
|
||
| vi.mock('@/apis/common/client', () => ({ | ||
| Client | ||
| })) | ||
|
|
||
| vi.mock('@/apis/user', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import('@/apis/user')>() | ||
| return { | ||
| ...actual, | ||
| getSignedInUser | ||
| } | ||
| }) | ||
|
|
||
| vi.mock('@/utils/query', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import('@/utils/query')>() | ||
| return { | ||
| ...actual, | ||
| useQueryWithCache: useVueQueryMock | ||
| } | ||
| }) | ||
|
|
||
| describe('signed-in user query key scope', () => { | ||
| beforeEach(() => { | ||
| localStorage.clear() | ||
| sessionStorage.clear() | ||
| exchangeForAccessToken.mockReset() | ||
| refreshAccessToken.mockReset() | ||
| signinRedirect.mockReset() | ||
| getSignedInUser.mockReset() | ||
| clientGet.mockReset() | ||
| Client.mockClear() | ||
| useVueQueryMock.mockReset() | ||
| useVueQueryMock.mockReturnValue({ | ||
| isLoading: ref(false), | ||
| data: shallowRef(null), | ||
| error: shallowRef(null), | ||
| progress: shallowRef({ percentage: 0, timeLeft: null, desc: null }), | ||
| refetch: vi.fn() | ||
| }) | ||
| vi.resetModules() | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| const userStore = await import('./signed-in') | ||
| userStore.signOut() | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it('should change the signed-in user query key across sign-in and sign-out transitions', async () => { | ||
| const userStore = await import('./signed-in') | ||
| clientGet.mockResolvedValue({ username: 'alice' }) | ||
|
|
||
| withSetup(() => userStore.useSignedInUser()) | ||
| expect(useVueQueryMock).toHaveBeenLastCalledWith(expect.objectContaining({ queryKey: expect.any(Object) })) | ||
| let queryKey = useVueQueryMock.mock.lastCall?.[0]?.queryKey | ||
| expect(queryKey.value).toEqual(['signed-in-user', 0]) | ||
|
|
||
| await userStore.signInWithAccessToken('token-a') | ||
| withSetup(() => userStore.useSignedInUser()) | ||
| queryKey = useVueQueryMock.mock.lastCall?.[0]?.queryKey | ||
| expect(queryKey.value).toEqual(['signed-in-user', 1]) | ||
|
|
||
| userStore.signOut() | ||
| withSetup(() => userStore.useSignedInUser()) | ||
| queryKey = useVueQueryMock.mock.lastCall?.[0]?.queryKey | ||
| expect(queryKey.value).toEqual(['signed-in-user', 2]) | ||
| }) | ||
|
|
||
| it('should resolve username from the access token when signing in with a token', async () => { | ||
| const userStore = await import('./signed-in') | ||
| clientGet.mockResolvedValue({ username: 'alice' }) | ||
|
|
||
| await expect(userStore.signInWithAccessToken('token-a')).resolves.toBeUndefined() | ||
| expect(userStore.isSignedIn()).toBe(true) | ||
| expect(userStore.getUnresolvedSignedInUsername()).toBe('alice') | ||
| expect(clientGet).toHaveBeenCalledWith('/user') | ||
| expect(getSignedInUser).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should not bump auth-session scope when resolving access token for a guest session', async () => { | ||
| const userStore = await import('./signed-in') | ||
|
|
||
| withSetup(() => userStore.useSignedInUser()) | ||
| let queryKey = useVueQueryMock.mock.lastCall?.[0]?.queryKey | ||
| expect(queryKey.value).toEqual(['signed-in-user', 0]) | ||
|
|
||
| await expect(userStore.ensureAccessToken()).resolves.toBeNull() | ||
|
|
||
| withSetup(() => userStore.useSignedInUser()) | ||
| queryKey = useVueQueryMock.mock.lastCall?.[0]?.queryKey | ||
| expect(queryKey.value).toEqual(['signed-in-user', 0]) | ||
| }) | ||
| }) |
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.