Files
logbuch/tests/domain/magic-link.test.ts
T
Matthias Giesselmann b90ff252d1 Add Logbuch: project update blog with admin, media and API
Public archive with sidebar navigation, project pages, month archive,
search and entry pages with image blocks. Admin area for brands,
projects, post types, users, api clients, media and the entry editor
with drag and drop images, preview per audience, scheduling and
publish checks. Read and write API with bearer tokens, audience
scoping, idempotent creation, OpenAPI document and editorial guide.
Magic link login with configurable allowed domains, whole app behind
the session gate. 456 tests including design rule checks.
2026-07-31 21:33:42 +02:00

143 lines
4.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { parseDomains } from '~/domain/email-domains'
import {
attemptsWithinWindow,
decideMagicLinkRequest,
magicLinkMaxAttempts,
magicLinkResponse,
type MagicLinkRequest,
} from '~/domain/magic-link'
const domains = parseDomains('nyo.de,pocketrocket.de,pocket-rocket.io')
function request(overrides: Partial<MagicLinkRequest> = {}): MagicLinkRequest {
return {
email: '[email protected]',
allowedDomains: domains,
hasAccount: false,
attempts: 1,
...overrides,
}
}
describe('decideMagicLinkRequest', () => {
it('schickt einen Link an jede freigegebene Domain, auch ohne Konto', () => {
for (const email of ['[email protected]', '[email protected]', '[email protected]']) {
expect(decideMagicLinkRequest(request({ email }))).toEqual({
email,
outcome: 'send',
reason: 'allowed-domain',
})
}
})
it('schickt einen Link an ein bestehendes Konto mit fremder Domain', () => {
expect(decideMagicLinkRequest(request({ email: '[email protected]', hasAccount: true })))
.toEqual({
email: '[email protected]',
outcome: 'send',
reason: 'known-account',
})
})
it('schickt keinen Link an eine fremde Domain ohne Konto', () => {
expect(decideMagicLinkRequest(request({ email: '[email protected]' }))).toEqual({
email: '[email protected]',
outcome: 'ignore',
reason: 'domain-not-allowed',
})
})
it('lässt sich nicht durch eine angehängte Domain täuschen', () => {
expect(decideMagicLinkRequest(request({ email: '[email protected]' })).outcome).toBe('ignore')
})
it('behandelt Groß- und Kleinschreibung und Leerraum gleich', () => {
expect(decideMagicLinkRequest(request({ email: ' [email protected] ' }))).toEqual({
email: '[email protected]',
outcome: 'send',
reason: 'allowed-domain',
})
})
it('erlaubt genau fünf Anforderungen im Zeitfenster', () => {
expect(decideMagicLinkRequest(request({ attempts: magicLinkMaxAttempts })).outcome).toBe('send')
expect(decideMagicLinkRequest(request({ attempts: magicLinkMaxAttempts + 1 }))).toEqual({
email: '[email protected]',
outcome: 'throttle',
reason: 'too-many-requests',
})
})
it('bremst jede Adresse, auch die abgewiesene', () => {
const decision = decideMagicLinkRequest(request({ email: '[email protected]', attempts: 6 }))
expect(decision.outcome).toBe('throttle')
})
it('bremst auch ein bestehendes Konto', () => {
const decision = decideMagicLinkRequest(request({
email: '[email protected]',
hasAccount: true,
attempts: 6,
}))
expect(decision.outcome).toBe('throttle')
})
})
describe('magicLinkResponse', () => {
it('antwortet gleich, egal ob ein Link rausgeht oder nicht', () => {
const sent = magicLinkResponse(decideMagicLinkRequest(request({ email: '[email protected]' })))
const known = magicLinkResponse(decideMagicLinkRequest(request({
email: '[email protected]',
allowedDomains: parseDomains('example.org'),
hasAccount: true,
})))
const ignored = magicLinkResponse(decideMagicLinkRequest(request({
email: '[email protected]',
allowedDomains: parseDomains('example.org'),
})))
expect(sent).toEqual({ status: 'sent', email: '[email protected]' })
expect(known).toEqual(sent)
expect(ignored).toEqual(sent)
})
it('verrät auch bei fremder Adresse nichts über das Konto', () => {
const withAccount = magicLinkResponse(decideMagicLinkRequest(request({
email: '[email protected]',
hasAccount: true,
})))
const withoutAccount = magicLinkResponse(decideMagicLinkRequest(request({
email: '[email protected]',
hasAccount: false,
})))
expect(withAccount).toEqual(withoutAccount)
})
it('meldet nur die Bremse offen', () => {
expect(magicLinkResponse(decideMagicLinkRequest(request({ attempts: 6 })))).toEqual({
status: 'error',
email: '[email protected]',
error: 'tooMany',
})
})
})
describe('attemptsWithinWindow', () => {
it('behält nur die Zeitpunkte im Fenster', () => {
const now = 1_000_000
expect(attemptsWithinWindow([now - 1000, now - 900_000, now - 100], now, 900_000)).toEqual([
now - 1000,
now - 100,
])
})
it('liefert für eine leere Liste nichts', () => {
expect(attemptsWithinWindow([], 10, 900_000)).toEqual([])
})
})