@@ -57,7 +57,7 @@ test("verifyRequestByKeyId()", async (t) => {
5757 "content-type" : "application/json" ,
5858 "x-request-id" : "<request-id>" ,
5959 } ,
60- } ,
60+ }
6161 ) ;
6262 const testRequest = defaultRequest . defaults ( {
6363 request : { fetch : fetchMock } ,
@@ -70,6 +70,55 @@ test("verifyRequestByKeyId()", async (t) => {
7070 t . deepEqual ( result , true ) ;
7171} ) ;
7272
73+ test ( "verifyRequestByKeyId() - throws if keyId not present in verification keys list" , async ( t ) => {
74+ const mockAgent = new MockAgent ( ) ;
75+ function fetchMock ( url , opts ) {
76+ opts ||= { } ;
77+ opts . dispatcher = mockAgent ;
78+ return fetch ( url , opts ) ;
79+ }
80+
81+ mockAgent . disableNetConnect ( ) ;
82+ const mockPool = mockAgent . get ( "https://api.github.com" ) ;
83+ mockPool
84+ . intercept ( {
85+ method : "get" ,
86+ path : `/meta/public_keys/copilot_api` ,
87+ } )
88+ . reply (
89+ 200 ,
90+ {
91+ public_keys : [
92+ {
93+ key : CURRENT_PUBLIC_KEY ,
94+ key_identifier : KEY_ID ,
95+ is_current : true ,
96+ } ,
97+ ] ,
98+ } ,
99+ {
100+ headers : {
101+ "content-type" : "application/json" ,
102+ "x-request-id" : "<request-id>" ,
103+ } ,
104+ }
105+ ) ;
106+ const testRequest = defaultRequest . defaults ( {
107+ request : { fetch : fetchMock } ,
108+ } ) ;
109+
110+ await t . throwsAsync (
111+ verifyRequestByKeyId ( RAW_BODY , SIGNATURE , "wrong_key" , {
112+ request : testRequest ,
113+ } ) ,
114+ {
115+ name : "Error" ,
116+ message :
117+ "[@copilot-extensions/preview-sdk] No public key found matching key identifier" ,
118+ }
119+ ) ;
120+ } ) ;
121+
73122test ( "verifyRequestByKeyId() - invalid arguments" , async ( t ) => {
74123 t . throwsAsync ( verifyRequestByKeyId ( RAW_BODY , SIGNATURE ) , {
75124 name : "Error" ,
@@ -170,13 +219,69 @@ test("fetchVerificationKeys()", async (t) => {
170219 "content-type" : "application/json" ,
171220 "x-request-id" : "<request-id>" ,
172221 } ,
222+ }
223+ ) ;
224+ const testRequest = defaultRequest . defaults ( {
225+ request : { fetch : fetchMock } ,
226+ } ) ;
227+
228+ const result = await fetchVerificationKeys ( {
229+ request : testRequest ,
230+ } ) ;
231+
232+ t . deepEqual ( result , publicKeys ) ;
233+ } ) ;
234+
235+ test ( "fetchVerificationKeys() - with token" , async ( t ) => {
236+ const mockAgent = new MockAgent ( ) ;
237+ function fetchMock ( url , opts ) {
238+ opts ||= { } ;
239+ opts . dispatcher = mockAgent ;
240+ return fetch ( url , opts ) ;
241+ }
242+
243+ const publicKeys = [
244+ {
245+ key : "<key 1>" ,
246+ key_identifier : "<key-id 1>" ,
247+ is_current : true ,
248+ } ,
249+ {
250+ key : "<key 2>" ,
251+ key_identifier : "<key-id 2>" ,
252+ is_current : true ,
253+ } ,
254+ ] ;
255+
256+ mockAgent . disableNetConnect ( ) ;
257+ const mockPool = mockAgent . get ( "https://api.github.com" ) ;
258+ const token = "secr3t" ;
259+ mockPool
260+ . intercept ( {
261+ method : "get" ,
262+ path : `/meta/public_keys/copilot_api` ,
263+ headers : {
264+ Authorization : `token ${ token } ` ,
173265 } ,
266+ } )
267+ . reply (
268+ 200 ,
269+ {
270+ public_keys : publicKeys ,
271+ } ,
272+ {
273+ headers : {
274+ "content-type" : "application/json" ,
275+ "x-request-id" : "<request-id>" ,
276+ } ,
277+ }
174278 ) ;
175279 const testRequest = defaultRequest . defaults ( {
176280 request : { fetch : fetchMock } ,
177281 } ) ;
178282
179283 const result = await fetchVerificationKeys ( {
284+ token,
180285 request : testRequest ,
181286 } ) ;
182287
0 commit comments