Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily retrieve transaction from transaction storage #1108

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions __tests__/Auth0Client/handleRedirectCallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Auth0Client', () => {
expect(error.error).toBe('some-error');
expect(error.error_description).toBe('some-error-description');
expect(error.state).toBe('error-state');
expect(error.appState).toBe(appState);
expect(error.appState).toEqual(appState);
});

it('should clear the transaction data when the /authorize call redirects with a code param', async () => {
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('Auth0Client', () => {
const result = await loginWithRedirect(auth0, { appState });

expect(result).toBeDefined();
expect(result.appState).toBe(appState);
expect(result.appState).toEqual(appState);
});

it('uses the custom http timeout value if specified', async () => {
Expand All @@ -234,7 +234,7 @@ describe('Auth0Client', () => {

expect((http.switchFetch as jest.Mock).mock.calls[0][6]).toEqual(40000);
expect(result).toBeDefined();
expect(result.appState).toBe(appState);
expect(result.appState).toEqual(appState);
});

it('does not store the scope from token endpoint if none was returned', async () => {
Expand Down Expand Up @@ -456,7 +456,7 @@ describe('Auth0Client', () => {
expect(error.error).toBe('some-error');
expect(error.error_description).toBe('some-error-description');
expect(error.state).toBe('error-state');
expect(error.appState).toBe(appState);
expect(error.appState).toEqual(appState);
});

it('should clear the transaction data when the /authorize call redirects with a code param', async () => {
Expand Down
4 changes: 3 additions & 1 deletion __tests__/Auth0Client/loginWithRedirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
} from '../constants';
import version from '../../src/version';

jest.mock('es-cookie');
jest.mock('../../src/jwt');
jest.mock('../../src/worker/token.worker');

Expand All @@ -59,6 +58,9 @@ describe('Auth0Client', () => {
const oldWindowLocation = window.location;

beforeEach(() => {
jest.spyOn(mockCookies, 'get');
jest.spyOn(mockCookies, 'set');
jest.spyOn(mockCookies, 'remove');
// https://www.benmvp.com/blog/mocking-window-location-methods-jest-jsdom/
delete window.location;
window.location = Object.defineProperties(
Expand Down
32 changes: 16 additions & 16 deletions __tests__/transaction-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ describe('transaction manager', () => {
jest.resetAllMocks();
});

describe('constructor', () => {
describe('get', () => {
it('loads transactions from storage (per key)', () => {
tm = new TransactionManager(SessionStorage, TEST_CLIENT_ID);

tm.get();

expect(sessionStorage.getItem).toHaveBeenCalledWith(transactionKey());
});
});
Expand Down Expand Up @@ -59,7 +62,7 @@ describe('transaction manager', () => {
});

it('`get` with a transaction should return the transaction', () => {
tm.create(transaction);
jest.mocked(sessionStorage.getItem).mockReturnValue(transactionJson);
expect(tm.get()).toMatchObject(transaction);
});

Expand All @@ -81,11 +84,11 @@ describe('transaction manager', () => {
expect(sessionStorage.removeItem).toHaveBeenCalledWith(transactionKey());
});
});
describe('CookieStorage usage', () => {
it("`create` saves the transaction in the storage with the provided domain", () => {

describe('CookieStorage usage', () => {
it('`create` saves the transaction in the storage with the provided domain', () => {
CookieStorage.save = jest.fn();
const cookieDomain = "vanity.auth.com";
const cookieDomain = 'vanity.auth.com';
tm = new TransactionManager(CookieStorage, TEST_CLIENT_ID, cookieDomain);
tm.create(transaction);

Expand All @@ -99,18 +102,15 @@ describe('transaction manager', () => {
);
});

it("`remove` deletes the transaction in the storage with the provided domain", () => {
it('`remove` deletes the transaction in the storage with the provided domain', () => {
CookieStorage.remove = jest.fn();
const cookieDomain = "vanity.auth.com";
const cookieDomain = 'vanity.auth.com';
tm = new TransactionManager(CookieStorage, TEST_CLIENT_ID, cookieDomain);
tm.remove();

expect(CookieStorage.remove).toHaveBeenCalledWith(
transactionKey(),
{
cookieDomain: cookieDomain
}
);
});
});
expect(CookieStorage.remove).toHaveBeenCalledWith(transactionKey(), {
cookieDomain: cookieDomain
});
});
});
});
15 changes: 7 additions & 8 deletions src/transaction-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,28 @@ interface Transaction {
}

export class TransactionManager {
private transaction: Transaction | undefined;
private storageKey: string;

constructor(private storage: ClientStorage, private clientId: string, private cookieDomain?: string) {
constructor(
private storage: ClientStorage,
private clientId: string,
private cookieDomain?: string
) {
this.storageKey = `${TRANSACTION_STORAGE_KEY_PREFIX}.${this.clientId}`;
this.transaction = this.storage.get(this.storageKey);
}

public create(transaction: Transaction) {
this.transaction = transaction;

this.storage.save(this.storageKey, transaction, {
daysUntilExpire: 1,
cookieDomain: this.cookieDomain,
cookieDomain: this.cookieDomain
});
}

public get(): Transaction | undefined {
return this.transaction;
return this.storage.get(this.storageKey);
}

public remove() {
delete this.transaction;
this.storage.remove(this.storageKey, {
cookieDomain: this.cookieDomain
});
Expand Down