11import test from "ava" ;
22import * as td from "testdouble" ;
33
4- import tokenExchange from ' ../../lib/trusted-publishing/token-exchange.js' ;
4+ import tokenExchange from " ../../lib/trusted-publishing/token-exchange.js" ;
55import { OFFICIAL_REGISTRY } from "../../lib/definitions/constants.js" ;
66
77// https://api-docs.npmjs.com/#tag/registry.npmjs.org/operation/exchangeOidcToken
88
9- const packageName = "some-package" ;
9+ const packageName = "@scope/ some-package" ;
1010const pkg = { name : packageName } ;
1111
1212test . beforeEach ( async ( t ) => {
@@ -19,15 +19,25 @@ test.afterEach.always((t) => {
1919
2020test . serial ( "that an access token is returned when token exchange succeeds" , async ( t ) => {
2121 const token = "token-value" ;
22- td . when ( fetch ( `${ OFFICIAL_REGISTRY } -/npm/v1/oidc/token/exchange/package/${ packageName } ` , { method : 'POST' } ) )
23- . thenResolve ( new Response ( JSON . stringify ( { token} ) , { status : 201 , headers : { 'Content-Type' : 'application/json' } } ) ) ;
22+ td . when (
23+ fetch ( `${ OFFICIAL_REGISTRY } -/npm/v1/oidc/token/exchange/package/${ encodeURIComponent ( packageName ) } ` , {
24+ method : "POST" ,
25+ } )
26+ ) . thenResolve (
27+ new Response ( JSON . stringify ( { token } ) , { status : 201 , headers : { "Content-Type" : "application/json" } } )
28+ ) ;
2429
2530 t . is ( await tokenExchange ( pkg ) , token ) ;
2631} ) ;
2732
2833test . serial ( "that `undefined` is returned when token exchange fails" , async ( t ) => {
29- td . when ( fetch ( `${ OFFICIAL_REGISTRY } -/npm/v1/oidc/token/exchange/package/${ packageName } ` , { method : 'POST' } ) )
30- . thenResolve ( new Response ( JSON . stringify ( { message : 'foo' } ) , { status : 401 , headers : { 'Content-Type' : 'application/json' } } ) ) ;
34+ td . when (
35+ fetch ( `${ OFFICIAL_REGISTRY } -/npm/v1/oidc/token/exchange/package/${ encodeURIComponent ( packageName ) } ` , {
36+ method : "POST" ,
37+ } )
38+ ) . thenResolve (
39+ new Response ( JSON . stringify ( { message : "foo" } ) , { status : 401 , headers : { "Content-Type" : "application/json" } } )
40+ ) ;
3141
3242 t . is ( await tokenExchange ( pkg ) , undefined ) ;
3343} ) ;
0 commit comments