diff --git a/src/extensions/hooks/notifications-format/src/index.ts b/src/extensions/hooks/notifications-format/src/index.ts index 00e95db..6d27752 100644 --- a/src/extensions/hooks/notifications-format/src/index.ts +++ b/src/extensions/hooks/notifications-format/src/index.ts @@ -16,7 +16,7 @@ export default defineHook(({ init, action }) => { init('middlewares.after', ({ app }) => { app.use((req: Request, _res: Response, next: NextFunction) => { // Unknown parameters passed to standard endpoints are removed by Directus. So we are passing them in accountability object. - if (req.method === 'GET' && req.query.format === 'html') { + if (req.accountability && req.method === 'GET' && req.query.format === 'html') { req.accountability = { ...req.accountability, customParams: { format: 'html' }, diff --git a/src/extensions/hooks/notifications-format/test/index.test.ts b/src/extensions/hooks/notifications-format/test/index.test.ts new file mode 100644 index 0000000..1732de2 --- /dev/null +++ b/src/extensions/hooks/notifications-format/test/index.test.ts @@ -0,0 +1,67 @@ +import { expect } from 'chai'; +import * as sinon from 'sinon'; +import defineHook from '../src/index.js'; + +type InitCallback = ({ app }: { app: any }) => Promise; +type ActionCallback = (meta: any, context: any) => Promise; + +describe('notifications-format hooks', () => { + const app = { use: sinon.stub() }; + + const callbacks = { + init: {} as Record, + action: {} as Record, + }; + const events = { + init: (name: string, cb: InitCallback) => { + callbacks.init[name] = cb; + }, + action: (name: string, cb: ActionCallback) => { + callbacks.action[name] = cb; + }, + } as any; + + defineHook(events); + + beforeEach(() => { + sinon.resetHistory(); + }); + + it('should return notification message as html if format=html param passed', () => { + callbacks.init['middlewares.after']?.({ app }); + const middleware = app.use.args?.[0]?.[0]; + const req: Record = { method: 'GET', query: { format: 'html' }, accountability: {} }; + middleware(req, {}, () => {}); + const payload = [{ + message: `Globalping API detected that your adopted probe with ip: 51.158.22.211 is located at “FR”. So its country value changed from “IT” to “FR”, and custom city value “Naples” is not applied right now.\n\nIf this change is not right please report in [that issue](https://github.com/jsdelivr/globalping/issues/268).`, + }]; + + callbacks.action['notifications.read']?.({ payload }, { accountability: { customParams: { format: 'html' } } }); + + expect(app.use.callCount).to.equal(1); + expect(req.accountability).to.deep.equal({ customParams: { format: 'html' } }); + + expect(payload).to.deep.equal([{ + message: '

Globalping API detected that your adopted probe with ip: 51.158.22.211 is located at “FR”. So its country value changed from “IT” to “FR”, and custom city value “Naples” is not applied right now.

\n

If this change is not right please report in that issue.

\n', + }]); + }); + + it('should return notification message as md by default', async () => { + callbacks.init['middlewares.after']?.({ app }); + const middleware = app.use.args?.[0]?.[0]; + const req: Record = { method: 'GET', query: { sort: 'asc' }, accountability: {} }; + middleware(req, {}, () => {}); + const payload = [{ + message: `Globalping API detected that your adopted probe with ip: 51.158.22.211 is located at “FR”. So its country value changed from “IT” to “FR”, and custom city value “Naples” is not applied right now.\n\nIf this change is not right please report in [that issue](https://github.com/jsdelivr/globalping/issues/268).`, + }]; + + callbacks.action['notifications.read']?.({ payload }, { accountability: {} }); + + expect(app.use.callCount).to.equal(1); + expect(req.accountability).to.deep.equal({}); + + expect(payload).to.deep.equal([{ + message: 'Globalping API detected that your adopted probe with ip: 51.158.22.211 is located at “FR”. So its country value changed from “IT” to “FR”, and custom city value “Naples” is not applied right now.\n\nIf this change is not right please report in [that issue](https://github.com/jsdelivr/globalping/issues/268).', + }]); + }); +});