-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
New-JiraIssue.Unit.Tests.ps1
166 lines (141 loc) · 6.42 KB
/
New-JiraIssue.Unit.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#requires -modules BuildHelpers
#requires -modules @{ ModuleName = "Pester"; ModuleVersion = "4.4.0" }
Describe "New-JiraIssue" -Tag 'Unit' {
BeforeAll {
Remove-Item -Path Env:\BH*
$projectRoot = (Resolve-Path "$PSScriptRoot/../..").Path
if ($projectRoot -like "*Release") {
$projectRoot = (Resolve-Path "$projectRoot/..").Path
}
Import-Module BuildHelpers
Set-BuildEnvironment -BuildOutput '$ProjectPath/Release' -Path $projectRoot -ErrorAction SilentlyContinue
$env:BHManifestToTest = $env:BHPSModuleManifest
$script:isBuild = $PSScriptRoot -like "$env:BHBuildOutput*"
if ($script:isBuild) {
$Pattern = [regex]::Escape($env:BHProjectPath)
$env:BHBuildModuleManifest = $env:BHPSModuleManifest -replace $Pattern, $env:BHBuildOutput
$env:BHManifestToTest = $env:BHBuildModuleManifest
}
Import-Module "$env:BHProjectPath/Tools/BuildTools.psm1"
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module $env:BHManifestToTest
}
AfterAll {
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Remove-Module BuildHelpers -ErrorAction SilentlyContinue
Remove-Item -Path Env:\BH*
}
InModuleScope JiraPS {
. "$PSScriptRoot/../Shared.ps1"
$jiraServer = 'https://jira.example.com'
$issueTypeTest = 1
Mock Get-JiraConfigServer {
$jiraServer
}
# If we don't override this in a context or test, we don't want it to
# actually try to query a JIRA instance
Mock Invoke-JiraMethod {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
@{ Key = "TEST-01"}
}
Mock Get-JiraIssue {
[PSCustomObject] @{ Key = "TEST-01"}
}
Mock Get-JiraProject {
$issueObject = [PSCustomObject] @{
ID = $issueTypeTest
Name = 'Test Issue Type'
}
$issueObject.PSObject.TypeNames.Insert(0, 'JiraPS.IssueType')
$object = [PSCustomObject] @{
'ID' = $Project
'Key' = "TEST"
}
Add-Member -InputObject $object -MemberType NoteProperty -Name "IssueTypes" -Value $issueObject
$object.PSObject.TypeNames.Insert(0, 'JiraPS.Project')
return $object
}
Mock Get-JiraUser {
$object = [PSCustomObject] @{
'Name' = $UserName;
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.User')
return $object
}
# This one needs to be able to output multiple objects
Mock Get-JiraField {
$Field | % {
$object = [PSCustomObject] @{
'Id' = $_
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.Field')
$object
}
}
Mock Get-JiraIssueCreateMetadata {
@(
@{Name = 'Project'; ID = 'Project'; Required = $true}
@{Name = 'IssueType'; ID = 'IssueType'; Required = $true}
@{Name = 'Priority'; ID = 'Priority'; Required = $true}
@{Name = 'Summary'; ID = 'Summary'; Required = $true}
@{Name = 'Description'; ID = 'Description'; Required = $true}
@{Name = 'Reporter'; ID = 'Reporter'; Required = $true}
)
}
$newParams = @{
'Project' = 'TEST';
'IssueType' = 1;
'Priority' = 1;
'Reporter' = 'testUsername';
'Summary' = 'Test summary';
'Description' = 'Test description';
}
$pipelineParams = New-Object -TypeName PSCustomObject -Property $newParams
Context "Sanity checking" {
$command = Get-Command -Name New-JiraIssue
defParam $command 'Project'
defParam $command 'IssueType'
defParam $command 'Priority'
defParam $command 'Summary'
defParam $command 'Description'
defParam $command 'Reporter'
defParam $command 'Labels'
defParam $command 'Fields'
defParam $command 'Credential'
}
Context "Behavior testing" {
It "Creates an issue in JIRA" {
{ New-JiraIssue @newParams } | Should Not Throw
# The String in the ParameterFilter is made from the keywords
# we should expect to see in the JSON that should be sent,
# including the summary provided in the test call above.
Assert-MockCalled -CommandName Invoke-JiraMethod -ModuleName JiraPS -Times 1 -Scope It -ParameterFilter { $Method -eq 'Post' -and $URI -like "$jiraServer/rest/api/*/issue" }
}
It "Creates an issue in JIRA from pipeline" {
{ $pipelineParams | New-JiraIssue } | Should Not Throw
# The String in the ParameterFilter is made from the keywords
# we should expect to see in the JSON that should be sent,
# including the summary provided in the test call above.
Assert-MockCalled -CommandName Invoke-JiraMethod -ModuleName JiraPS -Times 1 -Scope It -ParameterFilter { $Method -eq 'Post' -and $URI -like "$jiraServer/rest/api/*/issue" }
}
}
Context "Input testing" {
It "Checks to make sure all required fields are provided" {
# We'll create a custom field that's required, then see what happens when we don't provide it
Mock Get-JiraIssueCreateMetadata {
@(
@{Name = 'Project'; ID = 'Project'; Required = $true}
@{Name = 'IssueType'; ID = 'IssueType'; Required = $true}
@{Name = 'Priority'; ID = 'Priority'; Required = $true}
@{Name = 'Summary'; ID = 'Summary'; Required = $true}
@{Name = 'Description'; ID = 'Description'; Required = $true}
@{Name = 'Reporter'; ID = 'Reporter'; Required = $true}
@{Name = 'CustomField'; ID = 'CustomField'; Required = $true}
)
}
{ New-JiraIssue @newParams } | Should Throw
{ New-JiraIssue @newParams -Fields @{'CustomField' = '.'} } | Should Not Throw
}
}
}
}