Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcuzzo committed Nov 21, 2019
1 parent 811defc commit e39cad7
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 0 deletions.
118 changes: 118 additions & 0 deletions psake.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
Task default -Depends Deploy

Properties {

$ProjectRoot = $ENV:APPVEYOR_BUILD_FOLDER

if( ( -not $ProjectRoot ) )
{
[ValidateNotNullOrEmpty()]$ProjectRoot = $Psake.build_script_dir
}

$ProjectName = $ENV:APPVEYOR_PROJECT_NAME
if(-not $ProjectName) {
[ValidateNotNullOrEmpty()]$ProjectName = (Get-ChildItem -Include *.psd1 -Recurse)[0].BaseName
}

$Timestamp = Get-date -uformat "%Y%m%d-%H%M%S"
$PSVersion = $PSVersionTable.PSVersion.Major
$TestFile = "TestResults_PS$PSVersion`_$TimeStamp.xml"
$lines = '----------------------------------------------------------------------'
$Verbose = @{}
$CommitMsg = "$env:APPVEYOR_REPO_COMMIT_MESSAGE $env:APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED"
if($CommitMsg -match "!verbose")
{
$Verbose = @{Verbose = $True}
}
}

Task Init {
$lines
Set-Location $ProjectRoot
"Build System Details:"
Get-Item ENV:APPVEYOR*
"`n"
}

Task Analyze -depends Init {
"$lines`n`n`tSTATUS: Scanning for PSScriptAnalyzer Errors"

$ScanResults = Invoke-ScriptAnalyzer -Path "$ProjectRoot\$ProjectName" -Recurse -Severity Error

If ($ScanResults.count -gt 0)
{
Throw "Failed PSScriptAnalyzer Tests"
}
}

Task Help -depends Analyze {

"$lines`n`n`tSTATUS: Building Module Help"

Import-Module "$ProjectRoot\$ProjectName\$ProjectName.psd1" -Force

Try
{
New-ExternalHelp 'docs\cmdlets' -OutputPath "$ProjectName\en-US" -Force -ErrorAction Stop
Import-Module "$ProjectRoot\$ProjectName\$ProjectName.psd1" -Force
}
Catch
{
Throw
}

}


Task Test -Depends Help {
$lines
"`n`tSTATUS: Testing with PowerShell $PSVersion"
$TestResults = Invoke-Pester -Path $ProjectRoot\Tests -PassThru -OutputFormat NUnitXml -OutputFile "$ProjectRoot\$TestFile"

if ( $env:APPVEYOR_JOB_ID )
{
(New-Object 'System.Net.WebClient').UploadFile( "https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", "$ProjectRoot\$TestFile" )
}

Remove-Item "$ProjectRoot\$TestFile" -Force -ErrorAction SilentlyContinue

if($TestResults.FailedCount -gt 0)
{
Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}
"`n"
}

Task Deploy -Depends Test {

# Make sure we're using the Master branch and that it's not a pull request
# Environmental Variables Guide: https://www.appveyor.com/docs/environment-variables/
if ($env:APPVEYOR_REPO_BRANCH -ne 'master')
{
Write-Warning -Message "Skipping version increment and publish for branch $env:APPVEYOR_REPO_BRANCH"
}
elseif ($env:APPVEYOR_PULL_REQUEST_NUMBER -gt 0)
{
Write-Warning -Message "Skipping version increment and publish for pull request #$env:APPVEYOR_PULL_REQUEST_NUMBER"
}
else
{
Try
{
# Build a splat containing the required details and make sure to Stop for errors which will trigger the catch
$PM = @{
Path = '.\PSTeamViewer'
NuGetApiKey = $env:NuGetApiKey
ErrorAction = 'Stop'
}
Publish-Module @PM
Write-Host "TeamViewer Module published to the PowerShell Gallery." -ForegroundColor Cyan
}
Catch
{

Write-Warning "Publishing to the PowerShell Gallery failed."
throw $_
}
}
}
87 changes: 87 additions & 0 deletions tests/Get-TVUser.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Import-Module "$PSScriptRoot\..\PSTeamViewer\PSTeamViewer.psd1" -Force


InModuleScope PSTeamViewer {

Describe 'TVUser Management' {

BeforeEach {
[string] $TVUserTestData = '{"id":"u1111111","name":"firstname lastname","permissions":"EditConnections,EditFullProfile,ViewOwnAssets,EditOwnCustomModuleConfigs,ViewOwnConnections","active":false,"log_sessions":true,"show_comment_window":false}'
}

Mock -CommandName Invoke-RestMethod -Verifiable -MockWith {
if ( ( ! ( [string]::IsNullOrEmpty($Body.name))) -or ( ! ( [string]::IsNullOrEmpty($Body.email))))
{
return ( ('{{"users":[{0}]}}' -f $TVUserTestData) | ConvertFrom-Json )
}
else
{
return ( $TVUserTestData | ConvertFrom-Json )
}

} -ParameterFilter {
$uri -match "/api/v1/users" -and $Method -eq 'Get'
} -ModuleName PSTeamViewer

Mock -CommandName Invoke-RestMethod -Verifiable -MockWith {
$json = $TVUserTestData | ConvertFrom-Json
if ( ! ( [string]::IsNullOrEmpty($Body.name)))
{
$json.name = $Body.name
}
return $json;

} -ParameterFilter {
$uri -match "/api/v1/users" -and $Method -eq 'Put'
} -ModuleName PSTeamViewer


Mock -CommandName Invoke-RestMethod -Verifiable -MockWith {
return ( $TVUserTestData | ConvertFrom-Json )
} -ParameterFilter {
$uri -match "/api/v1/account" -and $Method -eq 'Get'
} -ModuleName PSTeamViewer

Mock -CommandName Invoke-RestMethod -Verifiable -MockWith {
$user_id = ('u{0}' -f (Get-Random -Maximum 9999999 -Minimum 1000000) )

$str = @'
{{"id":"{0}",
"name":"{1}",
"permissions":"EditConnections,EditFullProfile,ViewOwnAssets,EditOwnCustomModuleConfigs,ViewOwnConnections",
"active":true,
"log_sessions":true,
"show_comment_window":false}}
'@
$TVUSerData = $str -f $user_id, $Name
return ( $TVUSerData | ConvertFrom-Json )
} -ParameterFilter {
$uri -match "/api/v1/users" -and $Method -eq 'Post'
} -ModuleName PSTeamViewer

Context 'Get-TVUser' {
It 'Fetching TeamViewer user' {
$TVUser = Get-TVUser -UserID 'u1111111' -Token "ABC123"
$TVUser.GetType().Name | should be 'TVUser'
$TVUser.ID | Should be 'u1111111'
$TVUser.Name | should be 'firstname lastname'
Assert-MockCalled -CommandName Invoke-RestMethod -ModuleName PSTeamviewer
}
}


# When Set-TVUser call's Get-TVUser it will return an object wtith the same data...
Context 'Set-TVUser' {
It 'Updating TeamViewer user' {
$TVUser = Set-TVUser -UserID 'u1111111' -Name 'Lastname, Firstname' -Token "ABC123" -Verbose
$TVUser.GetType().Name | should be 'TVUser'
$TVUser.ID | Should be 'u1111111'
$TVUser.Name | should be 'Lastname, Firstname'
Assert-MockCalled -CommandName Invoke-RestMethod -ModuleName PSTeamviewer
}
}


}

}

0 comments on commit e39cad7

Please sign in to comment.