|
| 1 | +import assert from 'assert'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import auth from '../../../../Auth.js'; |
| 4 | +import { CommandError } from '../../../../Command.js'; |
| 5 | +import { CommandInfo } from '../../../../cli/CommandInfo.js'; |
| 6 | +import { Logger } from '../../../../cli/Logger.js'; |
| 7 | +import { cli } from '../../../../cli/cli.js'; |
| 8 | +import request from '../../../../request.js'; |
| 9 | +import { telemetry } from '../../../../telemetry.js'; |
| 10 | +import { accessToken } from '../../../../utils/accessToken.js'; |
| 11 | +import { pid } from '../../../../utils/pid.js'; |
| 12 | +import { session } from '../../../../utils/session.js'; |
| 13 | +import { sinonUtil } from '../../../../utils/sinonUtil.js'; |
| 14 | +import commands from '../../commands.js'; |
| 15 | +import command, { options } from './calendargroup-get.js'; |
| 16 | + |
| 17 | +describe(commands.CALENDARGROUP_GET, () => { |
| 18 | + const calendarGroupId = 'AAMkAGE0MGM1Y2M5LWEzMmUtNGVlNy05MjRlLTk0YmYyY2I5NTM3ZAAuAAAAAAC_0WfqSjt_SqLtNkuO-bj1AQAbfYq5lmBxQ6a4t1fGbeYAAAAAAEOAAA='; |
| 19 | + const calendarGroupName = 'Personal Events'; |
| 20 | + const resolvedCalendarGroupId = 'AAMkAGE0MGM1Y2M5LWEzMmUtNGVlNy05MjRlLTk0YmYyY2I5NTM3ZAAuAAAAAAC_0WfqSjt_SqLtNkuO-bj1AQAbfYq5lmBxQ6a4t1fGbeYAAAAAAEPAAA='; |
| 21 | + const otherUserId = '44288f7d-7710-4293-8c8e-36f310ed2e6a'; |
| 22 | + const userId = 'b743445a-112c-4fda-9afd-05943f9c7b36'; |
| 23 | + const userName = 'john.doe@contoso.com'; |
| 24 | + const currentUserId = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; |
| 25 | + const currentUserName = 'current.user@contoso.com'; |
| 26 | + |
| 27 | + const calendarGroupResponse = { |
| 28 | + id: calendarGroupId, |
| 29 | + name: 'My Calendars', |
| 30 | + changeKey: 'nfZyf7VcrEKLNoU37KWlkQAAA0x0+w==', |
| 31 | + classId: '0006f0b7-0000-0000-c000-000000000046' |
| 32 | + }; |
| 33 | + |
| 34 | + const calendarGroupsResponseForFilter = { |
| 35 | + value: [ |
| 36 | + { |
| 37 | + id: resolvedCalendarGroupId, |
| 38 | + name: calendarGroupName |
| 39 | + } |
| 40 | + ] |
| 41 | + }; |
| 42 | + |
| 43 | + let logger: Logger; |
| 44 | + let commandInfo: CommandInfo; |
| 45 | + let loggerLogSpy: sinon.SinonSpy; |
| 46 | + let commandOptionsSchema: typeof options; |
| 47 | + |
| 48 | + before(() => { |
| 49 | + sinon.stub(auth, 'restoreAuth').resolves(); |
| 50 | + sinon.stub(telemetry, 'trackEvent').resolves(); |
| 51 | + sinon.stub(pid, 'getProcessName').returns(''); |
| 52 | + sinon.stub(session, 'getId').returns(''); |
| 53 | + |
| 54 | + auth.connection.active = true; |
| 55 | + if (!auth.connection.accessTokens[auth.defaultResource]) { |
| 56 | + auth.connection.accessTokens[auth.defaultResource] = { |
| 57 | + expiresOn: 'abc', |
| 58 | + accessToken: 'abc' |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + commandInfo = cli.getCommandInfo(command); |
| 63 | + commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options; |
| 64 | + }); |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + logger = { |
| 68 | + log: async () => undefined, |
| 69 | + logRaw: async () => undefined, |
| 70 | + logToStderr: async () => undefined |
| 71 | + }; |
| 72 | + |
| 73 | + loggerLogSpy = sinon.spy(logger, 'log'); |
| 74 | + |
| 75 | + sinon.stub(accessToken, 'isAppOnlyAccessToken').returns(false); |
| 76 | + sinon.stub(accessToken, 'getScopesFromAccessToken').returns([]); |
| 77 | + sinon.stub(accessToken, 'getUserIdFromAccessToken').returns(currentUserId); |
| 78 | + sinon.stub(accessToken, 'getUserNameFromAccessToken').returns(currentUserName); |
| 79 | + }); |
| 80 | + |
| 81 | + afterEach(() => { |
| 82 | + sinonUtil.restore([ |
| 83 | + accessToken.isAppOnlyAccessToken, |
| 84 | + accessToken.getScopesFromAccessToken, |
| 85 | + accessToken.getUserIdFromAccessToken, |
| 86 | + accessToken.getUserNameFromAccessToken, |
| 87 | + request.get |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + after(() => { |
| 92 | + sinon.restore(); |
| 93 | + auth.connection.active = false; |
| 94 | + }); |
| 95 | + |
| 96 | + it('has correct name', () => { |
| 97 | + assert.strictEqual(command.name, commands.CALENDARGROUP_GET); |
| 98 | + }); |
| 99 | + |
| 100 | + it('has a description', () => { |
| 101 | + assert.notStrictEqual(command.description, null); |
| 102 | + }); |
| 103 | + |
| 104 | + it('defines correct properties for the default output', () => { |
| 105 | + assert.deepStrictEqual(command.defaultProperties(), ['id', 'name']); |
| 106 | + }); |
| 107 | + |
| 108 | + it('passes validation with id', () => { |
| 109 | + const actual = commandOptionsSchema.safeParse({ id: calendarGroupId }); |
| 110 | + assert.strictEqual(actual.success, true); |
| 111 | + }); |
| 112 | + |
| 113 | + it('passes validation with name', () => { |
| 114 | + const actual = commandOptionsSchema.safeParse({ name: calendarGroupName }); |
| 115 | + assert.strictEqual(actual.success, true); |
| 116 | + }); |
| 117 | + |
| 118 | + it('fails validation if both id and name are specified', () => { |
| 119 | + const actual = commandOptionsSchema.safeParse({ id: calendarGroupId, name: calendarGroupName }); |
| 120 | + assert.notStrictEqual(actual.success, true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('fails validation if neither id nor name is specified', () => { |
| 124 | + const actual = commandOptionsSchema.safeParse({}); |
| 125 | + assert.notStrictEqual(actual.success, true); |
| 126 | + }); |
| 127 | + |
| 128 | + it('fails validation if id is empty', () => { |
| 129 | + const actual = commandOptionsSchema.safeParse({ id: '' }); |
| 130 | + assert.notStrictEqual(actual.success, true); |
| 131 | + }); |
| 132 | + |
| 133 | + it('fails validation if name is empty', () => { |
| 134 | + const actual = commandOptionsSchema.safeParse({ name: '' }); |
| 135 | + assert.notStrictEqual(actual.success, true); |
| 136 | + }); |
| 137 | + |
| 138 | + it('fails validation if userId is not a valid GUID', () => { |
| 139 | + const actual = commandOptionsSchema.safeParse({ id: calendarGroupId, userId: 'foo' }); |
| 140 | + assert.notStrictEqual(actual.success, true); |
| 141 | + }); |
| 142 | + |
| 143 | + it('fails validation if userName is not a valid UPN', () => { |
| 144 | + const actual = commandOptionsSchema.safeParse({ id: calendarGroupId, userName: 'foo' }); |
| 145 | + assert.notStrictEqual(actual.success, true); |
| 146 | + }); |
| 147 | + |
| 148 | + it('fails validation if both userId and userName are specified', () => { |
| 149 | + const actual = commandOptionsSchema.safeParse({ id: calendarGroupId, userId: userId, userName: userName }); |
| 150 | + assert.notStrictEqual(actual.success, true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('fails validation with unknown options', () => { |
| 154 | + const actual = commandOptionsSchema.safeParse({ id: calendarGroupId, unknownOption: 'value' }); |
| 155 | + assert.notStrictEqual(actual.success, true); |
| 156 | + }); |
| 157 | + |
| 158 | + it('retrieves calendar group for the signed-in user by id using delegated permissions', async () => { |
| 159 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 160 | + if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups/${calendarGroupId}`) { |
| 161 | + return calendarGroupResponse; |
| 162 | + } |
| 163 | + |
| 164 | + throw 'Invalid request'; |
| 165 | + }); |
| 166 | + |
| 167 | + await command.action(logger, { options: commandOptionsSchema.parse({ id: calendarGroupId }) }); |
| 168 | + assert(loggerLogSpy.calledOnceWith(calendarGroupResponse)); |
| 169 | + }); |
| 170 | + |
| 171 | + it('retrieves calendar group for the signed-in user by name using delegated permissions', async () => { |
| 172 | + const expectedFilterUrl = `https://graph.microsoft.com/v1.0/me/calendarGroups?$select=id,name&$filter=name eq 'Personal%20Events'`; |
| 173 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 174 | + if (opts.url === expectedFilterUrl) { |
| 175 | + return calendarGroupsResponseForFilter; |
| 176 | + } |
| 177 | + |
| 178 | + if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups/${resolvedCalendarGroupId}`) { |
| 179 | + return calendarGroupResponse; |
| 180 | + } |
| 181 | + |
| 182 | + throw 'Invalid request'; |
| 183 | + }); |
| 184 | + |
| 185 | + await command.action(logger, { options: commandOptionsSchema.parse({ name: calendarGroupName }) }); |
| 186 | + assert(loggerLogSpy.calledOnceWith(calendarGroupResponse)); |
| 187 | + }); |
| 188 | + |
| 189 | + it('retrieves calendar group for a user specified by id using app-only permissions', async () => { |
| 190 | + sinonUtil.restore(accessToken.isAppOnlyAccessToken); |
| 191 | + sinon.stub(accessToken, 'isAppOnlyAccessToken').returns(true); |
| 192 | + |
| 193 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 194 | + if (opts.url === `https://graph.microsoft.com/v1.0/users('${userId}')/calendarGroups/${calendarGroupId}`) { |
| 195 | + return calendarGroupResponse; |
| 196 | + } |
| 197 | + |
| 198 | + throw 'Invalid request'; |
| 199 | + }); |
| 200 | + |
| 201 | + await command.action(logger, { options: commandOptionsSchema.parse({ id: calendarGroupId, userId }) }); |
| 202 | + assert(loggerLogSpy.calledOnceWith(calendarGroupResponse)); |
| 203 | + }); |
| 204 | + |
| 205 | + it('throws error when running with app-only permissions without userId or userName', async () => { |
| 206 | + sinonUtil.restore(accessToken.isAppOnlyAccessToken); |
| 207 | + sinon.stub(accessToken, 'isAppOnlyAccessToken').returns(true); |
| 208 | + |
| 209 | + await assert.rejects( |
| 210 | + command.action(logger, { options: commandOptionsSchema.parse({ id: calendarGroupId }) }), |
| 211 | + new CommandError('When running with application permissions either userId or userName is required.') |
| 212 | + ); |
| 213 | + }); |
| 214 | + |
| 215 | + it('throws error when using delegated permissions for other users without shared scope', async () => { |
| 216 | + await assert.rejects( |
| 217 | + command.action(logger, { options: commandOptionsSchema.parse({ id: calendarGroupId, userId: otherUserId }) }), |
| 218 | + new CommandError(`To retrieve calendar groups of other users, the Entra ID application used for authentication must have either the Calendars.Read.Shared or Calendars.ReadWrite.Shared delegated permission assigned.`) |
| 219 | + ); |
| 220 | + }); |
| 221 | + |
| 222 | + it('retrieves calendar group for a user specified by id using delegated permissions with shared scope', async () => { |
| 223 | + sinonUtil.restore(accessToken.getScopesFromAccessToken); |
| 224 | + sinon.stub(accessToken, 'getScopesFromAccessToken').returns(['Calendars.Read.Shared']); |
| 225 | + |
| 226 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 227 | + if (opts.url === `https://graph.microsoft.com/v1.0/users('${otherUserId}')/calendarGroups/${calendarGroupId}`) { |
| 228 | + return calendarGroupResponse; |
| 229 | + } |
| 230 | + |
| 231 | + throw 'Invalid request'; |
| 232 | + }); |
| 233 | + |
| 234 | + await command.action(logger, { options: commandOptionsSchema.parse({ id: calendarGroupId, userId: otherUserId }) }); |
| 235 | + assert(loggerLogSpy.calledOnceWith(calendarGroupResponse)); |
| 236 | + }); |
| 237 | +}); |
| 238 | + |
0 commit comments