1
1
namespace Microsoft . Graph . Authentication . Test . TokenCache
2
2
{
3
+ using Microsoft . Graph . PowerShell . Authentication ;
3
4
using Microsoft . Graph . PowerShell . Authentication . TokenCache ;
4
5
using System ;
5
6
using System . Text ;
6
7
using System . Threading ;
7
8
using System . Threading . Tasks ;
8
9
using Xunit ;
9
10
10
- public class TokenCacheStorageTests : IDisposable
11
+ public class CurrentUserTokenCacheStorageTests : IDisposable
11
12
{
12
- private const string TestAppId1 = "test_app_id_1" ;
13
+ private const ContextScope _userContextScope = ContextScope . CurrentUser ;
14
+ private readonly IAuthContext _testAppContext1 = new AuthContext { ClientId = "test_app_id_1" , ContextScope = _userContextScope } ;
13
15
14
16
[ Fact ]
15
17
public void ShouldStoreNewTokenToPlatformCache ( )
@@ -19,15 +21,15 @@ public void ShouldStoreNewTokenToPlatformCache()
19
21
byte [ ] bufferToStore = Encoding . UTF8 . GetBytes ( strContent ) ;
20
22
21
23
// Act
22
- TokenCacheStorage . SetToken ( TestAppId1 , bufferToStore ) ;
24
+ TokenCacheStorage . SetToken ( _testAppContext1 , bufferToStore ) ;
23
25
24
26
// Assert
25
- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
27
+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
26
28
Assert . Equal ( bufferToStore . Length , storedBuffer . Length ) ;
27
29
Assert . Equal ( strContent , Encoding . UTF8 . GetString ( storedBuffer ) ) ;
28
30
29
31
// Cleanup
30
- CleanTokenCache ( TestAppId1 ) ;
32
+ CleanTokenCache ( _testAppContext1 ) ;
31
33
}
32
34
33
35
[ Fact ]
@@ -37,26 +39,26 @@ public void ShouldStoreMultipleAppTokensInPlatformCache()
37
39
string app1StrContent = "random data for app 1." ;
38
40
byte [ ] app1BufferToStore = Encoding . UTF8 . GetBytes ( app1StrContent ) ;
39
41
40
- string TestAppId2 = "test_app_id_2" ;
42
+ IAuthContext testAppContext2 = new AuthContext { ClientId = "test_app_id_2" , ContextScope = _userContextScope } ;
41
43
string app2StrContent = "random data for app 2 plus more data." ;
42
44
byte [ ] app2BufferToStore = Encoding . UTF8 . GetBytes ( app2StrContent ) ;
43
45
44
46
// Act
45
- TokenCacheStorage . SetToken ( TestAppId1 , app1BufferToStore ) ;
46
- TokenCacheStorage . SetToken ( TestAppId2 , app2BufferToStore ) ;
47
+ TokenCacheStorage . SetToken ( _testAppContext1 , app1BufferToStore ) ;
48
+ TokenCacheStorage . SetToken ( testAppContext2 , app2BufferToStore ) ;
47
49
48
50
// Assert
49
- byte [ ] app1StoredBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
51
+ byte [ ] app1StoredBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
50
52
Assert . Equal ( app1BufferToStore . Length , app1StoredBuffer . Length ) ;
51
53
Assert . Equal ( app1StrContent , Encoding . UTF8 . GetString ( app1StoredBuffer ) ) ;
52
54
53
- byte [ ] app2StoredBuffer = TokenCacheStorage . GetToken ( TestAppId2 ) ;
55
+ byte [ ] app2StoredBuffer = TokenCacheStorage . GetToken ( testAppContext2 ) ;
54
56
Assert . Equal ( app2BufferToStore . Length , app2StoredBuffer . Length ) ;
55
57
Assert . Equal ( app2StrContent , Encoding . UTF8 . GetString ( app2StoredBuffer ) ) ;
56
58
57
59
// Cleanup
58
- CleanTokenCache ( TestAppId1 ) ;
59
- CleanTokenCache ( TestAppId2 ) ;
60
+ CleanTokenCache ( _testAppContext1 ) ;
61
+ CleanTokenCache ( testAppContext2 ) ;
60
62
}
61
63
62
64
@@ -66,31 +68,31 @@ public void ShouldUpdateTokenInPlatformCache()
66
68
// Arrange
67
69
string originalStrContent = "random data for app." ;
68
70
byte [ ] originalBuffer = Encoding . UTF8 . GetBytes ( originalStrContent ) ;
69
- TokenCacheStorage . SetToken ( TestAppId1 , originalBuffer ) ;
71
+ TokenCacheStorage . SetToken ( _testAppContext1 , originalBuffer ) ;
70
72
71
73
// Act
72
74
string strContentToUpdate = "updated random data for app." ;
73
75
byte [ ] updateBuffer = Encoding . UTF8 . GetBytes ( strContentToUpdate ) ;
74
- TokenCacheStorage . SetToken ( TestAppId1 , updateBuffer ) ;
76
+ TokenCacheStorage . SetToken ( _testAppContext1 , updateBuffer ) ;
75
77
76
78
// Assert
77
- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
79
+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
78
80
Assert . NotEqual ( originalBuffer . Length , storedBuffer . Length ) ;
79
81
Assert . Equal ( updateBuffer . Length , storedBuffer . Length ) ;
80
82
Assert . Equal ( strContentToUpdate , Encoding . UTF8 . GetString ( storedBuffer ) ) ;
81
83
82
84
// Cleanup
83
- CleanTokenCache ( TestAppId1 ) ;
85
+ CleanTokenCache ( _testAppContext1 ) ;
84
86
}
85
87
86
88
[ Fact ]
87
89
public void ShouldReturnNoContentWhenPlatformCacheIsEmpty ( )
88
90
{
89
91
// Arrange
90
- CleanTokenCache ( TestAppId1 ) ;
92
+ CleanTokenCache ( _testAppContext1 ) ;
91
93
92
94
// Act
93
- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
95
+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
94
96
95
97
// Assert
96
98
Assert . Empty ( storedBuffer ) ;
@@ -102,13 +104,13 @@ public void ShouldDeleteCache()
102
104
// Arrange
103
105
string originalStrContent = "random data for app." ;
104
106
byte [ ] originalBuffer = Encoding . UTF8 . GetBytes ( originalStrContent ) ;
105
- TokenCacheStorage . SetToken ( TestAppId1 , originalBuffer ) ;
107
+ TokenCacheStorage . SetToken ( _testAppContext1 , originalBuffer ) ;
106
108
107
109
// Act
108
- TokenCacheStorage . DeleteToken ( TestAppId1 ) ;
110
+ TokenCacheStorage . DeleteToken ( _testAppContext1 ) ;
109
111
110
112
// Assert
111
- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
113
+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
112
114
Assert . Empty ( storedBuffer ) ;
113
115
}
114
116
@@ -124,15 +126,16 @@ public void ShouldMakeParallelCallsToTokenCache()
124
126
// Act
125
127
Parallel . For ( 0 , executions , ( index ) => {
126
128
byte [ ] contentBuffer = Encoding . UTF8 . GetBytes ( index . ToString ( ) ) ;
127
- TokenCacheStorage . SetToken ( $ "{ index } ", contentBuffer ) ;
129
+ var testAuthContext = new AuthContext { ClientId = index . ToString ( ) , ContextScope = _userContextScope } ;
130
+ TokenCacheStorage . SetToken ( testAuthContext , contentBuffer ) ;
128
131
129
- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( index . ToString ( ) ) ;
132
+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( testAuthContext ) ;
130
133
if ( index . ToString ( ) != Encoding . UTF8 . GetString ( storedBuffer ) )
131
134
{
132
135
failed = true ;
133
136
}
134
137
135
- CleanTokenCache ( index . ToString ( ) ) ;
138
+ CleanTokenCache ( testAuthContext ) ;
136
139
Interlocked . Increment ( ref count ) ;
137
140
} ) ;
138
141
@@ -143,12 +146,12 @@ public void ShouldMakeParallelCallsToTokenCache()
143
146
144
147
public void Dispose ( )
145
148
{
146
- CleanTokenCache ( TestAppId1 ) ;
149
+ CleanTokenCache ( _testAppContext1 ) ;
147
150
}
148
151
149
- private void CleanTokenCache ( string appId )
152
+ private void CleanTokenCache ( IAuthContext authContext )
150
153
{
151
- TokenCacheStorage . DeleteToken ( appId ) ;
154
+ TokenCacheStorage . DeleteToken ( authContext ) ;
152
155
}
153
156
}
154
157
}
0 commit comments