1+ Import-Module - Name $PSScriptRoot \..\Arcus.Scripting.Security - ErrorAction Stop
2+ Import-Module - Name $PSScriptRoot \..\Arcus.Scripting.LogicApps - ErrorAction Stop
3+
4+ InModuleScope Arcus.Scripting.LogicApps {
5+ Describe " Arcus Azure Logic Apps unit tests" {
6+ Context " Enable Logic Apps without configuration" {
7+ It " Fails to enable an unknown Azure Logic App" {
8+ # Arrange
9+ $resourceGroupName = " codit-arcus-scripting"
10+ $logicAppName = " arc-dev-we-rcv-unknown-http"
11+ $errorContent = " {"" error"" :{"" code"" :"" ResourceNotFound"" ,"" message"" :"" Unable to find the resource Microsoft.Logic/workflows/$logicAppName within resourcegroup codit-arcus-scripting."" }}"
12+ Mock Write-Warning - MockWith { } - ParameterFilter {$Message -like " Failed to enable $logicAppName " }
13+ Mock Write-Warning - MockWith { } - ParameterFilter {$Message -like " Error: $errorContent " }
14+ Mock Invoke-WebRequest - MockWith {
15+ $status = [System.Net.WebExceptionStatus ]::ConnectionClosed
16+ $response = New-Object - type ' System.Net.HttpWebResponse'
17+ $response | Add-Member - MemberType noteProperty - Name ' StatusCode' - Value 404 - force
18+ $exception = New-Object System.Net.WebException $errorContent , $null , $status , $response
19+
20+ Throw $exception
21+ }
22+ Mock Get-AzCachedAccessToken - MockWith {
23+ return @ {
24+ SubscriptionId = " 123456"
25+ AccessToken = " accessToken"
26+ }
27+ }
28+
29+ # Act
30+ Enable-AzLogicApp - ResourceGroupName $resourceGroupName - LogicAppName $logicAppName
31+
32+
33+ # Assert
34+ Assert-VerifiableMock
35+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
36+ Assert-MockCalled Write-Warning - Scope It - Times 1 - ParameterFilter { $Message -eq " Failed to enable $logicAppName " }
37+ Assert-MockCalled Write-Warning - Scope It - Times 1 - ParameterFilter { $Message -eq " Error: $errorContent " }
38+ }
39+ }
40+ Context " Enable Logic Apps with configuration" {
41+ It " Doesn't enable anything when the stopType is not recognized" {
42+ # Arrange
43+ Mock Get-AzLogicAppRunHistory {}
44+ Mock Enable-AzLogicApp {}
45+ Mock Get-AzCachedAccessToken - MockWith {
46+ return @ {
47+ SubscriptionId = " 123456"
48+ AccessToken = " accessToken"
49+ }
50+ }
51+
52+ # Act
53+ Enable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-unknownStopType.json" - ResourceGroupName " ignored-resource-group"
54+
55+ # Assert
56+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
57+ Assert-MockCalled Get-AzLogicAppRunHistory - Scope It - Times 0
58+ Assert-MockCalled Enable-AzLogicApp - Scope It - Exactly 0
59+ }
60+ It " Doesn't enable anything when both checkType & stopType is 'None'" {
61+ # Arrange
62+ Mock Get-AzLogicAppRunHistory {}
63+ Mock Enable-AzLogicApp {}
64+ Mock Get-AzCachedAccessToken - MockWith {
65+ return @ {
66+ SubscriptionId = " 123456"
67+ AccessToken = " accessToken"
68+ }
69+ }
70+
71+ # Act
72+ Enable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-none.json" - ResourceGroupName " ignored-resource-group"
73+
74+ # Assert
75+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
76+ Assert-MockCalled Get-AzLogicAppRunHistory - Scope It - Times 0
77+ Assert-MockCalled Enable-AzLogicApp - Scope It - Exactly 0
78+ }
79+ }
80+ Context " Disable Logic Apps without configuration" {
81+ It " Fails to disable an unknown Azure Logic App" {
82+ # Arrange
83+ $resourceGroupName = " codit-arcus-scripting"
84+ $logicAppName = " arc-dev-we-rcv-unknown-http"
85+ $errorContent = " {"" error"" :{"" code"" :"" ResourceNotFound"" ,"" message"" :"" Unable to find the resource Microsoft.Logic/workflows/$logicAppName within resourcegroup codit-arcus-scripting."" }}"
86+ Mock Write-Warning - MockWith { } - ParameterFilter {$Message -like " Failed to disable $logicAppName " }
87+ Mock Write-Warning - MockWith { } - ParameterFilter {$Message -like " Error: $errorContent " }
88+ Mock Invoke-WebRequest - MockWith {
89+ $status = [System.Net.WebExceptionStatus ]::ConnectionClosed
90+ $response = New-Object - type ' System.Net.HttpWebResponse'
91+ $response | Add-Member - MemberType noteProperty - Name ' StatusCode' - Value 404 - force
92+ $exception = New-Object System.Net.WebException $errorContent , $null , $status , $response
93+
94+ Throw $exception
95+ }
96+ Mock Get-AzCachedAccessToken - MockWith {
97+ return @ {
98+ SubscriptionId = " 123456"
99+ AccessToken = " accessToken"
100+ }
101+ }
102+
103+ # Act
104+ Disable-AzLogicApp - ResourceGroupName $resourceGroupName - LogicAppName $logicAppName
105+
106+
107+ # Assert
108+ Assert-VerifiableMock
109+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
110+ Assert-MockCalled Write-Warning - Scope It - Times 1 - ParameterFilter { $Message -eq " Failed to disable $logicAppName " }
111+ Assert-MockCalled Write-Warning - Scope It - Times 1 - ParameterFilter { $Message -eq " Error: $errorContent " }
112+ }
113+ }
114+ Context " Disable Logic Apps with configuration" {
115+ It " Doesn't disable anything when the checkType is not recognized" {
116+ # Arrange
117+ Mock Get-AzLogicAppRunHistory {}
118+ Mock Disable-AzLogicApp {}
119+ Mock Get-AzCachedAccessToken - MockWith {
120+ return @ {
121+ SubscriptionId = " 123456"
122+ AccessToken = " accessToken"
123+ }
124+ }
125+
126+ # Act
127+ Disable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-unknownCheckType.json" - ResourceGroup " ignored-resource-group"
128+
129+ # Assert
130+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
131+ Assert-MockCalled Get-AzLogicAppRunHistory - Scope It - Times 0
132+ Assert-MockCalled Disable-AzLogicApp - Scope It - Exactly 0
133+ }
134+ It " Doesn't disable anything when the stopType is not recognized" {
135+ # Arrange
136+ Mock Get-AzLogicAppRunHistory {}
137+ Mock Disable-AzLogicApp {}
138+ Mock Get-AzCachedAccessToken - MockWith {
139+ return @ {
140+ SubscriptionId = " 123456"
141+ AccessToken = " accessToken"
142+ }
143+ }
144+
145+ # Act
146+ Disable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-unknownStopType.json" - ResourceGroupName " ignored-resource-group"
147+
148+ # Assert
149+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
150+ Assert-MockCalled Get-AzLogicAppRunHistory - Scope It - Times 0
151+ Assert-MockCalled Disable-AzLogicApp - Scope It - Exactly 0
152+ }
153+ It " Doesn't disable anything when both checkType & stopType is 'None'" {
154+ # Arrange
155+ Mock Get-AzLogicAppRunHistory {}
156+ Mock Disable-AzLogicApp {}
157+ Mock Get-AzCachedAccessToken - MockWith {
158+ return @ {
159+ SubscriptionId = " 123456"
160+ AccessToken = " accessToken"
161+ }
162+ }
163+
164+ # Act
165+ Disable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-none.json" - ResourceGroupName " ignored-resource-group"
166+
167+ # Assert
168+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
169+ Assert-MockCalled Get-AzLogicAppRunHistory - Scope It - Times 0
170+ Assert-MockCalled Disable-AzLogicApp - Scope It - Exactly 0
171+ }
172+ It " Doesn't disable anything when checkType = NoWaitingOrRunningRuns but returns a zero count on the running runs for unknown stopType" {
173+ # Arrange
174+ $resourceGroup = " my-resource-group"
175+ $logicAppNames = @ (" snd-async" , " ord-sthp-harvest-order-doublechecker" , " ord-sthp-harvest-order-doublechecker" )
176+ Mock Get-AzLogicAppRunHistory {
177+ $ResourceGroupName | Should - Be $resourceGroup
178+ $Name | Should - BeIn $logicAppNames
179+ return @ ([pscustomobject ]@ { Status = " Waiting" })
180+ }
181+ Mock Disable-AzLogicApp {}
182+ Mock Get-AzCachedAccessToken - MockWith {
183+ return @ {
184+ SubscriptionId = " 123456"
185+ AccessToken = " accessToken"
186+ }
187+ }
188+
189+ # Act
190+ Disable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json" - ResourceGroupName $resourceGroup
191+
192+ # Assert
193+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
194+ Assert-MockCalled Get-AzLogicAppRunHistory - Scope It - Times 6 - ParameterFilter { $ResourceGroupName -eq $resourceGroup }
195+ Assert-MockCalled Disable-AzLogicApp - Scope It - Exactly 0
196+ }
197+ It " Doesn't disable anything when checkType = NoWaitingOrRunningRuns but returns a zero count on an the waiting runs for stopType = None" {
198+ # Arrange
199+ $resourceGroup = " my-resource-group"
200+ $logicAppNames = @ (" snd-async" , " ord-sthp-harvest-order-doublechecker" )
201+ Mock Get-AzLogicAppRunHistory {
202+ $ResourceGroupName | Should - Be $resourceGroup
203+ $Name | Should - BeIn $logicAppNames
204+ return @ ([pscustomobject ]{ Status = " Running" })
205+ }
206+ Mock Disable-AzLogicApp {}
207+ Mock Get-AzCachedAccessToken - MockWith {
208+ return @ {
209+ SubscriptionId = " 123456"
210+ AccessToken = " accessToken"
211+ }
212+ }
213+
214+ # Act
215+ Disable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json" - ResourceGroupName $resourceGroup
216+
217+ # Assert
218+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
219+ Assert-MockCalled Get-AzLogicAppRunHistory - Times 4 - ParameterFilter { $ResourceGroupName -eq $resourceGroup }
220+ Assert-MockCalled Disable-AzLogicApp - Scope It - Exactly 0
221+ }
222+ It " Disbales all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and no running runs for stopType = Immediate" {
223+ # Arrange
224+ $resourceGroup = " my-resource-group"
225+ $logicAppNames = @ (" snd-async" , " ord-sthp-harvest-order-doublechecker" , " rcv-sthp-harvest-order-af-ftp" , " rcv-sthp-harvest-order-af-sft" , " rcv-sthp-harvest-order-af-file" )
226+ Mock Get-AzLogicAppRunHistory {
227+ $ResourceGroupName | Should - Be $resourceGroup
228+ $Name | Should - BeIn $logicAppNames
229+ return @ ([pscustomobject ]@ { Status = " Waiting" })
230+ }
231+ Mock Disable-AzLogicApp {
232+ $ResourceGroupName | Should - Be $resourceGroup
233+ $LogicAppName | Should - BeIn $logicAppNames
234+ }
235+ Mock Get-AzCachedAccessToken - MockWith {
236+ return @ {
237+ SubscriptionId = " 123456"
238+ AccessToken = " accessToken"
239+ }
240+ }
241+
242+ # Act
243+ Disable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json" - ResourceGroupName $resourceGroup
244+
245+ # Assert
246+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
247+ Assert-MockCalled Get-AzLogicAppRunHistory - Scope It - Times 10 - ParameterFilter { $ResourceGroupName -eq $resourceGroup }
248+ Assert-MockCalled Disable-AzLogicApp - Scope It - Exactly 5 - ParameterFilter { $resourceGroupName -eq $resourceGroup }
249+ }
250+ It " Disables all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and running runs for stopType = Immediate" {
251+ # Arrange
252+ $resourceGroup = " my-resource-group"
253+ $i = 0
254+ Mock Get-AzLogicAppRunHistory {
255+ $ResourceGroupName | Should - Be $resourceGroup
256+ $Name | Should - Be " snd-async"
257+ $script :i ++
258+ if ($script :i -gt 2 ) {
259+ Write-Host " Returning empty (no running, no waiting) runs"
260+ return @ ()
261+ } else {
262+ Write-Host " Returning 1 running & 1 waiting runs"
263+ return @ (
264+ [pscustomobject ]@ { Status = " Running" },
265+ [pscustomobject ]@ { Status = " Waiting" }
266+ )
267+ }
268+ }
269+ Mock Disable-AzLogicApp {
270+ $ResourceGroupName | Should - Be $resourceGroup
271+ $LogicAppName | Should - Be " snd-async"
272+ }
273+ Mock Get-AzCachedAccessToken - MockWith {
274+ return @ {
275+ SubscriptionId = " 123456"
276+ AccessToken = " accessToken"
277+ }
278+ }
279+
280+ # Act
281+ Disable-AzLogicAppsFromConfig - DeployFileName " $PSScriptRoot \Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json" - ResourceGroupName $resourceGroup
282+
283+ # Assert
284+ Assert-MockCalled Get-AzCachedAccessToken - Scope It - Times 1
285+ Assert-MockCalled Disable-AzLogicApp - Scope It - Exactly 1 - ParameterFilter { $ResourceGroupName -eq $resourceGroup -and $LogicAppName -eq " snd-async" }
286+ }
287+ }
288+ }
289+ }
0 commit comments