Skip to content

Commit

Permalink
add test for both public and private client
Browse files Browse the repository at this point in the history
  • Loading branch information
yunakim714 committed Sep 30, 2024
1 parent ac79df7 commit 9bce651
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 24 deletions.
99 changes: 86 additions & 13 deletions src/static/helpers/slasHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,52 @@ describe('Authorize user', () => {
slasHelper.authorize(mockSlasClient, codeVerifier, parameters)
).rejects.toThrow(ResponseError);
});

test('generate code challenge for public client only', async () => {
const authorizeCustomerMock = jest.fn();
const mockSlasClient = {
clientConfig: {
parameters: {
shortCode: 'short_code',
organizationId: 'organization_id',
clientId: 'client_id',
siteId: 'site_id',
},
},
authorizeCustomer: authorizeCustomerMock,
} as unknown as ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>;
const {shortCode, organizationId} = mockSlasClient.clientConfig.parameters;

let capturedQueryParams;
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
.get(`/shopper/auth/v1/organizations/${organizationId}/oauth2/authorize`)
.query(true)
.reply((uri) => {

Check warning on line 224 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (12)

Replace `(uri)` with `uri`

Check warning on line 224 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (14)

Replace `(uri)` with `uri`

Check warning on line 224 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (16)

Replace `(uri)` with `uri`

Check warning on line 224 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (18)

Replace `(uri)` with `uri`
const urlObject = new URL(
`https://${shortCode}.api.commercecloud.salesforce.com${uri}`
);
capturedQueryParams = Object.fromEntries(urlObject.searchParams); // Capture the query params
return [303, {response_body: 'response_body'}, {location: url}];
});

await slasHelper.authorize(mockSlasClient, codeVerifier, parameters, true);

// There should be no code_challenge for private client
const expectedReqOptions = {
client_id: 'client_id',
channel_id: 'site_id',
hint: 'hint',
redirect_uri: 'redirect_uri',
response_type: 'code',
usid: 'usid',
};
expect(capturedQueryParams).toEqual(expectedReqOptions);
});
});

test('throws error on 400 response', async () => {
Expand Down Expand Up @@ -225,7 +271,6 @@ describe('Authorize IDP User', () => {

const authResponse = await slasHelper.authorizeIDP(
mockSlasClient,
{},
parameters
);
const expectedAuthURL =
Expand All @@ -237,22 +282,50 @@ describe('Authorize IDP User', () => {
});

describe('IDP Login flow', () => {
test('retrieves usid and code and generates an access token', async () => {
const mockSlasClient = createMockSlasClient();
const {shortCode, organizationId} = mockSlasClient.clientConfig.parameters;
const loginParams = {
...parameters,
usid: '048adcfb-aa93-4978-be9e-09cb569fdcb9',
code: 'J2lHm0cgXmnXpwDhjhLoyLJBoUAlBfxDY-AhjqGMC-o',
};

// Mock authorizeCustomer
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
.get(`/shopper/auth/v1/organizations/${organizationId}/oauth2/authorize`)
.query(true)
.reply(303, {response_body: 'response_body'}, {location: url});
const mockSlasClient = createMockSlasClient();
const {shortCode, organizationId} = mockSlasClient.clientConfig.parameters;

const loginParams = {
...parameters,
usid: '048adcfb-aa93-4978-be9e-09cb569fdcb9',
code: 'J2lHm0cgXmnXpwDhjhLoyLJBoUAlBfxDY-AhjqGMC-o',
// Mock authorizeCustomer
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
.get(`/shopper/auth/v1/organizations/${organizationId}/oauth2/authorize`)
.query(true)
.reply(303, {response_body: 'response_body'}, {location: url});

test('retrieves usid and code and generates an access token for private client', async () => {
const accessToken = await slasHelper.loginIDPUser(
mockSlasClient,
{clientSecret: credentialsPrivate.clientSecret},
loginParams
);

const expectedReqOptions = {
headers: {
Authorization: `Basic ${stringToBase64(
`client_id:${credentialsPrivate.clientSecret}`
)}`,
},
body: {
grant_type: 'authorization_code',
redirect_uri: 'redirect_uri',
client_id: 'client_id',
channel_id: 'site_id',
organizationId: 'organization_id',
usid: '048adcfb-aa93-4978-be9e-09cb569fdcb9',
code: 'J2lHm0cgXmnXpwDhjhLoyLJBoUAlBfxDY-AhjqGMC-o',
dnt: 'false',
},
};
expect(getAccessTokenMock).toBeCalledWith(expectedReqOptions);
expect(accessToken).toBe(expectedTokenResponse);
});

test('retrieves usid and code and generates an access token for public client', async () => {
const accessToken = await slasHelper.loginIDPUser(
mockSlasClient,
{codeVerifier: 'code_verifier'},
Expand Down
17 changes: 6 additions & 11 deletions src/static/helpers/slasHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,14 @@ export async function authorizeIDP(
clientId: string;
siteId: string;
}>,
credentials: {
clientSecret?: string;
},
parameters: {
redirectURI: string;
hint: string;
usid?: string;
}
},
privateClient = false
): Promise<{url: string; codeVerifier: string}> {
const codeVerifier = createCodeVerifier();

// Create a copy to override specific fetchOptions
const slasClientCopy = new ShopperLogin(slasClient.clientConfig);

const privateClient = !!credentials.clientSecret;

interface ClientOptions {
codeChallenge?: string;
}
Expand All @@ -212,6 +204,9 @@ export async function authorizeIDP(
clientOptions.codeChallenge = await generateCodeChallenge(codeVerifier);
}

// Create a copy to override specific fetchOptions
const slasClientCopy = new ShopperLogin(slasClient.clientConfig);

const options = {
parameters: {
client_id: slasClient.clientConfig.parameters.clientId,
Expand Down Expand Up @@ -269,12 +264,12 @@ export async function loginIDPUser(
client_id: slasClient.clientConfig.parameters.clientId,
channel_id: slasClient.clientConfig.parameters.siteId,
code: parameters.code,
organizationId: slasClient.clientConfig.parameters.organizationId,
...(!privateClient &&
credentials.codeVerifier && {code_verifier: credentials.codeVerifier}),
grant_type: privateClient
? 'authorization_code'
: 'authorization_code_pkce',
organizationId: slasClient.clientConfig.parameters.organizationId,
redirect_uri: parameters.redirectURI,
...(parameters.dnt !== undefined && {dnt: parameters.dnt.toString()}),
...(parameters.usid && {usid: parameters.usid}),
Expand Down

0 comments on commit 9bce651

Please sign in to comment.