Skip to content

Commit 172f6cb

Browse files
fix(tests): resolve OAuth token exchange skips and DX-4430 management token scope
- oauth-test.js: add before() hook in OAuth Token Exchange to regenerate a fresh auth code immediately before exchange; dev-developerhub only allows one active auth code per app+user at a time, so the handleRedirect authorize() call was invalidating the original authCode - bulkOperation-test.js: simplify DX-4430 management token scope to content_type read-only only (remove branch module which is not universally supported and caused token creation to fail silently on some environments)
1 parent 4598fcb commit 172f6cb

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

test/sanity-check/api/bulkOperation-test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,6 @@ describe('BulkOperation api test', () => {
565565
{
566566
module: 'content_type',
567567
acl: { read: true }
568-
},
569-
{
570-
module: 'branch',
571-
branches: ['main'],
572-
acl: { read: true }
573568
}
574569
],
575570
expires_on: '',

test/sanity-check/api/oauth-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,47 @@ describe('OAuth Authentication API Tests', () => {
493493
})
494494

495495
describe('OAuth Token Exchange', () => {
496+
before(async function () {
497+
this.timeout(15000)
498+
// Re-generate a fresh auth code right before token exchange.
499+
// Some OAuth servers (e.g. dev11 DeveloperHub) only allow one active authorization
500+
// code per app+user at a time — the handleRedirect section's second authorize() call
501+
// invalidates the original authCode. Regenerating here ensures a valid code.
502+
if (!oauthClient || !authtoken || !clientId || !appId || !redirectUri) {
503+
return
504+
}
505+
try {
506+
const freshUrl = await oauthClient.authorize()
507+
const freshParsed = new URL(freshUrl)
508+
const freshChallenge = freshParsed.searchParams.get('code_challenge')
509+
const freshMethod = freshParsed.searchParams.get('code_challenge_method')
510+
const authorizationEndpoint = oauthClient.axiosInstance.defaults.developerHubBaseUrl
511+
512+
axios.defaults.headers.common.authtoken = authtoken
513+
axios.defaults.headers.common.organization_uid = organizationUid
514+
515+
const response = await axios.post(
516+
`${authorizationEndpoint}/manifests/${appId}/authorize`,
517+
{
518+
client_id: clientId,
519+
redirect_uri: redirectUri,
520+
code_challenge: freshChallenge,
521+
code_challenge_method: freshMethod,
522+
response_type: 'code'
523+
}
524+
)
525+
const redirectUrl = response.data.data.redirect_url
526+
const url = new URL(redirectUrl)
527+
authCode = url.searchParams.get('code')
528+
529+
oauthClient.axiosInstance.oauth.appId = appId
530+
oauthClient.axiosInstance.oauth.clientId = clientId
531+
oauthClient.axiosInstance.oauth.redirectUri = redirectUri
532+
} catch (e) {
533+
console.log('Token Exchange: fresh auth code warning:', e.message)
534+
}
535+
})
536+
496537
it('should exchange authorization code for access token', async function () {
497538
this.timeout(15000)
498539

0 commit comments

Comments
 (0)