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

ref: migrate stream feedback flow to TypeScript #733

Merged
merged 12 commits into from
Nov 26, 2020
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
56 changes: 0 additions & 56 deletions src/app/controllers/admin-forms.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
const mongoose = require('mongoose')
const moment = require('moment-timezone')
const _ = require('lodash')
const JSONStream = require('JSONStream')
const { StatusCodes } = require('http-status-codes')

const logger = require('../../config/logger').createLoggerWithLabel(module)
Expand Down Expand Up @@ -463,61 +462,6 @@ function makeModule(connection) {
}
})
},
/**
* Stream download feedback for a form
* @param {Object} req - Express request object
* @param {Object} req.form - the form to download
* @param {Object} res - Express response object
*/
streamFeedback: function (req, res) {
let FormFeedback = getFormFeedbackModel(connection)
FormFeedback.find({ formId: req.form._id })
.cursor()
.on('error', function (err) {
logger.error({
message: 'Error streaming feedback from MongoDB',
meta: {
action: 'makeModule.streamFeedback',
...createReqMeta(req),
},
error: err,
})
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
message: 'Error retrieving from database.',
})
})
.pipe(JSONStream.stringify())
.on('error', function (err) {
logger.error({
message: 'Error converting feedback to JSON',
meta: {
action: 'makeModule.streamFeedback',
...createReqMeta(req),
},
error: err,
})
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
message: 'Error converting feedback to JSON',
})
})
.pipe(res.type('json'))
.on('error', function (err) {
logger.error({
message: 'Error writing feedback to HTTP stream',
meta: {
action: 'makeModule.streamFeedback',
...createReqMeta(req),
},
error: err,
})
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
message: 'Error writing feedback to HTTP stream',
})
})
.on('end', function () {
res.end()
})
},
/**
* Submit feedback when previewing forms
* Preview feedback is not stored
Expand Down
14 changes: 14 additions & 0 deletions src/app/models/form_feedback.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ const FormFeedbackSchema = new Schema<IFormFeedbackSchema>(
},
)

/**
* Returns a cursor for all feedback for the form with formId.
* @param formId the form id to return the submissions cursor for
* @returns a cursor to the feedback retrieved
*/
const getFeedbackCursorByFormId: IFormFeedbackModel['getFeedbackCursorByFormId'] = function (
this: IFormFeedbackModel,
formId,
) {
return this.find({ formId }).batchSize(2000).read('secondary').lean().cursor()
mantariksh marked this conversation as resolved.
Show resolved Hide resolved
}

FormFeedbackSchema.statics.getFeedbackCursorByFormId = getFeedbackCursorByFormId

