Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions Cmdlets/Public/Set-TeamViewerManagedDevice.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ function Set-TeamViewerManagedDevice {
[ValidateScript( { $_ | Resolve-TeamViewerManagedGroupId } )]
[Alias('ManagedGroupId')]
[object]
$ManagedGroup
$ManagedGroup,

[Parameter(ParameterSetName = 'UpdateDescription')]
[Alias('Description')]
[string]
$deviceDescription
)
Begin {
begin {
$body = @{}

if ($Name) {
Expand All @@ -38,10 +43,20 @@ function Set-TeamViewerManagedDevice {
$body['managedGroupId'] = $ManagedGroup | Resolve-TeamViewerManagedGroupId
}

if ($Policy -And $ManagedGroup) {
if ($Policy -and $ManagedGroup) {
$PSCmdlet.ThrowTerminatingError(
('The combination of parameters -Policy and -ManagedGroup is not allowed.' | `
ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
ConvertToErrorRecord -ErrorCategory InvalidArgument))
}

if ($deviceDescription -and ($Policy -or $ManagedGroup)) {
$PSCmdlet.ThrowTerminatingError(
('The parameter -deviceDescription cannot be combined with -Policy or -ManagedGroup.' |
ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
}

if ($deviceDescription) {
$body['deviceDescription'] = $deviceDescription
}

if ($body.Count -eq 0) {
Expand All @@ -50,10 +65,16 @@ function Set-TeamViewerManagedDevice {
ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
}
}
Process {
process {
$deviceId = $Device | Resolve-TeamViewerManagedDeviceId
$resourceUri = "$(Get-TeamViewerApiUri)/managed/devices/$deviceId"

switch ($PsCmdlet.ParameterSetName) {
'UpdateDescription' {
$resourceUri += '/description'
}
}

if ($PSCmdlet.ShouldProcess($Device.ToString(), 'Change managed device entry')) {
Invoke-TeamViewerRestMethod `
-ApiToken $ApiToken `
Expand Down
39 changes: 35 additions & 4 deletions Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Describe 'Set-TeamViewerManagedDevice' {
It 'Should call the correct API endpoint to update a managed device' {
Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDeviceId -Name 'Foo Bar'
Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter {
$ApiToken -eq $testApiToken -And `
$Uri -eq "//unit.test/managed/devices/$testDeviceId" -And `
$ApiToken -eq $testApiToken -and `
$Uri -eq "//unit.test/managed/devices/$testDeviceId" -and `
$Method -eq 'Put' }
}

Expand All @@ -45,6 +45,37 @@ Describe 'Set-TeamViewerManagedDevice' {
$body.managedGroupId | Should -Be 'e579cfeb-0b29-4d91-9e81-2d9507f53ff8'
}

It 'Should update the managed device description' {
Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDeviceId -deviceDescription 'Test description'
$mockArgs.Body | Should -Not -BeNullOrEmpty

$body = [System.Text.Encoding]::UTF8.GetString($mockArgs.Body) | ConvertFrom-Json
$body.deviceDescription | Should -Be 'Test description'

Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter {
$ApiToken -eq $testApiToken -and `
$Uri -eq "//unit.test/managed/devices/$testDeviceId/description" -and `
$Method -eq 'Put'
}
}

It 'Should not allow description together with policy' {
{ Set-TeamViewerManagedDevice `
-ApiToken $testApiToken `
-Device $testDeviceId `
-deviceDescription 'Test description' `
-Policy '2871c013-3040-4969-9ba4-ce970f4375e8' } | Should -Throw
}

It 'Should not allow description together with managed group' {
{ Set-TeamViewerManagedDevice `
-ApiToken $testApiToken `
-Device $testDeviceId `
-deviceDescription 'Test description' `
-ManagedGroup 'e579cfeb-0b29-4d91-9e81-2d9507f53ff8' } | Should -Throw
}


It 'Should not be possible to inherit and set a policy at the same time' {
{ Set-TeamViewerManagedDevice `
-ApiToken $testApiToken `
Expand All @@ -68,8 +99,8 @@ Describe 'Set-TeamViewerManagedDevice' {
$testDevice = @{ id = $testDeviceId; name = 'test device' } | ConvertTo-TeamViewerManagedDevice
Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDevice -Name 'Foo Bar'
Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter {
$ApiToken -eq $testApiToken -And `
$Uri -eq "//unit.test/managed/devices/$testDeviceId" -And `
$ApiToken -eq $testApiToken -and `
$Uri -eq "//unit.test/managed/devices/$testDeviceId" -and `
$Method -eq 'Put' }
}

Expand Down