forked from AtlassianPS/JiraPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-JiraUser.Tests.ps1
105 lines (83 loc) · 4.13 KB
/
Set-JiraUser.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
. $PSScriptRoot\Shared.ps1
InModuleScope JiraPS {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope='*', Target='SuppressImportModule')]
$SuppressImportModule = $true
. $PSScriptRoot\Shared.ps1
$jiraServer = 'http://jiraserver.example.com'
$testUsername = 'powershell-test'
$testDisplayName = 'PowerShell Test User'
$testEmail = "[email protected]"
$testDisplayNameChanged = "$testDisplayName Modified"
$testEmailChanged = "[email protected]"
$restResultGet = @"
{
"self": "$jiraServer/rest/api/2/user?username=$testUsername",
"key": "$testUsername",
"name": "$testUsername",
"displayName": "$testDisplayName",
"emailAddress": "$testEmail",
"active": true
}
"@
Describe "Set-JiraUser" {
Mock Get-JiraConfigServer -ModuleName JiraPS {
Write-Output $jiraServer
}
Mock Get-JiraUser -ModuleName JiraPS {
ConvertTo-JiraUser (ConvertFrom-Json2 $restResultGet)
}
Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter {$Method -eq 'Put' -and $URI -eq "$jiraServer/rest/api/latest/user?username=$testUsername"} {
if ($ShowMockData)
{
Write-Host " Mocked Invoke-JiraMethod with GET method" -ForegroundColor Cyan
Write-Host " [Method] $Method" -ForegroundColor Cyan
Write-Host " [URI] $URI" -ForegroundColor Cyan
}
ConvertFrom-Json2 $restResultGet
}
# Generic catch-all. This will throw an exception if we forgot to mock something.
Mock Invoke-JiraMethod -ModuleName JiraPS {
Write-Host " Mocked Invoke-JiraMethod with no parameter filter." -ForegroundColor DarkRed
Write-Host " [Method] $Method" -ForegroundColor DarkRed
Write-Host " [URI] $URI" -ForegroundColor DarkRed
throw "Unidentified call to Invoke-JiraMethod"
}
# Mock Write-Debug {
# Write-Host "DEBUG: $Message" -ForegroundColor Yellow
# }
#############
# Tests
#############
It "Accepts a username as a String to the -User parameter" {
{ Set-JiraUser -User $testUsername -DisplayName $testDisplayNameChanged } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly -Times 1 -Scope It
}
It "Accepts a JiraPS.User object to the -User parameter" {
$user = Get-JiraUser -UserName $testUsername
{ Set-JiraUser -User $user -DisplayName $testDisplayNameChanged } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly -Times 1 -Scope It
}
It "Accepts pipeline input from Get-JiraUser" {
{ Get-JiraUser -UserName $testUsername | Set-JiraUser -DisplayName $testDisplayNameChanged } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly -Times 1 -Scope It
}
It "Modifies a user's DisplayName if the -DisplayName parameter is passed" {
# This is not a great test.
{ Set-JiraUser -User $testUsername -DisplayName $testDisplayNameChanged } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly -Times 1 -Scope It
}
It "Modifies a user's EmailAddress if the -EmailAddress parameter is passed" {
# Neither is this one.
{ Set-JiraUser -User $testUsername -EmailAddress $testEmailChanged } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly -Times 1 -Scope It
}
It "Provides no output if the -PassThru parameter is not passed" {
$output = Set-JiraUser -User $testUsername -DisplayName $testDisplayNameChanged
$output | Should BeNullOrEmpty
}
It "Outputs a JiraPS.User object if the -PassThru parameter is passed" {
$output = Set-JiraUser -User $testUsername -DisplayName $testDisplayNameChanged -PassThru
$output | Should Not BeNullOrEmpty
}
}
}