Skip to content

Commit 1da8adc

Browse files
authored
Create verificationRequired Function (canada-ca#2423)
* implement new verified required function * export verifiedRequired * update translations
1 parent 25822c9 commit 1da8adc

7 files changed

Lines changed: 187 additions & 44 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { setupI18n } from '@lingui/core'
2+
3+
import { verifiedRequired } from '../index'
4+
import englishMessages from '../../locale/en/messages'
5+
import frenchMessages from '../../locale/fr/messages'
6+
7+
describe('given the verifiedRequired function', () => {
8+
let i18n, user
9+
10+
const consoleOutput = []
11+
const mockedWarn = (output) => consoleOutput.push(output)
12+
13+
beforeAll(() => {
14+
console.warn = mockedWarn
15+
})
16+
afterEach(() => {
17+
consoleOutput.length = 0
18+
})
19+
20+
describe('provided an email validated user', () => {
21+
beforeEach(() => {
22+
user = {
23+
userName: 'test.account@istio.actually.exists',
24+
displayName: 'Test Account',
25+
preferredLang: 'french',
26+
tfaValidated: false,
27+
emailValidated: true,
28+
}
29+
})
30+
it('returns true', () => {
31+
const verifiedFunc = verifiedRequired({})
32+
33+
const verifiedUser = verifiedFunc({ user })
34+
35+
expect(verifiedUser).toBe(true)
36+
})
37+
})
38+
describe('language is set to english', () => {
39+
beforeAll(() => {
40+
i18n = setupI18n({
41+
locale: 'en',
42+
localeData: {
43+
en: { plurals: {} },
44+
fr: { plurals: {} },
45+
},
46+
locales: ['en', 'fr'],
47+
messages: {
48+
en: englishMessages.messages,
49+
fr: frenchMessages.messages,
50+
},
51+
})
52+
})
53+
describe('user is not email validated', () => {
54+
beforeEach(() => {
55+
user = {
56+
userName: 'test.account@istio.actually.exists',
57+
displayName: 'Test Account',
58+
preferredLang: 'french',
59+
tfaValidated: false,
60+
emailValidated: false,
61+
}
62+
})
63+
it('throws an error', () => {
64+
const verifiedFunc = verifiedRequired({ i18n })
65+
66+
try {
67+
verifiedFunc({ user })
68+
} catch (err) {
69+
expect(err).toEqual(
70+
new Error(
71+
'Verification error. Please verify your account via email to access content.',
72+
),
73+
)
74+
}
75+
})
76+
})
77+
})
78+
describe('language is set to french', () => {
79+
beforeAll(() => {
80+
i18n = setupI18n({
81+
locale: 'fr',
82+
localeData: {
83+
en: { plurals: {} },
84+
fr: { plurals: {} },
85+
},
86+
locales: ['en', 'fr'],
87+
messages: {
88+
en: englishMessages.messages,
89+
fr: frenchMessages.messages,
90+
},
91+
})
92+
})
93+
describe('user is not email validated', () => {
94+
beforeEach(() => {
95+
user = {
96+
userName: 'test.account@istio.actually.exists',
97+
displayName: 'Test Account',
98+
preferredLang: 'french',
99+
tfaValidated: false,
100+
emailValidated: false,
101+
}
102+
})
103+
it('throws an error', () => {
104+
const verifiedFunc = verifiedRequired({ i18n })
105+
106+
try {
107+
verifiedFunc({ user })
108+
} catch (err) {
109+
expect(err).toEqual(new Error('todo'))
110+
}
111+
})
112+
})
113+
})
114+
})

api-js/src/auth/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export * from './check-super-admin'
55
export * from './check-user-is-admin-for-user'
66
export * from './generate-jwt'
77
export * from './user-required'
8+
export * from './verified-required'
89
export * from './verify-jwt'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { t } from '@lingui/macro'
2+
3+
export const verifiedRequired =
4+
({ i18n }) =>
5+
({ user }) => {
6+
if (user.emailValidated) {
7+
return true
8+
}
9+
10+
console.warn(
11+
`User: ${user._key} attempted to access controlled functionality without verification.`,
12+
)
13+
throw new Error(
14+
i18n._(
15+
t`Verification error. Please verify your account via email to access content.`,
16+
),
17+
)
18+
}

api-js/src/locale/en/messages.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'Authentication error. Please sign in.',
77
'Cannot query affiliations on organization without admin permission or higher.':
88
'Cannot query affiliations on organization without admin permission or higher.',
9+
'Email already in use.': 'Email already in use.',
910
'Error when retrieving dmarc report information. Please try again.':
1011
'Error when retrieving dmarc report information. Please try again.',
1112
'If an account with this username is found, a password reset link will be found in your inbox.':
@@ -501,9 +502,10 @@
501502
'User could not be queried.': 'User could not be queried.',
502503
'User role was updated successfully.':
503504
'User role was updated successfully.',
504-
'Email already in use.': 'Email already in use.',
505505
'Username not available, please try another.':
506506
'Username not available, please try another.',
507+
'Verification error. Please verify your account via email to access content.':
508+
'Verification error. Please verify your account via email to access content.',
507509
'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.':
508510
'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.',
509511
'You must provide a `first` or `last` value to properly paginate the `DKIM` connection.':

api-js/src/locale/en/messages.po

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ msgstr "Authentication error. Please sign in."
2626
msgid "Cannot query affiliations on organization without admin permission or higher."
2727
msgstr "Cannot query affiliations on organization without admin permission or higher."
2828

29+
#: src/user/mutations/sign-up.js:105
30+
msgid "Email already in use."
31+
msgstr "Email already in use."
32+
2933
#: src/auth/check-domain-ownership.js:31
3034
#: src/auth/check-domain-ownership.js:45
3135
#: src/auth/check-domain-ownership.js:73
@@ -227,7 +231,7 @@ msgstr "Permission Denied: Please contact organization user for help with creati
227231
msgid "Permission Denied: Please contact organization user for help with retrieving this domain."
228232
msgstr "Permission Denied: Please contact organization user for help with retrieving this domain."
229233

230-
#: src/domain/mutations/request-scan.js:74
234+
#: src/domain/mutations/request-scan.js:67
231235
msgid "Permission Denied: Please contact organization user for help with scanning this domain."
232236
msgstr "Permission Denied: Please contact organization user for help with scanning this domain."
233237

@@ -350,11 +354,11 @@ msgstr "Requesting {amount} records on the `SPF` connection exceeds the `{argSet
350354
msgid "Requesting {amount} records on the `SSL` connection exceeds the `{argSet}` limit of 100 records."
351355
msgstr "Requesting {amount} records on the `SSL` connection exceeds the `{argSet}` limit of 100 records."
352356

353-
#: src/domain/mutations/request-scan.js:134
357+
#: src/domain/mutations/request-scan.js:125
354358
msgid "Successfully dispatched one time scan."
355359
msgstr "Successfully dispatched one time scan."
356360

357-
#: src/user/mutations/verify-account.js:122
361+
#: src/user/mutations/verify-account.js:111
358362
msgid "Successfully email verified account, and set TFA send method to email."
359363
msgstr "Successfully email verified account, and set TFA send method to email."
360364

@@ -429,9 +433,9 @@ msgstr "Unable to create domain, organization has already claimed it."
429433
#: src/domain/mutations/create-domain.js:128
430434
#: src/domain/mutations/create-domain.js:179
431435
#: src/domain/mutations/create-domain.js:199
432-
#: src/domain/mutations/create-domain.js:225
433-
#: src/domain/mutations/create-domain.js:243
434-
#: src/domain/mutations/create-domain.js:253
436+
#: src/domain/mutations/create-domain.js:229
437+
#: src/domain/mutations/create-domain.js:247
438+
#: src/domain/mutations/create-domain.js:257
435439
msgid "Unable to create domain. Please try again."
436440
msgstr "Unable to create domain. Please try again."
437441

@@ -441,9 +445,9 @@ msgstr "Unable to create domain. Please try again."
441445
msgid "Unable to create organization. Please try again."
442446
msgstr "Unable to create organization. Please try again."
443447

444-
#: src/domain/mutations/request-scan.js:97
445-
#: src/domain/mutations/request-scan.js:111
446-
#: src/domain/mutations/request-scan.js:125
448+
#: src/domain/mutations/request-scan.js:89
449+
#: src/domain/mutations/request-scan.js:103
450+
#: src/domain/mutations/request-scan.js:117
447451
msgid "Unable to dispatch one time scan. Please try again."
448452
msgstr "Unable to dispatch one time scan. Please try again."
449453

@@ -657,8 +661,8 @@ msgstr "Unable to load mail summary. Please try again."
657661

658662
#: src/organization/loaders/load-organization-by-key.js:33
659663
#: src/organization/loaders/load-organization-by-key.js:47
660-
#: src/organization/loaders/load-organization-by-slug.js:34
661-
#: src/organization/loaders/load-organization-by-slug.js:48
664+
#: src/organization/loaders/load-organization-by-slug.js:36
665+
#: src/organization/loaders/load-organization-by-slug.js:51
662666
#: src/organization/loaders/load-organization-connections-by-domain-id.js:516
663667
#: src/organization/loaders/load-organization-connections-by-domain-id.js:528
664668
#: src/organization/loaders/load-organization-connections-by-user-id.js:507
@@ -765,7 +769,7 @@ msgstr "Unable to remove user from this organization. Please try again."
765769
msgid "Unable to remove user from unknown organization."
766770
msgstr "Unable to remove user from unknown organization."
767771

768-
#: src/domain/mutations/request-scan.js:61
772+
#: src/domain/mutations/request-scan.js:54
769773
msgid "Unable to request a one time scan on an unknown domain."
770774
msgstr "Unable to request a one time scan on an unknown domain."
771775

@@ -910,14 +914,13 @@ msgstr "Unable to update user's role. Please try again."
910914
msgid "Unable to update your own role."
911915
msgstr "Unable to update your own role."
912916

913-
#: src/user/mutations/verify-account.js:80
914-
#: src/user/mutations/verify-account.js:94
917+
#: src/user/mutations/verify-account.js:53
918+
#: src/user/mutations/verify-account.js:70
919+
#: src/user/mutations/verify-account.js:83
915920
msgid "Unable to verify account. Please request a new email."
916921
msgstr "Unable to verify account. Please request a new email."
917922

918-
#: src/user/mutations/verify-account.js:46
919-
#: src/user/mutations/verify-account.js:61
920-
#: src/user/mutations/verify-account.js:112
923+
#: src/user/mutations/verify-account.js:101
921924
msgid "Unable to verify account. Please try again."
922925
msgstr "Unable to verify account. Please try again."
923926

@@ -946,14 +949,14 @@ msgstr "User could not be queried."
946949
msgid "User role was updated successfully."
947950
msgstr "User role was updated successfully."
948951

949-
#: src/user/mutations/sign-up.js:105
950-
msgid "Username already in use."
951-
msgstr "Username already in use."
952-
953952
#: src/user/mutations/update-user-profile.js:71
954953
msgid "Username not available, please try another."
955954
msgstr "Username not available, please try another."
956955

956+
#: src/auth/verified-required.js:15
957+
msgid "Verification error. Please verify your account via email to access content."
958+
msgstr "Verification error. Please verify your account via email to access content."
959+
957960
#: src/email-scan/loaders/load-dkim-results-connections-by-dkim-id.js:86
958961
msgid "You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection."
959962
msgstr "You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection."

api-js/src/locale/fr/messages.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'Authentication error. Please sign in.': 'todo',
55
'Cannot query affiliations on organization without admin permission or higher.':
66
'todo',
7+
'Email already in use.': 'todo',
78
'Error when retrieving dmarc report information. Please try again.': 'todo',
89
'If an account with this username is found, a password reset link will be found in your inbox.':
910
'todo',
@@ -261,8 +262,9 @@
261262
'Unable to verify unknown organization.': 'todo',
262263
'User could not be queried.': 'todo',
263264
'User role was updated successfully.': 'todo',
264-
'Email already in use.': 'todo',
265265
'Username not available, please try another.': 'todo',
266+
'Verification error. Please verify your account via email to access content.':
267+
'todo',
266268
'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.':
267269
'todo',
268270
'You must provide a `first` or `last` value to properly paginate the `DKIM` connection.':

0 commit comments

Comments
 (0)