Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 update existing recovery words (#99) #103

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/tom-server/src/vault-api/controllers/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('Vault controllers', () => {
const dbManager: Partial<TwakeDB> = {
get: jest.fn(),
insert: jest.fn(),
deleteWhere: jest.fn()
deleteWhere: jest.fn(),
update: jest.fn()
}
let mockRequest: ITestRequest
let mockResponse: Partial<Response>
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('Vault controllers', () => {
// Testing saveRecoveryWords
it('should return response with status code 201 on save success', async () => {
jest.spyOn(dbManager, 'insert').mockResolvedValue([{ words }])
jest.spyOn(dbManager, 'get').mockResolvedValue([])
const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB)
handler(mockRequest as Request, mockResponse as Response, nextFunction)
await new Promise(process.nextTick)
Expand All @@ -78,12 +80,26 @@ describe('Vault controllers', () => {
it('should call next function to throw error on saving failed', async () => {
const errorMsg = 'Insert failed'
jest.spyOn(dbManager, 'insert').mockRejectedValue(new Error(errorMsg))
jest.spyOn(dbManager, 'get').mockResolvedValue([])
const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB)
handler(mockRequest as Request, mockResponse as Response, nextFunction)
await new Promise(process.nextTick)
expect(nextFunction).toHaveBeenCalledWith(new Error(errorMsg))
})

it('should update the recoverywords when it already exists', async () => {
jest
.spyOn(dbManager, 'get')
.mockResolvedValue([{ words: 'Another sentence for the same user' }])
jest.spyOn(dbManager, 'update').mockResolvedValue([{ words }])
const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB)
handler(mockRequest as Request, mockResponse as Response, nextFunction)
await new Promise(process.nextTick)
expect(mockResponse.statusCode).toEqual(200)
expect(dbManager.update).toHaveBeenCalled()
expect(dbManager.insert).not.toHaveBeenCalled()
})

// Testing getRecoveryWords

it('should return response with status code 200 on get success', async () => {
Expand Down
38 changes: 30 additions & 8 deletions packages/tom-server/src/vault-api/controllers/vault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-useless-return */
import { type TwakeDB } from '../../db'
import type { Collections } from '../../types'
import { VaultAPIError, type expressAppHandler } from '../utils'

export type VaultController = (db: TwakeDB) => expressAppHandler
Expand All @@ -9,14 +11,34 @@ export const methodNotAllowed: expressAppHandler = (req, res, next) => {

export const saveRecoveryWords = (db: TwakeDB): expressAppHandler => {
return (req, res, next) => {
const data: Record<string, string> = {
userId: req.token.content.sub,
words: req.body.words
}
// @ts-expect-error 'recoveryWords' isn't declared in Collection
db.insert('recoveryWords', data)
.then((_) => {
res.status(201).json({ message: 'Saved recovery words sucessfully' })
const { words } = req.body
const userId = req.token.content.sub

db.get('recoveryWords' as Collections, ['words'], { userId })
.then((data) => {
if (data.length > 0) {
db.update('recoveryWords' as Collections, { words }, 'userId', userId)
.then((_) => {
res
.status(200)
.json({ message: 'Updated recovery words sucessfully' })
})
.catch((err) => {
next(err)
return
})
} else {
db.insert('recoveryWords' as Collections, { userId, words })
.then((_) => {
res
.status(201)
.json({ message: 'Saved recovery words sucessfully' })
})
.catch((err) => {
next(err)
return
})
}
})
.catch((err) => {
next(err)
Expand Down
Loading