|
| 1 | +import { it, describe, beforeEach, afterEach } from 'node:test' |
| 2 | +import assert from 'node:assert' |
| 3 | +import { build } from '../../../helper.js' |
| 4 | +import { FastifyInstance } from 'fastify' |
| 5 | +import { scryptHash } from '../../../../src/plugins/custom/scrypt.js' |
| 6 | + |
| 7 | +async function createUser (app: FastifyInstance, userData: Partial<{ username: string; password: string }>) { |
| 8 | + const [id] = await app.knex('users').insert(userData) |
| 9 | + return id |
| 10 | +} |
| 11 | + |
| 12 | +async function deleteUser (app: FastifyInstance, username: string) { |
| 13 | + await app.knex('users').delete().where({ username }) |
| 14 | +} |
| 15 | + |
| 16 | +async function updatePasswordWithLoginInjection (app: FastifyInstance, username: string, payload: { currentPassword: string; newPassword: string }) { |
| 17 | + return app.injectWithLogin(username, { |
| 18 | + method: 'PUT', |
| 19 | + url: '/api/users/update-password', |
| 20 | + payload |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +describe('Users API', async () => { |
| 25 | + const hash = await scryptHash('Password123$') |
| 26 | + let app: FastifyInstance |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + app = await build() |
| 30 | + }) |
| 31 | + |
| 32 | + afterEach(async () => { |
| 33 | + await app.close() |
| 34 | + }) |
| 35 | + |
| 36 | + it('Should enforce rate limiting by returning a 429 status after exceeding 3 password update attempts within 1 minute', async () => { |
| 37 | + await createUser(app, { username: 'random-user-0', password: hash }) |
| 38 | + |
| 39 | + const loginResponse = await app.injectWithLogin('random-user-0', { |
| 40 | + method: 'POST', |
| 41 | + url: '/api/auth/login', |
| 42 | + payload: { |
| 43 | + username: 'random-user-0', |
| 44 | + password: 'Password123$' |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + app.config = { |
| 49 | + ...app.config, |
| 50 | + COOKIE_SECRET: loginResponse.cookies[0].value |
| 51 | + } |
| 52 | + |
| 53 | + for (let i = 0; i < 3; i++) { |
| 54 | + const resInner = await app.inject({ |
| 55 | + method: 'PUT', |
| 56 | + url: '/api/users/update-password', |
| 57 | + payload: { |
| 58 | + currentPassword: 'Password1234$', |
| 59 | + newPassword: 'Password123$' |
| 60 | + }, |
| 61 | + cookies: { |
| 62 | + [app.config.COOKIE_NAME]: loginResponse.cookies[0].value |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + assert.strictEqual(resInner.statusCode, 401) |
| 67 | + } |
| 68 | + |
| 69 | + const res = await app.inject({ |
| 70 | + method: 'PUT', |
| 71 | + url: '/api/users/update-password', |
| 72 | + payload: { |
| 73 | + currentPassword: 'Password1234$', |
| 74 | + newPassword: 'Password123$' |
| 75 | + }, |
| 76 | + cookies: { |
| 77 | + [app.config.COOKIE_NAME]: loginResponse.cookies[0].value |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + assert.strictEqual(res.statusCode, 429) |
| 82 | + await deleteUser(app, 'random-user-0') |
| 83 | + }) |
| 84 | + |
| 85 | + it('Should update the password successfully', async () => { |
| 86 | + await createUser(app, { username: 'random-user-1', password: hash }) |
| 87 | + const res = await updatePasswordWithLoginInjection(app, 'random-user-1', { |
| 88 | + currentPassword: 'Password123$', |
| 89 | + newPassword: 'NewPassword123$' |
| 90 | + }) |
| 91 | + |
| 92 | + assert.strictEqual(res.statusCode, 200) |
| 93 | + assert.deepStrictEqual(JSON.parse(res.payload), { message: 'Password updated successfully' }) |
| 94 | + |
| 95 | + await deleteUser(app, 'random-user-1') |
| 96 | + }) |
| 97 | + |
| 98 | + it('Should return 400 if the new password is the same as current password', async () => { |
| 99 | + await createUser(app, { username: 'random-user-2', password: hash }) |
| 100 | + const res = await updatePasswordWithLoginInjection(app, 'random-user-2', { |
| 101 | + currentPassword: 'Password123$', |
| 102 | + newPassword: 'Password123$' |
| 103 | + }) |
| 104 | + |
| 105 | + assert.strictEqual(res.statusCode, 400) |
| 106 | + assert.deepStrictEqual(JSON.parse(res.payload), { message: 'New password cannot be the same as the current password.' }) |
| 107 | + |
| 108 | + await deleteUser(app, 'random-user-2') |
| 109 | + }) |
| 110 | + |
| 111 | + it('Should return 400 if the newPassword password not match the required pattern', async () => { |
| 112 | + await createUser(app, { username: 'random-user-3', password: hash }) |
| 113 | + const res = await updatePasswordWithLoginInjection(app, 'random-user-3', { |
| 114 | + currentPassword: 'Password123$', |
| 115 | + newPassword: 'password123$' |
| 116 | + }) |
| 117 | + |
| 118 | + assert.strictEqual(res.statusCode, 400) |
| 119 | + assert.deepStrictEqual(JSON.parse(res.payload), { message: 'body/newPassword must match pattern "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).*$"' }) |
| 120 | + |
| 121 | + await deleteUser(app, 'random-user-3') |
| 122 | + }) |
| 123 | + |
| 124 | + it('Should return 401 the current password is incorrect', async () => { |
| 125 | + await createUser(app, { username: 'random-user-4', password: hash }) |
| 126 | + const res = await updatePasswordWithLoginInjection(app, 'random-user-4', { |
| 127 | + currentPassword: 'WrongPassword123$', |
| 128 | + newPassword: 'Password123$' |
| 129 | + }) |
| 130 | + |
| 131 | + assert.strictEqual(res.statusCode, 401) |
| 132 | + assert.deepStrictEqual(JSON.parse(res.payload), { message: 'Invalid current password.' }) |
| 133 | + |
| 134 | + await deleteUser(app, 'random-user-4') |
| 135 | + }) |
| 136 | + |
| 137 | + it('Should return 401 if user does not exist in the database', async () => { |
| 138 | + await createUser(app, { username: 'random-user-5', password: hash }) |
| 139 | + const loginResponse = await app.injectWithLogin('random-user-5', { |
| 140 | + method: 'POST', |
| 141 | + url: '/api/auth/login', |
| 142 | + payload: { |
| 143 | + username: 'random-user-5', |
| 144 | + password: 'Password123$' |
| 145 | + } |
| 146 | + }) |
| 147 | + |
| 148 | + assert.strictEqual(loginResponse.statusCode, 200) |
| 149 | + |
| 150 | + await deleteUser(app, 'random-user-5') |
| 151 | + console.log('%c LOG loginResponse.cookies', 'background: #222; color: #bada55', loginResponse.cookies) |
| 152 | + app.config = { |
| 153 | + ...app.config, |
| 154 | + COOKIE_SECRET: loginResponse.cookies[0].value |
| 155 | + } |
| 156 | + |
| 157 | + const res = await app.inject({ |
| 158 | + method: 'PUT', |
| 159 | + url: '/api/users/update-password', |
| 160 | + payload: { |
| 161 | + currentPassword: 'Password123$', |
| 162 | + newPassword: 'NewPassword123$' |
| 163 | + }, |
| 164 | + cookies: { |
| 165 | + [app.config.COOKIE_NAME]: loginResponse.cookies[0].value |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + assert.strictEqual(res.statusCode, 401) |
| 170 | + assert.deepStrictEqual(JSON.parse(res.payload), { message: 'User does not exist.' }) |
| 171 | + await deleteUser(app, 'random-user-5') |
| 172 | + }) |
| 173 | + |
| 174 | + it('Should handle errors gracefully and return 500 Internal Server Error when an unexpected error occurs', async (t) => { |
| 175 | + const { mock: mockKnex } = t.mock.method(app, 'hash') |
| 176 | + mockKnex.mockImplementation(() => { |
| 177 | + throw new Error() |
| 178 | + }) |
| 179 | + |
| 180 | + await createUser(app, { username: 'random-user-6', password: hash }) |
| 181 | + |
| 182 | + const res = await updatePasswordWithLoginInjection(app, 'random-user-6', { |
| 183 | + currentPassword: 'Password123$', |
| 184 | + newPassword: 'NewPassword123$' |
| 185 | + }) |
| 186 | + |
| 187 | + assert.strictEqual(res.statusCode, 500) |
| 188 | + assert.deepStrictEqual(JSON.parse(res.payload), { message: 'Internal Server Error' }) |
| 189 | + |
| 190 | + await deleteUser(app, 'random-user-6') |
| 191 | + }) |
| 192 | +}) |
0 commit comments