Skip to content

Commit

Permalink
add test for private client
Browse files Browse the repository at this point in the history
  • Loading branch information
yunakim714 committed Sep 16, 2024
1 parent 9b73db4 commit c510758
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/static/helpers/IDPLoginHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@
import nock from 'nock';
import {ShopperLogin, TokenResponse} from '../../lib/shopperLogin';
import loginIDPUser from './IDPLoginHelper';
import {stringToBase64} from './slasHelper';
import ResponseError from '../responseError';

const credentialsPublic = {};

const credentialsPrivate = {
username: 'shopper_user_id',
password: 'shopper_password',
clientSecret: 'slas_private_secret',
};

const expectedTokenResponse: TokenResponse = {
access_token: 'access_token',
id_token: 'id_token',
Expand Down Expand Up @@ -68,6 +75,11 @@ const createMockSlasClient = () =>
siteId: string;
}>);

beforeEach(() => {
jest.clearAllMocks();
nock.cleanAll();
});

describe('Social login user flow', () => {
test('loginIDPUser does not stop when authorizeCustomer returns 303', async () => {
// slasClient is copied and tries to make an actual API call
Expand All @@ -84,4 +96,41 @@ describe('Social login user flow', () => {
loginIDPUser(mockSlasClient, credentialsPublic, parameters)
).resolves.not.toThrow(ResponseError);
});

test('generates an access token using slas private client', async () => {
const mockSlasClient = createMockSlasClient();
const {shortCode, organizationId} = mockSlasClient.clientConfig.parameters;

// Mock authorizeCustomer
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
.get(`/shopper/auth/v1/organizations/${organizationId}/oauth2/authorize`)
.query(true)
.reply(303, {message: 'Oh yes!'});

const accessToken = await loginIDPUser(
mockSlasClient,
credentialsPrivate,
parameters
);

const expectedReqOptions = {
headers: {
Authorization: `Basic ${stringToBase64(
`client_id:${credentialsPrivate.clientSecret}`
)}`,
},
body: {
grant_type: 'authorization_code_pkce',
redirect_uri: 'redirect_uri',
client_id: 'client_id',
channel_id: 'site_id',
usid: 'usid',
code_verifier: expect.stringMatching(/./) as string,
code: expect.any(String) as string,
dnt: 'false',
},
};
expect(getAccessTokenMock).toBeCalledWith(expectedReqOptions);
expect(accessToken).toBe(expectedTokenResponse);
})

Check warning on line 135 in src/static/helpers/IDPLoginHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (12)

Insert `;`

Check warning on line 135 in src/static/helpers/IDPLoginHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (14)

Insert `;`

Check warning on line 135 in src/static/helpers/IDPLoginHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (16)

Insert `;`

Check warning on line 135 in src/static/helpers/IDPLoginHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (18)

Insert `;`
});

0 comments on commit c510758

Please sign in to comment.