Skip to content

Commit

Permalink
build: Fixing tests that use reqheaders improperly (#1589)
Browse files Browse the repository at this point in the history
* fix: Fixing tests that use reqheaders improperly

* fix lint
  • Loading branch information
aeitzman authored Jul 11, 2023
1 parent f95a153 commit 1bf3376
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 561 deletions.
71 changes: 30 additions & 41 deletions test/externalclienthelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ interface NockMockStsToken {
response: StsSuccessfulResponse | OAuthErrorResponse;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
request: {[key: string]: any};
additionalHeaders?: {[key: string]: string};
}

interface NockMockGenerateAccessToken {
Expand All @@ -58,51 +57,43 @@ const saBaseUrl = 'https://iamcredentials.googleapis.com';
const saPath = `/v1/projects/-/serviceAccounts/${saEmail}:generateAccessToken`;

export function mockStsTokenExchange(
nockParams: NockMockStsToken[]
nockParams: NockMockStsToken[],
additionalHeaders?: {[key: string]: string}
): nock.Scope {
const scope = nock(baseUrl);
const headers = Object.assign(
{
'content-type': 'application/x-www-form-urlencoded',
},
additionalHeaders || {}
);
const scope = nock(baseUrl, {reqheaders: headers});
nockParams.forEach(nockMockStsToken => {
const headers = Object.assign(
{
'content-type': 'application/x-www-form-urlencoded',
},
nockMockStsToken.additionalHeaders || {}
);
scope
.post(path, qs.stringify(nockMockStsToken.request), {
reqheaders: headers,
})
.post(path, qs.stringify(nockMockStsToken.request))
.reply(nockMockStsToken.statusCode, nockMockStsToken.response);
});
return scope;
}

export function mockGenerateAccessToken(
nockParams: NockMockGenerateAccessToken[]
nockMockGenerateAccessToken: NockMockGenerateAccessToken
): nock.Scope {
const scope = nock(saBaseUrl);
nockParams.forEach(nockMockGenerateAccessToken => {
const token = nockMockGenerateAccessToken.token;
scope
.post(
saPath,
{
scope: nockMockGenerateAccessToken.scopes,
lifetime:
(nockMockGenerateAccessToken.lifetime ?? defaultLifetime) + 's',
},
{
reqheaders: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
}
)
.reply(
nockMockGenerateAccessToken.statusCode,
nockMockGenerateAccessToken.response
);
const token = nockMockGenerateAccessToken.token;
const scope = nock(saBaseUrl, {
reqheaders: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
scope
.post(saPath, {
scope: nockMockGenerateAccessToken.scopes,
lifetime: (nockMockGenerateAccessToken.lifetime ?? defaultLifetime) + 's',
})
.reply(
nockMockGenerateAccessToken.statusCode,
nockMockGenerateAccessToken.response
);
return scope;
}

Expand Down Expand Up @@ -135,11 +126,9 @@ export function mockCloudResourceManager(
statusCode: number,
response: ProjectInfo | CloudRequestError
): nock.Scope {
return nock('https://cloudresourcemanager.googleapis.com')
.get(`/v1/projects/${projectNumber}`, undefined, {
reqheaders: {
Authorization: `Bearer ${accessToken}`,
},
})
return nock('https://cloudresourcemanager.googleapis.com', {
reqheaders: {Authorization: `Bearer ${accessToken}`},
})
.get(`/v1/projects/${projectNumber}`)
.reply(statusCode, response);
}
15 changes: 7 additions & 8 deletions test/test.awsclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ describe('AwsClient', () => {
if (clock) {
clock.restore();
}
nock.cleanAll();
});

it('should be a subclass of ExternalAccountClient', () => {
Expand Down Expand Up @@ -574,14 +575,12 @@ describe('AwsClient', () => {
},
},
]),
mockGenerateAccessToken([
{
statusCode: 200,
response: saSuccessResponse,
token: stsSuccessfulResponse.access_token,
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
},
])
mockGenerateAccessToken({
statusCode: 200,
response: saSuccessResponse,
token: stsSuccessfulResponse.access_token,
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
})
);

const client = new AwsClient(awsOptionsWithSA);
Expand Down
Loading

0 comments on commit 1bf3376

Please sign in to comment.