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

fix: Fix setApiKey and setTwilioEmailAuth to prevent overwriting baseUrl #1386

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 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,10 @@ class Client {
setTwilioEmailAuth(username, password) {
const b64Auth = Buffer.from(username + ':' + password).toString('base64');
this.auth = 'Basic ' + b64Auth;
this.setDefaultRequest('baseUrl', TWILIO_BASE_URL);
// Prevent implicit overwriting if baseUrl is not the default value
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
Loading