Skip to content

Commit 8ccc96e

Browse files
committed
Add AadToken tests
1 parent 7bace69 commit 8ccc96e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tests/Get-AadToken.Tests.ps1

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Get-Module cosmos-db | Remove-Module -Force
2+
Import-Module $PSScriptRoot\..\cosmos-db\cosmos-db.psm1 -Force
3+
4+
InModuleScope cosmos-db {
5+
Describe "Get-AadToken" {
6+
BeforeAll {
7+
$MOCK_TOKEN = @{
8+
accessToken = "MOCK_TOKEN"
9+
expires_on = [System.DateTimeOffset]::UtcNow.AddHours(1).ToUnixTimeSeconds()
10+
}
11+
12+
Mock Get-AadTokenWithoutCaching {
13+
return $MOCK_TOKEN
14+
}
15+
}
16+
17+
BeforeEach {
18+
# This is defined in the main module
19+
$AAD_TOKEN_CACHE = @{}
20+
}
21+
22+
It "Only calls the core logic once with caching enabled" {
23+
Use-CosmosDbInternalFlag -EnableCaching $true
24+
25+
$key = Get-AadToken
26+
$key | Should -Be $MOCK_TOKEN.accessToken | Out-Null
27+
28+
$key = Get-AadToken
29+
$key | Should -Be $MOCK_TOKEN.accessToken | Out-Null
30+
31+
Assert-MockCalled Get-AadTokenWithoutCaching -Times 1 -Exactly
32+
}
33+
34+
It "Calls the core logic for each call with caching disabled" {
35+
Use-CosmosDbInternalFlag -EnableCaching $false
36+
37+
$key1 = Get-AadToken
38+
$key1 | Should -Be $MOCK_TOKEN.accessToken | Out-Null
39+
40+
$key2 = Get-AadToken
41+
$key2 | Should -Be $MOCK_TOKEN.accessToken | Out-Null
42+
43+
Assert-MockCalled Get-AadTokenWithoutCaching -Times 2 -Exactly
44+
}
45+
46+
It "Respects token expiration" {
47+
Use-CosmosDbInternalFlag -EnableCaching $true
48+
49+
$MOCK_TOKEN = @{
50+
accessToken = "MOCK_TOKEN"
51+
expires_on = [System.DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
52+
}
53+
54+
Mock Get-AadTokenWithoutCaching {
55+
return $MOCK_TOKEN
56+
}
57+
58+
$key1 = Get-AadToken
59+
$key1 | Should -Be $MOCK_TOKEN.accessToken | Out-Null
60+
61+
$key2 = Get-AadToken
62+
$key2 | Should -Be $MOCK_TOKEN.accessToken | Out-Null
63+
64+
Assert-MockCalled Get-AadTokenWithoutCaching -Times 2 -Exactly
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)