Files
accounted/extensions/general/skatteverket/__tests__/oauth-scopes.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

54 lines
1.9 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { buildAuthorizeUrl } from '../lib/oauth'
/**
* Scope-set regression guard.
*
* Every entry here was added because removing it broke a live filing, and the
* damage is always delayed: SKV issues the token happily and the missing scope
* only surfaces as a 403 from the one API that needed it. A "remove unused
* scopes" cleanup has already cost us this twice (#431 for `ska`, and `agd`
* alone for AGI kvittenser). Pin them.
*/
describe('Skatteverket per-flow OAuth scopes', () => {
const originalClientId = process.env.SKATTEVERKET_OAUTH2_CLIENT_ID
beforeEach(() => {
process.env.SKATTEVERKET_OAUTH2_CLIENT_ID = 'test-client-id'
})
afterEach(() => {
if (originalClientId === undefined) delete process.env.SKATTEVERKET_OAUTH2_CLIENT_ID
else process.env.SKATTEVERKET_OAUTH2_CLIENT_ID = originalClientId
})
function requestedScopes(): string[] {
const url = new URL(buildAuthorizeUrl('https://app.example/callback', 'state-123'))
return (url.searchParams.get('scope') ?? '').split(' ').filter(Boolean)
}
it('requests both AGI scopes, one per backing API', () => {
// `agd` backs inlamning (submit/sign); `agdredovisningperiod` backs
// hanteraredovisningsperiod (kvittenser/las). A token with only the first
// completes a filing and then fails on "Hämta kvittens".
expect(requestedScopes()).toEqual(
expect.arrayContaining(['agd', 'agdredovisningperiod']),
)
})
it('keeps the interactive skattekonto scope', () => {
expect(requestedScopes()).toContain('ska')
})
it('keeps the momsdeklaration scope', () => {
expect(requestedScopes()).toContain('momsdeklaration')
})
it('lets an explicit scope option override the defaults', () => {
const url = new URL(
buildAuthorizeUrl('https://app.example/callback', 'state-123', { scope: 'agd' }),
)
expect(url.searchParams.get('scope')).toBe('agd')
})
})