Skip to content

Commit 708d4ab

Browse files
nicodecleyremartinlingstuyl
authored andcommitted
Refactors app commands to use getAppRegistrationByAppId util. Closes #5234
1 parent add4854 commit 708d4ab

18 files changed

+449
-804
lines changed

src/m365/app/commands/app-get.spec.ts

Lines changed: 13 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { session } from '../../../utils/session.js';
1111
import { sinonUtil } from '../../../utils/sinonUtil.js';
1212
import commands from '../commands.js';
1313
import command from './app-get.js';
14+
import { entraApp } from '../../../utils/entraApp.js';
1415

1516
describe(commands.GET, () => {
1617
let log: string[];
1718
let logger: Logger;
1819
let loggerLogSpy: sinon.SinonSpy;
19-
let loggerLogToStderrSpy: sinon.SinonSpy;
2020

2121
before(() => {
2222
sinon.stub(auth, 'restoreAuth').resolves();
@@ -49,12 +49,12 @@ describe(commands.GET, () => {
4949
}
5050
};
5151
loggerLogSpy = sinon.spy(logger, 'log');
52-
loggerLogToStderrSpy = sinon.spy(logger, 'logToStderr');
5352
});
5453

5554
afterEach(() => {
5655
sinonUtil.restore([
57-
request.get
56+
request.get,
57+
entraApp.getAppRegistrationByAppId
5858
]);
5959
});
6060

@@ -72,59 +72,30 @@ describe(commands.GET, () => {
7272
});
7373

7474
it('handles error when the app specified with the appId not found', async () => {
75-
sinon.stub(request, 'get').callsFake(async opts => {
76-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications?$filter=appId eq '9b1b1e42-794b-4c71-93ac-5ed92488b67f'&$select=id`) {
77-
return { value: [] };
78-
}
79-
80-
throw `Invalid request ${JSON.stringify(opts)}`;
81-
});
82-
83-
await assert.rejects(command.action(logger, {
84-
options: {
85-
appId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f'
86-
}
87-
}), new CommandError(`No Microsoft Entra application registration with ID 9b1b1e42-794b-4c71-93ac-5ed92488b67f found`));
88-
});
89-
90-
it('handles error when retrieving information about app through appId failed', async () => {
91-
sinon.stub(request, 'get').rejects(new Error('An error has occurred'));
75+
const error = `App with appId '9b1b1e42-794b-4c71-93ac-5ed92488b67f' not found in Microsoft Entra ID`;
76+
sinon.stub(entraApp, 'getAppRegistrationByAppId').rejects(new Error(error));
9277

9378
await assert.rejects(command.action(logger, {
9479
options: {
9580
appId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f'
9681
}
97-
}), new CommandError(`An error has occurred`));
82+
}), new CommandError(`App with appId '9b1b1e42-794b-4c71-93ac-5ed92488b67f' not found in Microsoft Entra ID`));
9883
});
9984

10085
it(`gets an Microsoft Entra app registration by its app (client) ID.`, async () => {
101-
sinon.stub(request, 'get').callsFake(async (opts) => {
102-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications?$filter=appId eq '9b1b1e42-794b-4c71-93ac-5ed92488b67f'&$select=id`) {
103-
return {
104-
value: [
105-
{
106-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
107-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
108-
"createdDateTime": "2019-10-29T17:46:55Z",
109-
"displayName": "My App",
110-
"description": null
111-
}
112-
]
113-
};
114-
}
115-
116-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
117-
return {
86+
const appResponse = {
87+
value: [
88+
{
11889
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
11990
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
12091
"createdDateTime": "2019-10-29T17:46:55Z",
12192
"displayName": "My App",
12293
"description": null
123-
};
124-
}
94+
}
95+
]
96+
};
12597

126-
throw 'Invalid request';
127-
});
98+
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
12899

129100
await command.action(logger, {
130101
options: {
@@ -136,43 +107,4 @@ describe(commands.GET, () => {
136107
assert.strictEqual(call.args[0].appId, '9b1b1e42-794b-4c71-93ac-5ed92488b67f');
137108
assert.strictEqual(call.args[0].displayName, 'My App');
138109
});
139-
140-
it(`shows underlying debug information in debug mode`, async () => {
141-
sinon.stub(request, 'get').callsFake(async (opts) => {
142-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications?$filter=appId eq '9b1b1e42-794b-4c71-93ac-5ed92488b67f'&$select=id`) {
143-
return {
144-
value: [
145-
{
146-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
147-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
148-
"createdDateTime": "2019-10-29T17:46:55Z",
149-
"displayName": "My App",
150-
"description": null
151-
}
152-
]
153-
};
154-
}
155-
156-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
157-
return {
158-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
159-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
160-
"createdDateTime": "2019-10-29T17:46:55Z",
161-
"displayName": "My App",
162-
"description": null
163-
};
164-
}
165-
166-
throw 'Invalid request';
167-
});
168-
169-
await command.action(logger, {
170-
options: {
171-
appId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f',
172-
debug: true
173-
}
174-
});
175-
const call: sinon.SinonSpyCall = loggerLogToStderrSpy.firstCall;
176-
assert(call.args[0].includes('Executing command entra app get with options'));
177-
});
178110
});

src/m365/app/commands/app-get.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { cli } from '../../../cli/cli.js';
21
import { Logger } from '../../../cli/Logger.js';
3-
import Command from '../../../Command.js';
4-
import entraAppGetCommand, { Options as EntraAppGetCommandOptions } from '../../entra/commands/app/app-get.js';
52
import AppCommand, { AppCommandArgs } from '../../base/AppCommand.js';
63
import commands from '../commands.js';
4+
import { entraApp } from '../../../utils/entraApp.js';
75

86
class AppGetCommand extends AppCommand {
97
public get name(): string {
@@ -15,20 +13,9 @@ class AppGetCommand extends AppCommand {
1513
}
1614

1715
public async commandAction(logger: Logger, args: AppCommandArgs): Promise<void> {
18-
const options: EntraAppGetCommandOptions = {
19-
appId: this.appId,
20-
output: 'json',
21-
debug: args.options.debug,
22-
verbose: args.options.verbose
23-
};
24-
2516
try {
26-
const appGetOutput = await cli.executeCommandWithOutput(entraAppGetCommand as Command, { options: { ...options, _: [] } });
27-
if (this.verbose) {
28-
await logger.logToStderr(appGetOutput.stderr);
29-
}
30-
31-
await logger.log(JSON.parse(appGetOutput.stdout));
17+
const app = await entraApp.getAppRegistrationByAppId(args.options.appId!);
18+
await logger.log(app);
3219
}
3320
catch (err: any) {
3421
this.handleRejectedODataJsonPromise(err);

0 commit comments

Comments
 (0)