From 3d107c1443a3759150e5c801de0eb12f6c89b34c Mon Sep 17 00:00:00 2001 From: "Md. Ashikul Alam" Date: Tue, 5 Nov 2024 17:05:54 +0600 Subject: [PATCH 1/2] chore: amend notification changes with scopes --- packages/notification/src/config/routes.ts | 68 +++++++++++----------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/notification/src/config/routes.ts b/packages/notification/src/config/routes.ts index 9bf78f9cae8..5bb99766f1a 100644 --- a/packages/notification/src/config/routes.ts +++ b/packages/notification/src/config/routes.ts @@ -63,15 +63,7 @@ import { birthSentForUpdatesNotification, deathSentForUpdatesNotification } from '@notification/features/sentForUpdates/handler' - -const enum RouteScope { - DECLARE = 'declare', - VALIDATE = 'validate', - REGISTER = 'register', - CERTIFY = 'certify', - SYSADMIN = 'sysadmin', - NATIONAL_SYSTEM_ADMIN = 'natlsysadmin' -} +import { SCOPES } from '@opencrvs/commons/authentication' const recordValidation: RouteOptionsValidate = { payload: Joi.object() @@ -226,10 +218,12 @@ export default function getRoutes(): ServerRoute[] { description: 'Sends an sms to a user for birth in-progress entry', auth: { scope: [ - RouteScope.DECLARE, - RouteScope.VALIDATE, - RouteScope.REGISTER, - RouteScope.CERTIFY + SCOPES.RECORD_DECLARE_BIRTH, + SCOPES.RECORD_DECLARE_DEATH, + SCOPES.RECORD_DECLARE_MARRIAGE, + SCOPES.RECORD_REGISTER, + SCOPES.RECORD_CERTIFY, + SCOPES.RECORD_SUBMIT_FOR_APPROVAL ] }, validate: { @@ -247,10 +241,12 @@ export default function getRoutes(): ServerRoute[] { 'Sends an sms or email to a user for birth declaration entry', auth: { scope: [ - RouteScope.DECLARE, - RouteScope.VALIDATE, - RouteScope.REGISTER, - RouteScope.CERTIFY + SCOPES.RECORD_DECLARE_BIRTH, + SCOPES.RECORD_DECLARE_DEATH, + SCOPES.RECORD_DECLARE_MARRIAGE, + SCOPES.RECORD_REGISTER, + SCOPES.RECORD_CERTIFY, + SCOPES.RECORD_SUBMIT_FOR_APPROVAL ] }, validate: { @@ -267,7 +263,7 @@ export default function getRoutes(): ServerRoute[] { description: 'Sends an sms or email to a user for birth registration entry', auth: { - scope: [RouteScope.REGISTER] + scope: [SCOPES.RECORD_REGISTER] }, validate: { payload: registrationNotificationSchema @@ -283,7 +279,7 @@ export default function getRoutes(): ServerRoute[] { description: 'Sends an sms or email to a user for birth declaration rejection entry', auth: { - scope: [RouteScope.VALIDATE, RouteScope.REGISTER] + scope: [SCOPES.RECORD_SUBMIT_FOR_APPROVAL, SCOPES.RECORD_REGISTER] }, validate: { payload: rejectionNotificationSchema @@ -300,10 +296,12 @@ export default function getRoutes(): ServerRoute[] { 'Sends an sms or email to a user for death in-progress entry', auth: { scope: [ - RouteScope.DECLARE, - RouteScope.VALIDATE, - RouteScope.REGISTER, - RouteScope.CERTIFY + SCOPES.RECORD_DECLARE_BIRTH, + SCOPES.RECORD_DECLARE_DEATH, + SCOPES.RECORD_DECLARE_MARRIAGE, + SCOPES.RECORD_REGISTER, + SCOPES.RECORD_CERTIFY, + SCOPES.RECORD_SUBMIT_FOR_APPROVAL ] }, validate: { @@ -321,10 +319,12 @@ export default function getRoutes(): ServerRoute[] { 'Sends an sms or email to a user for death declaration entry', auth: { scope: [ - RouteScope.DECLARE, - RouteScope.VALIDATE, - RouteScope.REGISTER, - RouteScope.CERTIFY + SCOPES.RECORD_DECLARE_BIRTH, + SCOPES.RECORD_DECLARE_DEATH, + SCOPES.RECORD_DECLARE_MARRIAGE, + SCOPES.RECORD_REGISTER, + SCOPES.RECORD_CERTIFY, + SCOPES.RECORD_SUBMIT_FOR_APPROVAL ] }, validate: { @@ -340,7 +340,7 @@ export default function getRoutes(): ServerRoute[] { tags: ['api'], description: 'Sends an sms to a user for death registration entry', auth: { - scope: [RouteScope.REGISTER] + scope: [SCOPES.RECORD_REGISTER] }, validate: { payload: registrationNotificationSchema @@ -356,7 +356,7 @@ export default function getRoutes(): ServerRoute[] { description: 'Sends an sms to a user for death declaration rejection entry', auth: { - scope: [RouteScope.VALIDATE, RouteScope.REGISTER] + scope: [SCOPES.RECORD_SUBMIT_FOR_APPROVAL, SCOPES.RECORD_REGISTER] }, validate: { payload: rejectionNotificationSchema @@ -371,7 +371,7 @@ export default function getRoutes(): ServerRoute[] { tags: ['api'], description: 'Sends an sms to a user with credentials', auth: { - scope: [RouteScope.SYSADMIN] + scope: [SCOPES.CONFIG_UPDATE_ALL] }, validate: { payload: userCredentialsNotificationSchema @@ -386,7 +386,7 @@ export default function getRoutes(): ServerRoute[] { tags: ['api'], description: 'Sends an sms to a user with credentials', auth: { - scope: [RouteScope.REGISTER] + scope: [SCOPES.RECORD_REGISTER] }, validate: { payload: sendCorrectionRejectedNotificationInput @@ -401,7 +401,7 @@ export default function getRoutes(): ServerRoute[] { tags: ['api'], description: 'Sends an sms to a user with credentials', auth: { - scope: [RouteScope.REGISTER] + scope: [SCOPES.RECORD_REGISTER] }, validate: { payload: sendCorrectionApprovedNotificationInput @@ -416,7 +416,7 @@ export default function getRoutes(): ServerRoute[] { tags: ['api'], description: 'Sends an sms to a user with new temporary password', auth: { - scope: [RouteScope.SYSADMIN] + scope: [SCOPES.CONFIG_UPDATE_ALL] }, validate: { payload: userPasswordResetNotificationSchema @@ -454,7 +454,7 @@ export default function getRoutes(): ServerRoute[] { options: { tags: ['api'], auth: { - scope: [RouteScope.NATIONAL_SYSTEM_ADMIN] + scope: [SCOPES.CONFIG_UPDATE_ALL] }, description: 'Sends emails to all users given in payload', validate: { From 592681a6dcd569824bc21dc72b5bd759219d0587 Mon Sep 17 00:00:00 2001 From: "Md. Ashikul Alam" Date: Thu, 7 Nov 2024 17:22:55 +0600 Subject: [PATCH 2/2] chore: clean up notification routes and tests --- packages/notification/src/config/routes.ts | 177 +------ .../src/features/sms/birth-handler.test.ts | 448 ----------------- .../src/features/sms/birth-handler.ts | 193 -------- .../src/features/sms/death-handler.test.ts | 450 ------------------ .../src/features/sms/death-handler.ts | 108 ----- .../src/features/sms/user-handler.test.ts | 10 +- packages/notification/src/index.test.ts | 66 --- 7 files changed, 10 insertions(+), 1442 deletions(-) delete mode 100644 packages/notification/src/features/sms/birth-handler.test.ts delete mode 100644 packages/notification/src/features/sms/birth-handler.ts delete mode 100644 packages/notification/src/features/sms/death-handler.test.ts delete mode 100644 packages/notification/src/features/sms/death-handler.ts diff --git a/packages/notification/src/config/routes.ts b/packages/notification/src/config/routes.ts index 5bb99766f1a..245f6966337 100644 --- a/packages/notification/src/config/routes.ts +++ b/packages/notification/src/config/routes.ts @@ -12,28 +12,12 @@ import { allUsersEmailHandler, allUsersEmailPayloadSchema } from '@notification/features/email/all-user-handler' -import { - sendBirthDeclarationConfirmation, - sendBirthRegistrationConfirmation, - sendBirthRejectionConfirmation, - inProgressNotificationSchema, - declarationNotificationSchema, - registrationNotificationSchema, - rejectionNotificationSchema, - sendBirthInProgressConfirmation -} from '@notification/features/sms/birth-handler' import { sendCorrectionApprovedNotification, sendCorrectionApprovedNotificationInput, sendCorrectionRejectedNotification, sendCorrectionRejectedNotificationInput } from '@notification/features/sms/correction' -import { - sendDeathDeclarationConfirmation, - sendDeathRegistrationConfirmation, - sendDeathRejectionConfirmation, - sendDeathInProgressConfirmation -} from '@notification/features/sms/death-handler' import { sendUserCredentials, retrieveUserName, @@ -203,164 +187,13 @@ export default function getRoutes(): ServerRoute[] { path: '/death/sent-for-updates', handler: deathSentForUpdatesNotification, options: { - tags: ['api'], - description: - 'Sends a notification to country config for rejected death declaration', - validate: recordValidation - } - }, - { - method: 'POST', - path: '/birthInProgressSMS', - handler: sendBirthInProgressConfirmation, - options: { - tags: ['api'], - description: 'Sends an sms to a user for birth in-progress entry', - auth: { - scope: [ - SCOPES.RECORD_DECLARE_BIRTH, - SCOPES.RECORD_DECLARE_DEATH, - SCOPES.RECORD_DECLARE_MARRIAGE, - SCOPES.RECORD_REGISTER, - SCOPES.RECORD_CERTIFY, - SCOPES.RECORD_SUBMIT_FOR_APPROVAL - ] - }, - validate: { - payload: inProgressNotificationSchema - } - } - }, - { - method: 'POST', - path: '/birthDeclarationSMS', - handler: sendBirthDeclarationConfirmation, - options: { - tags: ['api'], - description: - 'Sends an sms or email to a user for birth declaration entry', - auth: { - scope: [ - SCOPES.RECORD_DECLARE_BIRTH, - SCOPES.RECORD_DECLARE_DEATH, - SCOPES.RECORD_DECLARE_MARRIAGE, - SCOPES.RECORD_REGISTER, - SCOPES.RECORD_CERTIFY, - SCOPES.RECORD_SUBMIT_FOR_APPROVAL - ] - }, - validate: { - payload: declarationNotificationSchema - } - } - }, - { - method: 'POST', - path: '/birthRegistrationSMS', - handler: sendBirthRegistrationConfirmation, - options: { - tags: ['api'], - description: - 'Sends an sms or email to a user for birth registration entry', - auth: { - scope: [SCOPES.RECORD_REGISTER] - }, - validate: { - payload: registrationNotificationSchema - } - } - }, - { - method: 'POST', - path: '/birthRejectionSMS', - handler: sendBirthRejectionConfirmation, - options: { - tags: ['api'], - description: - 'Sends an sms or email to a user for birth declaration rejection entry', - auth: { - scope: [SCOPES.RECORD_SUBMIT_FOR_APPROVAL, SCOPES.RECORD_REGISTER] - }, - validate: { - payload: rejectionNotificationSchema - } - } - }, - { - method: 'POST', - path: '/deathInProgressSMS', - handler: sendDeathInProgressConfirmation, - options: { - tags: ['api'], - description: - 'Sends an sms or email to a user for death in-progress entry', auth: { - scope: [ - SCOPES.RECORD_DECLARE_BIRTH, - SCOPES.RECORD_DECLARE_DEATH, - SCOPES.RECORD_DECLARE_MARRIAGE, - SCOPES.RECORD_REGISTER, - SCOPES.RECORD_CERTIFY, - SCOPES.RECORD_SUBMIT_FOR_APPROVAL - ] + scope: [SCOPES.RECORD_SUBMIT_FOR_UPDATES] }, - validate: { - payload: inProgressNotificationSchema - } - } - }, - { - method: 'POST', - path: '/deathDeclarationSMS', - handler: sendDeathDeclarationConfirmation, - options: { tags: ['api'], description: - 'Sends an sms or email to a user for death declaration entry', - auth: { - scope: [ - SCOPES.RECORD_DECLARE_BIRTH, - SCOPES.RECORD_DECLARE_DEATH, - SCOPES.RECORD_DECLARE_MARRIAGE, - SCOPES.RECORD_REGISTER, - SCOPES.RECORD_CERTIFY, - SCOPES.RECORD_SUBMIT_FOR_APPROVAL - ] - }, - validate: { - payload: declarationNotificationSchema - } - } - }, - { - method: 'POST', - path: '/deathRegistrationSMS', - handler: sendDeathRegistrationConfirmation, - options: { - tags: ['api'], - description: 'Sends an sms to a user for death registration entry', - auth: { - scope: [SCOPES.RECORD_REGISTER] - }, - validate: { - payload: registrationNotificationSchema - } - } - }, - { - method: 'POST', - path: '/deathRejectionSMS', - handler: sendDeathRejectionConfirmation, - options: { - tags: ['api'], - description: - 'Sends an sms to a user for death declaration rejection entry', - auth: { - scope: [SCOPES.RECORD_SUBMIT_FOR_APPROVAL, SCOPES.RECORD_REGISTER] - }, - validate: { - payload: rejectionNotificationSchema - } + 'Sends a notification to country config for rejected death declaration', + validate: recordValidation } }, { @@ -371,7 +204,7 @@ export default function getRoutes(): ServerRoute[] { tags: ['api'], description: 'Sends an sms to a user with credentials', auth: { - scope: [SCOPES.CONFIG_UPDATE_ALL] + scope: [SCOPES.USER_READ] }, validate: { payload: userCredentialsNotificationSchema @@ -416,7 +249,7 @@ export default function getRoutes(): ServerRoute[] { tags: ['api'], description: 'Sends an sms to a user with new temporary password', auth: { - scope: [SCOPES.CONFIG_UPDATE_ALL] + scope: [SCOPES.USER_READ] }, validate: { payload: userPasswordResetNotificationSchema diff --git a/packages/notification/src/features/sms/birth-handler.test.ts b/packages/notification/src/features/sms/birth-handler.test.ts deleted file mode 100644 index 3bc55f48b4a..00000000000 --- a/packages/notification/src/features/sms/birth-handler.test.ts +++ /dev/null @@ -1,448 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * OpenCRVS is also distributed under the terms of the Civil Registration - * & Healthcare Disclaimer located at http://opencrvs.org/license. - * - * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. - */ -import { readFileSync } from 'fs' -import * as jwt from 'jsonwebtoken' -import * as utils from '@notification/features/sms/utils' -import { createServer } from '@notification/server' -import * as fetchAny from 'jest-fetch-mock' -import { translationsMock } from '@notification/tests/util' - -const fetch = fetchAny as any -describe('Verify birth handlers', () => { - let server: any - - beforeEach(async () => { - server = await createServer() - }) - describe('sendBirthInProgressConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/birthInProgressSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - crvsOffice: 'আলকবালী ইউনিয়ন পরিষদ', - registrationLocation: 'Blah', - trackingId: 'B123456' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - it('returns 400 if called with invalid trackingId', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/birthInProgressSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - crvsOffice: 'আলকবালী ইউনিয়ন পরিষদ', - registrationLocation: 'Blah', - trackingId: 'aeUxkeoseSd-afsdasdf-safasfasf' - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthInProgressSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - crvsOffice: 'আলকবালী ইউনিয়ন পরিষদ', - registrationLocation: 'Blah', - trackingId: 'B123456' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) - describe('sendBirthDeclarationConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthDeclarationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - it('returns 400 if called with invalid trackingId', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthDeclarationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'childName', - trackingId: 'aeUxkeoseSd-afsdasdf-safasfasf', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthDeclarationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) - describe('sendBirthRegistrationConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthRegistrationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - registrationNumber: '20196816020000129', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - it('returns 400 if called with invalid data', async () => { - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthRegistrationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - } - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthRegistrationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - registrationNumber: '20196816020000129', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) - describe('sendBirthRejectionConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['validate'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/birthRejectionSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - it('returns 400 if called with invalid trackingId', async () => { - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/birthRejectionSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'childName', - trackingId: 'aeUxkeoseSd-afsdasdf-safasfasf', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['validate'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthRejectionSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) -}) diff --git a/packages/notification/src/features/sms/birth-handler.ts b/packages/notification/src/features/sms/birth-handler.ts deleted file mode 100644 index 59fd8da5edd..00000000000 --- a/packages/notification/src/features/sms/birth-handler.ts +++ /dev/null @@ -1,193 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * OpenCRVS is also distributed under the terms of the Civil Registration - * & Healthcare Disclaimer located at http://opencrvs.org/license. - * - * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. - */ -import * as Hapi from '@hapi/hapi' -import * as Joi from 'joi' -import { - sendNotification, - IEventMessageRecipient -} from '@notification/features/sms/utils' -import { logger } from '@opencrvs/commons' -import { messageKeys } from '@notification/i18n/messages' - -export interface IInProgressPayload extends IEventMessageRecipient { - trackingId: string - crvsOffice: string - registrationLocation: string - informantName: string -} - -export interface IDeclarationPayload extends IEventMessageRecipient { - trackingId: string - name: string - crvsOffice: string - registrationLocation: string - informantName: string -} - -export interface IRegistrationPayload extends IEventMessageRecipient { - name: string - informantName: string - registrationNumber: string - trackingId: string - crvsOffice: string - registrationLocation: string -} - -export interface IRejectionPayload extends IEventMessageRecipient { - trackingId: string - name: string - informantName: string - crvsOffice: string - registrationLocation: string -} - -export async function sendBirthInProgressConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IInProgressPayload - logger.info(`Notifying from sendBirthInProgressConfirmation`) - const templateName = messageKeys.birthInProgressNotification - await sendNotification( - { - sms: templateName, - email: templateName - }, - { - sms: payload.recipient.sms, - email: payload.recipient.email - }, - 'informant', - { - trackingId: payload.trackingId, - crvsOffice: payload.crvsOffice, - registrationLocation: payload.registrationLocation, - informantName: payload.informantName - } - ) - return h.response().code(200) -} - -export async function sendBirthDeclarationConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IDeclarationPayload - logger.info(`Notifying from sendBirthDeclarationConfirmation`) - const templateName = messageKeys.birthDeclarationNotification - await sendNotification( - { sms: templateName, email: templateName }, - { sms: payload.recipient.sms, email: payload.recipient.email }, - 'informant', - { - name: payload.name, - crvsOffice: payload.crvsOffice, - registrationLocation: payload.registrationLocation, - trackingId: payload.trackingId, - informantName: payload.informantName - } - ) - return h.response().code(200) -} - -export async function sendBirthRegistrationConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IRegistrationPayload - logger.info(`Notifying from sendBirthRegistrationConfirmation`) - const templateName = messageKeys.birthRegistrationNotification - await sendNotification( - { sms: templateName, email: templateName }, - { sms: payload.recipient.sms, email: payload.recipient.email }, - 'informant', - { - name: payload.name, - informantName: payload.informantName, - trackingId: payload.trackingId, - crvsOffice: payload.crvsOffice, - registrationLocation: payload.registrationLocation, - registrationNumber: payload.registrationNumber - } - ) - return h.response().code(200) -} - -export async function sendBirthRejectionConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IRejectionPayload - logger.info(`Notifying from sendBirthRejectionConfirmation`) - - const templateName = messageKeys.birthRejectionNotification - await sendNotification( - { sms: templateName, email: templateName }, - { sms: payload.recipient.sms, email: payload.recipient.email }, - 'informant', - { - name: payload.name, - informantName: payload.informantName, - crvsOffice: payload.crvsOffice, - registrationLocation: payload.registrationLocation, - trackingId: payload.trackingId - } - ) - return h.response().code(200) -} - -export const inProgressNotificationSchema = Joi.object({ - recipient: Joi.object({ - email: Joi.string().allow(null), - sms: Joi.string().allow(null) - }), - trackingId: Joi.string().length(7).required(), - crvsOffice: Joi.string().required(), - registrationLocation: Joi.string().required(), - informantName: Joi.string() -}) - -export const declarationNotificationSchema = Joi.object({ - recipient: Joi.object({ - email: Joi.string().allow(null), - sms: Joi.string().allow(null) - }), - trackingId: Joi.string().length(7).required(), - crvsOffice: Joi.string().required(), - registrationLocation: Joi.string().required(), - name: Joi.string().required(), - informantName: Joi.string() -}) - -export const registrationNotificationSchema = Joi.object({ - recipient: Joi.object({ - email: Joi.string().allow(null), - sms: Joi.string().allow(null) - }), - name: Joi.string().required(), - informantName: Joi.string(), - crvsOffice: Joi.string().required(), - registrationLocation: Joi.string().required(), - trackingId: Joi.string().length(7).required(), - registrationNumber: Joi.string().required() -}) - -export const rejectionNotificationSchema = Joi.object({ - recipient: Joi.object({ - email: Joi.string().allow(null), - sms: Joi.string().allow(null) - }), - trackingId: Joi.string().length(7).required(), - crvsOffice: Joi.string().required(), - registrationLocation: Joi.string().required(), - informantName: Joi.string(), - name: Joi.string().required() -}) diff --git a/packages/notification/src/features/sms/death-handler.test.ts b/packages/notification/src/features/sms/death-handler.test.ts deleted file mode 100644 index 0125204d2ce..00000000000 --- a/packages/notification/src/features/sms/death-handler.test.ts +++ /dev/null @@ -1,450 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * OpenCRVS is also distributed under the terms of the Civil Registration - * & Healthcare Disclaimer located at http://opencrvs.org/license. - * - * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. - */ -import { readFileSync } from 'fs' -import * as jwt from 'jsonwebtoken' -import * as utils from '@notification/features/sms/utils' -import { createServer } from '@notification/server' -import * as fetchAny from 'jest-fetch-mock' -import { translationsMock } from '@notification/tests/util' - -const fetch = fetchAny as any -describe('Verify death handlers', () => { - let server: any - - beforeEach(async () => { - server = await createServer() - }) - describe('sendDeathInProgressConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/deathInProgressSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - crvsOffice: 'আলকবালী ইউনিয়ন পরিষদ', - registrationLocation: 'Blah', - trackingId: 'B123456' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - - it('returns 400 if called with invalid trackingId', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/deathInProgressSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - crvsOffice: 'আলকবালী ইউনিয়ন পরিষদ', - registrationLocation: 'Blah', - trackingId: 'aeUxkeoseSd-afsdasdf-safasfasf' - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/deathInProgressSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - crvsOffice: 'আলকবালী ইউনিয়ন পরিষদ', - registrationLocation: 'Blah', - trackingId: 'B123456' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) - describe('sendDeathDeclarationConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/deathDeclarationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - - it('returns 400 if called with invalid trackingId', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/deathDeclarationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'childName', - trackingId: 'aeUxkeoseSd-afsdasdf-safasfasf', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/deathDeclarationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) - describe('sendDeathRegistrationConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/deathRegistrationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'D123456', - registrationNumber: '20196816020000129', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - it('returns 400 if called with invalid payload', async () => { - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/deathRegistrationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - } - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/deathRegistrationSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'D123456', - registrationNumber: '20196816020000129', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) - describe('sendDeathRejectionConfirmation', () => { - it('returns OK the sms gets sent', async () => { - const token = jwt.sign( - { scope: ['validate'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - - const res = await server.server.inject({ - method: 'POST', - url: '/deathRejectionSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - registrationLocation: 'Blah', - crvsOffice: 'ALASKA', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(res.statusCode).toBe(200) - }) - it('returns 400 if called with invalid trackingId', async () => { - const token = jwt.sign( - { scope: ['register'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/deathRejectionSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'childName', - trackingId: 'aeUxkeoseSd-afsdasdf-safasfasf', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}`, - language: 'en' // default is bn - } - }) - - expect(res.statusCode).toBe(400) - }) - it('returns 500 the sms is not sent', async () => { - const spy = jest - .spyOn(utils, 'sendNotification') - .mockImplementationOnce(() => Promise.reject(new Error())) - - const token = jwt.sign( - { scope: ['validate'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/deathRejectionSMS', - payload: { - recipient: { - sms: '447789778823', - email: 'email@email.com' - }, - name: 'অনিক', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'Blah', - informantName: 'Sadman Anik' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - - expect(spy).toHaveBeenCalled() - - expect(res.statusCode).toBe(500) - }) - }) -}) diff --git a/packages/notification/src/features/sms/death-handler.ts b/packages/notification/src/features/sms/death-handler.ts deleted file mode 100644 index 2817d909157..00000000000 --- a/packages/notification/src/features/sms/death-handler.ts +++ /dev/null @@ -1,108 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * OpenCRVS is also distributed under the terms of the Civil Registration - * & Healthcare Disclaimer located at http://opencrvs.org/license. - * - * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. - */ -import * as Hapi from '@hapi/hapi' -import { - IInProgressPayload, - IDeclarationPayload, - IRegistrationPayload, - IRejectionPayload -} from '@notification/features/sms/birth-handler' -import { sendNotification } from '@notification/features/sms/utils' -import { logger } from '@opencrvs/commons' -import { messageKeys } from '@notification/i18n/messages' - -export async function sendDeathInProgressConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IInProgressPayload - logger.info('Notifying from sendDeathInProgressConfirmation') - const templateName = messageKeys.deathInProgressNotification - await sendNotification( - { sms: templateName, email: templateName }, - { sms: payload.recipient.sms, email: payload.recipient.email }, - 'informant', - { - trackingId: payload.trackingId, - crvsOffice: payload.crvsOffice, - registrationLocation: payload.registrationLocation, - informantName: payload.informantName - } - ) - return h.response().code(200) -} - -export async function sendDeathDeclarationConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IDeclarationPayload - logger.info('Notifying from sendDeathDeclarationConfirmation') - const templateName = messageKeys.deathDeclarationNotification - await sendNotification( - { sms: templateName, email: templateName }, - { sms: payload.recipient.sms, email: payload.recipient.email }, - 'informant', - { - name: payload.name, - trackingId: payload.trackingId, - crvsOffice: payload.crvsOffice, - registrationLocation: payload.registrationLocation, - informantName: payload.informantName - } - ) - return h.response().code(200) -} - -export async function sendDeathRegistrationConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IRegistrationPayload - logger.info('Notifying from sendDeathRegistrationConfirmation') - const templateName = messageKeys.deathRegistrationNotification - await sendNotification( - { sms: templateName, email: templateName }, - { sms: payload.recipient.sms, email: payload.recipient.email }, - 'informant', - { - name: payload.name, - informantName: payload.informantName, - trackingId: payload.trackingId, - registrationNumber: payload.registrationNumber, - registrationLocation: payload.registrationLocation, - crvsOffice: payload.crvsOffice - } - ) - return h.response().code(200) -} - -export async function sendDeathRejectionConfirmation( - request: Hapi.Request, - h: Hapi.ResponseToolkit -) { - const payload = request.payload as IRejectionPayload - logger.info('Notifying from sendDeathRejectionConfirmation') - const templateName = messageKeys.deathRejectionNotification - await sendNotification( - { sms: templateName, email: templateName }, - { sms: payload.recipient.sms, email: payload.recipient.email }, - 'informant', - { - name: payload.name, - informantName: payload.informantName, - trackingId: payload.trackingId, - registrationLocation: payload.registrationLocation, - crvsOffice: payload.crvsOffice - } - ) - return h.response().code(200) -} diff --git a/packages/notification/src/features/sms/user-handler.test.ts b/packages/notification/src/features/sms/user-handler.test.ts index 32d85edf216..89b2424cb03 100644 --- a/packages/notification/src/features/sms/user-handler.test.ts +++ b/packages/notification/src/features/sms/user-handler.test.ts @@ -30,7 +30,7 @@ describe('Verify user handlers', () => { server = await createServerWithEnvironment() const token = jwt.sign( - { scope: ['sysadmin'] }, + { scope: ['user.read:all'] }, readFileSync('./test/cert.key'), { algorithm: 'RS256', @@ -64,7 +64,7 @@ describe('Verify user handlers', () => { }) it('returns 400 if called with no username', async () => { const token = jwt.sign( - { scope: ['sysadmin'] }, + { scope: ['user.read:all'] }, readFileSync('./test/cert.key'), { algorithm: 'RS256', @@ -94,7 +94,7 @@ describe('Verify user handlers', () => { .mockImplementationOnce(() => Promise.reject(new Error())) const token = jwt.sign( - { scope: ['sysadmin'] }, + { scope: ['user.read:all'] }, readFileSync('./test/cert.key'), { algorithm: 'RS256', @@ -454,7 +454,7 @@ describe('Verify user handlers', () => { server = await createServerWithEnvironment() const token = jwt.sign( - { scope: ['sysadmin'] }, + { scope: ['user.read:all'] }, readFileSync('./test/cert.key'), { algorithm: 'RS256', @@ -492,7 +492,7 @@ describe('Verify user handlers', () => { .mockImplementationOnce(() => Promise.reject(new Error())) const token = jwt.sign( - { scope: ['sysadmin'] }, + { scope: ['user.read:all'] }, readFileSync('./test/cert.key'), { algorithm: 'RS256', diff --git a/packages/notification/src/index.test.ts b/packages/notification/src/index.test.ts index ec0da712840..c82641f87c4 100644 --- a/packages/notification/src/index.test.ts +++ b/packages/notification/src/index.test.ts @@ -8,13 +8,7 @@ * * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ -import { readFileSync } from 'fs' -import * as jwt from 'jsonwebtoken' import { createServer } from '@notification/server' -import { translationsMock } from '@notification/tests/util' -import * as fetchMock from 'jest-fetch-mock' - -const fetch: fetchMock.FetchMock = fetchMock as fetchMock.FetchMock describe('Route authorization', () => { let server: any @@ -30,64 +24,4 @@ describe('Route authorization', () => { expect(res.statusCode).toBe(200) expect(res.payload).toBe(JSON.stringify({ success: true })) }) - - it('accepts requests with a valid token and valid user scope', async () => { - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthDeclarationSMS', - payload: { - recipient: { - sms: '+447789778865', - email: 'email@email.com' - }, - name: 'test', - trackingId: 'B123456', - crvsOffice: 'ALASKA', - registrationLocation: 'ALASKA', - informantName: 'SADMAN ANIK' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - expect(res.statusCode).toBe(200) - }) - it('blocks requests with a invalid user scope', async () => { - const token = jwt.sign( - { scope: ['demo'] }, // required declare | register | certify - readFileSync('./test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:notification-user' - } - ) - fetch.mockResponse(JSON.stringify(translationsMock)) - const res = await server.server.inject({ - method: 'POST', - url: '/birthDeclarationSMS', - payload: { - recipient: { - sms: '+447789778865', - email: 'email@email.com' - }, - name: 'test', - trackingId: 'B123456' - }, - headers: { - Authorization: `Bearer ${token}` - } - }) - expect(res.statusCode).toBe(403) - }) })