Skip to content

Commit

Permalink
Fix setApiKey and setTwilioEmailAuth to prevent overwriting baseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Masaya Sonobe committed Nov 6, 2023
1 parent b8125d8 commit fff9f4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/client/src/classes/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Client {

setApiKey(apiKey) {
this.auth = 'Bearer ' + apiKey;
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL);

if (!this.isValidApiKey(apiKey)) {
console.warn(`API key does not start with "${API_KEY_PREFIX}".`);
Expand All @@ -48,7 +47,9 @@ class Client {
setTwilioEmailAuth(username, password) {
const b64Auth = Buffer.from(username + ':' + password).toString('base64');
this.auth = 'Basic ' + b64Auth;
this.setDefaultRequest('baseUrl', TWILIO_BASE_URL);
if (this.defaultRequest.baseUrl === SENDGRID_BASE_URL) {
this.setDefaultRequest('baseUrl', TWILIO_BASE_URL);
}

if (!this.isValidTwilioAuth(username, password)) {
console.warn('Twilio Email credentials must be non-empty strings.');
Expand Down
17 changes: 17 additions & 0 deletions packages/client/src/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ describe('setImpersonateSubuser', () => {
});
});

describe('setDefaultRequest', () => {
const customBaseUrl = 'localhost:3030';
const sgClient = require('./client');

it('should set the custom base URL without being overwritten by setApiKey', () => {
sgClient.setDefaultRequest('baseUrl', customBaseUrl);
sgClient.setApiKey('SG\.1234567890');
expect(sgClient.defaultRequest.baseUrl).to.equal(customBaseUrl);
});

it('should set the custom base URL without being overwritten by setTwilioEmailAuth', () => {
sgClient.setDefaultRequest('baseUrl', customBaseUrl);
sgClient.setTwilioEmailAuth('username', 'password');
expect(sgClient.defaultRequest.baseUrl).to.equal(customBaseUrl);
});
});

describe('test_access_settings_activity_get', () => {
const request = {};
request.qs = {
Expand Down

0 comments on commit fff9f4c

Please sign in to comment.