|
| 1 | +import { Client } from '@microsoft/microsoft-graph-client' |
| 2 | +import { DeviceConfigurationPolicies } from './deviceConfigurationPolicies' |
| 3 | +import { mockClient } from '../../../__fixtures__/@microsoft/microsoft-graph-client' |
| 4 | + |
| 5 | +describe('Device Configuration Policies', () => { |
| 6 | + let graphClient: Client |
| 7 | + let configurationPolicies: DeviceConfigurationPolicies |
| 8 | + |
| 9 | + const configurationPolicy = { |
| 10 | + name: 'test policy', |
| 11 | + '@odata.type': '#microsoft.graph.deviceManagementConfigurationPolicy', |
| 12 | + } |
| 13 | + |
| 14 | + const policyAssignment = { |
| 15 | + '@odata.type': '#microsoft.graph.deviceManagementConfigurationPolicyAssignment', |
| 16 | + target: { |
| 17 | + '@odata.type': '#microsoft.graph.groupAssignmentTarget', |
| 18 | + groupId: '1', |
| 19 | + }, |
| 20 | + } |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + graphClient = mockClient() as never as Client |
| 24 | + configurationPolicies = new DeviceConfigurationPolicies(graphClient) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should list all configuration policies', async () => { |
| 28 | + jest.spyOn(graphClient.api(''), 'get').mockResolvedValueOnce({ |
| 29 | + value: [configurationPolicy], |
| 30 | + '@odata.nextLink': 'next', |
| 31 | + }) |
| 32 | + jest.spyOn(graphClient.api(''), 'get').mockResolvedValueOnce({ |
| 33 | + value: [configurationPolicy], |
| 34 | + }) |
| 35 | + |
| 36 | + const result = await configurationPolicies.list() |
| 37 | + expect(result).toEqual([configurationPolicy, configurationPolicy]) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should get a configuration policy', async () => { |
| 41 | + jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(configurationPolicy) |
| 42 | + const result = await configurationPolicies.get('id') |
| 43 | + expect(result).toEqual(configurationPolicy) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should create a configuration policy', async () => { |
| 47 | + jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(configurationPolicy) |
| 48 | + const result = await configurationPolicies.create(configurationPolicy) |
| 49 | + expect(result).toEqual(configurationPolicy) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should update a configuration policy', async () => { |
| 53 | + jest.spyOn(graphClient.api(''), 'patch') |
| 54 | + const result = await configurationPolicies.update('id', configurationPolicy) |
| 55 | + expect(result).toBeUndefined() |
| 56 | + }) |
| 57 | + |
| 58 | + it('should delete a configuration policy', async () => { |
| 59 | + jest.spyOn(graphClient.api(''), 'delete') |
| 60 | + const result = await configurationPolicies.delete('id') |
| 61 | + expect(result).toBeUndefined() |
| 62 | + }) |
| 63 | + |
| 64 | + describe('setAssignments', () => { |
| 65 | + it('should assign to all devices with exclusions', async () => { |
| 66 | + const spy = jest.spyOn(graphClient.api(''), 'post') |
| 67 | + |
| 68 | + await configurationPolicies.setAssignments('id', { |
| 69 | + allDevices: true, |
| 70 | + excludeGroups: ['group1', 'group2'], |
| 71 | + }) |
| 72 | + |
| 73 | + expect(spy).toHaveBeenCalledWith({ |
| 74 | + assignments: [ |
| 75 | + { |
| 76 | + id: '', |
| 77 | + source: 'direct', |
| 78 | + target: { |
| 79 | + '@odata.type': '#microsoft.graph.allDevicesAssignmentTarget', |
| 80 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + id: '', |
| 85 | + source: 'direct', |
| 86 | + target: { |
| 87 | + '@odata.type': '#microsoft.graph.exclusionGroupAssignmentTarget', |
| 88 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 89 | + groupId: 'group1', |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + id: '', |
| 94 | + source: 'direct', |
| 95 | + target: { |
| 96 | + '@odata.type': '#microsoft.graph.exclusionGroupAssignmentTarget', |
| 97 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 98 | + groupId: 'group2', |
| 99 | + }, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should assign to all licensed users', async () => { |
| 106 | + const spy = jest.spyOn(graphClient.api(''), 'post') |
| 107 | + |
| 108 | + await configurationPolicies.setAssignments('id', { |
| 109 | + allUsers: true, |
| 110 | + }) |
| 111 | + |
| 112 | + expect(spy).toHaveBeenCalledWith({ |
| 113 | + assignments: [ |
| 114 | + { |
| 115 | + id: '', |
| 116 | + source: 'direct', |
| 117 | + target: { |
| 118 | + '@odata.type': '#microsoft.graph.allLicensedUsersAssignmentTarget', |
| 119 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 120 | + }, |
| 121 | + }, |
| 122 | + ], |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should assign to specific included groups', async () => { |
| 127 | + const spy = jest.spyOn(graphClient.api(''), 'post') |
| 128 | + |
| 129 | + await configurationPolicies.setAssignments('id', { |
| 130 | + includeGroups: ['group1', 'group2'], |
| 131 | + }) |
| 132 | + |
| 133 | + expect(spy).toHaveBeenCalledWith({ |
| 134 | + assignments: [ |
| 135 | + { |
| 136 | + id: '', |
| 137 | + source: 'direct', |
| 138 | + target: { |
| 139 | + '@odata.type': '#microsoft.graph.groupAssignmentTarget', |
| 140 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 141 | + groupId: 'group1', |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + id: '', |
| 146 | + source: 'direct', |
| 147 | + target: { |
| 148 | + '@odata.type': '#microsoft.graph.groupAssignmentTarget', |
| 149 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 150 | + groupId: 'group2', |
| 151 | + }, |
| 152 | + }, |
| 153 | + ], |
| 154 | + }) |
| 155 | + }) |
| 156 | + |
| 157 | + it('should throw error when including groups with allDevices', async () => { |
| 158 | + await expect( |
| 159 | + configurationPolicies.setAssignments('id', { |
| 160 | + allDevices: true, |
| 161 | + includeGroups: ['group1'], |
| 162 | + }), |
| 163 | + ).rejects.toThrow('Cannot include specific groups when allDevices is true') |
| 164 | + }) |
| 165 | + |
| 166 | + it('should support mix of include and exclude groups', async () => { |
| 167 | + const spy = jest.spyOn(graphClient.api(''), 'post') |
| 168 | + |
| 169 | + await configurationPolicies.setAssignments('id', { |
| 170 | + includeGroups: ['group1'], |
| 171 | + excludeGroups: ['group2'], |
| 172 | + }) |
| 173 | + |
| 174 | + expect(spy).toHaveBeenCalledWith({ |
| 175 | + assignments: [ |
| 176 | + { |
| 177 | + id: '', |
| 178 | + source: 'direct', |
| 179 | + target: { |
| 180 | + '@odata.type': '#microsoft.graph.groupAssignmentTarget', |
| 181 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 182 | + groupId: 'group1', |
| 183 | + }, |
| 184 | + }, |
| 185 | + { |
| 186 | + id: '', |
| 187 | + source: 'direct', |
| 188 | + target: { |
| 189 | + '@odata.type': '#microsoft.graph.exclusionGroupAssignmentTarget', |
| 190 | + deviceAndAppManagementAssignmentFilterType: 'none', |
| 191 | + groupId: 'group2', |
| 192 | + }, |
| 193 | + }, |
| 194 | + ], |
| 195 | + }) |
| 196 | + }) |
| 197 | + }) |
| 198 | + |
| 199 | + describe('pagination', () => { |
| 200 | + it('should handle pagination for list method', async () => { |
| 201 | + const firstPage = { |
| 202 | + value: [{ ...configurationPolicy, id: '1' }], |
| 203 | + '@odata.nextLink': 'https://graph.microsoft.com/beta/next-page', |
| 204 | + } |
| 205 | + const secondPage = { |
| 206 | + value: [{ ...configurationPolicy, id: '2' }], |
| 207 | + } |
| 208 | + |
| 209 | + jest.spyOn(graphClient.api(''), 'get') |
| 210 | + .mockResolvedValueOnce(firstPage) |
| 211 | + .mockResolvedValueOnce(secondPage) |
| 212 | + |
| 213 | + const result = await configurationPolicies.list() |
| 214 | + |
| 215 | + expect(result).toHaveLength(2) |
| 216 | + expect(result[0].id).toBe('1') |
| 217 | + expect(result[1].id).toBe('2') |
| 218 | + }) |
| 219 | + }) |
| 220 | +}) |
0 commit comments