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

[FIXUP] check email config up front #136

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions src/server/common/connectors/notify/notify.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { config } from '~/src/config/config.js'
import { proxyFetch } from '~/src/server/common/helpers/proxy.js'
import { createToken } from '~/src/server/common/connectors/notify/notify-token-utils.js'
import Joi from 'joi'

/**
* @typedef {{ content: string}} NotifyContent
*/
const notifyConfig = config.get('notify')

const emailConfigSchema = Joi.object({
apiKey: Joi.string().required(),
templateId: Joi.string().required(),
caseDeliveryEmailAddress: Joi.string().required()
})

/**
* @param {NotifyContent} data
*/
export async function sendNotification(data) {
const { error } = emailConfigSchema.validate(notifyConfig, {
abortEarly: false,
allowUnknown: true
})

if (error) {
throw new Error(`Invalid notify configuration: ${error.message}`)

Check failure on line 27 in src/server/common/connectors/notify/notify.js

View workflow job for this annotation

GitHub Actions / Run Pull Request Checks

sendNotification › should send a notification successfully

Invalid notify configuration: "apiKey" must be a string. "templateId" must be a string. "caseDeliveryEmailAddress" must be a string at sendNotification (src/server/common/connectors/notify/notify.js:27:11) at Object.<anonymous> (src/server/common/connectors/notify/notify.test.js:23:44)

Check failure on line 27 in src/server/common/connectors/notify/notify.js

View workflow job for this annotation

GitHub Actions / Run Pull Request Checks

sendNotification › should throw an error if the response is not ok

expect(received).rejects.toThrow(expected) Expected substring: "HTTP failure from GOV.uk notify: status 400 with the following errors: Can't send to this recipient using a team-only API key, Can't send to this recipient when service is in trial mode" Received message: "Invalid notify configuration: \"apiKey\" must be a string. \"templateId\" must be a string. \"caseDeliveryEmailAddress\" must be a string" 25 | 26 | if (error) { > 27 | throw new Error(`Invalid notify configuration: ${error.message}`) | ^ 28 | } 29 | 30 | const body = JSON.stringify({ at sendNotification (src/server/common/connectors/notify/notify.js:27:11) at Object.<anonymous> (src/server/common/connectors/notify/notify.test.js:67:34) at Object.toThrow (node_modules/expect/build/index.js:218:22) at Object.toThrow (src/server/common/connectors/notify/notify.test.js:67:54)
}

const body = JSON.stringify({
template_id: notifyConfig.templateId,
email_address: notifyConfig.caseDeliveryEmailAddress,
Expand Down
Loading