Files
accounted/lib/customers/__tests__/mask-personal-number.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

48 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
PERSONAL_NUMBER_PLAINTEXT_RE,
UNDECRYPTABLE_PERSONAL_NUMBER_MASK,
customerListIdentifier,
} from '@/lib/customers/mask-personal-number'
// Synthetic personnummer throughout, never a real one.
describe('customerListIdentifier', () => {
it('shows org_number for business rows', () => {
expect(customerListIdentifier({ customer_type: 'swedish_business', org_number: '556677-8899' })).toBe('556677-8899')
expect(customerListIdentifier({ customer_type: 'swedish_business', org_number: null })).toBe('')
})
it('shows the masked personal_number for individual rows, passing an API mask through', () => {
expect(customerListIdentifier({ customer_type: 'individual', personal_number: '********-1234' })).toBe('********-1234')
expect(customerListIdentifier({ customer_type: 'individual', personal_number: '19900101-1234' })).toBe('********-1234')
expect(
customerListIdentifier({ customer_type: 'individual', personal_number: UNDECRYPTABLE_PERSONAL_NUMBER_MASK }),
).toBe(UNDECRYPTABLE_PERSONAL_NUMBER_MASK)
})
it('masks a legacy individual row that still carries its personnummer in org_number', () => {
expect(customerListIdentifier({ customer_type: 'individual', org_number: '19900101-1234', personal_number: null })).toBe(
'********-1234',
)
})
it('leaves a non-personnummer org_number on an individual visible', () => {
expect(customerListIdentifier({ customer_type: 'individual', org_number: 'CHE-123.456.789' })).toBe('CHE-123.456.789')
expect(customerListIdentifier({ customer_type: 'individual' })).toBe('')
})
})
describe('PERSONAL_NUMBER_PLAINTEXT_RE', () => {
it('accepts the four written forms and nothing else', () => {
for (const value of ['900101-1234', '900101+1234', '19900101-1234', '9001011234', '199001011234']) {
expect(PERSONAL_NUMBER_PLAINTEXT_RE.test(value)).toBe(true)
}
// Shape only: a legal-entity orgnr with a dash has the same digit shape
// and is kept out by looksLikeSwedishPersonalNumber at the call sites,
// not by this regex.
for (const value of ['********-1234', '********-????', '19900101 1234', 'abc', '1234']) {
expect(PERSONAL_NUMBER_PLAINTEXT_RE.test(value)).toBe(false)
}
})
})