The Android app's embedded push distributor (M7 Part 2) needs the shard's
client-facing ntfy relay URL to build its device topic endpoint, but the M7
Part 1 backend only used the NTFY_* vars server-side and never surfaced them.
Add a `push: { ntfyUrl }` block to settings.getPublic(), sourced from
NTFY_PUBLIC_URL or the first NTFY_ALLOWED_ORIGINS entry (never the possibly
internal NTFY_BASE_URL); null when unconfigured, so the app shows push as
unavailable for that shard. Additive, non-sensitive, forward-compatible.
- Extend the PublicSettings swagger schema; regenerate swagger-output.json.
- publicBrand.test.js: cover null / NTFY_PUBLIC_URL / NTFY_ALLOWED_ORIGINS.
- Document NTFY_PUBLIC_URL in .env.example and (docs PR) BACKEND_DESIGN.md.
Full server suite green (250 pass).
Co-Authored-By: Claude <noreply@anthropic.com>
106 lines
4.4 KiB
JavaScript
106 lines
4.4 KiB
JavaScript
// Point the DB at a closed port before the pool is built; getPublic() is fully
|
|
// monkeypatched below so no query runs, and db.close() releases the pool at the
|
|
// end so the process exits cleanly.
|
|
process.env.DB_HOST = '127.0.0.1'
|
|
process.env.DB_PORT = '59999'
|
|
|
|
const { test, beforeEach, afterEach, after } = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
|
|
// Lock the /public/settings brand contract the mobile app themes itself from
|
|
// (§8.6 of the Android plan). Exercises settings.getPublic() against an in-memory
|
|
// fake by monkeypatching settings.db — no DB. The brand block is sourced from the
|
|
// BRAND_* config defaults, with admin site_title / contact_email overriding.
|
|
const settingsDb = require('../src/model/settings/settings.db')
|
|
const settings = require('../src/model/settings/settings.model')
|
|
const brand = require('../src/config/brand')
|
|
const db = require('../src/utils/db')
|
|
|
|
after(() => db.close())
|
|
|
|
let savedGetAll
|
|
beforeEach(() => {
|
|
savedGetAll = settingsDb.getAll
|
|
settingsDb.getAll = async () => [] // no stored settings → pure BRAND_* defaults
|
|
})
|
|
afterEach(() => {
|
|
settingsDb.getAll = savedGetAll
|
|
})
|
|
|
|
const THEMING_FIELDS = ['name', 'shortName', 'tagline', 'description', 'contactEmail', 'url', 'accent', 'logo', 'hero', 'favicon']
|
|
|
|
test('getPublic exposes the full brand theming contract the app depends on', async () => {
|
|
const pub = await settings.getPublic()
|
|
assert.ok(pub.brand, 'brand block present')
|
|
for (const key of THEMING_FIELDS) {
|
|
assert.ok(key in pub.brand, `brand.${key} present`)
|
|
}
|
|
// Defaults flow from BRAND_* config when nothing is stored.
|
|
assert.equal(pub.brand.name, brand.name)
|
|
assert.equal(pub.brand.accent, brand.accent)
|
|
assert.equal(pub.brand.logo, brand.logo)
|
|
assert.equal(pub.brand.hero, brand.hero)
|
|
assert.equal(pub.brand.favicon, brand.favicon)
|
|
// Never leak the Discord-only integer accent form to a public client.
|
|
assert.equal(pub.brand.accentInt, undefined)
|
|
})
|
|
|
|
test('admin site_title / contact_email override the brand defaults', async () => {
|
|
settingsDb.getAll = async () => [
|
|
{ key: 'site_title', value: 'My Shard' },
|
|
{ key: 'contact_email', value: 'hi@shard.tld' },
|
|
]
|
|
const pub = await settings.getPublic()
|
|
assert.equal(pub.brand.name, 'My Shard') // site_title overrides brand.name
|
|
assert.equal(pub.brand.contactEmail, 'hi@shard.tld') // contact_email overrides
|
|
assert.equal(pub.brand.accent, brand.accent) // colors still from config
|
|
})
|
|
|
|
// The push relay block the app's embedded distributor discovers its ntfy base
|
|
// URL from (M7 Part 2). Null when nothing is configured; NTFY_PUBLIC_URL wins,
|
|
// else the first NTFY_ALLOWED_ORIGINS entry; NTFY_BASE_URL is never surfaced.
|
|
test('push.ntfyUrl is null when no ntfy env is configured', async () => {
|
|
const saved = { pub: process.env.NTFY_PUBLIC_URL, allow: process.env.NTFY_ALLOWED_ORIGINS, base: process.env.NTFY_BASE_URL }
|
|
delete process.env.NTFY_PUBLIC_URL
|
|
delete process.env.NTFY_ALLOWED_ORIGINS
|
|
process.env.NTFY_BASE_URL = 'http://ntfy:80' // internal-only, must NOT leak
|
|
try {
|
|
const pub = await settings.getPublic()
|
|
assert.ok(pub.push, 'push block present')
|
|
assert.equal(pub.push.ntfyUrl, null)
|
|
} finally {
|
|
restoreNtfyEnv(saved)
|
|
}
|
|
})
|
|
|
|
test('push.ntfyUrl prefers NTFY_PUBLIC_URL and trims a trailing slash', async () => {
|
|
const saved = { pub: process.env.NTFY_PUBLIC_URL, allow: process.env.NTFY_ALLOWED_ORIGINS, base: process.env.NTFY_BASE_URL }
|
|
process.env.NTFY_PUBLIC_URL = 'https://ntfy.shard.tld/'
|
|
process.env.NTFY_ALLOWED_ORIGINS = 'https://other.tld'
|
|
try {
|
|
const pub = await settings.getPublic()
|
|
assert.equal(pub.push.ntfyUrl, 'https://ntfy.shard.tld')
|
|
} finally {
|
|
restoreNtfyEnv(saved)
|
|
}
|
|
})
|
|
|
|
test('push.ntfyUrl falls back to the first NTFY_ALLOWED_ORIGINS entry', async () => {
|
|
const saved = { pub: process.env.NTFY_PUBLIC_URL, allow: process.env.NTFY_ALLOWED_ORIGINS, base: process.env.NTFY_BASE_URL }
|
|
delete process.env.NTFY_PUBLIC_URL
|
|
process.env.NTFY_ALLOWED_ORIGINS = 'https://ntfy.shard.tld, https://second.tld'
|
|
try {
|
|
const pub = await settings.getPublic()
|
|
assert.equal(pub.push.ntfyUrl, 'https://ntfy.shard.tld')
|
|
} finally {
|
|
restoreNtfyEnv(saved)
|
|
}
|
|
})
|
|
|
|
function restoreNtfyEnv(saved) {
|
|
for (const [name, val] of [['NTFY_PUBLIC_URL', saved.pub], ['NTFY_ALLOWED_ORIGINS', saved.allow], ['NTFY_BASE_URL', saved.base]]) {
|
|
if (val === undefined) delete process.env[name]
|
|
else process.env[name] = val
|
|
}
|
|
}
|