@@ -6,7 +6,7 @@ import { promisify } from "node:util";
66import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
77import { fetchWhoami } from "../src/auth/whoami" ;
88import {
9- resolveOrgSubject ,
9+ resolveActingOrg ,
1010 selectOrgForOwners ,
1111 type WhoamiOrg ,
1212} from "../src/auth/org" ;
@@ -39,6 +39,15 @@ const org = (id: string, url: string, source = "github"): WhoamiOrg =>
3939 url,
4040 } ) as WhoamiOrg ;
4141
42+ const entitled = ( restoreRules : boolean ) : WhoamiOrg => ( {
43+ ...org ( "uuid-acme" , "https://github.com/acme" ) ,
44+ entitlements : {
45+ remoteGeneration : true ,
46+ runtimeSignatures : true ,
47+ restoreRules,
48+ } ,
49+ } ) ;
50+
4251describe ( "selectOrgForOwners" , ( ) => {
4352 const orgs = [
4453 org ( "uuid-acme" , "https://github.com/acme" ) ,
@@ -78,7 +87,7 @@ describe("selectOrgForOwners", () => {
7887 } ) ;
7988} ) ;
8089
81- describe ( "resolveOrgSubject " , ( ) => {
90+ describe ( "resolveActingOrg " , ( ) => {
8291 let directory : string ;
8392 const token = makeJwt ( { orgId : 4242 } ) ;
8493
@@ -100,25 +109,84 @@ describe("resolveOrgSubject", () => {
100109 mockedFetchWhoami . mockResolvedValue (
101110 whoamiWith ( [ org ( "uuid-acme" , "https://github.com/acme" ) ] )
102111 ) ;
103- expect ( await resolveOrgSubject ( directory , token ) ) . toBe ( "uuid-acme" ) ;
112+ await expect ( resolveActingOrg ( directory , token ) ) . resolves . toMatchObject ( {
113+ subject : "uuid-acme" ,
114+ } ) ;
104115 } ) ;
105116
106117 it ( "falls back to the numeric claim when no whoami org matches the repo" , async ( ) => {
107118 mockedFetchWhoami . mockResolvedValue (
108119 whoamiWith ( [ org ( "uuid-other" , "https://github.com/other" ) ] )
109120 ) ;
110- expect ( await resolveOrgSubject ( directory , token ) ) . toBe ( 4242 ) ;
121+ await expect ( resolveActingOrg ( directory , token ) ) . resolves . toMatchObject ( {
122+ subject : 4242 ,
123+ } ) ;
111124 } ) ;
112125
113126 it ( "falls back to the numeric claim when whoami is unavailable" , async ( ) => {
114127 mockedFetchWhoami . mockResolvedValue ( WHOAMI_UNAVAILABLE ) ;
115- expect ( await resolveOrgSubject ( directory , token ) ) . toBe ( 4242 ) ;
128+ await expect ( resolveActingOrg ( directory , token ) ) . resolves . toMatchObject ( {
129+ subject : 4242 ,
130+ } ) ;
116131 } ) ;
117132
118133 it ( "falls back to the nil-UUID when whoami is unavailable and the token has no org claim" , async ( ) => {
119134 mockedFetchWhoami . mockResolvedValue ( WHOAMI_UNAVAILABLE ) ;
120- expect ( await resolveOrgSubject ( directory , makeJwt ( { sub : "u" } ) ) ) . toBe (
121- "00000000-0000-0000-0000-000000000000"
135+ await expect (
136+ resolveActingOrg ( directory , makeJwt ( { sub : "u" } ) )
137+ ) . resolves . toMatchObject ( {
138+ subject : "00000000-0000-0000-0000-000000000000" ,
139+ } ) ;
140+ } ) ;
141+
142+ it . each ( [ true , false ] ) (
143+ "carries the matched org's restoreRules (%s) from the same whoami call" ,
144+ async ( restoreRules ) => {
145+ mockedFetchWhoami . mockResolvedValue ( whoamiWith ( [ entitled ( restoreRules ) ] ) ) ;
146+ expect ( await resolveActingOrg ( directory , token ) ) . toEqual ( {
147+ subject : "uuid-acme" ,
148+ restoreRules,
149+ } ) ;
150+ expect ( mockedFetchWhoami ) . toHaveBeenCalledTimes ( 1 ) ;
151+ }
152+ ) ;
153+
154+ it ( "leaves restoreRules unknown when the matched org has no entitlements" , async ( ) => {
155+ mockedFetchWhoami . mockResolvedValue (
156+ whoamiWith ( [ org ( "uuid-acme" , "https://github.com/acme" ) ] )
122157 ) ;
158+ expect ( await resolveActingOrg ( directory , token ) ) . toEqual ( {
159+ subject : "uuid-acme" ,
160+ } ) ;
161+ } ) ;
162+
163+ it ( "leaves restoreRules unknown when entitlements carries no boolean for it" , async ( ) => {
164+ mockedFetchWhoami . mockResolvedValue (
165+ whoamiWith ( [
166+ {
167+ ...org ( "uuid-acme" , "https://github.com/acme" ) ,
168+ entitlements : { remoteGeneration : true } as WhoamiOrg [ "entitlements" ] ,
169+ } ,
170+ ] )
171+ ) ;
172+ expect ( await resolveActingOrg ( directory , token ) ) . toEqual ( {
173+ subject : "uuid-acme" ,
174+ } ) ;
175+ } ) ;
176+
177+ it ( "leaves restoreRules unknown when no org matches, even if another org has it" , async ( ) => {
178+ mockedFetchWhoami . mockResolvedValue (
179+ whoamiWith ( [
180+ { ...entitled ( false ) , url : "https://github.com/other" } as WhoamiOrg ,
181+ ] )
182+ ) ;
183+ expect ( await resolveActingOrg ( directory , token ) ) . toEqual ( {
184+ subject : 4242 ,
185+ } ) ;
186+ } ) ;
187+
188+ it ( "leaves restoreRules unknown when whoami is unavailable" , async ( ) => {
189+ mockedFetchWhoami . mockResolvedValue ( WHOAMI_UNAVAILABLE ) ;
190+ expect ( await resolveActingOrg ( directory , token ) ) . toEqual ( { subject : 4242 } ) ;
123191 } ) ;
124192} ) ;
0 commit comments