Skip to content

Commit

Permalink
Fixes script analyzer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljoos committed Nov 2, 2020
1 parent 751eda5 commit 0dac235
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ script:
- docker run -v ${TRAVIS_BUILD_DIR}:/tmp/src/ mcr.microsoft.com/powershell:7.0.0-alpine-3.10 pwsh -c "
\$ProgressPreference='SilentlyContinue';
Install-Module PSScriptAnalyzer -Force -SkipPublisherCheck -Scope CurrentUser;
Invoke-ScriptAnalyzer -Path /tmp/src -Recurse -ReportSummary -EnableExit;"
Invoke-ScriptAnalyzer -Path /tmp/src -Recurse -ReportSummary -EnableExit -ExcludeRule PSReviewUnusedParameter;"
6 changes: 4 additions & 2 deletions TeamViewerADConnector/Internal/ActiveDirectory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ function Select-ActiveDirectoryCommonName {
# , + " \ < > ; \r \n = /
# See https://msdn.microsoft.com/en-us/windows/desktop/aa366101
# See https://www.ietf.org/rfc/rfc2253.txt
if ($path -match 'CN=((?:[^,+"\\<>;\r\n=/]|(?:\\[,+"\\<>;\r\n=/]))+)') {
return $Matches.1 -replace '\\([,+"\\<>;\r\n=/])','$1'
Process {
if ($path -match 'CN=((?:[^,+"\\<>;\r\n=/]|(?:\\[,+"\\<>;\r\n=/]))+)') {
return $Matches.1 -replace '\\([,+"\\<>;\r\n=/])','$1'
}
}
}
14 changes: 9 additions & 5 deletions TeamViewerADConnector/Internal/Sync.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

function Write-SyncLog {
param([Parameter(ValueFromPipeline)] $message, [Parameter()] $Extra)
Write-Output -InputObject @{Date = (Get-Date); Message = $message; Extra = $Extra } -NoEnumerate
Process {
Write-Output -InputObject @{Date = (Get-Date); Message = $message; Extra = $Extra } -NoEnumerate
}
}

function Format-SyncLog {
Expand Down Expand Up @@ -37,10 +39,12 @@ function ConvertTo-SyncUpdateUserChangeset($userTV, $userAD) {

function Format-SyncUpdateUserChangeset {
param([Parameter(ValueFromPipeline)] $changeset)
$message = ""
if ($changeset.name) { $message += "Changing name to '$($changeset.name)'. " }
if ($changeset.active) { $message += "Changing account status to 'active'. " }
"$message"
Process {
$message = ""
if ($changeset.name) { $message += "Changing name to '$($changeset.name)'. " }
if ($changeset.active) { $message += "Changing account status to 'active'. " }
"$message"
}
}

function Invoke-SyncPrework($syncContext, $configuration, $progressHandler) {
Expand Down
6 changes: 4 additions & 2 deletions TeamViewerADConnector/Internal/TeamViewer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ $tvApiBaseUrl = "https://webapi.teamviewer.com"

function ConvertTo-TeamViewerRestError {
param([parameter(ValueFromPipeline)]$err)
try { return ($err | Out-String | ConvertFrom-Json) }
catch { return $err }
Process {
try { return ($err | Out-String | ConvertFrom-Json) }
catch { return $err }
}
}

function Invoke-TeamViewerRestMethod {
Expand Down
24 changes: 12 additions & 12 deletions Tests/TeamViewerADConnector/Internal/Configuration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@ BeforeAll {
Describe 'Confirm-Configuration' {

It 'Should throw if DefaultPassword and SsoCustomerId are used in conjunction' {
$input = @{
$inputData = @{
UseDefaultPassword = $true; DefaultPassword = 'Test123'
UseSsoCustomerId = $true; SsoCustomerId = 'TestCustomer'
}
{ Confirm-Configuration $input } | Should -Throw
{ Confirm-Configuration $inputData } | Should -Throw
}

It 'Should throw if DefaultPassword and GeneratePassword are used in conjunction' {
$input = @{
$inputData = @{
UseGeneratedPassword = $true
UseDefaultPassword = $true; DefaultPassword = 'Test123'
}
{ Confirm-Configuration $input } | Should -Throw
{ Confirm-Configuration $inputData } | Should -Throw
}

It 'Should throw if SsoCustomerId and GeneratePassword are used in conjunction' {
$input = @{
$inputData = @{
UseGeneratedPassword = $true
UseSsoCustomerId = $true; SsoCustomerId = 'TestCustomer'
}
{ Confirm-Configuration $input } | Should -Throw
{ Confirm-Configuration $inputData } | Should -Throw
}

It 'Should throw if neither DefaultPassword, GeneratePassword nor SsoCustomerId are set' {
$input = @{}
{ Confirm-Configuration $input } | Should -Throw
$inputData = @{}
{ Confirm-Configuration $inputData } | Should -Throw
}

It 'Should throw if DefaultPassword is configured but empty' {
$input = @{ UseDefaultPassword = $true; DefaultPassword = '' }
{ Confirm-Configuration $input } | Should -Throw
$inputData = @{ UseDefaultPassword = $true; DefaultPassword = '' }
{ Confirm-Configuration $inputData } | Should -Throw
}

It 'Should throw if SsoCustomerId is configured by empty' {
$input = @{ UseSsoCustomerId = $true; SsoCustomerId = '' }
{ Confirm-Configuration $input } | Should -Throw
$inputData = @{ UseSsoCustomerId = $true; SsoCustomerId = '' }
{ Confirm-Configuration $inputData } | Should -Throw
}
}
2 changes: 2 additions & 0 deletions Tests/TeamViewerADConnector/Internal/Sync.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ BeforeAll {
function Add-TeamViewerConditionalAccessGroupUser($accessToken, $groupID, $userIDs) { }
function Remove-TeamViewerConditionalAccessGroupUser {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'None')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'accessToken', Justification = 'Needs to be mockable')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'groupID', Justification = 'Needs to be mockable')]
param($accessToken, $groupID, $userIDs)
if ($PSCmdlet.ShouldProcess($userIDs)) { }
}
Expand Down
24 changes: 12 additions & 12 deletions Tests/TeamViewerADConnector/Internal/TeamViewer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,35 @@ Describe 'Add-TeamViewerUser' {
}

It 'Should call the API users endpoint' {
$input = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test'; 'language' = 'en' }
Add-TeamViewerUser 'TestAccessToken' $input | Should -Be $testUser
$inputData = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test'; 'language' = 'en' }
Add-TeamViewerUser 'TestAccessToken' $inputData | Should -Be $testUser
Assert-MockCalled Invoke-RestMethod -Times 1 -Scope It -ParameterFilter {
$Uri -And [System.Uri]$Uri.PathAndQuery -eq '/api/v1/users' -And
$Method -And $Method -eq 'Post'
}
}

It 'Should throw if required fields are missing' {
$inputs = @(
$inputDatas = @(
@{ 'email' = 'test1@example.test'; 'language' = 'en' }, # missing 'name'
@{ 'name' = 'Test User 1'; 'language' = 'en' }, # missing 'email'
@{ 'name' = 'Test User 1'; 'email' = 'test1@example.test' } # missing 'language'
)
$inputs | ForEach-Object { { Add-TeamViewerUser 'TestAccessToken' $_ } | Should -Throw }
$inputDatas | ForEach-Object { { Add-TeamViewerUser 'TestAccessToken' $_ } | Should -Throw }
}

It 'Should encode the payload using UTF-8' {
$input = @{ 'name' = 'Test User Müller'; 'email' = 'test1@example.test'; 'language' = 'en' }
Add-TeamViewerUser 'TestAccessToken' $input
$inputData = @{ 'name' = 'Test User Müller'; 'email' = 'test1@example.test'; 'language' = 'en' }
Add-TeamViewerUser 'TestAccessToken' $inputData
Assert-MockCalled Invoke-RestMethod -Times 1 -Scope It -ParameterFilter { $Body }
{ [System.Text.Encoding]::UTF8.GetString($lastMockParams.Body) } | Should -Not -Throw
{ [System.Text.Encoding]::UTF8.GetString($lastMockParams.Body) | ConvertFrom-Json } | Should -Not -Throw
([System.Text.Encoding]::UTF8.GetString($lastMockParams.Body) | ConvertFrom-Json).name | Should -Be 'Test User Müller'
}

It 'Should set the authorization header' {
$input = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test'; 'language' = 'en' }
Add-TeamViewerUser 'TestAccessToken' $input
$inputData = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test'; 'language' = 'en' }
Add-TeamViewerUser 'TestAccessToken' $inputData
Assert-MockCalled Invoke-RestMethod -Times 1 -Scope It -ParameterFilter {
$Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken'
}
Expand All @@ -151,8 +151,8 @@ Describe 'Edit-TeamViewerUser' {
}

It 'Should call the API users endpoint' {
$input = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test' }
$result = (Edit-TeamViewerUser 'TestAccessToken' 1234 $input)
$inputData = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test' }
$result = (Edit-TeamViewerUser 'TestAccessToken' 1234 $inputData)
$result.id | Should -Be $testUser.id
$result.name | Should -Be $testUser.name
Assert-MockCalled Invoke-WebRequest -Times 1 -Scope It -ParameterFilter {
Expand All @@ -162,8 +162,8 @@ Describe 'Edit-TeamViewerUser' {
}

It 'Should set the authorization header' {
$input = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test' }
Edit-TeamViewerUser 'TestAccessToken' 1234 $input
$inputData = @{ 'name' = 'Test User 1'; 'email' = 'test1@example.test' }
Edit-TeamViewerUser 'TestAccessToken' 1234 $inputData
Assert-MockCalled Invoke-WebRequest -Times 1 -Scope It -ParameterFilter {
$Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken'
}
Expand Down

0 comments on commit 0dac235

Please sign in to comment.