Skip to content
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
114 changes: 114 additions & 0 deletions api-js/src/auth/__tests__/verified-required.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { setupI18n } from '@lingui/core'

import { verifiedRequired } from '../index'
import englishMessages from '../../locale/en/messages'
import frenchMessages from '../../locale/fr/messages'

describe('given the verifiedRequired function', () => {
let i18n, user

const consoleOutput = []
const mockedWarn = (output) => consoleOutput.push(output)

beforeAll(() => {
console.warn = mockedWarn
})
afterEach(() => {
consoleOutput.length = 0
})

describe('provided an email validated user', () => {
beforeEach(() => {
user = {
userName: 'test.account@istio.actually.exists',
displayName: 'Test Account',
preferredLang: 'french',
tfaValidated: false,
emailValidated: true,
}
})
it('returns true', () => {
const verifiedFunc = verifiedRequired({})

const verifiedUser = verifiedFunc({ user })

expect(verifiedUser).toBe(true)
})
})
describe('language is set to english', () => {
beforeAll(() => {
i18n = setupI18n({
locale: 'en',
localeData: {
en: { plurals: {} },
fr: { plurals: {} },
},
locales: ['en', 'fr'],
messages: {
en: englishMessages.messages,
fr: frenchMessages.messages,
},
})
})
describe('user is not email validated', () => {
beforeEach(() => {
user = {
userName: 'test.account@istio.actually.exists',
displayName: 'Test Account',
preferredLang: 'french',
tfaValidated: false,
emailValidated: false,
}
})
it('throws an error', () => {
const verifiedFunc = verifiedRequired({ i18n })

try {
verifiedFunc({ user })
} catch (err) {
expect(err).toEqual(
new Error(
'Verification error. Please verify your account via email to access content.',
),
)
}
})
})
})
describe('language is set to french', () => {
beforeAll(() => {
i18n = setupI18n({
locale: 'fr',
localeData: {
en: { plurals: {} },
fr: { plurals: {} },
},
locales: ['en', 'fr'],
messages: {
en: englishMessages.messages,
fr: frenchMessages.messages,
},
})
})
describe('user is not email validated', () => {
beforeEach(() => {
user = {
userName: 'test.account@istio.actually.exists',
displayName: 'Test Account',
preferredLang: 'french',
tfaValidated: false,
emailValidated: false,
}
})
it('throws an error', () => {
const verifiedFunc = verifiedRequired({ i18n })

try {
verifiedFunc({ user })
} catch (err) {
expect(err).toEqual(new Error('todo'))
}
})
})
})
})
1 change: 1 addition & 0 deletions api-js/src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './check-super-admin'
export * from './check-user-is-admin-for-user'
export * from './generate-jwt'
export * from './user-required'
export * from './verified-required'
export * from './verify-jwt'
18 changes: 18 additions & 0 deletions api-js/src/auth/verified-required.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { t } from '@lingui/macro'

export const verifiedRequired =
({ i18n }) =>
({ user }) => {
if (user.emailValidated) {
return true
}

console.warn(
`User: ${user._key} attempted to access controlled functionality without verification.`,
)
throw new Error(
i18n._(
t`Verification error. Please verify your account via email to access content.`,
),
)
}
4 changes: 3 additions & 1 deletion api-js/src/locale/en/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'Authentication error. Please sign in.',
'Cannot query affiliations on organization without admin permission or higher.':
'Cannot query affiliations on organization without admin permission or higher.',
'Email already in use.': 'Email already in use.',
'Error when retrieving dmarc report information. Please try again.':
'Error when retrieving dmarc report information. Please try again.',
'If an account with this username is found, a password reset link will be found in your inbox.':
Expand Down Expand Up @@ -501,9 +502,10 @@
'User could not be queried.': 'User could not be queried.',
'User role was updated successfully.':
'User role was updated successfully.',
'Email already in use.': 'Email already in use.',
'Username not available, please try another.':
'Username not available, please try another.',
'Verification error. Please verify your account via email to access content.':
'Verification error. Please verify your account via email to access content.',
'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.':
'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.',
'You must provide a `first` or `last` value to properly paginate the `DKIM` connection.':
Expand Down
45 changes: 24 additions & 21 deletions api-js/src/locale/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ msgstr "Authentication error. Please sign in."
msgid "Cannot query affiliations on organization without admin permission or higher."
msgstr "Cannot query affiliations on organization without admin permission or higher."

#: src/user/mutations/sign-up.js:105
msgid "Email already in use."
msgstr "Email already in use."

