Skip to content

Commit e63aaba

Browse files
Enhances outlook calendargroup get command with additional test cases and improved handling of calendar group retrieval by name and user. Includes error handling for non-existent calendar groups. Closes #7111
1 parent 3da5852 commit e63aaba

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

src/m365/outlook/commands/calendargroup/calendargroup-get.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,80 @@ describe(commands.CALENDARGROUP_GET, () => {
234234
await command.action(logger, { options: commandOptionsSchema.parse({ id: calendarGroupId, userId: otherUserId }) });
235235
assert(loggerLogSpy.calledOnceWith(calendarGroupResponse));
236236
});
237+
238+
it('retrieves calendar group for a user specified by name using delegated permissions with shared scope', async () => {
239+
sinonUtil.restore(accessToken.getScopesFromAccessToken);
240+
sinon.stub(accessToken, 'getScopesFromAccessToken').returns(['Calendars.Read.Shared']);
241+
242+
const expectedFilterUrl = `https://graph.microsoft.com/v1.0/users('${otherUserId}')/calendarGroups?$select=id,name&$filter=name eq 'Personal%20Events'`;
243+
sinon.stub(request, 'get').callsFake(async (opts) => {
244+
if (opts.url === expectedFilterUrl) {
245+
return calendarGroupsResponseForFilter;
246+
}
247+
248+
if (opts.url === `https://graph.microsoft.com/v1.0/users('${otherUserId}')/calendarGroups/${resolvedCalendarGroupId}`) {
249+
return calendarGroupResponse;
250+
}
251+
252+
throw 'Invalid request';
253+
});
254+
255+
await command.action(logger, { options: commandOptionsSchema.parse({ name: calendarGroupName, userId: otherUserId }) });
256+
assert(loggerLogSpy.calledOnceWith(calendarGroupResponse));
257+
});
258+
259+
it('retrieves calendar group for the signed-in user with verbose output', async () => {
260+
const logToStderrSpy = sinon.spy(logger, 'logToStderr');
261+
262+
sinon.stub(request, 'get').callsFake(async (opts) => {
263+
if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups/${calendarGroupId}`) {
264+
return calendarGroupResponse;
265+
}
266+
267+
throw 'Invalid request';
268+
});
269+
270+
await command.action(logger, { options: commandOptionsSchema.parse({ id: calendarGroupId, verbose: true }) });
271+
272+
assert(loggerLogSpy.calledOnceWith(calendarGroupResponse));
273+
assert(logToStderrSpy.calledOnce);
274+
});
275+
276+
it('throws an error when calendar group name does not match any results', async () => {
277+
const expectedFilterUrl = `https://graph.microsoft.com/v1.0/me/calendarGroups?$select=id,name&$filter=name eq 'Personal%20Events'`;
278+
sinon.stub(request, 'get').callsFake(async (opts) => {
279+
if (opts.url === expectedFilterUrl) {
280+
return { value: [] };
281+
}
282+
283+
throw 'Invalid request';
284+
});
285+
286+
await assert.rejects(
287+
command.action(logger, { options: commandOptionsSchema.parse({ name: calendarGroupName }) }),
288+
new CommandError(`The specified calendar group '${calendarGroupName}' does not exist.`)
289+
);
290+
});
291+
292+
it('retrieves calendar group for a user specified by userName using delegated permissions with shared scope (ReadWrite.Shared)', async () => {
293+
sinonUtil.restore(accessToken.getScopesFromAccessToken);
294+
sinon.stub(accessToken, 'getScopesFromAccessToken').returns(['Calendars.ReadWrite.Shared']);
295+
296+
const expectedFilterUrl = `https://graph.microsoft.com/v1.0/users('${userName}')/calendarGroups?$select=id,name&$filter=name eq 'Personal%20Events'`;
297+
sinon.stub(request, 'get').callsFake(async (opts) => {
298+
if (opts.url === expectedFilterUrl) {
299+
return calendarGroupsResponseForFilter;
300+
}
301+
302+
if (opts.url === `https://graph.microsoft.com/v1.0/users('${userName}')/calendarGroups/${resolvedCalendarGroupId}`) {
303+
return calendarGroupResponse;
304+
}
305+
306+
throw 'Invalid request';
307+
});
308+
309+
await command.action(logger, { options: commandOptionsSchema.parse({ name: calendarGroupName, userName }) });
310+
assert(loggerLogSpy.calledOnceWith(calendarGroupResponse));
311+
});
237312
});
238313

src/m365/outlook/commands/calendargroup/calendargroup-get.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,15 @@ class OutlookCalendarGroupGetCommand extends GraphCommand {
107107
return calendarGroups[0].id!;
108108
};
109109

110-
const calendarGroupId = args.options.id ?? (
111-
args.options.name ? await getCalendarGroupId(args.options.name) : undefined
112-
);
110+
// Schema guarantees exactly one of `id` or `name` is present,
111+
// so avoid ternaries/undefined paths to keep coverage deterministic.
112+
let calendarGroupId: string;
113+
if (args.options.id) {
114+
calendarGroupId = args.options.id;
115+
}
116+
else {
117+
calendarGroupId = await getCalendarGroupId(args.options.name!);
118+
}
113119

114120
// For delegated access without userId/userName: use /me.
115121
const userPath = userIdentifier ? `users('${userIdentifier}')` : 'me';

0 commit comments

Comments
 (0)