Skip to content

Commit 7c18ea0

Browse files
authored
Merge pull request #415 from SalesforceCommerceCloud/W-18089100/allow-custom-params-helpers
W-18089100 - allow custom params in helpers 'loginGuestUser' and custom body for 'loginRegisteredUserB2C'
2 parents 7daa437 + 75af14d commit 7c18ea0

File tree

4 files changed

+222
-46
lines changed

4 files changed

+222
-46
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## CHANGELOG
22

3+
## v4.2.0 - future release
4+
- Allow custom params for 'loginGuestUser' and custom body for 'loginRegisteredUserB2C' function [#415](https://github.com/SalesforceCommerceCloud/commerce-sdk/pull/415)
5+
36
## v4.1.0
47

58
### Enchancements

src/static/helpers/slas.test.ts

Lines changed: 161 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ describe("Authorize user", () => {
141141
usid: "usid",
142142
};
143143

144+
const params = {
145+
redirectURI: parameters.redirectURI,
146+
hint: parameters.hint,
147+
usid: parameters.usid,
148+
};
144149
it("hits the authorize endpoint and receives authorization code", async () => {
145150
const mockSlasClient = createSlasClient();
146151
const { shortCode, organizationId } = clientConfig.parameters;
@@ -154,7 +159,7 @@ describe("Authorize user", () => {
154159
const authResponse = await slasHelper.authorize(
155160
mockSlasClient,
156161
codeVerifier,
157-
parameters
162+
params
158163
);
159164
expect(authResponse).to.be.deep.equal(expectedAuthResponse);
160165
});
@@ -172,7 +177,7 @@ describe("Authorize user", () => {
172177
const authResponse = await slasHelper.authorize(
173178
mockSlasClient,
174179
codeVerifier,
175-
parameters
180+
params
176181
);
177182

178183
const authURL = new URL(authResponse.url);
@@ -199,7 +204,7 @@ describe("Authorize user", () => {
199204
.reply(400);
200205

201206
await slasHelper
202-
.authorize(mockSlasClient, codeVerifier, parameters)
207+
.authorize(mockSlasClient, codeVerifier, params)
203208
.catch((error) => expect(error.message).to.be.equal("400 Bad Request"));
204209
});
205210
});
@@ -296,9 +301,55 @@ describe("Guest user flow", () => {
296301
expect(options.body).to.include.keys("code_verifier");
297302
expect(accessToken).to.be.deep.equals(expectedTokenResponse);
298303
});
304+
305+
it("can pass custom params when using a public client flow", async () => {
306+
const mockSlasClient = createSlasClient();
307+
const getTokenSpy = sinon.spy(mockSlasClient, "getAccessToken");
308+
const authorizeSpy = sinon.spy(mockSlasClient, "authorizeCustomer");
309+
const { shortCode, organizationId } = mockSlasClient.clientConfig
310+
.parameters as CommonParameters;
311+
312+
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
313+
.get(`/shopper/auth/v1/organizations/${organizationId}/oauth2/authorize`)
314+
.query(true)
315+
.reply(303, { response_body: "response_body" });
316+
317+
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
318+
.post(`/shopper/auth/v1/organizations/${organizationId}/oauth2/token`)
319+
.query(true)
320+
.reply(200, expectedTokenResponse);
321+
const accessToken = await slasHelper.loginGuestUser(mockSlasClient, {
322+
c_cloth: "jeans",
323+
redirectURI: parameters.redirectURI,
324+
});
325+
326+
const getTokenOpts = getTokenSpy.getCall(0).args[0];
327+
const authorizeOpt = authorizeSpy.getCall(0).args[0];
328+
329+
// have to match object since code_verifier is randomly generated
330+
sinon.assert.match(getTokenOpts, expectedOptionsPublic);
331+
sinon.assert.match(authorizeOpt, {
332+
parameters: {
333+
c_cloth: "jeans",
334+
client_id: clientConfig.parameters.clientId,
335+
code_challenge: sinon.match(/./),
336+
hint: "guest",
337+
organizationId: clientConfig.parameters.organizationId,
338+
redirect_uri: parameters.redirectURI,
339+
response_type: "code",
340+
},
341+
fetchOptions: { redirect: "manual" },
342+
});
343+
expect(getTokenOpts.body).to.include.keys("code_verifier");
344+
expect(accessToken).to.be.deep.equals(expectedTokenResponse);
345+
});
299346
});
300347

301348
describe("Registered B2C user flow", () => {
349+
const params = {
350+
redirectURI: parameters.redirectURI,
351+
usid: parameters.usid,
352+
};
302353
it("using a private client uses hits login and token endpoints to generate JWT", async () => {
303354
const mockSlasClient = createSlasClient();
304355
const { shortCode, organizationId } = mockSlasClient.clientConfig
@@ -316,7 +367,7 @@ describe("Registered B2C user flow", () => {
316367
const accessToken = await slasHelper.loginRegisteredUserB2Cprivate(
317368
mockSlasClient,
318369
credentials,
319-
parameters
370+
params
320371
);
321372
expect(accessToken).to.be.deep.equals(expectedTokenResponse);
322373
});
@@ -338,12 +389,90 @@ describe("Registered B2C user flow", () => {
338389
const accessToken = await slasHelper.loginRegisteredUserB2C(
339390
mockSlasClient,
340391
credentials,
341-
parameters
392+
params
342393
);
343394

344395
expect(accessToken).to.be.deep.equals(expectedTokenResponse);
345396
});
346397

398+
it("loginRegisteredUserB2C accepts custom body", async () => {
399+
const mockSlasClient = createSlasClient();
400+
const authenticateSpy = sinon.spy(mockSlasClient, "authenticateCustomer");
401+
const { shortCode, organizationId } = mockSlasClient.clientConfig
402+
.parameters as CommonParameters;
403+
404+
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
405+
.post(`/shopper/auth/v1/organizations/${organizationId}/oauth2/login`)
406+
.reply(303, { response_body: "response_body" }, { location: mockURL });
407+
408+
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
409+
.post(`/shopper/auth/v1/organizations/${organizationId}/oauth2/token`)
410+
.reply(200, expectedTokenResponse);
411+
412+
const accessToken = await slasHelper.loginRegisteredUserB2C(
413+
mockSlasClient,
414+
credentials,
415+
params,
416+
{ body: { c_body: "custom-body" } }
417+
);
418+
const authenticateOpts = authenticateSpy.getCall(0).args[0];
419+
sinon.assert.match(authenticateOpts, {
420+
headers: {
421+
Authorization: sinon.match(/./),
422+
},
423+
parameters: { organizationId: organizationId },
424+
body: {
425+
c_body: "custom-body",
426+
redirect_uri: params.redirectURI,
427+
client_id: clientConfig.parameters.clientId,
428+
code_challenge: sinon.match(/./),
429+
channel_id: clientConfig.parameters.siteId,
430+
usid: params.usid,
431+
},
432+
fetchOptions: { redirect: "manual" },
433+
});
434+
expect(accessToken).to.be.deep.equals(expectedTokenResponse);
435+
});
436+
437+
it("loginRegisteredUserB2Cprivate excepts custom body", async () => {
438+
const mockSlasClient = createSlasClient();
439+
const authenticateSpy = sinon.spy(mockSlasClient, "authenticateCustomer");
440+
441+
const { shortCode, organizationId } = mockSlasClient.clientConfig
442+
.parameters as CommonParameters;
443+
444+
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
445+
.post(`/shopper/auth/v1/organizations/${organizationId}/oauth2/login`)
446+
.reply(303, { response_body: "response_body" });
447+
448+
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
449+
.post(`/shopper/auth/v1/organizations/${organizationId}/oauth2/token`)
450+
.reply(200, expectedTokenResponse);
451+
452+
const accessToken = await slasHelper.loginRegisteredUserB2Cprivate(
453+
mockSlasClient,
454+
credentials,
455+
params,
456+
{ body: { c_body: "custom-body" } }
457+
);
458+
const authenticateOpts = authenticateSpy.getCall(0).args[0];
459+
sinon.assert.match(authenticateOpts, {
460+
headers: {
461+
Authorization: sinon.match(/./),
462+
},
463+
body: {
464+
c_body: "custom-body",
465+
code_challenge: sinon.match(/./),
466+
channel_id: clientConfig.parameters.siteId,
467+
client_id: clientConfig.parameters.clientId,
468+
redirect_uri: params.redirectURI,
469+
usid: params.usid,
470+
},
471+
fetchOptions: { redirect: "manual" },
472+
});
473+
expect(accessToken).to.be.deep.equals(expectedTokenResponse);
474+
});
475+
347476
it("throws an error when login is unsuccessful for public", async () => {
348477
const mockSlasClient = createSlasClient();
349478
const { shortCode, organizationId } = mockSlasClient.clientConfig
@@ -354,7 +483,7 @@ describe("Registered B2C user flow", () => {
354483
.reply(400);
355484

356485
await slasHelper
357-
.loginRegisteredUserB2C(mockSlasClient, credentials, parameters)
486+
.loginRegisteredUserB2C(mockSlasClient, credentials, params)
358487
.catch((error) => expect(error.message).to.be.equal("400 Bad Request"));
359488
});
360489

@@ -368,31 +497,20 @@ describe("Registered B2C user flow", () => {
368497
.reply(400);
369498

370499
await slasHelper
371-
.loginRegisteredUserB2Cprivate(mockSlasClient, credentials, parameters)
500+
.loginRegisteredUserB2Cprivate(mockSlasClient, credentials, params)
372501
.catch((error) => expect(error.message).to.be.equal("400 Bad Request"));
373502
});
374503
});
375504

376505
describe("Refresh Token", () => {
377-
const expectedBody = {
378-
body: {
379-
client_id: "client_id",
380-
grant_type: "refresh_token",
381-
refresh_token: "refresh_token",
382-
},
383-
};
384-
385-
const expectedOptions = {
386-
headers: {
387-
Authorization: "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=",
388-
},
389-
body: {
390-
grant_type: "refresh_token",
391-
refresh_token: parameters.refreshToken,
392-
},
393-
};
394-
395506
it("refreshes the token", async () => {
507+
const expectedOpts = {
508+
body: {
509+
client_id: "client_id",
510+
grant_type: "refresh_token",
511+
refresh_token: "refresh_token",
512+
},
513+
};
396514
const mockSlasClient = createSlasClient();
397515
const spy = sinon.spy(mockSlasClient, "getAccessToken");
398516
const { shortCode, organizationId } = clientConfig.parameters;
@@ -402,16 +520,23 @@ describe("Refresh Token", () => {
402520
.query(true)
403521
.reply(200, expectedTokenResponse);
404522

405-
const token = await slasHelper.refreshAccessToken(
406-
mockSlasClient,
407-
parameters
408-
);
409-
410-
expect(spy.getCall(0).args[0]).to.be.deep.equals(expectedBody);
523+
const token = await slasHelper.refreshAccessToken(mockSlasClient, {
524+
refreshToken: parameters.refreshToken,
525+
});
526+
expect(spy.getCall(0).args[0]).to.be.deep.equals(expectedOpts);
411527
expect(token).to.be.deep.equals(expectedTokenResponse);
412528
});
413529

414530
it("refreshes the token using client secret", async () => {
531+
const expectedOptions = {
532+
headers: {
533+
Authorization: "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=",
534+
},
535+
body: {
536+
grant_type: "refresh_token",
537+
refresh_token: parameters.refreshToken,
538+
},
539+
};
415540
const mockSlasClient = createSlasClient();
416541
const spy = sinon.spy(mockSlasClient, "getAccessToken");
417542
const { shortCode, organizationId } = clientConfig.parameters;
@@ -424,7 +549,7 @@ describe("Refresh Token", () => {
424549
const token = await slasHelper.refreshAccessTokenPrivate(
425550
mockSlasClient,
426551
credentials,
427-
parameters
552+
{ refreshToken: parameters.refreshToken }
428553
);
429554

430555
expect(spy.getCall(0).args[0]).to.be.deep.equals(expectedOptions);
@@ -454,7 +579,10 @@ describe("Logout", () => {
454579
.query(true)
455580
.reply(200, expectedTokenResponse);
456581

457-
const token = await slasHelper.logout(mockSlasClient, parameters);
582+
const token = await slasHelper.logout(mockSlasClient, {
583+
refreshToken: parameters.refreshToken,
584+
accessToken: parameters.accessToken,
585+
});
458586
expect(spy.getCall(0).args[0]).to.be.deep.equals(expectedOptions);
459587
expect(token).to.be.deep.equals(expectedTokenResponse);
460588
});

0 commit comments

Comments
 (0)