Skip to content

Commit 4f041df

Browse files
committed
Refactor app commands to use getAppRegistrationByAppName util
1 parent 9d7d19b commit 4f041df

22 files changed

+914
-2643
lines changed

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

Lines changed: 20 additions & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ describe(commands.APP_GET, () => {
2626
const appResponse = {
2727
value: [
2828
{
29-
"id": "340a4aa3-1af6-43ac-87d8-189819003952"
29+
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
30+
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
31+
"displayName": "My App"
3032
}
3133
]
3234
};
@@ -64,8 +66,9 @@ describe(commands.APP_GET, () => {
6466
fs.readFileSync,
6567
fs.writeFileSync,
6668
cli.getSettingWithDefaultValue,
67-
cli.handleMultipleResultsFound,
68-
entraApp.getAppRegistrationByAppId
69+
entraApp.getAppRegistrationByAppId,
70+
entraApp.getAppRegistrationByAppName,
71+
entraApp.getAppRegistrationByObjectId
6972
]);
7073
});
7174

@@ -94,90 +97,25 @@ describe(commands.APP_GET, () => {
9497
});
9598

9699
it('handles error when the app with the specified the name not found', async () => {
97-
sinon.stub(request, 'get').callsFake(async opts => {
98-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications?$filter=displayName eq 'My%20app'&$select=id`) {
99-
return { value: [] };
100-
}
101-
102-
throw `Invalid request ${JSON.stringify(opts)}`;
103-
});
100+
const error = `App with name 'My app' not found in Microsoft Entra ID`;
101+
sinon.stub(entraApp, 'getAppRegistrationByAppName').rejects(new Error(error));
104102

105103
await assert.rejects(command.action(logger, {
106104
options: {
107105
name: 'My app'
108106
}
109-
}), new CommandError(`No Microsoft Entra application registration with name My app found`));
107+
}), new CommandError(`App with name 'My app' not found in Microsoft Entra ID`));
110108
});
111109

112110
it('handles error when multiple apps with the specified name found', async () => {
113-
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
114-
if (settingName === settingsNames.prompt) {
115-
return false;
116-
}
117-
118-
return defaultValue;
119-
});
120-
121-
sinon.stub(request, 'get').callsFake(async opts => {
122-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications?$filter=displayName eq 'My%20app'&$select=id`) {
123-
return {
124-
value: [
125-
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
126-
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
127-
]
128-
};
129-
}
130-
131-
throw `Invalid request ${JSON.stringify(opts)}`;
132-
});
111+
const error = `Multiple apps with name 'My app' found in Microsoft Entra ID. Found: 9b1b1e42-794b-4c71-93ac-5ed92488b67f, 9b1b1e42-794b-4c71-93ac-5ed92488b67g.`;
112+
sinon.stub(entraApp, 'getAppRegistrationByAppName').rejects(new Error(error));
133113

134114
await assert.rejects(command.action(logger, {
135115
options: {
136116
name: 'My app'
137117
}
138-
}), new CommandError(`Multiple Microsoft Entra application registrations with name 'My app' found. Found: 9b1b1e42-794b-4c71-93ac-5ed92488b67f, 9b1b1e42-794b-4c71-93ac-5ed92488b67g.`));
139-
});
140-
141-
it('handles selecting single result when multiple apps with the specified name found and cli is set to prompt', async () => {
142-
sinon.stub(request, 'get').callsFake(async opts => {
143-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications?$filter=displayName eq 'My%20App'&$select=id`) {
144-
return {
145-
value: [
146-
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
147-
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
148-
]
149-
};
150-
}
151-
152-
if (opts.url === 'https://graph.microsoft.com/v1.0/myorganization/applications/9b1b1e42-794b-4c71-93ac-5ed92488b67f') {
153-
return {
154-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
155-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
156-
"createdDateTime": "2019-10-29T17:46:55Z",
157-
"displayName": "My App",
158-
"description": null
159-
};
160-
}
161-
162-
throw `Invalid request ${JSON.stringify(opts)}`;
163-
});
164-
165-
sinon.stub(cli, 'handleMultipleResultsFound').resolves({ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' });
166-
167-
await command.action(logger, {
168-
options: {
169-
name: 'My App',
170-
debug: true
171-
}
172-
});
173-
const call: sinon.SinonSpyCall = loggerLogSpy.lastCall;
174-
assert.deepEqual(call.args[0], {
175-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
176-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
177-
"createdDateTime": "2019-10-29T17:46:55Z",
178-
"displayName": "My App",
179-
"description": null
180-
});
118+
}), new CommandError(error));
181119
});
182120

183121
it('handles error when retrieving information about app through name failed', async () => {
@@ -270,23 +208,13 @@ describe(commands.APP_GET, () => {
270208
it(`should get an Microsoft Entra app registration by its app (client) ID. Doesn't save the app info if not requested`, async () => {
271209
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
272210

273-
sinon.stub(request, 'get').callsFake(async (opts) => {
274-
if (opts.url === "https://graph.microsoft.com/v1.0/myorganization/applications/340a4aa3-1af6-43ac-87d8-189819003952?$select=id,appId,displayName") {
275-
return {
276-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
277-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
278-
"displayName": "My App"
279-
};
280-
}
281-
282-
throw 'Invalid request';
283-
});
284211
const fsWriteFileSyncSpy = sinon.spy(fs, 'writeFileSync');
285212

286213
await command.action(logger, {
287214
options: {
288215
appId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f',
289-
properties: 'id,appId,displayName'
216+
properties: 'id,appId,displayName',
217+
verbose: true
290218
}
291219
});
292220
const call: sinon.SinonSpyCall = loggerLogSpy.lastCall;
@@ -297,38 +225,13 @@ describe(commands.APP_GET, () => {
297225
});
298226

299227
it(`should get an Microsoft Entra app registration by its name. Doesn't save the app info if not requested`, async () => {
300-
sinon.stub(request, 'get').callsFake(async (opts) => {
301-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications?$filter=displayName eq 'My%20App'&$select=id`) {
302-
return {
303-
value: [
304-
{
305-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
306-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
307-
"createdDateTime": "2019-10-29T17:46:55Z",
308-
"displayName": "My App",
309-
"description": null
310-
}
311-
]
312-
};
313-
}
314-
315-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
316-
return {
317-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
318-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
319-
"createdDateTime": "2019-10-29T17:46:55Z",
320-
"displayName": "My App",
321-
"description": null
322-
};
323-
}
324-
325-
throw 'Invalid request';
326-
});
228+
sinon.stub(entraApp, 'getAppRegistrationByAppName').resolves(appResponse.value[0]);
327229
const fsWriteFileSyncSpy = sinon.spy(fs, 'writeFileSync');
328230

329231
await command.action(logger, {
330232
options: {
331-
name: 'My App'
233+
name: 'My App',
234+
verbose: true
332235
}
333236
});
334237
const call: sinon.SinonSpyCall = loggerLogSpy.lastCall;
@@ -339,24 +242,14 @@ describe(commands.APP_GET, () => {
339242
});
340243

341244
it(`should get an Microsoft Entra app registration by its object ID. Doesn't save the app info if not requested`, async () => {
342-
sinon.stub(request, 'get').callsFake(async (opts) => {
343-
if (opts.url === `https://graph.microsoft.com/v1.0/myorganization/applications/340a4aa3-1af6-43ac-87d8-189819003952?$select=id,appId,displayName`) {
344-
return {
345-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
346-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
347-
"createdDateTime": "2019-10-29T17:46:55Z",
348-
"displayName": "My App",
349-
"description": null
350-
};
351-
}
352-
throw 'Invalid request';
353-
});
245+
sinon.stub(entraApp, 'getAppRegistrationByObjectId').resolves(appResponse.value[0]);
354246
const fsWriteFileSyncSpy = sinon.spy(fs, 'writeFileSync');
355247

356248
await command.action(logger, {
357249
options: {
358250
objectId: '340a4aa3-1af6-43ac-87d8-189819003952',
359-
properties: 'id,appId,displayName'
251+
properties: 'id,appId,displayName',
252+
verbose: true
360253
}
361254
});
362255
const call: sinon.SinonSpyCall = loggerLogSpy.lastCall;
@@ -372,19 +265,6 @@ describe(commands.APP_GET, () => {
372265

373266
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
374267

375-
sinon.stub(request, 'get').callsFake(async (opts) => {
376-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
377-
return {
378-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
379-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
380-
"createdDateTime": "2019-10-29T17:46:55Z",
381-
"displayName": "My App",
382-
"description": null
383-
};
384-
}
385-
386-
throw 'Invalid request';
387-
});
388268
sinon.stub(fs, 'existsSync').returns(false);
389269
sinon.stub(fs, 'writeFileSync').callsFake((_, contents) => {
390270
filePath = _.toString();
@@ -416,19 +296,6 @@ describe(commands.APP_GET, () => {
416296

417297
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
418298

419-
sinon.stub(request, 'get').callsFake(async (opts) => {
420-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
421-
return {
422-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
423-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
424-
"createdDateTime": "2019-10-29T17:46:55Z",
425-
"displayName": "My App",
426-
"description": null
427-
};
428-
}
429-
430-
throw 'Invalid request';
431-
});
432299
sinon.stub(fs, 'existsSync').returns(true);
433300
sinon.stub(fs, 'readFileSync').returns('');
434301
sinon.stub(fs, 'writeFileSync').callsFake((_, contents) => {
@@ -460,20 +327,6 @@ describe(commands.APP_GET, () => {
460327
let filePath: string | undefined;
461328

462329
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
463-
464-
sinon.stub(request, 'get').callsFake(async (opts) => {
465-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
466-
return {
467-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
468-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
469-
"createdDateTime": "2019-10-29T17:46:55Z",
470-
"displayName": "My App",
471-
"description": null
472-
};
473-
}
474-
475-
throw 'Invalid request';
476-
});
477330
sinon.stub(fs, 'existsSync').returns(true);
478331
sinon.stub(fs, 'readFileSync').returns(JSON.stringify({
479332
"apps": [
@@ -517,20 +370,6 @@ describe(commands.APP_GET, () => {
517370
let filePath: string | undefined;
518371

519372
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
520-
521-
sinon.stub(request, 'get').callsFake(async (opts) => {
522-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
523-
return {
524-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
525-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
526-
"createdDateTime": "2019-10-29T17:46:55Z",
527-
"displayName": "My App",
528-
"description": null
529-
};
530-
}
531-
532-
throw 'Invalid request';
533-
});
534373
sinon.stub(fs, 'existsSync').returns(true);
535374
sinon.stub(fs, 'readFileSync').returns(JSON.stringify({
536375
"apps": [
@@ -572,20 +411,6 @@ describe(commands.APP_GET, () => {
572411

573412
it(`doesn't save app info in the .m365rc.json file when there was error reading file contents`, async () => {
574413
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
575-
576-
sinon.stub(request, 'get').callsFake(async (opts) => {
577-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
578-
return {
579-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
580-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
581-
"createdDateTime": "2019-10-29T17:46:55Z",
582-
"displayName": "My App",
583-
"description": null
584-
};
585-
}
586-
587-
throw 'Invalid request';
588-
});
589414
sinon.stub(fs, 'existsSync').returns(true);
590415
sinon.stub(fs, 'readFileSync').throws(new Error('An error has occurred'));
591416
const fsWriteFileSyncSpy = sinon.spy(fs, 'writeFileSync');
@@ -601,20 +426,6 @@ describe(commands.APP_GET, () => {
601426

602427
it(`doesn't save app info in the .m365rc.json file when file has invalid JSON`, async () => {
603428
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
604-
605-
sinon.stub(request, 'get').callsFake(async (opts) => {
606-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
607-
return {
608-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
609-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
610-
"createdDateTime": "2019-10-29T17:46:55Z",
611-
"displayName": "My App",
612-
"description": null
613-
};
614-
}
615-
616-
throw 'Invalid request';
617-
});
618429
sinon.stub(fs, 'existsSync').returns(true);
619430
sinon.stub(fs, 'readFileSync').returns('{');
620431
const fsWriteFileSyncSpy = sinon.spy(fs, 'writeFileSync');
@@ -630,20 +441,6 @@ describe(commands.APP_GET, () => {
630441

631442
it(`doesn't fail execution when error occurred while saving app info`, async () => {
632443
sinon.stub(entraApp, 'getAppRegistrationByAppId').resolves(appResponse.value[0]);
633-
634-
sinon.stub(request, 'get').callsFake(async (opts) => {
635-
if ((opts.url as string).indexOf('/v1.0/myorganization/applications/') > -1) {
636-
return {
637-
"id": "340a4aa3-1af6-43ac-87d8-189819003952",
638-
"appId": "9b1b1e42-794b-4c71-93ac-5ed92488b67f",
639-
"createdDateTime": "2019-10-29T17:46:55Z",
640-
"displayName": "My App",
641-
"description": null
642-
};
643-
}
644-
645-
throw 'Invalid request';
646-
});
647444
sinon.stub(fs, 'existsSync').returns(false);
648445
sinon.stub(fs, 'writeFileSync').throws(new Error('Error occurred while saving app info'));
649446

0 commit comments

Comments
 (0)