101 lines
4.5 KiB
JavaScript
101 lines
4.5 KiB
JavaScript
// test/auftraegeSchalter.test.js
|
|
import { test, beforeEach, afterEach } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import path from 'node:path'
|
|
import { config } from '../server/config.js'
|
|
import { auftraegeAktiv } from '../server/services/auftraege/schalter.js'
|
|
|
|
const ENV = ['AUFTRAEGE_ENABLED', 'AUFTRAEGE_PILOT_EMAILS', 'AUFTRAEGE_UPLOAD_DIR', 'APPWRITE_COLLECTION_AUFTRAEGE', 'APPWRITE_COLLECTION_PROJEKT_RECHNUNGEN']
|
|
|
|
let original
|
|
beforeEach(() => {
|
|
original = { ...config.auftraege }
|
|
})
|
|
afterEach(() => {
|
|
Object.assign(config.auftraege, original)
|
|
})
|
|
|
|
/** Frische config.js-Instanz mit genau diesen Umgebungsvariablen (alle anderen entfernt). */
|
|
let freshCounter = 0
|
|
async function freshConfig(env = {}) {
|
|
const saved = Object.fromEntries(ENV.map((key) => [key, process.env[key]]))
|
|
for (const key of ENV) delete process.env[key]
|
|
Object.assign(process.env, env)
|
|
try {
|
|
freshCounter += 1
|
|
return (await import(`../server/config.js?auftraege-test=${freshCounter}`)).config
|
|
} finally {
|
|
for (const key of ENV) {
|
|
if (saved[key] === undefined) delete process.env[key]
|
|
else process.env[key] = saved[key]
|
|
}
|
|
}
|
|
}
|
|
|
|
test('Vorgabe: aus, keine Pilotadressen, Ablage unter upload-data/auftraege, Collection-IDs wie die Namen', async () => {
|
|
const fresh = await freshConfig()
|
|
assert.deepEqual(fresh.auftraege, { mode: 'off', pilotEmails: [], uploadDir: path.join(process.cwd(), 'upload-data', 'auftraege') })
|
|
assert.equal(fresh.collections.auftraege, 'auftraege')
|
|
assert.equal(fresh.collections.projektRechnungen, 'projektRechnungen')
|
|
})
|
|
|
|
test('AUFTRAEGE_ENABLED: leer und Unbekanntes sind off, " ON " ist on, " Pilot " ist pilot mit bereinigter Liste', async () => {
|
|
for (const value of ['', ' ', 'banane', 'true', '1']) {
|
|
assert.equal((await freshConfig({ AUFTRAEGE_ENABLED: value })).auftraege.mode, 'off', JSON.stringify(value))
|
|
}
|
|
assert.equal((await freshConfig({ AUFTRAEGE_ENABLED: ' ON ' })).auftraege.mode, 'on')
|
|
const fresh = await freshConfig({ AUFTRAEGE_ENABLED: ' Pilot ', AUFTRAEGE_PILOT_EMAILS: ' Kenso.Gri@Gmail.com, ,justin@webklar.com ' })
|
|
assert.equal(fresh.auftraege.mode, 'pilot')
|
|
assert.deepEqual(fresh.auftraege.pilotEmails, ['kenso.gri@gmail.com', 'justin@webklar.com'])
|
|
})
|
|
|
|
test('Ablage und Collection-IDs lassen sich per Umgebung ändern', async () => {
|
|
const fresh = await freshConfig({
|
|
AUFTRAEGE_UPLOAD_DIR: '/tmp/ab-test',
|
|
APPWRITE_COLLECTION_AUFTRAEGE: 'abX',
|
|
APPWRITE_COLLECTION_PROJEKT_RECHNUNGEN: 'prX',
|
|
})
|
|
assert.equal(fresh.auftraege.uploadDir, '/tmp/ab-test')
|
|
assert.deepEqual([fresh.collections.auftraege, fresh.collections.projektRechnungen], ['abX', 'prX'])
|
|
})
|
|
|
|
test('off sperrt alle, auch mit Stern in der Liste; ein unbekannter Modus gilt als off', () => {
|
|
config.auftraege.mode = 'off'
|
|
config.auftraege.pilotEmails = ['*', 'kenso.gri@gmail.com']
|
|
assert.equal(auftraegeAktiv({ email: 'kenso.gri@gmail.com' }), false)
|
|
assert.equal(auftraegeAktiv({ email: 'jemand@example.com' }), false)
|
|
for (const value of [undefined, null, '', 'banane', 'PILOT']) {
|
|
config.auftraege.mode = value
|
|
assert.equal(auftraegeAktiv({ email: 'kenso.gri@gmail.com' }), false, String(value))
|
|
}
|
|
})
|
|
|
|
test('pilot: nur Adressen der Liste, ohne Beachtung von Groß- und Kleinschreibung und Leerraum', () => {
|
|
config.auftraege.mode = 'pilot'
|
|
config.auftraege.pilotEmails = ['a@b.de']
|
|
assert.equal(auftraegeAktiv({ email: 'A@b.de' }), true)
|
|
assert.equal(auftraegeAktiv({ email: ' a@b.de ' }), true)
|
|
assert.equal(auftraegeAktiv({ email: 'x@y.de' }), false)
|
|
assert.equal(auftraegeAktiv({ email: 'a@b.de.evil.de' }), false)
|
|
for (const kunde of [undefined, null, {}, { email: '' }, { email: ' ' }]) assert.equal(auftraegeAktiv(kunde), false)
|
|
})
|
|
|
|
test('pilot mit Stern lässt alle durch, auch ohne Adresse; eine leere Liste niemanden', () => {
|
|
config.auftraege.mode = 'pilot'
|
|
config.auftraege.pilotEmails = ['*']
|
|
assert.equal(auftraegeAktiv({ email: 'x@y.de' }), true)
|
|
assert.equal(auftraegeAktiv(null), true)
|
|
config.auftraege.pilotEmails = []
|
|
assert.equal(auftraegeAktiv({ email: 'a@b.de' }), false)
|
|
})
|
|
|
|
test('on gilt für alle; ein Kundendokument zählt wie { email }', () => {
|
|
config.auftraege.mode = 'on'
|
|
config.auftraege.pilotEmails = []
|
|
assert.equal(auftraegeAktiv({ email: 'x@y.de' }), true)
|
|
assert.equal(auftraegeAktiv(undefined), true)
|
|
config.auftraege.mode = 'pilot'
|
|
config.auftraege.pilotEmails = ['k@example.invalid']
|
|
assert.equal(auftraegeAktiv({ $id: 'c1', email: 'k@example.invalid', name: 'Kunde Eins' }), true)
|
|
})
|