/**
* Form Feedback Schema
* @param db Active DB Connection
Expand Down
152 changes: 147 additions & 5 deletions src/app/modules/auth/__tests__/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { ObjectId } from 'bson-ext'
import mongoose from 'mongoose'
import { err, errAsync, ok, okAsync } from 'neverthrow'
import { mocked } from 'ts-jest/utils'
import { ImportMock } from 'ts-mock-imports'

import getTokenModel from 'src/app/models/token.server.model'
import * as OtpUtils from 'src/app/utils/otp'
import { IAgencySchema } from 'src/types'
import { IAgencySchema, IPopulatedForm, IPopulatedUser } from 'src/types'

import dbHandler from 'tests/unit/backend/helpers/jest-db'

import { DatabaseError } from '../../core/core.errors'
import { PermissionLevel } from '../../form/admin-form/admin-form.types'
import * as AdminFormUtils from '../../form/admin-form/admin-form.utils'
import {
ForbiddenFormError,
FormDeletedError,
FormNotFoundError,
} from '../../form/form.errors'
import * as FormService from '../../form/form.service'
import { InvalidDomainError, InvalidOtpError } from '../auth.errors'
import * as AuthService from '../auth.service'

jest.mock('../../form/form.service')
const MockFormService = mocked(FormService)
jest.mock('../../form/admin-form/admin-form.utils')
const MockAdminFormUtils = mocked(AdminFormUtils)

const TokenModel = getTokenModel(mongoose)

const VALID_EMAIL_DOMAIN = 'test.gov.sg'
Expand All @@ -30,10 +47,10 @@ describe('auth.service', () => {
})

// Only need to clear Token collection, and ignore other collections.
beforeEach(
async () =>
await dbHandler.clearCollection(TokenModel.collection.collectionName),
)
beforeEach(async () => {
await dbHandler.clearCollection(TokenModel.collection.collectionName)
jest.resetAllMocks()
})

afterAll(async () => await dbHandler.closeDatabase())

Expand Down Expand Up @@ -187,4 +204,129 @@ describe('auth.service', () => {
expect(actualResult._unsafeUnwrapErr()).toEqual(expectedError)
})
})

describe('getFormAfterPermissionChecks', () => {
const MOCK_USER = {
_id: new ObjectId(),
} as IPopulatedUser
it('should return form when user has permissions', async () => {
// Arrange
const mockFormId = new ObjectId().toHexString()
const expectedForm = {
title: 'mock form',
_id: mockFormId,
} as IPopulatedForm
MockFormService.retrieveFullFormById.mockReturnValueOnce(
okAsync(expectedForm),
)
MockAdminFormUtils.assertFormAvailable.mockReturnValueOnce(ok(true))
MockAdminFormUtils.getAssertPermissionFn.mockReturnValueOnce(() =>
ok(true),
)

// Act
const actualResult = await AuthService.getFormAfterPermissionChecks({
user: MOCK_USER,
formId: mockFormId,
level: PermissionLevel.Write,
})

// Assert
expect(actualResult.isOk()).toEqual(true)
expect(actualResult._unsafeUnwrap()).toEqual(expectedForm)
expect(MockAdminFormUtils.getAssertPermissionFn).toHaveBeenCalledWith(
PermissionLevel.Write,
)
})

it('should return FormNotFoundError when form does not exist in the database', async () => {
// Arrange
const expectedError = new FormNotFoundError('not found')
MockFormService.retrieveFullFormById.mockReturnValueOnce(
errAsync(expectedError),
)

// Act
const actualResult = await AuthService.getFormAfterPermissionChecks({
user: MOCK_USER,
formId: new ObjectId().toHexString(),
level: PermissionLevel.Read,
})

// Assert
expect(actualResult.isErr()).toEqual(true)
expect(actualResult._unsafeUnwrapErr()).toEqual(expectedError)
})

it('should return FormDeletedError when form is already archived', async () => {
// Arrange
const mockFormId = new ObjectId().toHexString()
const expectedError = new FormDeletedError('form deleted')
MockFormService.retrieveFullFormById.mockReturnValueOnce(
okAsync({} as IPopulatedForm),
)
MockAdminFormUtils.assertFormAvailable.mockReturnValueOnce(
err(expectedError),
)
// Act
const actualResult = await AuthService.getFormAfterPermissionChecks({
user: MOCK_USER,
formId: mockFormId,
level: PermissionLevel.Delete,
})

// Assert
expect(actualResult.isErr()).toEqual(true)
expect(actualResult._unsafeUnwrapErr()).toEqual(expectedError)
})

it('should return ForbiddenFormError when user does not have permission', async () => {
// Arrange
const mockFormId = new ObjectId().toHexString()
MockFormService.retrieveFullFormById.mockReturnValueOnce(
okAsync({} as IPopulatedForm),
)
const expectedError = new ForbiddenFormError('user not allowed')
MockAdminFormUtils.assertFormAvailable.mockReturnValueOnce(ok(true))
MockAdminFormUtils.getAssertPermissionFn.mockReturnValueOnce(() =>
err(expectedError),
)

// Act
const actualResult = await AuthService.getFormAfterPermissionChecks({
user: MOCK_USER,
formId: mockFormId,
level: PermissionLevel.Write,
})

// Assert
expect(actualResult.isErr()).toEqual(true)
expect(actualResult._unsafeUnwrapErr()).toEqual(expectedError)
expect(MockAdminFormUtils.getAssertPermissionFn).toHaveBeenCalledWith(
PermissionLevel.Write,
)
})

it('should return DatabaseError when error occurs whilst retrieving form', async () => {
// Arrange
const mockFormId = new ObjectId().toHexString()
const expectedError = new DatabaseError('db boom')
MockFormService.retrieveFullFormById.mockReturnValueOnce(
okAsync({} as IPopulatedForm),
)
MockAdminFormUtils.assertFormAvailable.mockReturnValueOnce(
err(expectedError),
)
// Act
const actualResult = await AuthService.getFormAfterPermissionChecks({
user: MOCK_USER,
formId: mockFormId,
level: PermissionLevel.Delete,
})

// Assert
expect(actualResult.isErr()).toEqual(true)
expect(actualResult._unsafeUnwrapErr()).toEqual(expectedError)
})
})
})
53 changes: 51 additions & 2 deletions src/app/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ import mongoose from 'mongoose'
import { errAsync, okAsync, ResultAsync } from 'neverthrow'
import validator from 'validator'

import { IAgencySchema, ITokenSchema } from 'src/types'

import config from '../../../config/config'
import { createLoggerWithLabel } from '../../../config/logger'
import { LINKS } from '../../../shared/constants'
import {
IAgencySchema,
IPopulatedForm,
ITokenSchema,
IUserSchema,
} from '../../../types'
import getAgencyModel from '../../models/agency.server.model'
import getTokenModel from '../../models/token.server.model'
import { compareHash, hashData } from '../../utils/hash'
import { generateOtp } from '../../utils/otp'
import { ApplicationError, DatabaseError } from '../core/core.errors'
import { PermissionLevel } from '../form/admin-form/admin-form.types'
import {
assertFormAvailable,
getAssertPermissionFn,
} from '../form/admin-form/admin-form.utils'
import {
ForbiddenFormError,
FormDeletedError,
FormNotFoundError,
} from '../form/form.errors'
import * as FormService from '../form/form.service'

import { InvalidDomainError, InvalidOtpError } from './auth.errors'

Expand Down Expand Up @@ -249,3 +264,37 @@ const removeTokenOnSuccess = (email: string) => {
},
)
}

/**
* Retrieves the form of given formId provided that the given user has the
* required permissions.
*
* @returns ok(form) if the user has the required permissions
* @returns err(FormNotFoundError) if form does not exist in the database
* @returns err(FormDeleteError) if form is already archived
* @returns err(ForbiddenFormError if user does not have permission
* @returns err(DatabaseError) if any database error occurs
*/
export const getFormAfterPermissionChecks = ({
user,
formId,
level,
}: {
user: IUserSchema
formId: string
level: PermissionLevel
}): ResultAsync<
IPopulatedForm,
FormNotFoundError | FormDeletedError | DatabaseError | ForbiddenFormError
> => {
// Step 1: Retrieve full form.
return FormService.retrieveFullFormById(formId).andThen((fullForm) =>
// Step 2: Check whether form is available to be retrieved.
assertFormAvailable(fullForm).andThen(() =>
// Step 3: Check required permission levels.
getAssertPermissionFn(level)(user, fullForm)
// Step 4: If success, return retrieved form.
.map(() => fullForm),
),
)
}
18 changes: 18 additions & 0 deletions src/app/modules/feedback/__tests__/feedback.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,22 @@ describe('feedback.service', () => {
expect(actualResult._unsafeUnwrapErr()).toBeInstanceOf(DatabaseError)
})
})

