Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client: Don't send auth header when token is empty #782

Merged
merged 7 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .test-env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configs for testing repo download:
SDK_TESTING_URL="https://github.com/algorand/algorand-sdk-testing"
SDK_TESTING_BRANCH="master"
SDK_TESTING_URL="https://github.com/Eric-Warehime/algorand-sdk-testing"
SDK_TESTING_BRANCH="client-no-header-tests"
SDK_TESTING_HARNESS="test-harness"

INSTALL_ONLY=0
Expand Down
3 changes: 3 additions & 0 deletions src/client/v2/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function convertTokenStringToTokenHeader(
headerIdentifier: TokenHeaderIdentifier
): TokenHeader {
const tokenHeader = {};
if (token === '') {
tzaffi marked this conversation as resolved.
Show resolved Hide resolved
return tokenHeader;
}
tokenHeader[headerIdentifier] = token;
return tokenHeader as TokenHeader;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/9.Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ describe('client', () => {
assert.strictEqual(client.c['bc']['baseURL']['port'], '456');
});

it('should not provide auth request headers when the token is empty', () => {
tzaffi marked this conversation as resolved.
Show resolved Hide resolved
const client = new AlgodClient('', `${baseServer}:${123}`, 456);
assert.deepStrictEqual(
{
...client.c['bc']['tokenHeaders'],
...client.c['bc']['defaultHeaders'],
},
{}
);
});

/* eslint-disable dot-notation */
});
});
29 changes: 21 additions & 8 deletions tests/cucumber/steps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ function setupMockServerForPaths(mockServer) {
});
}

function getMockServerRequestUrlsMethods(mockServer) {
function getMockServerRequestUrlsMethodsHeaders(mockServer) {
return mockServer
.requests()
.filter((req) => req.method !== 'OPTIONS') // ignore cors preflight requests from the browser
.map((req) => ({ method: req.method, url: req.url }));
.map((req) => ({ method: req.method, url: req.url, headers: req.headers }));
}

function isFunction(functionToCheck) {
Expand Down Expand Up @@ -478,10 +478,10 @@ for (const name of Object.keys(steps.then)) {
if (name === 'expect the path used to be {string}') {
Then(name, function (expectedRequestPath) {
// get all requests the mockservers have seen since reset
const algodSeenRequests = getMockServerRequestUrlsMethods(
const algodSeenRequests = getMockServerRequestUrlsMethodsHeaders(
algodMockServerPathRecorder
);
const indexerSeenRequests = getMockServerRequestUrlsMethods(
const indexerSeenRequests = getMockServerRequestUrlsMethodsHeaders(
indexerMockServerPathRecorder
);
return fn.call(
Expand All @@ -494,10 +494,10 @@ for (const name of Object.keys(steps.then)) {
} else if (name === 'expect the request to be {string} {string}') {
Then(name, function (expectedRequestType, expectedRequestPath) {
// get all requests the mockservers have seen since reset
const algodSeenRequests = getMockServerRequestUrlsMethods(
const algodSeenRequests = getMockServerRequestUrlsMethodsHeaders(
algodMockServerPathRecorder
);
const indexerSeenRequests = getMockServerRequestUrlsMethods(
const indexerSeenRequests = getMockServerRequestUrlsMethodsHeaders(
indexerMockServerPathRecorder
);
return fn.call(
Expand All @@ -511,10 +511,10 @@ for (const name of Object.keys(steps.then)) {
} else if (name === 'we expect the path used to be {string}') {
Then(name, function (expectedRequestPath) {
// get all requests the mockservers have seen since reset
const algodSeenRequests = getMockServerRequestUrlsMethods(
const algodSeenRequests = getMockServerRequestUrlsMethodsHeaders(
algodMockServerPathRecorder
);
const indexerSeenRequests = getMockServerRequestUrlsMethods(
const indexerSeenRequests = getMockServerRequestUrlsMethodsHeaders(
indexerMockServerPathRecorder
);
return fn.call(
Expand All @@ -524,6 +524,19 @@ for (const name of Object.keys(steps.then)) {
expectedRequestPath
);
});
} else if (
name === 'expect the observed header keys to equal the expected header keys'
) {
Then(name, function () {
// get all requests the mockservers have seen since reset
const algodSeenRequests = getMockServerRequestUrlsMethodsHeaders(
algodMockServerPathRecorder
);
const indexerSeenRequests = getMockServerRequestUrlsMethodsHeaders(
indexerMockServerPathRecorder
);
return fn.call(this, algodSeenRequests, indexerSeenRequests);
});
} else if (
name === 'the produced json should equal {string} loaded from {string}'
) {
Expand Down
54 changes: 54 additions & 0 deletions tests/cucumber/steps/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,28 @@ module.exports = function getSteps(options) {
);
});

Given('expected headers', (headersTable) => {
this.expectedHeaders = headersTable.hashes();
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
});

Then(
'expect the observed header keys to equal the expected header keys',
(algodSeenRequests, indexerSeenRequests) => {
let actualRequests;
if (algodSeenRequests.length !== 0) {
[actualRequests] = algodSeenRequests;
} else if (indexerSeenRequests.length !== 0) {
[actualRequests] = indexerSeenRequests;
} else {
throw new Error('no requests observed.');
}
assert.deepStrictEqual(
Object.keys(actualRequests.headers).sort(),
this.expectedHeaders.map((entry) => entry.key).sort()
);
}
);

Then(
'expect the path used to be {string}',
(algodSeenRequests, indexerSeenRequests, expectedRequestPath) => {
Expand Down Expand Up @@ -3269,6 +3291,30 @@ module.exports = function getSteps(options) {
}
);

Given(
'an algod v2 client connected to mock server with token {string}',
function (token) {
this.v2Client = new algosdk.Algodv2(
token,
`http://${mockAlgodPathRecorderHost}`,
mockAlgodPathRecorderPort,
{}
);
}
);

Given(
'an indexer v2 client connected to mock server with token {string}',
function (token) {
this.indexerV2client = new algosdk.Indexer(
token,
`http://${mockIndexerPathRecorderHost}`,
mockIndexerPathRecorderPort,
{}
);
}
);

Given(
'I create a new transient account and fund it with {int} microalgos.',
async function (fundingAmount) {
Expand Down Expand Up @@ -4781,6 +4827,14 @@ module.exports = function getSteps(options) {
await this.v2Client.unsetSyncRound().do();
});

When('we make an arbitrary algod call', async function () {
await this.v2Client.healthCheck().do();
});

When('we make an arbitrary indexer call', async function () {
await this.indexerV2client.makeHealthCheck().do();
});

if (!options.ignoreReturn) {
return steps;
}
Expand Down
1 change: 1 addition & 0 deletions tests/cucumber/unit.tags
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@unit.applications.boxes
@unit.atomic_transaction_composer
@unit.blocksummary
@unit.client-no-headers
@unit.dryrun
@unit.dryrun.trace.application
@unit.feetest
Expand Down