also looking at this
feat: mailbox recovery addresses and password reset by email
#13
21 files
+1503
-10
| @@ -0,0 +1,41 @@ | ||
| 1 | import { describe, expect, test } from 'bun:test' | |
| 2 | import { newPasswordProblem, recoveryAddressProblem, resetErrorMessage, tokenFromHash } from './functions/recovery' | |
| 3 | ||
| 4 | const token = 'ab'.repeat(32) | |
| 5 | ||
| 6 | describe('reset links', () => { | |
| 7 | test('read the token from the fragment and nothing else', () => { | |
| 8 | expect(tokenFromHash(`#token=${token}`)).toBe(token) | |
| 9 | expect(tokenFromHash(`token=${token.toUpperCase()}`)).toBe(token) | |
| 10 | expect(tokenFromHash('')).toBe('') | |
| 11 | expect(tokenFromHash('#token=short')).toBe('') | |
| 12 | expect(tokenFromHash(`#token=${token}zz`)).toBe('') | |
| 13 | expect(tokenFromHash(`#other=${token}`)).toBe('') | |
| 14 | }) | |
| 15 | ||
| 16 | test('explain failures without telling anything about the mailbox', () => { | |
| 17 | expect(resetErrorMessage(400)).toContain('invalid, already used or expired') | |
| 18 | expect(resetErrorMessage(429)).toContain('wait') | |
| 19 | expect(resetErrorMessage(500, 'fallback')).toBe('fallback') | |
| 20 | }) | |
| 21 | }) | |
| 22 | ||
| 23 | describe('new password', () => { | |
| 24 | test('matches the server bounds and asks for a matching confirmation', () => { | |
| 25 | expect(newPasswordProblem('short', 'short')).toContain('12') | |
| 26 | expect(newPasswordProblem('long-enough-pass', 'different-pass!')).toContain('do not match') | |
| 27 | expect(newPasswordProblem('x'.repeat(1025), 'x'.repeat(1025))).toContain('1024') | |
| 28 | expect(newPasswordProblem('long-enough-pass', 'long-enough-pass')).toBe('') | |
| 29 | }) | |
| 30 | }) | |
| 31 | ||
| 32 | describe('recovery address', () => { | |
| 33 | test('is an address outside the mailbox, or empty to remove it', () => { | |
| 34 | expect(recoveryAddressProblem('pawel@hq.training', 'pawel@outlook.de')).toBe('') | |
| 35 | expect(recoveryAddressProblem('pawel@hq.training', ' ')).toBe('') | |
| 36 | expect(recoveryAddressProblem('pawel@hq.training', 'Pawel@HQ.training')).toContain('outside this mailbox') | |
| 37 | expect(recoveryAddressProblem('pawel@hq.training', 'not-an-address')).toContain('valid') | |
| 38 | expect(recoveryAddressProblem('pawel@hq.training', 'x@localhost')).toContain('valid') | |
| 39 | expect(recoveryAddressProblem('pawel@hq.training', 'a b@c.de')).toContain('valid') | |
| 40 | }) | |
| 41 | }) | |