feat: usage of ESM modules imports (instead of CommonJS) (#5)
Replace `jest` with `tap`.
This commit is contained in:
@ -1,66 +1,115 @@
|
||||
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 { userSettingsExample } from '../../../../models/UserSettings.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { emailTransporter } from '../../../../tools/email/emailTransporter.js'
|
||||
|
||||
describe('POST /users/reset-password', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(200)
|
||||
await tap.test('POST /users/reset-password', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it("fails with email that doesn't exist", async () => {
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour')),
|
||||
temporaryToken: 'random-token'
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return userSettingsExample
|
||||
}
|
||||
})
|
||||
sinon.stub(emailTransporter, 'sendMail').value(() => {})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 200)
|
||||
})
|
||||
|
||||
it('fails with unconfirmed account', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue({
|
||||
...userExample,
|
||||
isConfirmed: false
|
||||
await t.test("fails with email that doesn't exist", async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it("fails if userSettings doenst' exist", async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails with unconfirmed account', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
isConfirmed: false
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails with a request already in progress', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue({
|
||||
...userExample,
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
||||
await t.test("fails if userSettings doesn't exist", async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
await t.test('fails with a request already in progress', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
temporaryToken: 'random-token',
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return userSettingsExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user