|
1 | 1 | import type { H3Event } from 'h3' |
2 | | -import { describe, expect, it } from 'vitest' |
3 | | -import { assertSiteAdministrator } from '../../server/utils/admin-auth' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { assertSiteAdministrator, isSiteAdministrator } from '../../server/utils/admin-auth' |
4 | 4 |
|
5 | 5 | describe('site administrator authorization', () => { |
6 | | - it('allows only the root site-token identity', () => { |
7 | | - expect(() => assertSiteAdministrator(eventWithIdentity('site-token', 'root'))).not.toThrow() |
8 | | - expect(() => assertSiteAdministrator(eventWithIdentity('oidc-session', 'root'))).toThrowError( |
| 6 | + beforeEach(() => { |
| 7 | + vi.stubGlobal('useRuntimeConfig', () => ({ |
| 8 | + siteAdminEmails: ' admin@example.com,OWNER@example.com ', |
| 9 | + })) |
| 10 | + }) |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + vi.unstubAllGlobals() |
| 14 | + }) |
| 15 | + |
| 16 | + it('allows the root site-token identity', () => { |
| 17 | + expect(isSiteAdministrator(eventWithIdentity('site-token', 'root', 'root@example.com'))).toBe(true) |
| 18 | + expect(() => assertSiteAdministrator(eventWithIdentity('site-token', 'root', 'root@example.com'))).not.toThrow() |
| 19 | + }) |
| 20 | + |
| 21 | + it('allows configured OIDC administrators case-insensitively', () => { |
| 22 | + expect(isSiteAdministrator(eventWithIdentity('oidc-session', 'test-user', 'owner@example.com'))).toBe(true) |
| 23 | + expect(() => assertSiteAdministrator(eventWithIdentity('oidc-session', 'test-user', 'OWNER@example.com'))).not.toThrow() |
| 24 | + }) |
| 25 | + |
| 26 | + it('rejects identities outside the administrator allowlist', () => { |
| 27 | + expect(isSiteAdministrator(eventWithIdentity('oidc-session', 'root', 'user@example.com'))).toBe(false) |
| 28 | + expect(() => assertSiteAdministrator(eventWithIdentity('oidc-session', 'test-user', 'user@example.com'))).toThrowError( |
9 | 29 | expect.objectContaining({ statusCode: 403 }), |
10 | 30 | ) |
11 | | - expect(() => assertSiteAdministrator(eventWithIdentity('oidc-session', 'test-user'))).toThrowError( |
| 31 | + expect(() => assertSiteAdministrator(eventWithIdentity('access-user', 'test-user', 'admin@example.com'))).toThrowError( |
12 | 32 | expect.objectContaining({ statusCode: 403 }), |
13 | 33 | ) |
14 | 34 | }) |
15 | 35 | }) |
16 | 36 |
|
17 | | -function eventWithIdentity(authMethod: string, userID: string): H3Event { |
| 37 | +function eventWithIdentity(authMethod: string, userID: string, userEmail: string): H3Event { |
18 | 38 | return { |
19 | | - context: { authMethod, userID }, |
| 39 | + context: { authMethod, userID, userEmail }, |
20 | 40 | } as H3Event |
21 | 41 | } |
0 commit comments