#: src/auth/check-domain-ownership.js:31
#: src/auth/check-domain-ownership.js:45
#: src/auth/check-domain-ownership.js:73
Expand Down Expand Up @@ -227,7 +231,7 @@ msgstr "Permission Denied: Please contact organization user for help with creati
msgid "Permission Denied: Please contact organization user for help with retrieving this domain."
msgstr "Permission Denied: Please contact organization user for help with retrieving this domain."

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

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

#: src/domain/mutations/request-scan.js:134
#: src/domain/mutations/request-scan.js:125
msgid "Successfully dispatched one time scan."
msgstr "Successfully dispatched one time scan."

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

Expand Down Expand Up @@ -429,9 +433,9 @@ msgstr "Unable to create domain, organization has already claimed it."
#: src/domain/mutations/create-domain.js:128
#: src/domain/mutations/create-domain.js:179
#: src/domain/mutations/create-domain.js:199
#: src/domain/mutations/create-domain.js:225
#: src/domain/mutations/create-domain.js:243
#: src/domain/mutations/create-domain.js:253
#: src/domain/mutations/create-domain.js:229
#: src/domain/mutations/create-domain.js:247
#: src/domain/mutations/create-domain.js:257
msgid "Unable to create domain. Please try again."
msgstr "Unable to create domain. Please try again."

Expand All @@ -441,9 +445,9 @@ msgstr "Unable to create domain. Please try again."
msgid "Unable to create organization. Please try again."
msgstr "Unable to create organization. Please try again."

#: src/domain/mutations/request-scan.js:97
#: src/domain/mutations/request-scan.js:111
#: src/domain/mutations/request-scan.js:125
#: src/domain/mutations/request-scan.js:89
#: src/domain/mutations/request-scan.js:103
#: src/domain/mutations/request-scan.js:117
msgid "Unable to dispatch one time scan. Please try again."
msgstr "Unable to dispatch one time scan. Please try again."

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

#: src/organization/loaders/load-organization-by-key.js:33
#: src/organization/loaders/load-organization-by-key.js:47
#: src/organization/loaders/load-organization-by-slug.js:34
#: src/organization/loaders/load-organization-by-slug.js:48
#: src/organization/loaders/load-organization-by-slug.js:36
#: src/organization/loaders/load-organization-by-slug.js:51
#: src/organization/loaders/load-organization-connections-by-domain-id.js:516
#: src/organization/loaders/load-organization-connections-by-domain-id.js:528
#: src/organization/loaders/load-organization-connections-by-user-id.js:507
Expand Down Expand Up @@ -765,7 +769,7 @@ msgstr "Unable to remove user from this organization. Please try again."
msgid "Unable to remove user from unknown organization."
msgstr "Unable to remove user from unknown organization."

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

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

#: src/user/mutations/verify-account.js:80
#: src/user/mutations/verify-account.js:94
#: src/user/mutations/verify-account.js:53
#: src/user/mutations/verify-account.js:70
#: src/user/mutations/verify-account.js:83
msgid "Unable to verify account. Please request a new email."
msgstr "Unable to verify account. Please request a new email."

#: src/user/mutations/verify-account.js:46
#: src/user/mutations/verify-account.js:61
#: src/user/mutations/verify-account.js:112
#: src/user/mutations/verify-account.js:101
msgid "Unable to verify account. Please try again."
msgstr "Unable to verify account. Please try again."

Expand Down Expand Up @@ -946,14 +949,14 @@ msgstr "User could not be queried."
msgid "User role was updated successfully."
msgstr "User role was updated successfully."

#: src/user/mutations/sign-up.js:105
msgid "Username already in use."
msgstr "Username already in use."

#: src/user/mutations/update-user-profile.js:71
msgid "Username not available, please try another."
msgstr "Username not available, please try another."

#: src/auth/verified-required.js:15
msgid "Verification error. Please verify your account via email to access content."
msgstr "Verification error. Please verify your account via email to access content."

#: src/email-scan/loaders/load-dkim-results-connections-by-dkim-id.js:86
msgid "You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection."
msgstr "You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection."
Expand Down
4 changes: 3 additions & 1 deletion api-js/src/locale/fr/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'Authentication error. Please sign in.': 'todo',
'Cannot query affiliations on organization without admin permission or higher.':
'todo',
'Email already in use.': 'todo',
'Error when retrieving dmarc report information. Please try again.': 'todo',
'If an account with this username is found, a password reset link will be found in your inbox.':
'todo',
Expand Down Expand Up @@ -261,8 +262,9 @@
'Unable to verify unknown organization.': 'todo',
'User could not be queried.': 'todo',
'User role was updated successfully.': 'todo',
'Email already in use.': 'todo',
'Username not available, please try another.': 'todo',
'Verification error. Please verify your account via email to access content.':
'todo',
'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.':
'todo',
'You must provide a `first` or `last` value to properly paginate the `DKIM` connection.':
Expand Down
Loading