65 lines
3.9 KiB
JavaScript
65 lines
3.9 KiB
JavaScript
import { test, after, beforeEach } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { startAppwriteMock } from './support/appwriteMock.js'
|
|
import { startFakeStripe } from './support/fakeStripe.js'
|
|
import { useTestEnv } from './support/testEnv.js'
|
|
|
|
// Kundenart in der Kundenverwaltung (V4): Pflicht beim Anlegen nur ab Schalter pilot, beim Aendern optional
|
|
const aw = await startAppwriteMock()
|
|
const st = await startFakeStripe()
|
|
useTestEnv({ appwriteUrl: aw.url, stripeUrl: st.url })
|
|
const { config } = await import('../server/config.js')
|
|
const { startTestApp, post, send } = await import('./support/testApp.js')
|
|
const { default: customersRouter } = await import('../server/routes/admin/customers.js')
|
|
const app = await startTestApp({ '/api/admin/customers': customersRouter })
|
|
const urspruenglich = config.kundenansichtNeu.mode
|
|
after(() => {
|
|
config.kundenansichtNeu.mode = urspruenglich
|
|
return Promise.all([app.close(), aw.close(), st.close()])
|
|
})
|
|
const modus = (m) => { config.kundenansichtNeu.mode = m }
|
|
beforeEach(() => modus('pilot'))
|
|
|
|
const admin = { headers: { Authorization: 'Bearer jwt-admin' } }
|
|
const neu = (extra = {}) => ({ name: 'Bäckerei Müller', email: 'mueller@example.invalid', password: 'geheim-1234', ...extra })
|
|
|
|
test('pilot und on: Anlegen ohne oder mit ungültiger Kundenart: 400 kundenart_fehlt, nichts geschrieben', async () => {
|
|
for (const mode of ['pilot', 'on']) {
|
|
modus(mode)
|
|
for (const kundenart of [undefined, '', 'Unternehmen', 'firma', 5, null]) {
|
|
const res = await post('/api/admin/customers', neu({ kundenart }), admin)
|
|
assert.deepEqual([res.status, res.body], [400, { error: 'Kundenart ist erforderlich (unternehmen oder privat).', code: 'kundenart_fehlt' }], `${mode} ${String(kundenart)}`)
|
|
}
|
|
}
|
|
assert.equal(aw.docs('customers').size, 0)
|
|
})
|
|
|
|
test('Anlegen mit unternehmen oder privat; Antwort und Dokument tragen die Kundenart', async () => {
|
|
const a = await post('/api/admin/customers', neu({ kundenart: 'unternehmen' }), admin)
|
|
assert.deepEqual([a.status, a.body.customer.kundenart], [201, 'unternehmen'])
|
|
assert.equal(aw.docs('customers').get(a.body.customer.$id).kundenart, 'unternehmen')
|
|
const b = await post('/api/admin/customers', neu({ email: 'frau@example.invalid', kundenart: 'privat' }), admin)
|
|
assert.equal(b.body.customer.kundenart, 'privat')
|
|
})
|
|
|
|
test('Ändern: ohne Feld bleibt die Kundenart, gültig ändert sie, ungültig ist 400 ohne Änderung', async () => {
|
|
const id = [...aw.docs('customers').keys()][0]
|
|
assert.equal((await send('PATCH', `/api/admin/customers/${id}`, { body: { name: 'Neu' }, ...admin })).body.customer.kundenart, 'unternehmen')
|
|
assert.equal((await send('PATCH', `/api/admin/customers/${id}`, { body: { kundenart: 'privat' }, ...admin })).body.customer.kundenart, 'privat')
|
|
const bad = await send('PATCH', `/api/admin/customers/${id}`, { body: { kundenart: 'x' }, ...admin })
|
|
assert.deepEqual([bad.status, bad.body.code], [400, 'kundenart_fehlt'])
|
|
assert.equal(aw.docs('customers').get(id).kundenart, 'privat')
|
|
})
|
|
|
|
test('off (Schalter aus): Anlegen ohne Kundenart wie heute, kein Feld im Dokument; ungültig mitgeschickt bleibt 400', async () => {
|
|
// als letzter Test der Datei: legt Kunden an, die die Tests davor nicht sehen sollen
|
|
modus('off')
|
|
const a = await post('/api/admin/customers', neu({ email: 'off1@example.invalid' }), admin)
|
|
assert.deepEqual([a.status, a.body.customer.kundenart], [201, ''])
|
|
assert.equal(Object.hasOwn(aw.docs('customers').get(a.body.customer.$id), 'kundenart'), false)
|
|
assert.equal((await post('/api/admin/customers', neu({ email: 'off2@example.invalid', kundenart: '' }), admin)).status, 201)
|
|
assert.equal((await post('/api/admin/customers', neu({ email: 'off3@example.invalid', kundenart: 'privat' }), admin)).status, 201)
|
|
const bad = await post('/api/admin/customers', neu({ email: 'off4@example.invalid', kundenart: 'firma' }), admin)
|
|
assert.deepEqual([bad.status, bad.body.code], [400, 'kundenart_fehlt'])
|
|
})
|