Skip to content

Commit

Permalink
only add id if metrics optin
Browse files Browse the repository at this point in the history
and test
  • Loading branch information
NicolasMassart committed Dec 11, 2024
1 parent 372ae06 commit 3b90efc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
49 changes: 49 additions & 0 deletions app/util/logs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import initialRootState, {
backgroundState,
} from '../../util/test/initial-root-state';
import { merge } from 'lodash';
import MetaMetrics from '../../core/Analytics/MetaMetrics';

jest.mock('react-native-fs', () => ({
DocumentDirectoryPath: '/mock/path',
Expand Down Expand Up @@ -47,6 +48,17 @@ jest.mock('../../core/Engine', () => ({
},
}));

jest.mock('../../core/Analytics/MetaMetrics');

const mockMetrics = {
isEnabled: jest.fn(() => true),
getMetaMetricsId: jest.fn(() =>
Promise.resolve('6D796265-7374-4953-6D65-74616D61736B'),
),
};

(MetaMetrics.getInstance as jest.Mock).mockReturnValue(mockMetrics);

describe('logs :: generateStateLogs', () => {

it('generates a valid json export', async () => {
Expand Down Expand Up @@ -278,4 +290,41 @@ describe('logs :: downloadStateLogs', () => {
url: expect.stringContaining('data:text/plain;base64,'),
});
});

it('does not include metametrics id if not opt-in', async () => {
(getApplicationName as jest.Mock).mockResolvedValue('TestApp');
(getVersion as jest.Mock).mockResolvedValue('1.0.0');
(getBuildNumber as jest.Mock).mockResolvedValue('100');
(Device.isIos as jest.Mock).mockReturnValue(false);
(mockMetrics.isEnabled as jest.Mock).mockReturnValue(false);

const mockStateInput = merge({}, initialRootState, {
engine: {
backgroundState: {
...backgroundState,
KeyringController: {
vault: 'vault mock',
},
},
},
});

await downloadStateLogs(mockStateInput);

expect(Share.open).toHaveBeenCalledWith({
subject: 'TestApp State logs - v1.0.0 (100)',
title: 'TestApp State logs - v1.0.0 (100)',
url: expect.stringContaining('data:text/plain;base64,'),
});

// Access the arguments passed to Share.open
const shareOpenCalls = (Share.open as jest.Mock).mock.calls;
expect(shareOpenCalls.length).toBeGreaterThan(0);
const [shareOpenArgs] = shareOpenCalls[0];
const { url } = shareOpenArgs;
const base64Data = url.replace('data:text/plain;base64,', '');
const decodedData = Buffer.from(base64Data, 'base64').toString('utf-8');
const jsonData = JSON.parse(decodedData);
expect(jsonData).not.toHaveProperty('metaMetricsId');
});
});
3 changes: 2 additions & 1 deletion app/util/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export const downloadStateLogs = async (
const appName = await getApplicationName();
const appVersion = await getVersion();
const buildNumber = await getBuildNumber();
const metaMetricsId = await MetaMetrics.getInstance().getMetaMetricsId();
const metrics = MetaMetrics.getInstance();
const metaMetricsId = metrics.isEnabled() ? await metrics.getMetaMetricsId() : undefined;
const path =
RNFS.DocumentDirectoryPath +
`/state-logs-v${appVersion}-(${buildNumber}).json`;
Expand Down

0 comments on commit 3b90efc

Please sign in to comment.