forked from opengovsg/FormSG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email-submission.e2e.js
250 lines (233 loc) · 8.17 KB
/
email-submission.e2e.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const {
makeMongooseFixtures,
makeModel,
deleteDocById,
createFormFromTemplate,
createForm,
getOptionalVersion,
getBlankVersion,
verifySubmissionDisabled,
getFeatureState,
makeField,
} = require('./helpers/util')
const { verifySubmissionE2e } = require('./helpers/email-mode')
const { verifiableEmailField } = require('./helpers/verifiable-email-field')
const { allFields } = require('./helpers/all-fields')
const { templateFields } = require('./helpers/template-fields')
const {
hiddenFieldsData,
hiddenFieldsLogicData,
} = require('./helpers/all-hidden-form')
const { tripleAttachment } = require('./helpers/triple-attachment')
const chainDisabled = require('./helpers/disabled-form-chained')
const { cloneDeep } = require('lodash')
const { myInfoFields } = require('./helpers/myinfo-form')
let db
let User
let Form
let Agency
let govTech
const testSpNric = 'S9912374E'
const testCpNric = 'S8979373D'
const testCpUen = '123456789A'
let captchaEnabled
fixture('Email mode submissions')
.before(async () => {
db = await makeMongooseFixtures()
Agency = makeModel(db, 'agency.server.model', 'Agency')
User = makeModel(db, 'user.server.model', 'User')
Form = makeModel(db, 'form.server.model', 'Form')
govTech = await Agency.findOne({ shortName: 'govtech' }).exec()
// Check whether captcha is enabled in environment
captchaEnabled = await getFeatureState('captcha')
})
.after(async () => {
// Delete models defined by mongoose and close connection
db.models = {}
await db.close()
})
.beforeEach(async (t) => {
await t.resizeWindow(1280, 800)
})
.afterEach(async (t) => {
await deleteDocById(User, t.ctx.formData.user._id)
await deleteDocById(Form, t.ctx.form._id)
})
// For each of the following tests, a public form is created in the DB
// using the fields and options passed to createForm.
// The tests check that a browser is able to navigate to the form start
// page, fill in the form using the values given in formFields.val, submit
// the form and reach the end page.
// Form with all basic field types
test.before(async (t) => {
const formData = await getDefaultFormOptions()
formData.formFields = cloneDeep(allFields)
t.ctx.formData = formData
})('Create and submit form with all form fields', async (t) => {
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData)
})
// Form where all basic field types are hidden by logic
test.before(async (t) => {
const formData = await getDefaultFormOptions()
formData.formFields = cloneDeep(hiddenFieldsData)
formData.logicData = cloneDeep(hiddenFieldsLogicData)
t.ctx.formData = formData
})('Create and submit form with all field types hidden', async (t) => {
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData)
})
// Form where all fields are optional and no field is answered
test.before(async (t) => {
const formData = await getDefaultFormOptions()
formData.formFields = allFields.map((field) => {
return getBlankVersion(getOptionalVersion(field))
})
t.ctx.formData = formData
})('Create and submit form with all field types optional', async (t) => {
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData)
})
// Form with three attachments to test de-duplication of attachment names
test.before(async (t) => {
const formData = await getDefaultFormOptions()
formData.formFields = cloneDeep(tripleAttachment)
t.ctx.formData = formData
})('Create and submit form with identical attachment names', async (t) => {
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData)
})
// Form with optional attachment in between mandatory ones
test.before(async (t) => {
const formData = await getDefaultFormOptions()
formData.formFields = cloneDeep(tripleAttachment)
// Modify middle attachment field to be optional and unfilled
formData.formFields[1] = getBlankVersion(
getOptionalVersion(formData.formFields[1]),
)
// Modify first filename to account for middle field left blank
formData.formFields[0].val = '1-test-att.txt'
t.ctx.formData = formData
})(
'Create and submit form with optional and required attachments',
async (t) => {
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData)
},
)
// Form where submission is prevented using chained logic
test.before(async (t) => {
const formData = await getDefaultFormOptions()
formData.formFields = cloneDeep(chainDisabled.fields)
formData.logicData = cloneDeep(chainDisabled.logicData)
t.ctx.formData = formData
})('Create and disable form with chained logic', async (t) => {
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionDisabled(
t,
t.ctx.form,
t.ctx.formData,
chainDisabled.toastMessage,
)
})
test.before(async (t) => {
const formData = await getDefaultFormOptions()
t.ctx.formData = formData
// cloneDeep in case other tests in future import and modify templateFields
t.ctx.formData.formFields = cloneDeep(templateFields)
})('Create a form from COVID19 Templates', async (t) => {
t.ctx.form = await createFormFromTemplate(
t,
t.ctx.formData,
Form,
captchaEnabled,
)
t.ctx.endPageTitle = 'Thank you for registering your interest.'
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData)
})
// Basic form with only one field and CP authentication
test.before(async (t) => {
const formData = await getDefaultFormOptions({
authType: 'CP',
status: 'PRIVATE',
esrvcId: 'Test-eServiceId-Cp',
})
formData.formFields = [
{
title: 'short text',
fieldType: 'textfield',
val: 'Lorem Ipsum',
},
].map(makeField)
t.ctx.formData = formData
})('Create and submit basic form with CorpPass authentication', async (t) => {
let authData = { testCpNric, testCpUen }
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData, authData)
})
// Basic form with only one field and SP authentication
test.before(async (t) => {
const formData = await getDefaultFormOptions({
authType: 'SP',
status: 'PRIVATE',
esrvcId: 'Test-eServiceId-Sp',
})
formData.formFields = [
{
title: 'short text',
fieldType: 'textfield',
val: 'Lorem Ipsum',
},
].map(makeField)
t.ctx.formData = formData
})('Create and submit basic form with SingPass authentication', async (t) => {
let authData = { testSpNric }
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData, authData)
})
// Form with a mix of autofilled and non-autofilled MyInfo fields
test.before(async (t) => {
const formData = await getDefaultFormOptions({
authType: 'MyInfo',
esrvcId: 'Test-eServiceId-Sp',
})
formData.formFields = myInfoFields
t.ctx.formData = formData
})('Create and submit basic MyInfo form', async (t) => {
let authData = { testSpNric }
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData, authData)
})
test.before(async (t) => {
const formData = await getDefaultFormOptions()
formData.formFields = cloneDeep(verifiableEmailField)
t.ctx.formData = formData
})('Create and submit form with verifiable email field', async (t) => {
t.ctx.form = await createForm(t, t.ctx.formData, Form, captchaEnabled)
await verifySubmissionE2e(t, t.ctx.form, t.ctx.formData)
})
// Creates an object with default form options, with optional modifications.
const getDefaultFormOptions = async ({
title = 'Submission e2e Form',
authType = 'NIL',
status = 'PUBLIC',
esrvcId = '',
} = {}) => {
title += String(Date.now())
const user = await User.create({
email: String(Date.now()) + '@data.gov.sg',
agency: govTech._id,
contact: '+6587654321',
})
return {
user,
formOptions: {
responseMode: 'email',
hasCaptcha: false,
status,
title,
authType,
esrvcId,
},
}
}