From 30782b28e427e59dd0a360554e4d8d5f6a422097 Mon Sep 17 00:00:00 2001 From: Manfred Martin Date: Tue, 12 Nov 2024 20:20:41 +0100 Subject: [PATCH] :fire: [NTFY] - Fix basic/bearer authentication --- app/triggers/providers/ntfy/Ntfy.js | 19 ++++-- app/triggers/providers/ntfy/Ntfy.test.js | 82 +++++++++++++++++++++++- docs/changelog/README.md | 3 + e2e/features/api-container.feature | 2 +- 4 files changed, 100 insertions(+), 6 deletions(-) diff --git a/app/triggers/providers/ntfy/Ntfy.js b/app/triggers/providers/ntfy/Ntfy.js index 3c1eb114..60961151 100644 --- a/app/triggers/providers/ntfy/Ntfy.js +++ b/app/triggers/providers/ntfy/Ntfy.js @@ -29,7 +29,14 @@ class Ntfy extends Trigger { * @returns {*} */ maskConfiguration() { - return this.configuration; + return { + ...this.configuration, + auth: this.configuration.auth ? { + user: Ntfy.mask(this.configuration.user), + password: Ntfy.mask(this.configuration.password), + token: Ntfy.mask(this.configuration.token), + } : undefined, + }; } /** @@ -75,15 +82,19 @@ class Ntfy extends Trigger { body, json: true, }; - if (this.configuration.auth && this.configuration.user && this.configuration.password) { + if ( + this.configuration.auth + && this.configuration.auth.user + && this.configuration.auth.password + ) { options.auth = { user: this.configuration.auth.user, pass: this.configuration.auth.password, }; } - if (this.configuration.auth && this.configuration.token) { + if (this.configuration.auth && this.configuration.auth.token) { options.auth = { - bearer: this.configuration.auth.bearer, + bearer: this.configuration.auth.token, }; } return rp(options); diff --git a/app/triggers/providers/ntfy/Ntfy.test.js b/app/triggers/providers/ntfy/Ntfy.test.js index 1a11556b..d0caba4a 100644 --- a/app/triggers/providers/ntfy/Ntfy.test.js +++ b/app/triggers/providers/ntfy/Ntfy.test.js @@ -1,7 +1,9 @@ const { ValidationError } = require('joi'); - +const rp = require('request-promise-native'); const Ntfy = require('./Ntfy'); +jest.mock('request-promise-native'); + const ntfy = new Ntfy(); const configurationValid = { @@ -19,6 +21,10 @@ const configurationValid = { batchtitle: '${count} updates available', }; +beforeEach(() => { + jest.resetAllMocks(); +}); + test('validateConfiguration should return validated configuration when valid', () => { const validatedConfiguration = ntfy.validateConfiguration(configurationValid); expect(validatedConfiguration).toStrictEqual(configurationValid); @@ -32,3 +38,77 @@ test('validateConfiguration should throw error when invalid', () => { ntfy.validateConfiguration(configuration); }).toThrowError(ValidationError); }); + +test('trigger should call http client', async () => { + ntfy.configuration = configurationValid; + const container = { + name: 'container1', + }; + await ntfy.trigger(container); + expect(rp).toHaveBeenCalledWith({ + body: { + message: 'Container container1 running with can be updated to \n', + priority: 2, + title: 'New found for container container1', + topic: 'xxx', + }, + headers: { + 'Content-Type': 'application/json', + }, + method: 'POST', + json: true, + uri: 'http://xxx.com', + }); +}); + +test('trigger should use basic auth when configured like that', async () => { + ntfy.configuration = { + ...configurationValid, + auth: { user: 'user', password: 'pass' }, + }; + const container = { + name: 'container1', + }; + await ntfy.trigger(container); + expect(rp).toHaveBeenCalledWith({ + body: { + message: 'Container container1 running with can be updated to \n', + priority: 2, + title: 'New found for container container1', + topic: 'xxx', + }, + headers: { + 'Content-Type': 'application/json', + }, + method: 'POST', + json: true, + uri: 'http://xxx.com', + auth: { user: 'user', pass: 'pass' }, + }); +}); + +test('trigger should use bearer auth when configured like that', async () => { + ntfy.configuration = { + ...configurationValid, + auth: { token: 'token' }, + }; + const container = { + name: 'container1', + }; + await ntfy.trigger(container); + expect(rp).toHaveBeenCalledWith({ + body: { + message: 'Container container1 running with can be updated to \n', + priority: 2, + title: 'New found for container container1', + topic: 'xxx', + }, + headers: { + 'Content-Type': 'application/json', + }, + method: 'POST', + json: true, + uri: 'http://xxx.com', + auth: { bearer: 'token' }, + }); +}); diff --git a/docs/changelog/README.md b/docs/changelog/README.md index 8d9705eb..5a7e553b 100644 --- a/docs/changelog/README.md +++ b/docs/changelog/README.md @@ -1,5 +1,8 @@ # Changelog +# 7.1.1 (wip) +- :fire: [NTFY] - Fix basic/bearer authentication + # 7.1.0 - :star: [GOTIFY] - Add support for [Gotify](/configuration/triggers/gotify/) trigger - :star: [NTFY] - Add support for [Ntfy](/configuration/triggers/ntfy/) trigger diff --git a/e2e/features/api-container.feature b/e2e/features/api-container.feature index d56dc5f5..182bfc48 100644 --- a/e2e/features/api-container.feature +++ b/e2e/features/api-container.feature @@ -75,7 +75,7 @@ Feature: WUD Container API Exposure And response body path $.image.tag.semver should be false And response body path $.image.digest.value should be sha256:f94d6dd9b5761f33a21bb92848a1f70ea11a1c15f3a142c19a44ea3a4c545a4d And response body path $.result.tag should be latest - And response body path $.result.digest should be sha256:367678a80c0be120f67f3adfccc2f408bd2c1319ed98c1975ac88e750d0efe26 + And response body path $.result.digest should be sha256:5341b734e75ce46bbb8e02476434217fd771e23df9a4bfea756a6f3a4a521d3e And response body path $.updateAvailable should be true Scenario: WUD must allow to get a container with its link