forked from rmbolger/Posh-ACME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzure.Tests.ps1
142 lines (124 loc) · 6.92 KB
/
Azure.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
. $PSScriptRoot\..\Posh-ACME\DnsPlugins\Azure.ps1
. $PSScriptRoot\..\Posh-ACME\Private\MockWrappers.ps1
Describe "Connect-AZTenant" {
$fakeTokenResponse = [pscustomobject]@{
expires_on = '1530691200' # 2018-07-04 08:00:00 UTC
access_token = 'faketoken'
}
$script:UseBasic = @{}
Mock -CommandName Invoke-RestMethod -MockWith { return $fakeTokenResponse }
Mock -CommandName ConvertFrom-AccessToken -MockWith { return $fakeTokenResponse }
Mock -CommandName Get-DateTimeOffsetNow -MockWith {
return [DateTimeOffset]::Parse('2018-07-04T09:00:00Z')
}
$fakeGoodToken = [pscustomobject]@{
Expires = [DateTimeOffset]::Parse('2018-07-04T09:05:00Z') # just after mocked "Now"
AuthHeader = @{ Authorization = 'Bearer fakegoodtoken' }
}
$fakeExpiredToken = [pscustomobject]@{
Expires = [DateTimeOffset]::Parse('2018-07-04T08:55:00Z') # just before mocked "Now"
AuthHeader = @{ Authorization = 'Bearer fakeexpiredtoken' }
}
Context "Credential param set" {
$fakeTenant = '00000000-0000-0000-0000-000000000000'
$fakePass = "fake+p&ss" | ConvertTo-SecureString -AsPlainText -Force
$fakeCred = New-Object System.Management.Automation.PSCredential('fake user', $fakePass)
It "calls Invoke-RestMethod if no existing token" {
$script:AZToken = $null
Connect-AZTenant -AZTenantId $fakeTenant -AZAppCred $fakeCred
Assert-MockCalled -CommandName Invoke-RestMethod -Times 1 -Exactly -Scope It -ParameterFilter {
$Body -match "[&?]client_id=fake%20user(&|$)" -and $Body -match "[&?]client_secret=fake%2[Bb]p%26ss(&|$)"
}
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 0 -Exactly -Scope It
}
It "calls Invoke-RestMethod if token expired" {
$script:AZToken = $fakeExpiredToken
Connect-AZTenant -AZTenantId $fakeTenant -AZAppCred $fakeCred
Assert-MockCalled -CommandName Invoke-RestMethod -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 0 -Exactly -Scope It
}
It "sets new AZToken if token expired" {
$script:AZToken | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader.Authorization | Should -BeExactly 'Bearer faketoken'
$script:AZToken.Expires | Should -Be ([DateTimeOffset]::Parse('2018-07-04T07:55:00Z'))
}
It "calls nothing if current token is valid" {
$script:AZToken = $fakeGoodToken
Connect-AZTenant -AZTenantId $fakeTenant -AZAppCred $fakeCred
Assert-MockCalled -CommandName Invoke-RestMethod -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 0 -Exactly -Scope It
}
It "does not overwrite existing token if current token is valid" {
$script:AZToken | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader.Authorization | Should -BeExactly $fakeGoodToken.AuthHeader.Authorization
$script:AZToken.Expires | Should -Be $fakeGoodToken.Expires
}
}
Context "Token param set" {
$fakeAccessToken = 'blah.blah.blah'
It "calls ConvertFrom-AccessToken if no existing token" {
$script:AZToken = $null
Connect-AZTenant -AZAccessToken $fakeAccessToken
Assert-MockCalled -CommandName Invoke-RestMethod -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 1 -Exactly -Scope It
}
It "calls ConvertFrom-AccessToken if token expired" {
$script:AZToken = $fakeExpiredToken
Connect-AZTenant -AZAccessToken $fakeAccessToken
Assert-MockCalled -CommandName Invoke-RestMethod -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 1 -Exactly -Scope It
}
It "uses passed in token if AZToken expired" {
$script:AZToken | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader.Authorization | Should -BeExactly 'Bearer faketoken'
$script:AZToken.Expires | Should -Be ([DateTimeOffset]::Parse('2018-07-04T07:55:00Z'))
}
It "calls nothing if current token is valid" {
$script:AZToken = $fakeGoodToken
Connect-AZTenant -AZAccessToken $fakeAccessToken
Assert-MockCalled -CommandName Invoke-RestMethod -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 0 -Exactly -Scope It
}
It "does not overwrite existing token if current token is valid" {
$script:AZToken | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader.Authorization | Should -BeExactly $fakeGoodToken.AuthHeader.Authorization
$script:AZToken.Expires | Should -Be $fakeGoodToken.Expires
}
}
Context "IMDS param set" {
It "calls Invoke-RestMethod if no existing token" {
$script:AZToken = $null
Connect-AZTenant -AZUseIMDS
Assert-MockCalled -CommandName Invoke-RestMethod -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 0 -Exactly -Scope It
}
It "calls Invoke-RestMethod if token expired" {
$script:AZToken = $fakeExpiredToken
Connect-AZTenant -AZUseIMDS
Assert-MockCalled -CommandName Invoke-RestMethod -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 0 -Exactly -Scope It
}
It "sets new AZToken if token expired" {
$script:AZToken | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader.Authorization | Should -BeExactly 'Bearer faketoken'
$script:AZToken.Expires | Should -Be ([DateTimeOffset]::Parse('2018-07-04T07:55:00Z'))
}
It "calls nothing if current token is valid" {
$script:AZToken = $fakeGoodToken
Connect-AZTenant -AZUseIMDS
Assert-MockCalled -CommandName Invoke-RestMethod -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName ConvertFrom-AccessToken -Times 0 -Exactly -Scope It
}
It "does not overwrite existing token if current token is valid" {
$script:AZToken | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader | Should -Not -BeNullOrEmpty
$script:AZToken.AuthHeader.Authorization | Should -BeExactly $fakeGoodToken.AuthHeader.Authorization
$script:AZToken.Expires | Should -Be $fakeGoodToken.Expires
}
}
}