describe('getFormFeedbackStream', () => {
it('should return stream successfully', async () => {
// Arrange
const mockFormId = 'some form id'
const mockCursor = ('some cursor' as unknown) as mongoose.QueryCursor<any>
const streamSpy = jest
.spyOn(FormFeedback, 'getFeedbackCursorByFormId')
.mockReturnValue(mockCursor)

// Act
const actual = FeedbackService.getFormFeedbackStream(mockFormId)

// Assert
expect(actual).toEqual(mockCursor)
expect(streamSpy).toHaveBeenCalledWith(mockFormId)
})
})
})
12 changes: 12 additions & 0 deletions src/app/modules/feedback/feedback.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mongoose from 'mongoose'
import { ResultAsync } from 'neverthrow'

import { createLoggerWithLabel } from '../../../config/logger'
import { IFormFeedbackSchema } from '../../../types'
import getFormFeedbackModel from '../../models/form_feedback.server.model'
import { getMongoErrorMessage } from '../../utils/handle-mongo-error'
import { DatabaseError } from '../core/core.errors'
Expand Down Expand Up @@ -35,3 +36,14 @@ export const getFormFeedbackCount = (
},
)
}

/**
* Retrieves form feedback stream.
* @param formId the formId of form to retrieve feedback for
* @returns form feedback stream
*/
export const getFormFeedbackStream = (
formId: string,
): mongoose.QueryCursor<IFormFeedbackSchema> => {
return FormFeedbackModel.getFeedbackCursorByFormId(formId)
}
Loading