diff --git a/src/lib/request/requestManager.ts b/src/lib/request/requestManager.ts index 5446b8e..fc56a7b 100644 --- a/src/lib/request/requestManager.ts +++ b/src/lib/request/requestManager.ts @@ -47,7 +47,7 @@ function getRESTAPI(endpoint: string): string { } function getOauthToken(): string { - const oauthToken: string = process.env.OAUTH_BEARER_TOKEN || ''; + const oauthToken: string = process.env.SNYK_OAUTH_TOKEN || ''; return oauthToken; } diff --git a/test/lib/request/request.test.ts b/test/lib/request/request.test.ts index 20339d5..fa5c479 100644 --- a/test/lib/request/request.test.ts +++ b/test/lib/request/request.test.ts @@ -229,3 +229,85 @@ describe('Test Snyk Utils error handling/classification', () => { } }); }); + +describe('Test makeSnykRequest with oauthBearerToken', () => { + beforeEach(() => { + nock.cleanAll(); + }); + + afterEach(() => { + nock.cleanAll(); + }); + + it('should set Bearer token in Authorization header when oauthBearerToken is provided for DEFAULT_API', async () => { + const testToken = 'test-oauth-token'; + const request = { + verb: 'GET', + url: '/test-endpoint', + }; + + const scope = nock('https://api.snyk.io/v1') + .get('/test-endpoint') + .matchHeader('Authorization', `Bearer ${testToken}`) + .reply(200, { success: true }); + + await makeSnykRequest(request, '', testToken); + + expect(scope.isDone()).toBe(true); + }); + + it('should set Bearer token in Authorization header when oauthBearerToken is provided for DEFAULT_REST_API', async () => { + const testToken = 'test-oauth-token'; + const request = { + verb: 'GET', + url: '/test-endpoint', + useRESTApi: true, + }; + + const scope = nock('https://api.snyk.io/rest/') + .get('/test-endpoint') + .matchHeader('Authorization', `Bearer ${testToken}`) + .reply(200, { success: true }); + + await makeSnykRequest(request, '', testToken); + + expect(scope.isDone()).toBe(true); + }); + + it('should prioritize snykToken over oauthBearerToken when both are provided for DEFAULT_API', async () => { + const snykToken = 'test-snyk-token'; + const oauthToken = 'test-oauth-token'; + const request = { + verb: 'GET', + url: '/test-endpoint', + }; + + const scope = nock('https://api.snyk.io/v1') + .get('/test-endpoint') + .matchHeader('Authorization', `token ${snykToken}`) + .reply(200, { success: true }); + + await makeSnykRequest(request, snykToken, oauthToken); + + expect(scope.isDone()).toBe(true); + }); + + it('should prioritize snykToken over oauthBearerToken when both are provided for DEFAULT_REST_API', async () => { + const snykToken = 'test-snyk-token'; + const oauthToken = 'test-oauth-token'; + const request = { + verb: 'GET', + url: '/test-endpoint', + useRESTApi: true, + }; + + const scope = nock('https://api.snyk.io/rest/') + .get('/test-endpoint') + .matchHeader('Authorization', `token ${snykToken}`) + .reply(200, { success: true }); + + await makeSnykRequest(request, snykToken, oauthToken); + + expect(scope.isDone()).toBe(true); + }); +});