Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"dependencies": {
"@adobe/aio-lib-core-config": "^5",
"@adobe/aio-lib-core-networking": "^5",
"@adobe/aio-lib-runtime": "^7",
"@adobe/aio-lib-env": "^3.0.1",
"@adobe/aio-lib-ims": "^8.0.1",
"@adobe/aio-lib-runtime": "^7.1.0",
"@oclif/core": "^1.3.0",
"@types/jest": "^29.5.3",
"chalk": "^4.1.2",
Expand Down
17 changes: 17 additions & 0 deletions src/RuntimeBaseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const debug = createDebug('aio-cli-plugin-runtime')
const http = require('http')
const runtimeLib = require('@adobe/aio-lib-runtime')
const config = require('@adobe/aio-lib-core-config')
const { getToken, context } = require('@adobe/aio-lib-ims')
const { getCliEnv } = require('@adobe/aio-lib-env')
const { CLI } = require('@adobe/aio-lib-ims/src/context')

class RuntimeBaseCommand extends Command {
async getOptions () {
Expand Down Expand Up @@ -64,7 +67,21 @@ class RuntimeBaseCommand extends Command {

async wsk (options) {
if (!options) {
const authHandler = {
getAuthHeader: async () => {
await context.setCli({ 'cli.bare-output': true }, false) // set this globally
const env = getCliEnv()
console.debug(`Retrieving CLI Token using env=${env}`)
const accessToken = await getToken(CLI)

return `Bearer ${accessToken}`
}
}
options = await this.getOptions()
if (process.env.IS_DEPLOY_SERVICE_ENABLED === 'true') {
options.auth_handler = authHandler
options.apihost = options.apihost ?? PropertyDefault.DEPLOYSERVICEURL
}
}
return runtimeLib.init(options)
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/runtime/action/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ governing permissions and limitations under the License.
*/

const fs = require('fs')
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const { createKeyValueArrayFromFlag, createKeyValueArrayFromFile, createComponentsfromSequence, getKeyValueArrayFromMergedParameters } = require('@adobe/aio-lib-runtime').utils
const { kindForFileExtension } = require('../../../kinds')
const { Flags } = require('@oclif/core')
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')

class ActionCreate extends RuntimeBaseCommand {
isUpdate () { return false }
Expand Down
1 change: 1 addition & 0 deletions src/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const PropertyEnv = {
const PropertyDefault = {
AUTH: '',
APIHOST: 'https://adobeioruntime.net',
DEPLOYSERVICEURL: 'https://adobeioruntime.net',
APIVERSION: 'v1',
NAMESPACE: '_',
CERT: '',
Expand Down
190 changes: 183 additions & 7 deletions test/RuntimeBaseCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ const { Command } = require('@oclif/core')
const { PropertyEnv } = require('../src/properties')
const RuntimeLib = require('@adobe/aio-lib-runtime')
const OpenWhiskError = require('openwhisk/lib/openwhisk_error')
const { getToken, context } = require('@adobe/aio-lib-ims')
const { getCliEnv } = require('@adobe/aio-lib-env')

jest.mock('@adobe/aio-lib-ims', () => ({
getToken: jest.fn(),
context: {
setCli: jest.fn()
}
}))

jest.mock('@adobe/aio-lib-env', () => ({
getCliEnv: jest.fn()
}))

jest.mock('@adobe/aio-lib-runtime', () => ({
init: jest.fn()
}))

beforeEach(() => {
fakeFileSystem.reset()
Expand Down Expand Up @@ -50,6 +67,11 @@ describe('instance methods', () => {
})

describe('init', () => {
const response = {
apihost: 'https://adobeioruntime.net',
api_key: 1234,
apiversion: 'v1'
}
test('is a function', async () => {
expect(command.init).toBeInstanceOf(Function)
})
Expand All @@ -60,9 +82,7 @@ describe('instance methods', () => {
fakeFileSystem.addJson(files)

return command.wsk().then(() => {
expect(RuntimeLib.init).toHaveBeenLastCalledWith(
{ apihost: 'https://adobeioruntime.net', api_key: 1234, apiversion: 'v1' }
)
expect(RuntimeLib.init).toHaveBeenLastCalledWith(response)
})
})

Expand Down Expand Up @@ -92,9 +112,7 @@ describe('instance methods', () => {
fakeFileSystem.addJson(files)

return command.wsk().then(() => {
expect(RuntimeLib.init).toHaveBeenLastCalledWith(
{ api_key: 1234, apihost: 'https://adobeioruntime.net', apiversion: 'v1' }
)
expect(RuntimeLib.init).toHaveBeenLastCalledWith(response)
delete process.env[PropertyEnv.APIHOST]
})
})
Expand All @@ -106,7 +124,11 @@ describe('instance methods', () => {

return command.wsk().then(() => {
expect(RuntimeLib.init).toHaveBeenLastCalledWith(
{ api_key: 123, apihost: 'https://adobeioruntime.net', apiversion: 'v1' }
{
api_key: 123,
apihost: 'https://adobeioruntime.net',
apiversion: 'v1'
}
)
delete process.env[PropertyEnv.APIHOST]
})
Expand Down Expand Up @@ -312,4 +334,158 @@ describe('instance methods', () => {
expect(command.error).toHaveBeenCalledWith('msg' + suffix)
})
})

describe('authHandler', () => {
describe('when IS_DEPLOY_SERVICE_ENABLED = true', () => {
beforeEach(() => {
process.env.IS_DEPLOY_SERVICE_ENABLED = true
})

afterEach(() => {
process.env.IS_DEPLOY_SERVICE_ENABLED = false
})
test('No Options : should return the correct Authorization header using getAuthHeader', async () => {
const mockToken = 'mock-access-token'
getToken.mockResolvedValue(mockToken)

// Spy on runtimeLib.init to capture options before it's used
let capturedOptions
RuntimeLib.init.mockImplementation(async (options) => {
capturedOptions = options // Store options for later verification
return {} // Mock runtimeLib.init() return value
})

// Call wsk() which internally sets auth_handler
await command.wsk()

// Ensure options were captured
expect(capturedOptions).toBeDefined()
expect(capturedOptions.auth_handler).toBeDefined()
expect(capturedOptions.apihost).toBeDefined()
expect(capturedOptions.apihost).toBe('some.host')

// Call getAuthHeader() from captured options
const authHeader = await capturedOptions.auth_handler.getAuthHeader()

expect(context.setCli).toHaveBeenCalledWith({ 'cli.bare-output': true }, false)
expect(getCliEnv).toHaveBeenCalled()
expect(getToken).toHaveBeenCalled()
expect(authHeader).toBe(`Bearer ${mockToken}`)
})

test('With Options : should return the correct Authorization header using getAuthHeader', async () => {
const mockToken = 'mock-access-token'
getToken.mockResolvedValue(mockToken)

const options = {
auth_handler: {
getAuthHeader: async () => `Bearer ${mockToken}`
},
apihost: 'https://custom-api.adobe.com'
}

await command.wsk(options) // Call wsk() with an existing options object

expect(RuntimeLib.init).toHaveBeenCalledWith(options)
})

test('Default OW Host testing', async () => {
delete process.env[PropertyEnv.APIHOST]

const mockToken = 'mock-access-token'
getToken.mockResolvedValue(mockToken)

command.getOptions = jest.fn().mockResolvedValue({})

// Mock runtimeLib.init to track its calls
const mockInit = jest.fn().mockResolvedValue({})
RuntimeLib.init = mockInit

// Call wsk() without options
await command.wsk()

// Assertions
expect(RuntimeLib.init).toHaveBeenCalled()

// Verify the passed options contain the default apihost
const optionsPassedToInit = mockInit.mock.calls[0][0] // Get the options passed to init
expect(optionsPassedToInit.apihost).toBe('https://adobeioruntime.net')

// Ensure the Authorization header is set correctly
expect(optionsPassedToInit.auth_handler).toBeDefined()
const authHeader = await optionsPassedToInit.auth_handler.getAuthHeader()
expect(authHeader).toBe(`Bearer ${mockToken}`)
})
})

describe('when IS_DEPLOY_SERVICE_ENABLED = false', () => {
beforeEach(() => {
process.env.IS_DEPLOY_SERVICE_ENABLED = false
})

test('No Options : should return the correct Authorization header using getAuthHeader', async () => {
const mockToken = 'mock-access-token'
getToken.mockResolvedValue(mockToken)

// Spy on runtimeLib.init to capture options before it's used
let capturedOptions
RuntimeLib.init.mockImplementation(async (options) => {
capturedOptions = options // Store options for later verification
return {} // Mock runtimeLib.init() return value
})

// Call wsk() which internally sets auth_handler
await command.wsk()

// Ensure options were captured
expect(capturedOptions).toBeDefined()
expect(capturedOptions.auth_handler).not.toBeDefined()
expect(capturedOptions.apihost).toBeDefined()
expect(capturedOptions.apihost).toBe('some.host')
})

test('With Options : should return the correct Authorization header using getAuthHeader', async () => {
const mockToken = 'mock-access-token'
getToken.mockResolvedValue(mockToken)

const options = {
auth_handler: {
getAuthHeader: async () => `Bearer ${mockToken}`
},
apihost: 'https://custom-api.adobe.com'
}

await command.wsk(options) // Call wsk() with an existing options object

expect(RuntimeLib.init).toHaveBeenCalledWith(options)
})

test('Default OW Host testing', async () => {
delete process.env[PropertyEnv.APIHOST]

const mockToken = 'mock-access-token'
getToken.mockResolvedValue(mockToken)

// command.getOptions = jest.fn().mockResolvedValue({})

// Mock runtimeLib.init to track its calls
const mockInit = jest.fn().mockResolvedValue({})
RuntimeLib.init = mockInit

// Call wsk() without options
await command.wsk()

// Assertions
expect(RuntimeLib.init).toHaveBeenCalled()

// Verify the passed options contain the default apihost
const optionsPassedToInit = mockInit.mock.calls[0][0] // Get the options passed to init
expect(optionsPassedToInit.apihost).toBe('some.host')
expect(optionsPassedToInit.namespace).toBe('some_namespace')

// Ensure the Authorization header is set correctly
expect(optionsPassedToInit.auth_handler).not.toBeDefined()
})
})
})
})