From 3b90efcfdc307b748da54174b7de0e5ae722e723 Mon Sep 17 00:00:00 2001 From: Nicolas MASSART Date: Wed, 11 Dec 2024 17:02:39 +0100 Subject: [PATCH] only add id if metrics optin and test --- app/util/logs/index.test.ts | 49 +++++++++++++++++++++++++++++++++++++ app/util/logs/index.ts | 3 ++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/app/util/logs/index.test.ts b/app/util/logs/index.test.ts index 69808b24350..ab0cf5b6b79 100644 --- a/app/util/logs/index.test.ts +++ b/app/util/logs/index.test.ts @@ -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', @@ -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 () => { @@ -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'); + }); }); diff --git a/app/util/logs/index.ts b/app/util/logs/index.ts index 40039fa79a6..6363be63977 100644 --- a/app/util/logs/index.ts +++ b/app/util/logs/index.ts @@ -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`;