Skip to content

Commit

Permalink
chore: Add test classes for oauth token and changed oauth environment…
Browse files Browse the repository at this point in the history
… variable to SNYK_OAUTH_TOKEN
  • Loading branch information
Robthreefold committed Dec 12, 2024
1 parent 7a88a8d commit 3858dd5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/request/requestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
82 changes: 82 additions & 0 deletions test/lib/request/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 3858dd5

Please sign in to comment.