feat: usage of ESM modules imports (instead of CommonJS) (#5)

Replace `jest` with `tap`.
This commit is contained in:
Divlo
2022-03-20 11:49:27 +01:00
committed by GitHub
parent 91a0e2a76f
commit 19b6f96ecf
70 changed files with 8017 additions and 6318 deletions

View File

@ -1,28 +1,30 @@
import tap from 'tap'
import sinon from 'sinon'
import ms from 'ms'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js'
import { prismaMock } from '../../../../__test__/setup.js'
describe('PUT /users/reset-password', () => {
it('succeeds', async () => {
prismaMock.user.findFirst.mockResolvedValue({
...userExample,
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
})
const response = await application.inject({
method: 'PUT',
url: '/users/reset-password',
payload: {
password: 'new password',
temporaryToken: userExample.temporaryToken
}
})
expect(response.statusCode).toEqual(200)
await tap.test('PUT /users/reset-password', async (t) => {
t.afterEach(() => {
sinon.restore()
})
it('fails with expired temporaryToken', async () => {
prismaMock.user.findFirst.mockResolvedValue(userExample)
await t.test('succeeds', async (t) => {
const temporaryToken = 'random-token'
sinon.stub(prisma, 'user').value({
findFirst: async () => {
return {
...userExample,
temporaryToken,
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
}
},
update: async () => {
return userExample
}
})
const response = await application.inject({
method: 'PUT',
url: '/users/reset-password',
@ -31,6 +33,31 @@ describe('PUT /users/reset-password', () => {
temporaryToken: userExample.temporaryToken
}
})
expect(response.statusCode).toEqual(400)
t.equal(response.statusCode, 200)
})
await t.test('fails with expired temporaryToken', async (t) => {
const temporaryToken = 'random-token'
sinon.stub(prisma, 'user').value({
findFirst: async () => {
return {
...userExample,
temporaryToken,
temporaryExpirationToken: new Date(Date.now() - ms('1 hour'))
}
},
update: async () => {
return userExample
}
})
const response = await application.inject({
method: 'PUT',
url: '/users/reset-password',
payload: {
password: 'new password',
temporaryToken: userExample.temporaryToken
}
})
t.equal(response.statusCode, 400)
})
})