Skip to content

Commit

Permalink
Add validation for used clientConfig arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-jung1 committed Sep 20, 2024
1 parent 10a7405 commit 5314b6b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/static/helpers/passwordlessHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,43 @@ describe('authorizePasswordless is working', () => {
},
};
expect(authorizePasswordlessCustomerMock).toBeCalledWith(
expectedReqOptions,
expectedReqOptions,
true
);
});
test('Throw when required parameters missing', async () => {
const mockSlasClient = {
clientConfig: {
parameters: {
shortCode: 'short_code',
organizationId: 'organization_id',
clientId: 'client_id',
},
},
authorizePasswordlessCustomer: authorizePasswordlessCustomerMock,
getPasswordLessAccessToken: getPasswordLessAccessTokenMock,
} as unknown as ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>;
const credentials = {
clientSecret: 'slas_private_secret',
};
const parameters = {
callbackURI: 'www.something.com/callback',
usid: 'a_usid',
userid: 'a_userid',
locale: 'a_locale',
mode: 'callback',
};
await expect(
authorizePasswordless(mockSlasClient, credentials, parameters)
).rejects.toThrow(
'Required argument channel_id is not provided through clientConfig.parameters.siteId'
);
});
});

describe('getPasswordLessAccessToken is working', () => {
Expand Down Expand Up @@ -131,4 +164,33 @@ describe('getPasswordLessAccessToken is working', () => {
};
expect(getPasswordLessAccessTokenMock).toBeCalledWith(expectedReqOptions);
});
test('Throw when required parameters missing', async () => {
const mockSlasClient = {
clientConfig: {
parameters: {
shortCode: 'short_code',
clientId: 'client_id',
},
},
authorizePasswordlessCustomer: authorizePasswordlessCustomerMock,
getPasswordLessAccessToken: getPasswordLessAccessTokenMock,
} as unknown as ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>;
const credentials = {
clientSecret: 'slas_private_secret',
};
const parameters = {
pwdlessLoginToken: '123456',

Check warning on line 187 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (12)

Delete `··`

Check warning on line 187 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (14)

Delete `··`

Check warning on line 187 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (16)

Delete `··`

Check warning on line 187 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (18)

Delete `··`
dnt: '1',

Check warning on line 188 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (12)

Delete `··`

Check warning on line 188 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (14)

Delete `··`

Check warning on line 188 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (16)

Delete `··`

Check warning on line 188 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (18)

Delete `··`
};

Check warning on line 189 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (12)

Delete `··`

Check warning on line 189 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (14)

Delete `··`

Check warning on line 189 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (16)

Delete `··`

Check warning on line 189 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (18)

Delete `··`
await expect(
getPasswordLessAccessToken(mockSlasClient, credentials, parameters)

Check warning on line 191 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (12)

Delete `··`

Check warning on line 191 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (14)

Delete `··`

Check warning on line 191 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (16)

Delete `··`

Check warning on line 191 in src/static/helpers/passwordlessHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (18)

Delete `··`
).rejects.toThrow(
'Required argument organizationId is not provided through clientConfig.parameters.organizationId'
);
});
});
10 changes: 10 additions & 0 deletions src/static/helpers/passwordlessHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export async function authorizePasswordless(
mode: string;
}
): Promise<Response> {
if (!slasClient.clientConfig.parameters.siteId) {
throw new Error(
'Required argument channel_id is not provided through clientConfig.parameters.siteId'
);
}
const authHeaderIdSecret = `Basic ${stringToBase64(
`${slasClient.clientConfig.parameters.clientId}:${credentials.clientSecret}`
)}`;
Expand Down Expand Up @@ -92,6 +97,11 @@ export async function getPasswordLessAccessToken(
dnt?: string;
}
): Promise<TokenResponse> {
if (!slasClient.clientConfig.parameters.organizationId) {
throw new Error(
'Required argument organizationId is not provided through clientConfig.parameters.organizationId'
);
}
const codeVerifier = createCodeVerifier();
const authHeaderIdSecret = `Basic ${stringToBase64(
`${slasClient.clientConfig.parameters.clientId}:${credentials.clientSecret}`
Expand Down

0 comments on commit 5314b6b

Please sign in to comment.