43 lines
1.9 KiB
JavaScript
43 lines
1.9 KiB
JavaScript
import assert from 'node:assert/strict'
|
||
import { mkdtempSync, rmSync } from 'node:fs'
|
||
import { tmpdir } from 'node:os'
|
||
import { join } from 'node:path'
|
||
import test from 'node:test'
|
||
import { createApplication } from '../src/application.js'
|
||
|
||
test('Вход с действующим кодом и восстановление после перезапуска', () => {
|
||
const dir = mkdtempSync(join(tmpdir(), 'ekomat-'))
|
||
const path = join(dir, 'test.db')
|
||
let app = createApplication(path)
|
||
try {
|
||
const { challengeId, mockOtp } = app.request('POST', '/v1/auth/otp/request', { phone: '+77012345678' })
|
||
const { token } = app.request('POST', '/v1/auth/otp/verify', { challengeId, otp: mockOtp, accepted: true })
|
||
const headers = { authorization: 'Bearer ' + token }
|
||
const profile = app.request('GET', '/v1/me', {}, headers)
|
||
assert.ok(profile.userId)
|
||
app.close()
|
||
app = createApplication(path)
|
||
assert.deepEqual(app.request('GET', '/v1/me', {}, headers), profile)
|
||
app.request('POST', '/v1/auth/logout', {}, headers)
|
||
assert.throws(() => app.request('GET', '/v1/me', {}, headers), /signInRequired/)
|
||
} finally {
|
||
app.close()
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('Неверный код ограничен пятью попытками', () => {
|
||
const app = createApplication()
|
||
try {
|
||
const input = { phone: '+77012345678' }
|
||
const { challengeId } = app.request('POST', '/v1/auth/otp/request', input)
|
||
assert.throws(() => app.request('POST', '/v1/auth/otp/request', input), /retryOtp/)
|
||
for (let index = 0; index < 5; index += 1) {
|
||
assert.equal(app.request('POST', '/v1/auth/otp/verify', { challengeId, otp: '000000', accepted: true }).status, 401)
|
||
}
|
||
assert.equal(app.request('POST', '/v1/auth/otp/verify', { challengeId, otp: '000000', accepted: true }).status, 410)
|
||
} finally {
|
||
app.close()
|
||
}
|
||
})
|