Skip to content

Commit

Permalink
DEVX-2521: End-to-end testing for template registry - Add env vars to…
Browse files Browse the repository at this point in the history
… override Template Registry API config values (#43)

* DEVX-2521: End-to-end testing for template registry - Add env vars to override Template Registry API config values

* Update README.md, oclif.manifest.json

* Improve tests
  • Loading branch information
slitviachenko authored Sep 21, 2022
1 parent 69921cb commit 3749322
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ DESCRIPTION
Discover, install, or uninstall a new template into an existing Adobe Developer App Builder App
```

_See code: [src/commands/templates/index.js](https://github.com/adobe/aio-cli-plugin-app-templates/blob/1.2.0/src/commands/templates/index.js)_
_See code: [src/commands/templates/index.js](https://github.com/adobe/aio-cli-plugin-app-templates/blob/1.3.1/src/commands/templates/index.js)_

## `aio templates discover`

Expand Down
2 changes: 1 addition & 1 deletion oclif.manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"1.2.0","commands":{"templates:discover":{"id":"templates:discover","description":"Discover App Builder templates to install","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:disco"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"interactive":{"name":"interactive","type":"boolean","char":"i","description":"interactive install mode","allowNo":false},"sort-field":{"name":"sort-field","type":"option","char":"f","description":"which column to sort, use the sort-order flag to specify sort direction","multiple":false,"options":["publishDate","names","adobeRecommended"],"default":"adobeRecommended"},"sort-order":{"name":"sort-order","type":"option","char":"o","description":"sort order for a column, use the sort-field flag to specify which column to sort","multiple":false,"options":["asc","desc"],"default":"desc"}},"args":[]},"templates":{"id":"templates","description":"Discover, install, or uninstall a new template into an existing Adobe Developer App Builder App","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":[],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[]},"templates:info":{"id":"templates:info","description":"List all App Builder templates that are installed","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":[],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"json":{"name":"json","type":"boolean","char":"j","description":"output raw json","allowNo":false},"yml":{"name":"yml","type":"boolean","char":"y","description":"output yml","allowNo":false,"exclusive":["json"]},"required-services":{"name":"required-services","type":"boolean","char":"s","description":"includes services required by a template in the output","allowNo":false}},"args":[]},"templates:install":{"id":"templates:install","description":"Install an Adobe Developer App Builder template","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:i"],"examples":["aio templates install https://github.com/org/repo","aio templates install git+https://github.com/org/repo","aio templates install ssh://github.com/org/repo","aio templates install git+ssh://github.com/org/repo","aio templates install file:../relative/path/to/template/folder","aio templates install file:/absolute/path/to/template/folder","aio templates install ../relative/path/to/template/folder","aio templates install /absolute/path/to/template/folder","aio templates install npm-package-name","aio templates install npm-package-name@tagOrVersion","aio templates install @scope/npm-package-name","aio templates install @scope/npm-package-name@tagOrVersion"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"yes":{"name":"yes","type":"boolean","char":"y","description":"Skip questions, and use all default values","allowNo":false},"install":{"name":"install","type":"boolean","description":"[default: true] Run npm installation after files are created","allowNo":true},"process-install-config":{"name":"process-install-config","type":"boolean","description":"[default: true] Process the template install.yml configuration file, defaults to true, to skip processing install.yml use --no-process-install-config","allowNo":true},"template-options":{"name":"template-options","type":"option","description":"Additional template options, as a base64-encoded json string","multiple":false}},"args":[{"name":"path","description":"path to the template (npm package name, file path, url). See examples","required":true}]},"templates:remove":{"id":"templates:remove","description":"Remove an Adobe Developer App Builder template from the Template Registry","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:rm"],"examples":["aio templates remove @adobe/app-builder-template"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[{"name":"name","description":"The name of the package implementing the template on npmjs.com","required":true}]},"templates:rollback":{"id":"templates:rollback","description":"Clears all installed templates","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":[],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"interactive":{"name":"interactive","type":"boolean","char":"i","description":"interactive clear mode","allowNo":false},"list":{"name":"list","type":"boolean","char":"l","description":"list templates that will be uninstalled","allowNo":false},"confirm":{"name":"confirm","type":"boolean","char":"c","description":"confirmation needed for clear (defaults to true)","allowNo":true}},"args":[]},"templates:submit":{"id":"templates:submit","description":"Submit an Adobe Developer App Builder template","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:sub"],"examples":["aio templates submit @adobe/app-builder-template https://github.com/adobe/app-builder-template"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[{"name":"name","description":"The name of the package implementing the template on npmjs.com","required":true},{"name":"githubRepoUrl","description":"A link to the Github repository containing the package's source code","required":true}]},"templates:uninstall":{"id":"templates:uninstall","description":"Uninstall an Adobe Developer App Builder template","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:un"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[{"name":"package-name","description":"package name of the template","required":true}]}}}
{"version":"1.3.1","commands":{"templates:discover":{"id":"templates:discover","description":"Discover App Builder templates to install","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:disco"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"interactive":{"name":"interactive","type":"boolean","char":"i","description":"interactive install mode","allowNo":false},"sort-field":{"name":"sort-field","type":"option","char":"f","description":"which column to sort, use the sort-order flag to specify sort direction","multiple":false,"options":["publishDate","names","adobeRecommended"],"default":"adobeRecommended"},"sort-order":{"name":"sort-order","type":"option","char":"o","description":"sort order for a column, use the sort-field flag to specify which column to sort","multiple":false,"options":["asc","desc"],"default":"desc"}},"args":[]},"templates":{"id":"templates","description":"Discover, install, or uninstall a new template into an existing Adobe Developer App Builder App","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":[],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[]},"templates:info":{"id":"templates:info","description":"List all App Builder templates that are installed","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":[],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"json":{"name":"json","type":"boolean","char":"j","description":"output raw json","allowNo":false},"yml":{"name":"yml","type":"boolean","char":"y","description":"output yml","allowNo":false,"exclusive":["json"]},"required-services":{"name":"required-services","type":"boolean","char":"s","description":"includes services required by a template in the output","allowNo":false}},"args":[]},"templates:install":{"id":"templates:install","description":"Install an Adobe Developer App Builder template","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:i"],"examples":["aio templates install https://github.com/org/repo","aio templates install git+https://github.com/org/repo","aio templates install ssh://github.com/org/repo","aio templates install git+ssh://github.com/org/repo","aio templates install file:../relative/path/to/template/folder","aio templates install file:/absolute/path/to/template/folder","aio templates install ../relative/path/to/template/folder","aio templates install /absolute/path/to/template/folder","aio templates install npm-package-name","aio templates install npm-package-name@tagOrVersion","aio templates install @scope/npm-package-name","aio templates install @scope/npm-package-name@tagOrVersion"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"yes":{"name":"yes","type":"boolean","char":"y","description":"Skip questions, and use all default values","allowNo":false},"install":{"name":"install","type":"boolean","description":"[default: true] Run npm installation after files are created","allowNo":true},"process-install-config":{"name":"process-install-config","type":"boolean","description":"[default: true] Process the template install.yml configuration file, defaults to true, to skip processing install.yml use --no-process-install-config","allowNo":true},"template-options":{"name":"template-options","type":"option","description":"Additional template options, as a base64-encoded json string","multiple":false}},"args":[{"name":"path","description":"path to the template (npm package name, file path, url). See examples","required":true}]},"templates:remove":{"id":"templates:remove","description":"Remove an Adobe Developer App Builder template from the Template Registry","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:rm"],"examples":["aio templates remove @adobe/app-builder-template"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[{"name":"name","description":"The name of the package implementing the template on npmjs.com","required":true}]},"templates:rollback":{"id":"templates:rollback","description":"Clears all installed templates","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":[],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false},"interactive":{"name":"interactive","type":"boolean","char":"i","description":"interactive clear mode","allowNo":false},"list":{"name":"list","type":"boolean","char":"l","description":"list templates that will be uninstalled","allowNo":false},"confirm":{"name":"confirm","type":"boolean","char":"c","description":"confirmation needed for clear (defaults to true)","allowNo":true}},"args":[]},"templates:submit":{"id":"templates:submit","description":"Submit an Adobe Developer App Builder template","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:sub"],"examples":["aio templates submit @adobe/app-builder-template https://github.com/adobe/app-builder-template"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[{"name":"name","description":"The name of the package implementing the template on npmjs.com","required":true},{"name":"githubRepoUrl","description":"A link to the Github repository containing the package's source code","required":true}]},"templates:uninstall":{"id":"templates:uninstall","description":"Uninstall an Adobe Developer App Builder template","strict":true,"pluginName":"@adobe/aio-cli-plugin-app-templates","pluginAlias":"@adobe/aio-cli-plugin-app-templates","pluginType":"core","aliases":["templates:un"],"flags":{"verbose":{"name":"verbose","type":"boolean","char":"v","description":"Verbose output","allowNo":false}},"args":[{"name":"package-name","description":"package name of the template","required":true}]}}}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/aio-cli-plugin-app-templates",
"version": "1.3.0",
"version": "1.3.1",
"description": "Discover, Install, Uninstall, Submit, and Remove Adobe App Builder templates",
"repository": {
"type": "git",
Expand Down
44 changes: 31 additions & 13 deletions src/lib/template-registry-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const templateRegistrySDK = require('@adobe/aio-lib-templates')
*/
async function getTemplates (searchCriteria, orderByCriteria) {
const templates = []
const templateRegistryClient = templateRegistrySDK.init()
const templateRegistryClient = templateRegistrySDK.init({
server: getTemplateRegistryConfig()
})
aioLogger.debug('Getting templates from Template Registry ...')
for await (const items of templateRegistryClient.getTemplates(searchCriteria, orderByCriteria)) {
templates.push(...items)
Expand All @@ -40,12 +42,12 @@ async function getTemplates (searchCriteria, orderByCriteria) {
* @returns {Promise<object>} A template data object added to Template Registry.
*/
async function addTemplate (accessToken, templateName, githubRepoUrl) {
const templateRegistryClient = templateRegistrySDK.init(
{
auth: {
token: accessToken
}
})
const templateRegistryClient = templateRegistrySDK.init({
server: getTemplateRegistryConfig(),
auth: {
token: accessToken
}
})
aioLogger.debug('Adding template to template registry...')
const template = await templateRegistryClient.addTemplate(templateName, githubRepoUrl)
return template
Expand All @@ -59,16 +61,32 @@ async function addTemplate (accessToken, templateName, githubRepoUrl) {
* @returns {Promise<undefined>}
*/
async function removeTemplate (accessToken, templateName) {
const templateRegistryClient = templateRegistrySDK.init(
{
auth: {
token: accessToken
}
})
const templateRegistryClient = templateRegistrySDK.init({
server: getTemplateRegistryConfig(),
auth: {
token: accessToken
}
})
aioLogger.debug('Removing template from template registry...')
await templateRegistryClient.deleteTemplate(templateName)
}

/**
* Checks TEMPLATE_REGISTRY_API_URL, TEMPLATE_REGISTRY_API_VERSION environment variables allowing overriding Template Registry API `url` and `version` config values.
*
* @returns {object} A config object containing optional `url`, `version` keys.
*/
function getTemplateRegistryConfig () {
const config = {}
if (process.env.TEMPLATE_REGISTRY_API_URL) {
config.url = process.env.TEMPLATE_REGISTRY_API_URL
}
if (process.env.TEMPLATE_REGISTRY_API_VERSION) {
config.version = process.env.TEMPLATE_REGISTRY_API_VERSION
}
return config
}

module.exports = {
getTemplates,
addTemplate,
Expand Down
33 changes: 33 additions & 0 deletions test/lib/template-registry-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const templateRegistrySDK = require('@adobe/aio-lib-templates')

jest.mock('@adobe/aio-lib-templates')

beforeEach(() => {
jest.clearAllMocks()
})

/**
* Emulates returning page results.
*
Expand All @@ -29,6 +33,11 @@ async function * createAsyncGenerator (pages) {
}

describe('Getting templates', () => {
afterEach(() => {
delete process.env.TEMPLATE_REGISTRY_API_URL
delete process.env.TEMPLATE_REGISTRY_API_VERSION
})

const template1 = { name: '@author1/app-builder-template1', latestVersion: '1.1.0', status: 'Approved', adobeRecommended: true }
const template2 = { name: '@author1/app-builder-template2', latestVersion: '3.1.0', status: 'Approved', adobeRecommended: true }
const template3 = { name: '@author2/app-builder-template1', latestVersion: '3.3.0', status: 'Approved', adobeRecommended: true }
Expand All @@ -55,6 +64,7 @@ describe('Getting templates', () => {
[template1, template2, template3, template4]
)
expect(mockClient.getTemplates).toHaveBeenCalledWith(searchCriteria, orderByCriteria)
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { } })
})

test('One chunk of templates', async () => {
Expand All @@ -68,6 +78,27 @@ describe('Getting templates', () => {
[template1, template2]
)
expect(mockClient.getTemplates).toHaveBeenCalledWith(searchCriteria, orderByCriteria)
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { } })
})

test('Overriding Template Registry API `url` and `version` config values', async () => {
const url = 'https://360030-templateregistryapi.adobeioruntime.net'
const version = 'v2'

process.env.TEMPLATE_REGISTRY_API_URL = url
process.env.TEMPLATE_REGISTRY_API_VERSION = version

const mockClient = {
getTemplates: jest.fn().mockReturnValue(createAsyncGenerator([
[template1, template2]
]))
}
templateRegistrySDK.init.mockReturnValue(mockClient)
await expect(getTemplates(searchCriteria, orderByCriteria)).resolves.toEqual(
[template1, template2]
)
expect(mockClient.getTemplates).toHaveBeenCalledWith(searchCriteria, orderByCriteria)
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { url, version } })
})
})

Expand All @@ -84,6 +115,7 @@ describe('Adding template', () => {
templateRegistrySDK.init.mockReturnValue(mockClient)
await expect(addTemplate(mockAccessToken, mockTemplateName, mockGithubRepoUrl)).resolves.toEqual(template)
expect(mockClient.addTemplate).toHaveBeenCalledWith(mockTemplateName, mockGithubRepoUrl)
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { }, auth: { token: mockAccessToken } })
})
})

Expand All @@ -99,5 +131,6 @@ describe('Removing template', () => {
templateRegistrySDK.init.mockReturnValue(mockClient)
await expect(removeTemplate(mockAccessToken, mockTemplateName)).resolves.not.toThrow()
expect(mockClient.deleteTemplate).toHaveBeenCalledWith(mockTemplateName)
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { }, auth: { token: mockAccessToken } })
})
})

0 comments on commit 3749322

Please sign in to comment.