Skip to content

Commit

Permalink
Merge pull request #814 from watson-developer-cloud/handle-bad-chars-…
Browse files Browse the repository at this point in the history
…in-urls-creds

chore: throw error if user makes common mistake
  • Loading branch information
dpopp07 committed Jan 8, 2019
2 parents 9360470 + 994169a commit ab4c1cb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/base_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ function usesBasicForIam(obj: any): boolean {
return obj.username === 'apikey' && !obj.password.startsWith('icp-');
}

// returns true if the string has a curly bracket or quote as the first or last character
// these are common user-issues that we should handle before they get a network error
function badCharAtAnEnd(value: string): boolean {
return value.startsWith('{') || value.startsWith('"') || value.endsWith('}') || value.endsWith('"');
}

// checks credentials for common user mistakes of copying {, }, or " characters from the documentation
function checkCredentials(obj: any) {
let errorMessage = '';
const credsToCheck = ['url', 'username', 'password', 'iam_apikey'];
credsToCheck.forEach(cred => {
if (obj[cred] && badCharAtAnEnd(obj[cred])) {
errorMessage += `The ${cred} shouldn't start or end with curly brackets or quotes. Be sure to remove any {, }, or "`;
}
});

if (errorMessage.length) {
errorMessage += 'Revise these credentials - they should not start or end with curly brackets or quotes.';
return errorMessage;
} else {
return null;
}
}

export class BaseService {
static URL: string;
name: string;
Expand Down Expand Up @@ -309,6 +333,11 @@ export class BaseService {
}
}
}
// check credentials for common user errors
const credentialProblems = checkCredentials(_options);
if (credentialProblems) {
throw new Error(credentialProblems);
}
return _options;
}
/**
Expand Down
82 changes: 82 additions & 0 deletions test/unit/baseService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,86 @@ describe('BaseService', function() {
});
expect(instance._options.rejectUnauthorized).toBe(true);
});

describe('check credentials for common problems', function() {
function assertConstructorThrows(params) {
expect(() => {
new TestService(params);
}).toThrowError(
'Revise these credentials - they should not start or end with curly brackets or quotes.'
);
}

it('should throw when username starts with {', function() {
assertConstructorThrows({
username: '{batman}',
password: 'goodpass',
});
});

it('should throw when username starts with "', function() {
assertConstructorThrows({
username: '"<batman">',
password: 'goodpass',
});
});

it('should throw when password starts with {', function() {
assertConstructorThrows({
username: 'batman',
password: '{badpass}',
});
});

it('should throw when password starts with "', function() {
assertConstructorThrows({
username: 'batman',
password: '"badpass"',
});
});

it('should throw when iam_apikey starts with {', function() {
assertConstructorThrows({
iam_apikey: '{abc123}',
});
});

it('should throw when iam_apikey starts with "', function() {
assertConstructorThrows({
iam_apikey: '"<abc123',
});
});

it('should throw when url starts with {', function() {
assertConstructorThrows({
username: 'batman',
password: 'goodpass',
url: '{watson-url}/some-api/v1/endpoint',
});
});

it('should throw when url ends with }', function() {
assertConstructorThrows({
username: 'batman',
password: 'goodpass',
url: 'watson-url.com/some-api/v1/endpoint}',
});
});

it('should throw when url starts with "', function() {
assertConstructorThrows({
username: 'batman',
password: 'goodpass',
url: '"watson-url.com/some-api/v1/endpoint',
});
});

it('should throw when mutiple creds are bad', function() {
assertConstructorThrows({
username: '{batman}',
password: '"<badpass>"',
url: '{watson-url}/some-api/v1/endpoint',
});
});
});
});

0 comments on commit ab4c1cb

Please sign in to comment.