Skip to content

Commit

Permalink
[ID-2146] chore: Add tracking metrics to root Passport class (#2133)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmuscat authored Sep 4, 2024
1 parent 5ed7e3d commit 297d325
Show file tree
Hide file tree
Showing 4 changed files with 400 additions and 89 deletions.
178 changes: 174 additions & 4 deletions packages/passport/sdk/src/Passport.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Environment, ImmutableConfiguration } from '@imtbl/config';
import { IMXClient } from '@imtbl/x-client';
import { ImxApiClients, imxApiConfig, MultiRollupApiClients } from '@imtbl/generated-clients';
import { trackError } from '@imtbl/metrics';
import { trackError, trackFlow } from '@imtbl/metrics';
import AuthManager from './authManager';
import MagicAdapter from './magicAdapter';
import { Passport } from './Passport';
Expand Down Expand Up @@ -90,6 +90,9 @@ describe('Passport', () => {
linkWalletV2: linkExternalWalletMock,
},
});
(trackFlow as unknown as jest.Mock).mockImplementation(() => ({
addEvent: jest.fn(),
}));
passport = new Passport({
baseConfig: new ImmutableConfiguration({
environment: Environment.SANDBOX,
Expand Down Expand Up @@ -142,6 +145,21 @@ describe('Passport', () => {
expect(result).toBe(passportImxProvider);
expect(getProviderMock).toHaveBeenCalled();
});

it('should call track error function if an error occurs', async () => {
const error = new Error('Error');
getProviderMock.mockRejectedValue(error);

try {
await passport.connectImx();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'connectImx',
e,
);
}
});
});

describe('connectImxSilent', () => {
Expand All @@ -166,6 +184,21 @@ describe('Passport', () => {
expect(getProviderSilentMock).toHaveBeenCalled();
});
});

it('should call track error function if an error occurs', async () => {
const error = new Error('Unknown or invalid refresh token.');
getProviderSilentMock.mockRejectedValue(error);

try {
await passport.connectImxSilent();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'connectImxSilent',
e,
);
}
});
});

describe('connectEvm', () => {
Expand All @@ -180,7 +213,7 @@ describe('Passport', () => {

it('should announce the provider by default', async () => {
passportProviderInfo.uuid = 'mock123';
const provider = await passport.connectEvm();
const provider = passport.connectEvm();

expect(announceProvider).toHaveBeenCalledWith({
info: passportProviderInfo,
Expand All @@ -196,10 +229,26 @@ describe('Passport', () => {
...oidcConfiguration,
});

await passportInstance.connectEvm({ announceProvider: false });
passportInstance.connectEvm({ announceProvider: false });

expect(announceProvider).not.toHaveBeenCalled();
});

it('should call track error function if an error occurs', () => {
(ZkEvmProvider as jest.Mock).mockImplementation(() => {
throw new Error('Error');
});

try {
passport.connectEvm();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'connectEvm',
e,
);
}
});
});

describe('loginCallback', () => {
Expand All @@ -208,6 +257,21 @@ describe('Passport', () => {

expect(loginCallbackMock).toBeCalledTimes(1);
});

it('should call track error function if an error occurs', async () => {
const error = new Error('error');
loginCallbackMock.mockRejectedValue(error);

try {
await passport.loginCallback();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'loginCallback',
e,
);
}
});
});

describe('logout', () => {
Expand All @@ -232,6 +296,21 @@ describe('Passport', () => {
expect(magicLogoutMockOrder).toBeLessThan(logoutMockOrder);
});
});

it('should call track error function if an error occurs', async () => {
const error = new Error('error');
magicLogoutMock.mockRejectedValue(error);

try {
await passport.logout();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'logout',
e,
);
}
});
});

describe('logoutDeviceFlow', () => {
Expand All @@ -246,6 +325,21 @@ describe('Passport', () => {
expect(magicLogoutMock).toHaveBeenCalledTimes(1);
expect(getDeviceFlowEndSessionEndpointMock).toHaveBeenCalledTimes(1);
});

it('should call track error function if an error occurs', async () => {
const error = new Error('error');
removeUserMock.mockRejectedValue(error);

try {
await passport.logoutDeviceFlow();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'logoutDeviceFlow',
e,
);
}
});
});

describe('getUserInfo', () => {
Expand All @@ -264,6 +358,21 @@ describe('Passport', () => {

expect(result).toEqual(undefined);
});

it('should call track error function if an error occurs', async () => {
const error = new Error('Unknown or invalid refresh token.');
getUserMock.mockRejectedValue(error);

try {
await passport.getUserInfo();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'getUserInfo',
e,
);
}
});
});

describe('getIdToken', () => {
Expand All @@ -282,6 +391,21 @@ describe('Passport', () => {

expect(result).toEqual(undefined);
});

it('should call track error function if an error occurs', async () => {
const error = new Error('Unknown or invalid refresh token.');
getUserMock.mockRejectedValue(error);

try {
await passport.getIdToken();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'getIdToken',
e,
);
}
});
});

describe('getAccessToken', () => {
Expand All @@ -300,6 +424,21 @@ describe('Passport', () => {

expect(result).toEqual(undefined);
});

it('should call track error function if an error occurs', async () => {
const error = new Error('Unknown or invalid refresh token.');
getUserMock.mockRejectedValue(error);

try {
await passport.getAccessToken();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'getAccessToken',
e,
);
}
});
});

describe('getLinkedAddresses', () => {
Expand All @@ -325,6 +464,21 @@ describe('Passport', () => {

expect(result).toHaveLength(0);
});

it('should call track error function if an error occurs', async () => {
const error = new Error('Unknown or invalid refresh token.');
getUserMock.mockRejectedValue(error);

try {
await passport.getLinkedAddresses();
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'getLinkedAddresses',
e,
);
}
});
});

describe('login', () => {
Expand Down Expand Up @@ -377,6 +531,22 @@ describe('Passport', () => {
expect(getUserMock).toBeCalledTimes(1);
expect(authLoginMock).toBeCalledTimes(0);
});

it('should call track error function if an error occurs', async () => {
const error = new Error('Unknown or invalid refresh token.');
getUserMock.mockRejectedValue(error);
authLoginMock.mockReturnValue(mockUserImx);

try {
await passport.login({ useCachedSession: true });
} catch (e) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'login',
e,
);
}
});
});

describe('linkExternalWallet', () => {
Expand Down Expand Up @@ -439,7 +609,7 @@ describe('Passport', () => {
} catch (error) {
expect(trackError).toHaveBeenCalledWith(
'passport',
'linkWallet',
'linkExternalWallet',
error,
);
}
Expand Down
Loading

0 comments on commit 297d325

Please sign in to comment.