Skip to content

Commit 3749322

Browse files
DEVX-2521: End-to-end testing for template registry - Add env vars to 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
1 parent 69921cb commit 3749322

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ DESCRIPTION
6969
Discover, install, or uninstall a new template into an existing Adobe Developer App Builder App
7070
```
7171

72-
_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)_
72+
_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)_
7373

7474
## `aio templates discover`
7575

oclif.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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}]}}}
1+
{"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}]}}}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adobe/aio-cli-plugin-app-templates",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Discover, Install, Uninstall, Submit, and Remove Adobe App Builder templates",
55
"repository": {
66
"type": "git",

src/lib/template-registry-helper.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const templateRegistrySDK = require('@adobe/aio-lib-templates')
2222
*/
2323
async function getTemplates (searchCriteria, orderByCriteria) {
2424
const templates = []
25-
const templateRegistryClient = templateRegistrySDK.init()
25+
const templateRegistryClient = templateRegistrySDK.init({
26+
server: getTemplateRegistryConfig()
27+
})
2628
aioLogger.debug('Getting templates from Template Registry ...')
2729
for await (const items of templateRegistryClient.getTemplates(searchCriteria, orderByCriteria)) {
2830
templates.push(...items)
@@ -40,12 +42,12 @@ async function getTemplates (searchCriteria, orderByCriteria) {
4042
* @returns {Promise<object>} A template data object added to Template Registry.
4143
*/
4244
async function addTemplate (accessToken, templateName, githubRepoUrl) {
43-
const templateRegistryClient = templateRegistrySDK.init(
44-
{
45-
auth: {
46-
token: accessToken
47-
}
48-
})
45+
const templateRegistryClient = templateRegistrySDK.init({
46+
server: getTemplateRegistryConfig(),
47+
auth: {
48+
token: accessToken
49+
}
50+
})
4951
aioLogger.debug('Adding template to template registry...')
5052
const template = await templateRegistryClient.addTemplate(templateName, githubRepoUrl)
5153
return template
@@ -59,16 +61,32 @@ async function addTemplate (accessToken, templateName, githubRepoUrl) {
5961
* @returns {Promise<undefined>}
6062
*/
6163
async function removeTemplate (accessToken, templateName) {
62-
const templateRegistryClient = templateRegistrySDK.init(
63-
{
64-
auth: {
65-
token: accessToken
66-
}
67-
})
64+
const templateRegistryClient = templateRegistrySDK.init({
65+
server: getTemplateRegistryConfig(),
66+
auth: {
67+
token: accessToken
68+
}
69+
})
6870
aioLogger.debug('Removing template from template registry...')
6971
await templateRegistryClient.deleteTemplate(templateName)
7072
}
7173

74+
/**
75+
* Checks TEMPLATE_REGISTRY_API_URL, TEMPLATE_REGISTRY_API_VERSION environment variables allowing overriding Template Registry API `url` and `version` config values.
76+
*
77+
* @returns {object} A config object containing optional `url`, `version` keys.
78+
*/
79+
function getTemplateRegistryConfig () {
80+
const config = {}
81+
if (process.env.TEMPLATE_REGISTRY_API_URL) {
82+
config.url = process.env.TEMPLATE_REGISTRY_API_URL
83+
}
84+
if (process.env.TEMPLATE_REGISTRY_API_VERSION) {
85+
config.version = process.env.TEMPLATE_REGISTRY_API_VERSION
86+
}
87+
return config
88+
}
89+
7290
module.exports = {
7391
getTemplates,
7492
addTemplate,

test/lib/template-registry-helper.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const templateRegistrySDK = require('@adobe/aio-lib-templates')
1515

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

18+
beforeEach(() => {
19+
jest.clearAllMocks()
20+
})
21+
1822
/**
1923
* Emulates returning page results.
2024
*
@@ -29,6 +33,11 @@ async function * createAsyncGenerator (pages) {
2933
}
3034

3135
describe('Getting templates', () => {
36+
afterEach(() => {
37+
delete process.env.TEMPLATE_REGISTRY_API_URL
38+
delete process.env.TEMPLATE_REGISTRY_API_VERSION
39+
})
40+
3241
const template1 = { name: '@author1/app-builder-template1', latestVersion: '1.1.0', status: 'Approved', adobeRecommended: true }
3342
const template2 = { name: '@author1/app-builder-template2', latestVersion: '3.1.0', status: 'Approved', adobeRecommended: true }
3443
const template3 = { name: '@author2/app-builder-template1', latestVersion: '3.3.0', status: 'Approved', adobeRecommended: true }
@@ -55,6 +64,7 @@ describe('Getting templates', () => {
5564
[template1, template2, template3, template4]
5665
)
5766
expect(mockClient.getTemplates).toHaveBeenCalledWith(searchCriteria, orderByCriteria)
67+
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { } })
5868
})
5969

6070
test('One chunk of templates', async () => {
@@ -68,6 +78,27 @@ describe('Getting templates', () => {
6878
[template1, template2]
6979
)
7080
expect(mockClient.getTemplates).toHaveBeenCalledWith(searchCriteria, orderByCriteria)
81+
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { } })
82+
})
83+
84+
test('Overriding Template Registry API `url` and `version` config values', async () => {
85+
const url = 'https://360030-templateregistryapi.adobeioruntime.net'
86+
const version = 'v2'
87+
88+
process.env.TEMPLATE_REGISTRY_API_URL = url
89+
process.env.TEMPLATE_REGISTRY_API_VERSION = version
90+
91+
const mockClient = {
92+
getTemplates: jest.fn().mockReturnValue(createAsyncGenerator([
93+
[template1, template2]
94+
]))
95+
}
96+
templateRegistrySDK.init.mockReturnValue(mockClient)
97+
await expect(getTemplates(searchCriteria, orderByCriteria)).resolves.toEqual(
98+
[template1, template2]
99+
)
100+
expect(mockClient.getTemplates).toHaveBeenCalledWith(searchCriteria, orderByCriteria)
101+
expect(templateRegistrySDK.init).toHaveBeenCalledWith({ server: { url, version } })
71102
})
72103
})
73104

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

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

0 commit comments

Comments
 (0)