-
Notifications
You must be signed in to change notification settings - Fork 1
#48 Add password reset flow to CMS UI #109
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
Draft
InteraktivPreuss
wants to merge
3
commits into
main
Choose a base branch
from
create-password-reset-functionality
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
3 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
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,204 @@ | ||
| import { expect, describe, it, vi, afterEach } from 'vitest'; | ||
| import { RouterContextProvider } from 'react-router'; | ||
| import { ploneClientContext } from '@plone/aurora/app/middleware.server'; | ||
| import { | ||
| action, | ||
| validateIdentifier, | ||
| validatePassword, | ||
| validatePasswordMatch, | ||
| } from './reset-password'; | ||
|
|
||
| vi.mock('@plone/react-router', () => ({ | ||
| getAuthFromRequest: vi.fn().mockResolvedValue(undefined), | ||
| })); | ||
|
|
||
| function buildRequest(fields: Record<string, string>) { | ||
| const body = new URLSearchParams(fields); | ||
| return new Request('http://example.com/reset-password', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| function callAction(request: Request, cli: Record<string, unknown>) { | ||
| const context = new RouterContextProvider(); | ||
| context.set(ploneClientContext, cli as any); | ||
| return action({ | ||
| request, | ||
| params: {}, | ||
| context, | ||
| unstable_pattern: '/reset-password', | ||
| unstable_url: new URL(request.url), | ||
| }) as Promise<any>; | ||
| } | ||
|
|
||
| describe('reset-password validators', () => { | ||
| it('rejects an empty identifier', () => { | ||
| expect(validateIdentifier(' ')).toEqual({ code: 'identifierRequired' }); | ||
| }); | ||
|
|
||
| it('rejects an email when email-as-login is disabled', () => { | ||
| expect(validateIdentifier('foo@bar.com')).toEqual({ | ||
| code: 'usernameNoEmail', | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects a too-short username and one with spaces', () => { | ||
| expect(validateIdentifier('a')).toEqual({ | ||
| code: 'usernameTooShort', | ||
| params: { min: 2 }, | ||
| }); | ||
| expect(validateIdentifier('john doe')).toEqual({ | ||
| code: 'usernameNoSpaces', | ||
| }); | ||
| expect(validateIdentifier('johndoe')).toBeNull(); | ||
| }); | ||
|
|
||
| it('enforces the minimum password length', () => { | ||
| expect(validatePassword('short')).toEqual({ | ||
| code: 'passwordTooShort', | ||
| params: { min: 8 }, | ||
| }); | ||
| expect(validatePassword('longenough')).toBeNull(); | ||
| }); | ||
|
|
||
| it('detects password mismatches', () => { | ||
| expect(validatePasswordMatch('abcdefgh', 'abcdefgi')).toEqual({ | ||
| code: 'passwordsDoNotMatch', | ||
| }); | ||
| expect(validatePasswordMatch('abcdefgh', 'abcdefgh')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reset-password action — request flow', () => { | ||
| afterEach(() => vi.restoreAllMocks()); | ||
|
|
||
| it('calls resetPassword with the trimmed identifier', async () => { | ||
| const resetPassword = vi.fn().mockResolvedValue({}); | ||
| const result = await callAction( | ||
| buildRequest({ mode: 'request', usernameOrEmail: ' john ' }), | ||
| { resetPassword }, | ||
| ); | ||
| expect(resetPassword).toHaveBeenCalledWith({ id: 'john' }); | ||
| expect(result).toEqual({ ok: true, mode: 'request' }); | ||
| }); | ||
|
|
||
| it('reports success on a 4xx to avoid user enumeration', async () => { | ||
| const resetPassword = vi.fn().mockRejectedValue({ status: 404 }); | ||
| const result = await callAction( | ||
| buildRequest({ mode: 'request', usernameOrEmail: 'ghost' }), | ||
| { resetPassword }, | ||
| ); | ||
| expect(result).toEqual({ ok: true, mode: 'request' }); | ||
| }); | ||
|
|
||
| it('surfaces a generic server error on a 5xx', async () => { | ||
| const resetPassword = vi.fn().mockRejectedValue({ status: 500 }); | ||
| const result = await callAction( | ||
| buildRequest({ mode: 'request', usernameOrEmail: 'john' }), | ||
| { resetPassword }, | ||
| ); | ||
| expect(result).toEqual({ | ||
| ok: false, | ||
| mode: 'request', | ||
| field: 'form', | ||
| code: 'serverError', | ||
| }); | ||
| }); | ||
|
|
||
| it('skips the call but reports success when the identifier is empty', async () => { | ||
| const resetPassword = vi.fn(); | ||
| const result = await callAction( | ||
| buildRequest({ mode: 'request', usernameOrEmail: '' }), | ||
| { resetPassword }, | ||
| ); | ||
| expect(resetPassword).not.toHaveBeenCalled(); | ||
| expect(result).toEqual({ ok: true, mode: 'request' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reset-password action — set flow', () => { | ||
| afterEach(() => vi.restoreAllMocks()); | ||
|
|
||
| const validSet = { | ||
| mode: 'set', | ||
| usernameOrEmail: 'john', | ||
| token: 'tok-123', | ||
| new_password: 'longenough', | ||
| confirm_password: 'longenough', | ||
| }; | ||
|
|
||
| it('calls resetPasswordWithToken with the token and password', async () => { | ||
| const resetPasswordWithToken = vi.fn().mockResolvedValue({}); | ||
| const result = await callAction(buildRequest(validSet), { | ||
| resetPasswordWithToken, | ||
| }); | ||
| expect(resetPasswordWithToken).toHaveBeenCalledWith({ | ||
| id: 'john', | ||
| data: { reset_token: 'tok-123', new_password: 'longenough' }, | ||
| }); | ||
| expect(result).toEqual({ ok: true, mode: 'set' }); | ||
| }); | ||
|
|
||
| it('rejects a too-short password against the new-password field', async () => { | ||
| const resetPasswordWithToken = vi.fn(); | ||
| const result = await callAction( | ||
| buildRequest({ | ||
| ...validSet, | ||
| new_password: 'short', | ||
| confirm_password: 'short', | ||
| }), | ||
| { resetPasswordWithToken }, | ||
| ); | ||
| expect(resetPasswordWithToken).not.toHaveBeenCalled(); | ||
| expect(result).toEqual({ | ||
| ok: false, | ||
| mode: 'set', | ||
| field: 'newPassword', | ||
| code: 'passwordTooShort', | ||
| params: { min: 8 }, | ||
| }); | ||
| }); | ||
|
|
||
| it('reports a mismatch against the confirm field', async () => { | ||
| const result = await callAction( | ||
| buildRequest({ ...validSet, confirm_password: 'different1' }), | ||
| { resetPasswordWithToken: vi.fn() }, | ||
| ); | ||
| expect(result).toEqual({ | ||
| ok: false, | ||
| mode: 'set', | ||
| field: 'confirmPassword', | ||
| code: 'passwordsDoNotMatch', | ||
| }); | ||
| }); | ||
|
|
||
| it('fails on a missing token', async () => { | ||
| const result = await callAction(buildRequest({ ...validSet, token: '' }), { | ||
| resetPasswordWithToken: vi.fn(), | ||
| }); | ||
| expect(result).toEqual({ | ||
| ok: false, | ||
| mode: 'set', | ||
| field: 'form', | ||
| code: 'setFailed', | ||
| }); | ||
| }); | ||
|
|
||
| it('maps a 4xx from the backend to a generic setFailed error', async () => { | ||
| const resetPasswordWithToken = vi.fn().mockRejectedValue({ | ||
| status: 400, | ||
| data: { error: { message: 'Invalid token internal detail' } }, | ||
| }); | ||
| const result = await callAction(buildRequest(validSet), { | ||
| resetPasswordWithToken, | ||
| }); | ||
| expect(result).toEqual({ | ||
| ok: false, | ||
| mode: 'set', | ||
| field: 'form', | ||
| code: 'setFailed', | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason for adding the
reset-passwordroute? Since the restapi already generates URLs with/passwordresetI would have only kept that oneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just kept it as i thought it was a necessity / design decision for the naming, since it was already defined here to some degree:
aurora/packages/cmsui/routes/auth/login.tsx
Lines 157 to 160 in 3fdc48c