158 lines
6.5 KiB
TypeScript
158 lines
6.5 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
|
import { Store } from '../src/magic/store'
|
|
import { hashToken, safeNextPath } from '../src/magic/tokens'
|
|
|
|
const T0 = '2026-01-01T00:00:00.000Z'
|
|
const T1 = '2026-01-02T00:00:00.000Z'
|
|
const T2 = '2026-01-03T00:00:00.000Z'
|
|
|
|
let store: Store
|
|
|
|
beforeEach(() => {
|
|
store = new Store(':memory:')
|
|
})
|
|
|
|
afterEach(() => {
|
|
store.close()
|
|
})
|
|
|
|
function recipient (email = 'ann@example.com', frequency: 'off' | 'daily' = 'off') {
|
|
return store.createRecipient({ name: 'Ann', email, frequency }, T0)
|
|
}
|
|
|
|
describe('magic links', () => {
|
|
it('signs in the recipient a live link belongs to', () => {
|
|
const ann = recipient()
|
|
const token = store.createMagicLink(ann.id, { purpose: 'invite', expiresAt: null }, T0)
|
|
expect(store.useMagicLink(token, T1)?.id).toBe(ann.id)
|
|
})
|
|
|
|
it('stores only a hash of the token', () => {
|
|
const ann = recipient()
|
|
const token = store.createMagicLink(ann.id, { purpose: 'invite', expiresAt: null }, T0)
|
|
const rows = store.db.prepare('SELECT token_hash FROM magic_links').all()
|
|
expect(rows).toEqual([{ token_hash: hashToken(token) }])
|
|
})
|
|
|
|
it('refuses an expired link, an unknown link, and a disabled recipient', () => {
|
|
const ann = recipient()
|
|
const expiring = store.createMagicLink(ann.id, { purpose: 'sign-in', expiresAt: T1 }, T0)
|
|
expect(store.useMagicLink(expiring, T1)).toBeUndefined()
|
|
expect(store.useMagicLink('not-a-real-token-at-all', T0)).toBeUndefined()
|
|
|
|
const forever = store.createMagicLink(ann.id, { purpose: 'invite', expiresAt: null }, T0)
|
|
store.updateRecipient(ann.id, { name: 'Ann', email: ann.email, disabled: true })
|
|
expect(store.useMagicLink(forever, T1)).toBeUndefined()
|
|
})
|
|
|
|
it('revoking access ends sessions and every link', () => {
|
|
const ann = recipient()
|
|
const link = store.createMagicLink(ann.id, { purpose: 'invite', expiresAt: null }, T0)
|
|
const session = store.createSession(ann.id, T0)
|
|
store.revokeAccess(ann.id)
|
|
expect(store.useMagicLink(link, T1)).toBeUndefined()
|
|
expect(store.getSession(session)).toBeUndefined()
|
|
})
|
|
|
|
it('rate-limits self-service link requests per address', () => {
|
|
expect(store.allowLinkRequest('ann@example.com', T1, T0)).toBe(true)
|
|
// Within the interval: refused
|
|
expect(store.allowLinkRequest('ANN@example.com', T1, T0)).toBe(false)
|
|
// After it: allowed
|
|
expect(store.allowLinkRequest('ann@example.com', T2, T1)).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('share access', () => {
|
|
it('grants a share by key or slug, only to recipients it was given to', () => {
|
|
const ann = recipient()
|
|
const bob = recipient('bob@example.com')
|
|
store.upsertShare({ key: 'key-a', slug: 'holiday', title: 'Holiday', password: null }, T0)
|
|
store.upsertShare({ key: 'key-b', slug: null, title: 'Other', password: null }, T0)
|
|
store.setRecipientShares(ann.id, ['key-a'], T0)
|
|
|
|
expect(store.grantedShare(ann.id, 'key-a')?.title).toBe('Holiday')
|
|
expect(store.grantedShare(ann.id, 'holiday')?.key).toBe('key-a')
|
|
expect(store.grantedShare(ann.id, 'key-b')).toBeUndefined()
|
|
expect(store.grantedShare(bob.id, 'key-a')).toBeUndefined()
|
|
})
|
|
|
|
it('keeps the original grant time when the grants are saved again', () => {
|
|
const ann = recipient()
|
|
store.upsertShare({ key: 'key-a', slug: null, title: 'A', password: null }, T0)
|
|
store.upsertShare({ key: 'key-b', slug: null, title: 'B', password: null }, T0)
|
|
store.setRecipientShares(ann.id, ['key-a'], T0)
|
|
store.setRecipientShares(ann.id, ['key-a', 'key-b'], T2)
|
|
expect(store.listRecipientShares(ann.id).map(s => [s.key, s.grantedAt])).toEqual([['key-a', T0], ['key-b', T2]])
|
|
store.setRecipientShares(ann.id, [], T2)
|
|
expect(store.listRecipientShares(ann.id)).toEqual([])
|
|
})
|
|
|
|
it('ignores grants for shares that do not exist', () => {
|
|
const ann = recipient()
|
|
store.setRecipientShares(ann.id, ['missing'], T0)
|
|
expect(store.listRecipientShares(ann.id)).toEqual([])
|
|
})
|
|
|
|
it('removing a share removes its grants and comments', () => {
|
|
const ann = recipient()
|
|
store.upsertShare({ key: 'key-a', slug: null, title: 'A', password: null }, T0)
|
|
store.setRecipientShares(ann.id, ['key-a'], T0)
|
|
store.addComment({ shareKey: 'key-a', assetId: 'x', recipient: ann, body: 'hi' }, T0)
|
|
store.deleteShare('key-a')
|
|
expect(store.grantedShare(ann.id, 'key-a')).toBeUndefined()
|
|
expect(store.countComments('key-a')).toEqual({})
|
|
})
|
|
})
|
|
|
|
describe('notification windows', () => {
|
|
it('opens the window when digests are turned on, not before', () => {
|
|
const ann = recipient()
|
|
expect(store.getRecipient(ann.id)?.lastDigestAt).toBeNull()
|
|
store.setFrequency(ann.id, 'weekly', T1)
|
|
expect(store.getRecipient(ann.id)?.lastDigestAt).toBe(T1)
|
|
// Switching between on-frequencies keeps the window
|
|
store.setFrequency(ann.id, 'daily', T2)
|
|
expect(store.getRecipient(ann.id)?.lastDigestAt).toBe(T1)
|
|
})
|
|
|
|
it('a recipient created with digests on starts their window then', () => {
|
|
expect(recipient('c@example.com', 'daily').lastDigestAt).toBe(T0)
|
|
})
|
|
|
|
it('finds assets first seen inside a window', () => {
|
|
store.upsertShare({ key: 'k', slug: null, title: 'K', password: null }, T0)
|
|
store.addAssets('k', ['old'], T0)
|
|
store.addAssets('k', ['new'], T1)
|
|
// A repeat insert keeps the first sighting
|
|
store.addAssets('k', ['old'], T2)
|
|
expect(store.assetsSeenBetween('k', T0, T2)).toEqual(['new'])
|
|
})
|
|
})
|
|
|
|
describe('comments', () => {
|
|
it('counts per asset and keeps the author name if the recipient is deleted', () => {
|
|
const ann = recipient()
|
|
store.upsertShare({ key: 'k', slug: null, title: 'K', password: null }, T0)
|
|
store.addComment({ shareKey: 'k', assetId: 'a', recipient: ann, body: 'one' }, T0)
|
|
store.addComment({ shareKey: 'k', assetId: 'a', recipient: ann, body: 'two' }, T1)
|
|
store.addComment({ shareKey: 'k', assetId: 'b', recipient: ann, body: 'three' }, T1)
|
|
expect(store.countComments('k')).toEqual({ a: 2, b: 1 })
|
|
|
|
store.deleteRecipient(ann.id)
|
|
const [first] = store.listComments('k', 'a')
|
|
expect(first).toMatchObject({ authorName: 'Ann', recipientId: null, body: 'one' })
|
|
})
|
|
})
|
|
|
|
describe('safeNextPath', () => {
|
|
it('allows same-site paths only', () => {
|
|
expect(safeNextPath('/share/abc')).toBe('/share/abc')
|
|
expect(safeNextPath('//evil.example.com')).toBeUndefined()
|
|
expect(safeNextPath('/\\evil.example.com')).toBeUndefined()
|
|
expect(safeNextPath('https://evil.example.com')).toBeUndefined()
|
|
expect(safeNextPath('/x\r\nSet-Cookie: a=b')).toBeUndefined()
|
|
expect(safeNextPath(['/a'])).toBeUndefined()
|
|
})
|
|
})
|