-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlist.test.js
More file actions
176 lines (151 loc) · 5.98 KB
/
Copy pathlist.test.js
File metadata and controls
176 lines (151 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright 2026 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.
*/
const mockEnabledServices = [
{
name: 'Adobe Analytics',
code: 'AdobeAnalyticsSDK',
type: 'entp',
properties: {
roles: [{ id: 1, code: 'role1', name: null }],
licenseConfigs: [
{ id: 'lc1', name: 'Analytics all access', productId: 'P1' },
{ id: 'lc2', name: 'Analytics - Adobe IO DEMO', productId: 'P1' }
]
}
},
{
name: 'I/O Events',
code: 'CloudIntegrationSDK',
type: 'entp',
properties: { roles: null, licenseConfigs: null }
},
{
name: 'App Builder Data Services',
code: 'AppBuilderDataServicesSDK',
type: 'entp',
properties: null
}
]
const mockConsoleCLIInstance = {
getEnabledServicesForOrg: jest.fn().mockResolvedValue(mockEnabledServices),
checkDevTermsForOrg: jest.fn().mockResolvedValue(true),
getDevTermsForOrg: jest.fn().mockResolvedValue({ text: 'terms' }),
acceptDevTermsForOrg: jest.fn().mockResolvedValue(true),
prompt: { promptConfirm: jest.fn().mockResolvedValue(true) }
}
jest.mock('@adobe/aio-cli-lib-console', () => {
const mock = {
init: jest.fn().mockResolvedValue(mockConsoleCLIInstance),
cleanStdOut: jest.fn()
}
return mock
})
const TheCommand = require('../../../../src/commands/console/api/list')
describe('console:api:list', () => {
let command
beforeEach(() => {
command = new TheCommand([])
jest.clearAllMocks()
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue(mockEnabledServices)
})
afterEach(() => {
command = null
})
it('should list enabled services for the org', async () => {
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true)
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true)
try {
command.argv = ['--orgId', '12345']
const result = await command.run()
expect(mockConsoleCLIInstance.getEnabledServicesForOrg).toHaveBeenCalledWith('12345')
expect(result).toEqual(mockEnabledServices)
const stdout = stdoutSpy.mock.calls.map(args => args[0]).join('')
expect(stdout).toContain('AdobeAnalyticsSDK')
expect(stdout).toContain('Requires product profile: yes')
expect(stdout).toContain('CloudIntegrationSDK')
expect(stdout).toContain('AppBuilderDataServicesSDK')
} finally {
stdoutSpy.mockRestore()
stderrSpy.mockRestore()
}
})
it('should output JSON when --json is used', async () => {
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true)
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true)
try {
command.argv = ['--orgId', '12345', '--json']
await command.run()
const stdout = stdoutSpy.mock.calls.map(args => args[0]).join('')
let parsed
expect(() => { parsed = JSON.parse(stdout) }).not.toThrow()
expect(parsed).toEqual(mockEnabledServices)
} finally {
stdoutSpy.mockRestore()
stderrSpy.mockRestore()
}
})
it('should output YAML when --yml is used', async () => {
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true)
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true)
try {
command.argv = ['--orgId', '12345', '--yml']
await command.run()
const stdout = stdoutSpy.mock.calls.map(args => args[0]).join('')
expect(stdout).toContain('AdobeAnalyticsSDK')
} finally {
stdoutSpy.mockRestore()
stderrSpy.mockRestore()
}
})
it('should handle empty services list', async () => {
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true)
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true)
try {
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue([])
command.argv = ['--orgId', '12345']
const result = await command.run()
expect(result).toEqual([])
const stdout = stdoutSpy.mock.calls.map(args => args[0]).join('')
expect(stdout).toContain('No enabled API services found')
} finally {
stdoutSpy.mockRestore()
stderrSpy.mockRestore()
}
})
it('should use config org.id if orgId flag is not provided', async () => {
command.argv = []
command.getConfig = jest.fn().mockImplementation(key => {
if (key === 'org.id') return '0987654321'
return null
})
await command.run()
expect(mockConsoleCLIInstance.getEnabledServicesForOrg).toHaveBeenCalledWith('0987654321')
})
it('should error if orgId is not provided and no config', async () => {
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true)
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true)
try {
command.argv = []
command.getConfig = jest.fn().mockReturnValue(null)
await expect(command.run()).rejects.toThrow()
const combined = stdoutSpy.mock.calls.map(a => a[0]).join('') + stderrSpy.mock.calls.map(a => a[0]).join('')
expect(combined).toContain('You have not selected an Organization. Please select one first.')
} finally {
stdoutSpy.mockRestore()
stderrSpy.mockRestore()
}
})
it('should handle SDK errors', async () => {
mockConsoleCLIInstance.getEnabledServicesForOrg.mockRejectedValue(new Error('API failure'))
command.argv = ['--orgId', '12345']
await expect(command.run()).rejects.toThrow('API failure')
})
})