|
| 1 | +/** |
| 2 | + * Password & Lock lifecycle tests |
| 3 | + * |
| 4 | + * Tests: set password → lock → unlock → change password → remove password |
| 5 | + * Also tests auto-lock timeout and encrypted state detection. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 9 | + |
| 10 | +// ── Mock password/lock state ── |
| 11 | + |
| 12 | +function createLockState() { |
| 13 | + let passwordHash = null; |
| 14 | + let isLocked = false; |
| 15 | + let autoLockTimeout = 15; // minutes |
| 16 | + let lockTimer = null; |
| 17 | + |
| 18 | + return { |
| 19 | + async setPassword(password) { |
| 20 | + if (!password || password.length < 1) throw new Error('Password required'); |
| 21 | + // In real code this is a PBKDF2 hash |
| 22 | + passwordHash = `hash:${password}`; |
| 23 | + isLocked = false; |
| 24 | + }, |
| 25 | + |
| 26 | + async changePassword(oldPassword, newPassword) { |
| 27 | + if (!passwordHash) throw new Error('No password set'); |
| 28 | + if (`hash:${oldPassword}` !== passwordHash) throw new Error('Wrong password'); |
| 29 | + if (!newPassword || newPassword.length < 1) throw new Error('New password required'); |
| 30 | + passwordHash = `hash:${newPassword}`; |
| 31 | + }, |
| 32 | + |
| 33 | + async removePassword(password) { |
| 34 | + if (!passwordHash) throw new Error('No password set'); |
| 35 | + if (`hash:${password}` !== passwordHash) throw new Error('Wrong password'); |
| 36 | + passwordHash = null; |
| 37 | + isLocked = false; |
| 38 | + }, |
| 39 | + |
| 40 | + async lock() { |
| 41 | + if (!passwordHash) throw new Error('Cannot lock without a password'); |
| 42 | + isLocked = true; |
| 43 | + }, |
| 44 | + |
| 45 | + async unlock(password) { |
| 46 | + if (!passwordHash) throw new Error('No password set'); |
| 47 | + if (`hash:${password}` !== passwordHash) throw new Error('Wrong password'); |
| 48 | + isLocked = false; |
| 49 | + }, |
| 50 | + |
| 51 | + async isEncrypted() { |
| 52 | + return passwordHash !== null; |
| 53 | + }, |
| 54 | + |
| 55 | + async getIsLocked() { |
| 56 | + return isLocked; |
| 57 | + }, |
| 58 | + |
| 59 | + async setAutoLockTimeout(minutes) { |
| 60 | + if (minutes < 0) throw new Error('Invalid timeout'); |
| 61 | + autoLockTimeout = minutes; |
| 62 | + }, |
| 63 | + |
| 64 | + async getAutoLockTimeout() { |
| 65 | + return autoLockTimeout; |
| 66 | + }, |
| 67 | + |
| 68 | + // Test helpers |
| 69 | + _getHash: () => passwordHash, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +// ── Tests ── |
| 74 | + |
| 75 | +describe('Password Management', () => { |
| 76 | + let lock; |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + lock = createLockState(); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('set password', () => { |
| 83 | + it('sets a master password', async () => { |
| 84 | + await lock.setPassword('mypassword123'); |
| 85 | + expect(await lock.isEncrypted()).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('is not locked after setting password', async () => { |
| 89 | + await lock.setPassword('mypassword123'); |
| 90 | + expect(await lock.getIsLocked()).toBe(false); |
| 91 | + }); |
| 92 | + |
| 93 | + it('rejects empty password', async () => { |
| 94 | + await expect(lock.setPassword('')).rejects.toThrow('Password required'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('lock / unlock', () => { |
| 99 | + it('locks after password is set', async () => { |
| 100 | + await lock.setPassword('test123'); |
| 101 | + await lock.lock(); |
| 102 | + expect(await lock.getIsLocked()).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it('unlocks with correct password', async () => { |
| 106 | + await lock.setPassword('test123'); |
| 107 | + await lock.lock(); |
| 108 | + await lock.unlock('test123'); |
| 109 | + expect(await lock.getIsLocked()).toBe(false); |
| 110 | + }); |
| 111 | + |
| 112 | + it('rejects wrong password on unlock', async () => { |
| 113 | + await lock.setPassword('test123'); |
| 114 | + await lock.lock(); |
| 115 | + await expect(lock.unlock('wrong')).rejects.toThrow('Wrong password'); |
| 116 | + expect(await lock.getIsLocked()).toBe(true); |
| 117 | + }); |
| 118 | + |
| 119 | + it('cannot lock without a password', async () => { |
| 120 | + await expect(lock.lock()).rejects.toThrow('Cannot lock without a password'); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('change password', () => { |
| 125 | + it('changes password with correct old password', async () => { |
| 126 | + await lock.setPassword('old123'); |
| 127 | + await lock.changePassword('old123', 'new456'); |
| 128 | + |
| 129 | + // Old password should no longer work |
| 130 | + await lock.lock(); |
| 131 | + await expect(lock.unlock('old123')).rejects.toThrow('Wrong password'); |
| 132 | + |
| 133 | + // New password should work |
| 134 | + await lock.unlock('new456'); |
| 135 | + expect(await lock.getIsLocked()).toBe(false); |
| 136 | + }); |
| 137 | + |
| 138 | + it('rejects wrong old password', async () => { |
| 139 | + await lock.setPassword('correct'); |
| 140 | + await expect(lock.changePassword('wrong', 'new')) |
| 141 | + .rejects.toThrow('Wrong password'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('rejects empty new password', async () => { |
| 145 | + await lock.setPassword('old'); |
| 146 | + await expect(lock.changePassword('old', '')) |
| 147 | + .rejects.toThrow('New password required'); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('remove password', () => { |
| 152 | + it('removes password with correct password', async () => { |
| 153 | + await lock.setPassword('test123'); |
| 154 | + await lock.removePassword('test123'); |
| 155 | + expect(await lock.isEncrypted()).toBe(false); |
| 156 | + expect(await lock.getIsLocked()).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('rejects wrong password', async () => { |
| 160 | + await lock.setPassword('test123'); |
| 161 | + await expect(lock.removePassword('wrong')) |
| 162 | + .rejects.toThrow('Wrong password'); |
| 163 | + expect(await lock.isEncrypted()).toBe(true); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('auto-lock timeout', () => { |
| 168 | + it('defaults to 15 minutes', async () => { |
| 169 | + expect(await lock.getAutoLockTimeout()).toBe(15); |
| 170 | + }); |
| 171 | + |
| 172 | + it('can be changed', async () => { |
| 173 | + await lock.setAutoLockTimeout(30); |
| 174 | + expect(await lock.getAutoLockTimeout()).toBe(30); |
| 175 | + }); |
| 176 | + |
| 177 | + it('can be set to 0 (never auto-lock)', async () => { |
| 178 | + await lock.setAutoLockTimeout(0); |
| 179 | + expect(await lock.getAutoLockTimeout()).toBe(0); |
| 180 | + }); |
| 181 | + |
| 182 | + it('rejects negative values', async () => { |
| 183 | + await expect(lock.setAutoLockTimeout(-1)) |
| 184 | + .rejects.toThrow('Invalid timeout'); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('full lifecycle', () => { |
| 189 | + it('set → lock → fail unlock → unlock → change → lock → unlock with new', async () => { |
| 190 | + // Set password |
| 191 | + await lock.setPassword('first'); |
| 192 | + expect(await lock.isEncrypted()).toBe(true); |
| 193 | + expect(await lock.getIsLocked()).toBe(false); |
| 194 | + |
| 195 | + // Lock |
| 196 | + await lock.lock(); |
| 197 | + expect(await lock.getIsLocked()).toBe(true); |
| 198 | + |
| 199 | + // Wrong password |
| 200 | + await expect(lock.unlock('nope')).rejects.toThrow(); |
| 201 | + expect(await lock.getIsLocked()).toBe(true); |
| 202 | + |
| 203 | + // Correct password |
| 204 | + await lock.unlock('first'); |
| 205 | + expect(await lock.getIsLocked()).toBe(false); |
| 206 | + |
| 207 | + // Change password |
| 208 | + await lock.changePassword('first', 'second'); |
| 209 | + |
| 210 | + // Lock again |
| 211 | + await lock.lock(); |
| 212 | + |
| 213 | + // Old password fails |
| 214 | + await expect(lock.unlock('first')).rejects.toThrow(); |
| 215 | + |
| 216 | + // New password works |
| 217 | + await lock.unlock('second'); |
| 218 | + expect(await lock.getIsLocked()).toBe(false); |
| 219 | + |
| 220 | + // Remove password |
| 221 | + await lock.removePassword('second'); |
| 222 | + expect(await lock.isEncrypted()).toBe(false); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments