Skip to content

Commit

Permalink
feat: Added unit test for user controller and service (#858)
Browse files Browse the repository at this point in the history
* added unit tests for user controller

* added unit tests for user service

* fix pnpm-lock issue

* add description to user service test
  • Loading branch information
domin191013 authored Oct 18, 2023
1 parent 83df47b commit 7caba09
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
109 changes: 109 additions & 0 deletions packages/wallet/backend/tests/user/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { withSession } from '@/middleware/withSession'
import type { UserController } from '@/user/controller'
import { mockLogInRequest } from '../mocks'
import { createUser } from '@/tests/helpers'
import { faker } from '@faker-js/faker'
import { getRandomToken, hashToken } from '@/utils/helpers'
import { User } from '@/user/model'

describe('User Controller', (): void => {
let bindings: Container<Bindings>
Expand All @@ -28,6 +31,7 @@ describe('User Controller', (): void => {
let userController: UserController
let req: MockRequest<Request>
let res: MockResponse<Response>
let userInfo: { id: string; email: string }

const next = jest.fn()
const args = mockLogInRequest().body
Expand Down Expand Up @@ -57,6 +61,11 @@ describe('User Controller', (): void => {
needsWallet: !user.rapydWalletId,
needsIDProof: !user.kycId
}

userInfo = {
id: user.id,
email: user.email
}
})

afterAll(async (): Promise<void> => {
Expand Down Expand Up @@ -93,4 +102,104 @@ describe('User Controller', (): void => {
expect(req.session).toEqual({})
})
})

describe('Request ResetPassword', () => {
it('should return a message that a reset email has been sent', async () => {
req.body = {
email: userInfo.email
}

await userController.requestResetPassword(req, res, next)
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toMatchObject({
success: true,
message: 'An email with reset password steps was sent to provided email'
})
})
})

describe('Reset password', () => {
it('should return a message that password has been rested', async () => {
const resetToken = getRandomToken()
const passwordResetToken = hashToken(resetToken)
const passwordResetExpiresAt = new Date(Date.now() + 10 * 60 * 1000)
await User.query()
.findById(userInfo.id)
.patch({ passwordResetToken, passwordResetExpiresAt })

const password = faker.internet.password()
req.body = {
password,
confirmPassword: password
}
req.params = {
token: resetToken
}

await userController.resetPassword(req, res, next)
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toMatchObject({
success: true,
message: 'Password was updated successfully'
})
})

it('should return invalid token', async () => {
const password = faker.internet.password()
req.body = {
password,
confirmPassword: password
}
req.params = {
token: 'resetToken'
}
await userController.resetPassword(req, res, (err) => {
next()
errorHandler(err, req, res, next)
})
expect(next).toHaveBeenCalledTimes(1)
expect(res.statusCode).toBe(400)
})
})

describe('Check Token', () => {
it('should return a boolean that the token is valid', async () => {
const resetToken = getRandomToken()
const passwordResetToken = hashToken(resetToken)
const passwordResetExpiresAt = new Date(Date.now() + 10 * 60 * 1000)
await User.query()
.findById(userInfo.id)
.patch({ passwordResetToken, passwordResetExpiresAt })

req.params = {
token: resetToken
}

await userController.checkToken(req, res, next)
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toMatchObject({
success: true,
message: 'Token was checked',
data: {
isValid: true
}
})
})

it('should return a boolean that the token is not valid', async () => {
req.params = {
token: 'resetToken'
}

await userController.checkToken(req, res, next)
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toMatchObject({
success: true,
message: 'Token was checked',
data: {
isValid: false
}
})
})
})
})
14 changes: 12 additions & 2 deletions packages/wallet/backend/tests/user/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { Knex } from 'knex'
import { truncateTables } from '@/tests/tables'
import { faker } from '@faker-js/faker'
import type { UserService } from '@/user/service'
import { getRandomToken } from '@/utils/helpers'
import { getRandomToken, hashToken } from '@/utils/helpers'

describe('User Service', (): void => {
let bindings: Container<Bindings>
let appContainer: TestApp
let knex: Knex
let userService: UserService
const emailToken = getRandomToken()

const args = {
email: faker.internet.email(),
Expand All @@ -22,7 +23,7 @@ describe('User Service', (): void => {

const createUserArgs = {
...args,
verifyEmailToken: getRandomToken()
verifyEmailToken: hashToken(emailToken)
}

beforeAll(async (): Promise<void> => {
Expand Down Expand Up @@ -66,4 +67,13 @@ describe('User Service', (): void => {
})
})
})

describe('verifyEmail', (): void => {
it('should return undefined', async () => {
await userService.create(createUserArgs)

const result = await userService.verifyEmail(emailToken)
expect(result).toBeUndefined()
})
})
})

0 comments on commit 7caba09

Please sign in to comment.