Skip to content

Commit

Permalink
refactor: update auth type for apis (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck authored Dec 10, 2024
1 parent 54f846a commit 81b83e9
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 53 deletions.
21 changes: 15 additions & 6 deletions packages/accounts/__tests__/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ describe('accounts', () => {
'autoReload': false,
};

nock('https://rest.nexmo.com')
nock('https://rest.nexmo.com', {
reqheaders: {
'authorization': 'Basic YWJjZDoxMjM0',
}
})
.persist()
.get('/account/get-balance')
.query({ api_key: 'abcd', api_secret: '1234' })
.reply(200, expectedResponse);

const lookup = await client.getBalance();
Expand All @@ -32,10 +35,13 @@ describe('accounts', () => {
'error-code-label': 'success',
};

nock('https://rest.nexmo.com')
nock('https://rest.nexmo.com', {
reqheaders: {
'authorization': 'Basic YWJjZDoxMjM0',
}
})
.persist()
.post('/account/top-up', /trx=8ef2447e69604f642ae59363aa5f781b/)
.query({ api_key: 'abcd', api_secret: '1234' })
.reply(200, expectedResponse);

const lookup = await client.topUpBalance(
Expand Down Expand Up @@ -63,10 +69,13 @@ describe('accounts', () => {
const queryString = new URLSearchParams(callbacks);
const re = new RegExp(queryString.toString(), 'g');

nock('https://rest.nexmo.com')
nock('https://rest.nexmo.com', {
reqheaders: {
'authorization': 'Basic YWJjZDoxMjM0',
}
} )
.persist()
.post('/account/settings', re)
.query({ api_key: 'abcd', api_secret: '1234' })
.reply(200, expectedResponse);

const lookup = await client.updateAccountCallbacks(callbacks);
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/lib/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Accounts extends Client {
/**
* @see {@link Client.authType}
*/
protected authType = AuthenticationType.QUERY_KEY_SECRET;
protected authType = AuthenticationType.BASIC;

/**
* Retrieves the current balance of the Vonage API account.
Expand Down
4 changes: 2 additions & 2 deletions packages/numbers/__tests__/__dataSets__/buyNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/buy?api_key=12345&api_secret=ABCDE',
'/number/buy',
'POST',
new URLSearchParams([
['country', 'US'],
Expand Down Expand Up @@ -43,7 +43,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/buy?api_key=12345&api_secret=ABCDE',
'/number/buy',
'POST',
new URLSearchParams([
['country', 'US'],
Expand Down
2 changes: 1 addition & 1 deletion packages/numbers/__tests__/__dataSets__/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/cancel?api_key=12345&api_secret=ABCDE',
'/number/cancel',
'POST',
new URLSearchParams([
['country', 'US'],
Expand Down
4 changes: 2 additions & 2 deletions packages/numbers/__tests__/__dataSets__/getOwnedNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default [
clientMethod: CLIENT_METHOD,
request: {
url: BASE_URL,
intercept: ['/account/numbers?api_key=12345&api_secret=ABCDE', 'GET'],
intercept: ['/account/numbers', 'GET'],
reply: [200, validResponse],
},
parameters: [{}],
Expand All @@ -35,7 +35,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/account/numbers?application_id=foo-bar&has_application=false&search_pattern=searchPattern&country=US&pattern=pattern&size=42&index=1&api_key=12345&api_secret=ABCDE',
'/account/numbers?application_id=foo-bar&has_application=false&search_pattern=searchPattern&country=US&pattern=pattern&size=42&index=1',
'GET',
],
reply: [200, validResponse],
Expand Down
22 changes: 11 additions & 11 deletions packages/numbers/__tests__/__dataSets__/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default [
clientMethod: CLIENT_METHOD,
request: {
url: BASE_URL,
intercept: ['/number/search?api_key=12345&api_secret=ABCDE', 'GET'],
intercept: ['/number/search?', 'GET'],
reply: [
200,
validResponse,
Expand All @@ -36,7 +36,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
`/number/search?api_key=12345&api_secret=ABCDE&country=US&features=${Feature.MMS}`,
`/number/search?country=US&features=${Feature.MMS}`,
'GET',
],
reply: [200, validResponse],
Expand All @@ -55,7 +55,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
`/number/search?api_key=12345&api_secret=ABCDE&country=US&features=${Feature.VOICE},${Feature.MMS}`,
`/number/search?country=US&features=${Feature.VOICE},${Feature.MMS}`,
'GET',
],
reply: [200, validResponse],
Expand All @@ -74,7 +74,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
`/number/search?api_key=12345&api_secret=ABCDE&country=US&features=${Feature.SMS},${Feature.MMS},${Feature.VOICE}`,
`/number/search?country=US&features=${Feature.SMS},${Feature.MMS},${Feature.VOICE}`,
'GET',
],
reply: [200, validResponse],
Expand All @@ -93,7 +93,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=0&country=US',
'/number/search?pattern=1234&search_pattern=0&country=US',
'GET',
],
reply: [200, validResponse],
Expand All @@ -112,7 +112,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=2&country=US',
'/number/search?pattern=1234&search_pattern=2&country=US',
'GET',
],
reply: [200, validResponse],
Expand All @@ -131,7 +131,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=1&country=US',
'/number/search?pattern=1234&search_pattern=1&country=US',
'GET',
],
reply: [200, validResponse],
Expand All @@ -150,7 +150,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=1&country=US',
'/number/search?pattern=1234&search_pattern=1&country=US',
'GET',
],
reply: [200, validResponse],
Expand All @@ -171,7 +171,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/search?api_key=12345&api_secret=ABCDE&pattern=0987&search_pattern=2&country=US',
'/number/search?pattern=0987&search_pattern=2&country=US',
'GET',
],
reply: [200, validResponse],
Expand All @@ -191,7 +191,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/search?api_key=12345&api_secret=ABCDE&pattern=5309&search_pattern=0&country=US',
'/number/search?pattern=5309&search_pattern=0&country=US',
'GET',
],
reply: [200, validResponse],
Expand All @@ -214,7 +214,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/search?api_key=12345&api_secret=ABCDE&pattern=5309&search_pattern=1&country=US',
'/number/search?pattern=5309&search_pattern=1&country=US',
'GET',
],
reply: [200, validResponse],
Expand Down
2 changes: 1 addition & 1 deletion packages/numbers/__tests__/__dataSets__/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default [
request: {
url: BASE_URL,
intercept: [
'/number/update?api_key=12345&api_secret=ABCDE',
'/number/update',
'POST',
new URLSearchParams([
['app_id', '123abc'],
Expand Down
7 changes: 3 additions & 4 deletions packages/numbers/__tests__/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
TestRequest,
TestTuple,
apiKeyAuth,
apiKey,
apiSecret,
} from '../../../testHelpers';

const applicationsTest = dataSet.map((dataSet): TestTuple<Numbers> => {
Expand All @@ -19,11 +17,12 @@ const applicationsTest = dataSet.map((dataSet): TestTuple<Numbers> => {
name: label,
tests: tests.map((test): SDKTestCase<Numbers> => {
const requestUrl = new URL(`${test.request.url}${test.request.intercept[0]}`);
requestUrl.searchParams.set('api_key', apiKey);
requestUrl.searchParams.set('api_secret', apiSecret);
return {
label: test.label,
baseUrl: 'https://rest.nexmo.com',
reqHeaders: {
'authorization': 'Basic dGVzdEtleTp0ZXN0U2VjcmV0',
},
requests: [
[
`${requestUrl.pathname}${requestUrl.search.toString()}`,
Expand Down
6 changes: 5 additions & 1 deletion packages/numbers/lib/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ const sortFeatures = (features: Feature[]): string => {
* ```
*/
export class Numbers extends Client {
protected authType = AuthenticationType.QUERY_KEY_SECRET;
/**
* @see {@link Client.authType}
*/
protected authType = AuthenticationType.BASIC;


/**
* Buy a phone number.
Expand Down
24 changes: 17 additions & 7 deletions packages/pricing/__tests__/pricing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('pricing', () => {
client = new Pricing(new Auth({ apiKey: 'abcd', apiSecret: '1234' }));
});


test('do a country lookup', async () => {
const expectedResponse = {
countryCode: 'CA',
Expand All @@ -33,10 +32,14 @@ describe('pricing', () => {
],
};

nock(BASE_URL)
nock(BASE_URL, {
reqheaders: {
'authorization': 'Basic YWJjZDoxMjM0',
}
})
.persist()
.get('/account/get-pricing/outbound/sms')
.query({ api_key: 'abcd', api_secret: '1234', country: 'CA' })
.query({ country: 'CA' })
.reply(200, expectedResponse);

const lookup = await client.listCountryPricing(ServiceType.SMS, 'CA');
Expand Down Expand Up @@ -76,10 +79,13 @@ describe('pricing', () => {
],
};

nock(BASE_URL)
nock(BASE_URL, {
reqheaders: {
'authorization': 'Basic YWJjZDoxMjM0',
}
})
.persist()
.get('/account/get-full-pricing/outbound/sms')
.query({ api_key: 'abcd', api_secret: '1234' })
.reply(200, expectedResponse);

const lookup = await client.listAllCountriesPricing(ServiceType.SMS);
Expand Down Expand Up @@ -112,10 +118,14 @@ describe('pricing', () => {
],
};

nock(BASE_URL)
nock(BASE_URL, {
reqheaders: {
'authorization': 'Basic YWJjZDoxMjM0',
}
})
.persist()
.get('/account/get-prefix-pricing/outbound/sms')
.query({ api_key: 'abcd', api_secret: '1234', prefix: '1' })
.query({ prefix: '1' })
.reply(200, expectedResponse);

const lookup = await client.listPrefixPricing(ServiceType.SMS, '1');
Expand Down
5 changes: 4 additions & 1 deletion packages/pricing/lib/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ import {
* ```
*/
export class Pricing extends Client {
protected authType = AuthenticationType.QUERY_KEY_SECRET;
/**
* @see {@link Client.authType}
*/
protected authType = AuthenticationType.BASIC;

/**
* Retrieves pricing information for a specific country and service type.
Expand Down
13 changes: 0 additions & 13 deletions packages/sms/__tests__/__dataSets__/sms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { apiKey, apiSecret} from '../../../../testHelpers';
import {
MessageClassEnum,
SMSStatus,
Expand All @@ -20,8 +19,6 @@ export default [
'/sms/json',
'POST',
{
api_key: apiKey,
api_secret: apiSecret,
from: '12126875309',
to: '14152739164',
text: 'too many secrets',
Expand Down Expand Up @@ -76,8 +73,6 @@ export default [
'/sms/json',
'POST',
{
api_key: apiKey,
api_secret: apiSecret,
from: '12126875309',
to: '19162255887',
text: 'I\'ll always dial the "K" for you',
Expand Down Expand Up @@ -148,8 +143,6 @@ export default [
'/sms/json',
'POST',
{
api_key: apiKey,
api_secret: apiSecret,
from: '12126875309',
to: '18187779311',
text: 'Whats your number? ⏳',
Expand Down Expand Up @@ -206,8 +199,6 @@ export default [
'/sms/json',
'POST',
{
api_key: apiKey,
api_secret: apiSecret,
from: '12126875309',
to: '18186345789',
type: TypeEnum.BINARY,
Expand Down Expand Up @@ -268,8 +259,6 @@ export default [
'/sms/json',
'POST',
{
api_key: apiKey,
api_secret: apiSecret,
from: '12126875309',
to: '14152739164',
text: 'too many secrets',
Expand Down Expand Up @@ -314,8 +303,6 @@ export default [
'/sms/json',
'POST',
{
api_key: apiKey,
api_secret: apiSecret,
from: '12126875309',
to: '14152739164',
text: 'too many secrets',
Expand Down
3 changes: 3 additions & 0 deletions packages/sms/__tests__/sms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const applicationsTest = testDataSets.map((dataSet): TestTuple<SMS> => {
return {
label: test.label,
baseUrl: 'https://rest.nexmo.com',
reqHeaders: {
'authorization': 'Basic dGVzdEtleTp0ZXN0U2VjcmV0',
},
requests: [test.request] as TestRequest[],
responses: [test.response] as TestResponse[],
client: new SMS(apiKeyAuth),
Expand Down
Loading

0 comments on commit 81b83e9

Please sign in to comment.