-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for device health scripts (#121)
* feat: Add support for device health scripts * chore: fix typos
- Loading branch information
Showing
5 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Client } from '@microsoft/microsoft-graph-client' | ||
import { DeviceHealthScript, DeviceHealthScriptAssignment } from 'lib/types' | ||
import { mockClient } from '../../../__fixtures__/@microsoft/microsoft-graph-client' | ||
import { DeviceHealthScripts } from './deviceHealthScripts' | ||
|
||
describe('Device Health Scripts', () => { | ||
let graphClient: Client | ||
let deviceHealthScripts: DeviceHealthScripts | ||
const deviceHealthScript = { | ||
name: 'test', | ||
'@odata.type': '#microsoft.graph.deviceHealthScript', | ||
id: '1', | ||
} as DeviceHealthScript | ||
|
||
const assignment = { | ||
'@odata.type': '#microsoft.graph.deviceHealthScriptAssignment', | ||
targetGroupId: '1', | ||
} as DeviceHealthScriptAssignment | ||
|
||
beforeEach(() => { | ||
graphClient = mockClient() as never as Client | ||
deviceHealthScripts = new DeviceHealthScripts(graphClient) | ||
}) | ||
|
||
it('should get a device health script', async () => { | ||
jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(deviceHealthScript) | ||
const result = await deviceHealthScripts.get('') | ||
expect(result).toEqual(deviceHealthScript) | ||
}) | ||
|
||
it('should list all device health scripts', async () => { | ||
jest.spyOn(graphClient.api(''), 'get').mockResolvedValue({ | ||
value: [deviceHealthScript], | ||
}) | ||
const result = await deviceHealthScripts.list() | ||
expect(result).toEqual([deviceHealthScript]) | ||
}) | ||
|
||
it('should create a device health script', async () => { | ||
jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(deviceHealthScript) | ||
const result = await deviceHealthScripts.create(deviceHealthScript) | ||
expect(result).toEqual(deviceHealthScript) | ||
}) | ||
|
||
it('should update a device health script', async () => { | ||
jest.spyOn(graphClient.api(''), 'patch') | ||
const result = await deviceHealthScripts.update('id', deviceHealthScript) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should delete a device health script', async () => { | ||
jest.spyOn(graphClient.api(''), 'delete') | ||
const result = await deviceHealthScripts.delete('id') | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should create an assignment', async () => { | ||
jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(assignment) | ||
const result = await deviceHealthScripts.createAssignment('id', assignment) | ||
expect(result).toEqual(assignment) | ||
}) | ||
|
||
it('should list assignments', async () => { | ||
jest.spyOn(graphClient.api(''), 'get').mockResolvedValue({ value: [assignment] }) | ||
const result = await deviceHealthScripts.listAssignments('id') | ||
expect(result).toEqual([assignment]) | ||
}) | ||
|
||
it('should get an assignment', async () => { | ||
jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(assignment) | ||
const result = await deviceHealthScripts.getAssignment('id', 'groupAssignmentId') | ||
expect(result).toEqual(assignment) | ||
}) | ||
|
||
it('should delete an assignment', async () => { | ||
jest.spyOn(graphClient.api(''), 'delete') | ||
const result = await deviceHealthScripts.deleteAssignment('id', 'groupId') | ||
expect(result).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Client } from '@microsoft/microsoft-graph-client' | ||
import { DeviceHealthScript, DeviceHealthScriptAssignment } from '../types' | ||
export class DeviceHealthScripts { | ||
constructor(private readonly graphClient: Client) {} | ||
|
||
async list() { | ||
let res = await this.graphClient.api('/deviceManagement/deviceHealthScripts').get() | ||
const scripts: DeviceHealthScript[] = res.value | ||
while (res['@odata.nextLink']) { | ||
const nextLink = res['@odata.nextLink'].replace('https://graph.microsoft.com/beta', '') | ||
res = await this.graphClient.api(nextLink).get() | ||
const nextScripts = res.value as DeviceHealthScript[] | ||
scripts.push(...nextScripts) | ||
} | ||
return scripts | ||
} | ||
|
||
async get(id: string): Promise<DeviceHealthScript> { | ||
return await this.graphClient.api(`/deviceManagement/deviceHealthScripts/${id}`).get() | ||
} | ||
|
||
async create(script: DeviceHealthScript): Promise<DeviceHealthScript> { | ||
return await this.graphClient.api('/deviceManagement/deviceHealthScripts').post(script) | ||
} | ||
|
||
async update(id: string, script: DeviceHealthScript): Promise<DeviceHealthScript> { | ||
return await this.graphClient | ||
.api(`/deviceManagement/deviceHealthScripts/${id}`) | ||
.patch(script) | ||
} | ||
|
||
async delete(id: string): Promise<void> { | ||
await this.graphClient.api(`/deviceManagement/deviceHealthScripts/${id}`).delete() | ||
} | ||
|
||
async createAssignment( | ||
id: string, | ||
assignment: DeviceHealthScriptAssignment, | ||
): Promise<DeviceHealthScriptAssignment> { | ||
return this.graphClient | ||
.api(`/deviceManagement/deviceHealthScripts/${id}/assignments`) | ||
.post(assignment) | ||
} | ||
|
||
async listAssignments(id: string): Promise<DeviceHealthScriptAssignment[]> { | ||
let res = await this.graphClient | ||
.api(`/deviceManagement/deviceHealthScripts/${id}/assignments`) | ||
.get() | ||
const groupAssignments: DeviceHealthScriptAssignment[] = res.value | ||
while (res['@odata.nextLink']) { | ||
const nextLink = res['@odata.nextLink'].replace('https://graph.microsoft.com/beta', '') | ||
res = await this.graphClient.api(nextLink).get() | ||
const nextGroupAssignments = res.value as DeviceHealthScriptAssignment[] | ||
groupAssignments.push(...nextGroupAssignments) | ||
} | ||
return groupAssignments | ||
} | ||
|
||
async deleteAssignment(id: string, assignmentId: string): Promise<void> { | ||
await this.graphClient | ||
.api(`/deviceManagement/deviceHealthScripts/${id}/assignments/${assignmentId}`) | ||
.delete() | ||
} | ||
|
||
async getAssignment(id: string, assignmentId: string): Promise<DeviceHealthScriptAssignment> { | ||
return this.graphClient | ||
.api(`/deviceManagement/deviceHealthScripts/${id}/assignments/${assignmentId}`) | ||
.get() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters