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

refactor: create addLoginFromForm static method #679

Merged
merged 4 commits into from
Nov 19, 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
22 changes: 1 addition & 21 deletions src/app/controllers/spcp.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@ const jwtNames = {
}
const destinationRegex = /^\/([\w]+)\/?/

const addLoginToDB = function (form) {
let login = new Login({
form: form._id,
admin: form.admin._id,
agency: form.admin.agency._id,
authType: form.authType,
esrvcId: form.esrvcId,
})
return login.save().catch((err) => {
logger.error({
message: 'Error adding login to database',
meta: {
action: 'addLoginToDB',
formId: form._id,
},
error: err,
})
})
}

const getForm = function (destination, cb) {
let formId = destinationRegex.exec(destination)[1]
Form.findById({ _id: formId })
Expand Down Expand Up @@ -176,7 +156,7 @@ const handleOOBAuthenticationWith = (ndiConfig, authType, extractUser) => {
// NOTE: cookieDuration is interpreted as a seconds count if numeric.
)
// Add login to DB
addLoginToDB(form).then(() => {
Login.addLoginFromForm(form).then(() => {
const spcpSettings = spcpCookieDomain
? { domain: spcpCookieDomain, path: '/' }
: {}
Expand Down
19 changes: 19 additions & 0 deletions src/app/models/login.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AuthType,
ILoginModel,
ILoginSchema,
IPopulatedForm,
LoginStatistic,
} from '../../types'

Expand Down Expand Up @@ -52,6 +53,24 @@ const LoginSchema = new Schema<ILoginSchema>(
},
)

LoginSchema.statics.addLoginFromForm = function (
this: ILoginModel,
form: IPopulatedForm,
): Promise<ILoginSchema> {
if (!form.authType || !form.esrvcId) {
return Promise.reject(
new Error('Form does not contain authType or e-service ID'),
)
}
return this.create({
form: form._id,
admin: form.admin._id,
agency: form.admin.agency._id,
authType: form.authType,
esrvcId: form.esrvcId,
})
}

LoginSchema.statics.aggregateLoginStats = function (
this: ILoginModel,
esrvcId: string,
Expand Down
3 changes: 2 additions & 1 deletion src/types/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Document, Model } from 'mongoose'

import { IAgencySchema } from './agency'
import { AuthType, IFormSchema } from './form'
import { AuthType, IFormSchema, IPopulatedForm } from './form'
import { IUserSchema } from './user'

export interface ILogin {
Expand Down Expand Up @@ -32,4 +32,5 @@ export interface ILoginModel extends Model<ILoginSchema> {
gte: Date,
lte: Date,
) => Promise<LoginStatistic[]>
addLoginFromForm: (form: IPopulatedForm) => Promise<ILoginSchema>
}
56 changes: 55 additions & 1 deletion tests/unit/backend/models/login.server.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import moment from 'moment-timezone'
import mongoose from 'mongoose'

import getLoginModel from 'src/app/models/login.server.model'
import { AuthType, IFormSchema, ILogin, IUserSchema } from 'src/types'
import {
AuthType,
IFormSchema,
ILogin,
IPopulatedForm,
IUserSchema,
} from 'src/types'

import dbHandler from '../helpers/jest-db'

Expand Down Expand Up @@ -114,6 +120,54 @@ describe('login.server.model', () => {
})

describe('Statics', () => {
describe('addLoginFromForm', () => {
const adminId = new ObjectId()
const formId = new ObjectId()
const agencyId = new ObjectId()
const mockEsrvcId = 'esrvcid'
const mockAuthType = 'SP'
const fullForm = ({
_id: formId,
admin: {
_id: adminId,
agency: {
_id: agencyId,
},
},
authType: mockAuthType,
esrvcId: mockEsrvcId,
} as unknown) as IPopulatedForm

it('should save the correct form data', async () => {
const saved = await LoginModel.addLoginFromForm(fullForm)
const found = await LoginModel.findOne({ form: formId })
// Returned document should match
expect(saved.form).toEqual(formId)
expect(saved.admin).toEqual(adminId)
expect(saved.agency).toEqual(agencyId)
expect(saved.authType).toBe(mockAuthType)
expect(saved.esrvcId).toBe(mockEsrvcId)
// Found document should match
expect(found!.form).toEqual(formId)
expect(found!.admin).toEqual(adminId)
expect(found!.agency).toEqual(agencyId)
expect(found!.authType).toBe(mockAuthType)
expect(found!.esrvcId).toBe(mockEsrvcId)
})

it('should reject when the form does not contain an e-service ID', async () => {
await expect(
LoginModel.addLoginFromForm(omit(fullForm, 'esrvcId')),
).rejects.toThrow('Form does not contain authType or e-service ID')
})

it('should reject when the form does not contain an authType', async () => {
await expect(
LoginModel.addLoginFromForm(omit(fullForm, 'authType')),
).rejects.toThrow('Form does not contain authType or e-service ID')
})
})

describe('aggregateLoginStats', () => {
const VALID_ESRVC_ID = 'MOCK-ESRVC-ID'
const CURR_MOMENT = moment()
Expand Down