From 31ebbbd893355d14080d8fa0f8796d31c84ae466 Mon Sep 17 00:00:00 2001 From: Jesse MacFadyen Date: Fri, 8 Nov 2024 16:07:21 -0800 Subject: [PATCH] Unblock dev (#816) * calling sendAuditLogs in prod is a noop * output message in prod that we are not logging, noop * coverage, linting, tests --- src/lib/audit-logger.js | 12 ++++++++++++ test/BaseCommand.test.js | 1 + test/__mocks__/@adobe/aio-lib-env.js | 19 +++++++++++++++++++ test/commands/lib/audit-logger.test.js | 15 ++++++++++++--- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test/__mocks__/@adobe/aio-lib-env.js diff --git a/src/lib/audit-logger.js b/src/lib/audit-logger.js index 24a1fd86..b434f948 100644 --- a/src/lib/audit-logger.js +++ b/src/lib/audit-logger.js @@ -12,6 +12,8 @@ const fetch = require('node-fetch') const fs = require('fs') const path = require('path') const chalk = require('chalk') +const { getCliEnv, PROD_ENV } = require('@adobe/aio-lib-env') +const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:lib-audit-logger', { provider: 'debug' }) const OPERATIONS = { AB_APP_DEPLOY: 'ab_app_deploy', @@ -33,6 +35,11 @@ const AUDIT_SERVICE_ENPOINTS = { * @param {string} env valid env stage|prod */ async function sendAuditLogs (accessToken, logEvent, env = 'prod') { + // TODO: this is blocked by the audit service only being available in stage + // remove this check once the service is available in prod + if (env !== 'stage') { + return + } const url = AUDIT_SERVICE_ENPOINTS[env] const payload = { event: logEvent @@ -60,6 +67,11 @@ async function sendAuditLogs (accessToken, logEvent, env = 'prod') { * @returns {object} logEvent */ function getAuditLogEvent (flags, project, event) { + if (getCliEnv() === PROD_ENV) { + aioLogger.debug('Audit logging is currently disabled in production environment') + return null + } + let logEvent, logStrMsg if (project && project.org && project.workspace) { if (event === 'AB_APP_DEPLOY') { diff --git a/test/BaseCommand.test.js b/test/BaseCommand.test.js index be3493d0..3cb5a894 100644 --- a/test/BaseCommand.test.js +++ b/test/BaseCommand.test.js @@ -104,6 +104,7 @@ test('getLaunchUrlPrefix() warns on older url', async () => { test('getLaunchUrlPrefix() uses stage launch prefix', async () => { const cmd = new TheCommand() libEnv.getCliEnv.mockReturnValue('stage') + mockAioConfig.get.mockReturnValue(0) expect(cmd.getLaunchUrlPrefix()).toBe('https://experience-stage.adobe.com/?devMode=true#/custom-apps/?localDevUrl=') }) diff --git a/test/__mocks__/@adobe/aio-lib-env.js b/test/__mocks__/@adobe/aio-lib-env.js new file mode 100644 index 00000000..6288e17c --- /dev/null +++ b/test/__mocks__/@adobe/aio-lib-env.js @@ -0,0 +1,19 @@ +/* +Copyright 2024 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +module.exports = { + getCliEnv: jest.fn(() => { + return 'stage' + }), + PROD_ENV: 'prod', + STAGE_ENV: 'stage' +} diff --git a/test/commands/lib/audit-logger.test.js b/test/commands/lib/audit-logger.test.js index 52f49472..5308c099 100644 --- a/test/commands/lib/audit-logger.test.js +++ b/test/commands/lib/audit-logger.test.js @@ -18,6 +18,7 @@ const chalk = require('chalk') const fetch = require('node-fetch') jest.mock('node-fetch', () => jest.fn()) const auditLogger = require('../../../src/lib/audit-logger') +const { getCliEnv } = require('@adobe/aio-lib-env') jest.mock('fs') jest.mock('chalk', () => ({ @@ -75,6 +76,7 @@ test('sendAuditLogs with valid params', async () => { expect(fetch).toHaveBeenCalledWith(auditLogger.AUDIT_SERVICE_ENPOINTS[mockEnv], options) }) +// NOTE: this test is blocked until the audit service is available in prod test('sendAuditLogs with default params', async () => { fetch.mockReturnValue(mockResponse) const options = { @@ -86,8 +88,8 @@ test('sendAuditLogs with default params', async () => { body: JSON.stringify({ event: mockLogEvent }) } await auditLogger.sendAuditLogs(mockToken, mockLogEvent) - expect(fetch).toHaveBeenCalledTimes(1) - expect(fetch).toHaveBeenCalledWith(auditLogger.AUDIT_SERVICE_ENPOINTS.prod, options) + expect(fetch).toHaveBeenCalledTimes(0) + // expect(fetch).toHaveBeenCalledWith(auditLogger.AUDIT_SERVICE_ENPOINTS.prod, options) }) test('sendAuditLogs error response', async () => { @@ -192,7 +194,14 @@ describe('getAuditLogEvent', () => { const event = 'AB_APP_DEPLOY' const result = auditLogger.getAuditLogEvent(flags, {}, event) - expect(result).toBeUndefined() + expect(result).toBeFalsy() + }) + + test('should return undefined in PROD (for now)', () => { + getCliEnv.mockReturnValueOnce('prod') + const event = 'AB_APP_DEPLOY' + const result = auditLogger.getAuditLogEvent(flags, project, event) + expect(result).toBeFalsy() }) test('should default operation to APP_TEST if event is not found in OPERATIONS', () => {