Skip to content

Commit

Permalink
Unblock dev (#816)
Browse files Browse the repository at this point in the history
* calling sendAuditLogs in prod is a noop
* output message in prod that we are not logging, noop
* coverage, linting, tests
  • Loading branch information
purplecabbage authored Nov 9, 2024
1 parent 9b36d06 commit 31ebbbd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/lib/audit-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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') {
Expand Down
1 change: 1 addition & 0 deletions test/BaseCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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=')
})

Expand Down
19 changes: 19 additions & 0 deletions test/__mocks__/@adobe/aio-lib-env.js
Original file line number Diff line number Diff line change
@@ -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'
}
15 changes: 12 additions & 3 deletions test/commands/lib/audit-logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 31ebbbd

Please sign in to comment.