ekomat-mobilnoe-prilozhenie/test/auth.test.js

56 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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: '+77000123478', mockProfileKey: 'mock-browser-profile-key-for-unit-test-sign-in-01' })
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: '+77000123478', mockProfileKey: 'mock-browser-profile-key-for-unit-test-sign-in-01' }
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()
}
})
test('Одинаковый тестовый номер на двух устройствах создаёт независимые профили', (t) => {
const app = createApplication()
t.after(app.close)
const phone = '+77000123478'
const signIn = (mockProfileKey) => {
const { challengeId, mockOtp } = app.request('POST', '/v1/auth/otp/request', { phone, mockProfileKey })
const { token } = app.request('POST', '/v1/auth/otp/verify', { challengeId, otp: mockOtp, accepted: true })
return app.request('GET', '/v1/me', {}, { authorization: 'Bearer ' + token }).userId
}
assert.notEqual(signIn('mock-device-key-used-only-for-unit-test-000000001'),
signIn('mock-device-key-used-only-for-unit-test-000000002'))
})