11namespace Microsoft . Graph . Authentication . Test . TokenCache
22{
3+ using Microsoft . Graph . PowerShell . Authentication ;
34 using Microsoft . Graph . PowerShell . Authentication . TokenCache ;
45 using System ;
56 using System . Text ;
67 using System . Threading ;
78 using System . Threading . Tasks ;
89 using Xunit ;
910
10- public class TokenCacheStorageTests : IDisposable
11+ public class CurrentUserTokenCacheStorageTests : IDisposable
1112 {
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 } ;
1315
1416 [ Fact ]
1517 public void ShouldStoreNewTokenToPlatformCache ( )
@@ -19,15 +21,15 @@ public void ShouldStoreNewTokenToPlatformCache()
1921 byte [ ] bufferToStore = Encoding . UTF8 . GetBytes ( strContent ) ;
2022
2123 // Act
22- TokenCacheStorage . SetToken ( TestAppId1 , bufferToStore ) ;
24+ TokenCacheStorage . SetToken ( _testAppContext1 , bufferToStore ) ;
2325
2426 // Assert
25- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
27+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
2628 Assert . Equal ( bufferToStore . Length , storedBuffer . Length ) ;
2729 Assert . Equal ( strContent , Encoding . UTF8 . GetString ( storedBuffer ) ) ;
2830
2931 // Cleanup
30- CleanTokenCache ( TestAppId1 ) ;
32+ CleanTokenCache ( _testAppContext1 ) ;
3133 }
3234
3335 [ Fact ]
@@ -37,26 +39,26 @@ public void ShouldStoreMultipleAppTokensInPlatformCache()
3739 string app1StrContent = "random data for app 1." ;
3840 byte [ ] app1BufferToStore = Encoding . UTF8 . GetBytes ( app1StrContent ) ;
3941
40- string TestAppId2 = "test_app_id_2" ;
42+ IAuthContext testAppContext2 = new AuthContext { ClientId = "test_app_id_2" , ContextScope = _userContextScope } ;
4143 string app2StrContent = "random data for app 2 plus more data." ;
4244 byte [ ] app2BufferToStore = Encoding . UTF8 . GetBytes ( app2StrContent ) ;
4345
4446 // Act
45- TokenCacheStorage . SetToken ( TestAppId1 , app1BufferToStore ) ;
46- TokenCacheStorage . SetToken ( TestAppId2 , app2BufferToStore ) ;
47+ TokenCacheStorage . SetToken ( _testAppContext1 , app1BufferToStore ) ;
48+ TokenCacheStorage . SetToken ( testAppContext2 , app2BufferToStore ) ;
4749
4850 // Assert
49- byte [ ] app1StoredBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
51+ byte [ ] app1StoredBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
5052 Assert . Equal ( app1BufferToStore . Length , app1StoredBuffer . Length ) ;
5153 Assert . Equal ( app1StrContent , Encoding . UTF8 . GetString ( app1StoredBuffer ) ) ;
5254
53- byte [ ] app2StoredBuffer = TokenCacheStorage . GetToken ( TestAppId2 ) ;
55+ byte [ ] app2StoredBuffer = TokenCacheStorage . GetToken ( testAppContext2 ) ;
5456 Assert . Equal ( app2BufferToStore . Length , app2StoredBuffer . Length ) ;
5557 Assert . Equal ( app2StrContent , Encoding . UTF8 . GetString ( app2StoredBuffer ) ) ;
5658
5759 // Cleanup
58- CleanTokenCache ( TestAppId1 ) ;
59- CleanTokenCache ( TestAppId2 ) ;
60+ CleanTokenCache ( _testAppContext1 ) ;
61+ CleanTokenCache ( testAppContext2 ) ;
6062 }
6163
6264
@@ -66,31 +68,31 @@ public void ShouldUpdateTokenInPlatformCache()
6668 // Arrange
6769 string originalStrContent = "random data for app." ;
6870 byte [ ] originalBuffer = Encoding . UTF8 . GetBytes ( originalStrContent ) ;
69- TokenCacheStorage . SetToken ( TestAppId1 , originalBuffer ) ;
71+ TokenCacheStorage . SetToken ( _testAppContext1 , originalBuffer ) ;
7072
7173 // Act
7274 string strContentToUpdate = "updated random data for app." ;
7375 byte [ ] updateBuffer = Encoding . UTF8 . GetBytes ( strContentToUpdate ) ;
74- TokenCacheStorage . SetToken ( TestAppId1 , updateBuffer ) ;
76+ TokenCacheStorage . SetToken ( _testAppContext1 , updateBuffer ) ;
7577
7678 // Assert
77- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
79+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
7880 Assert . NotEqual ( originalBuffer . Length , storedBuffer . Length ) ;
7981 Assert . Equal ( updateBuffer . Length , storedBuffer . Length ) ;
8082 Assert . Equal ( strContentToUpdate , Encoding . UTF8 . GetString ( storedBuffer ) ) ;
8183
8284 // Cleanup
83- CleanTokenCache ( TestAppId1 ) ;
85+ CleanTokenCache ( _testAppContext1 ) ;
8486 }
8587
8688 [ Fact ]
8789 public void ShouldReturnNoContentWhenPlatformCacheIsEmpty ( )
8890 {
8991 // Arrange
90- CleanTokenCache ( TestAppId1 ) ;
92+ CleanTokenCache ( _testAppContext1 ) ;
9193
9294 // Act
93- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
95+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
9496
9597 // Assert
9698 Assert . Empty ( storedBuffer ) ;
@@ -102,13 +104,13 @@ public void ShouldDeleteCache()
102104 // Arrange
103105 string originalStrContent = "random data for app." ;
104106 byte [ ] originalBuffer = Encoding . UTF8 . GetBytes ( originalStrContent ) ;
105- TokenCacheStorage . SetToken ( TestAppId1 , originalBuffer ) ;
107+ TokenCacheStorage . SetToken ( _testAppContext1 , originalBuffer ) ;
106108
107109 // Act
108- TokenCacheStorage . DeleteToken ( TestAppId1 ) ;
110+ TokenCacheStorage . DeleteToken ( _testAppContext1 ) ;
109111
110112 // Assert
111- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( TestAppId1 ) ;
113+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( _testAppContext1 ) ;
112114 Assert . Empty ( storedBuffer ) ;
113115 }
114116
@@ -124,15 +126,16 @@ public void ShouldMakeParallelCallsToTokenCache()
124126 // Act
125127 Parallel . For ( 0 , executions , ( index ) => {
126128 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 ) ;
128131
129- byte [ ] storedBuffer = TokenCacheStorage . GetToken ( index . ToString ( ) ) ;
132+ byte [ ] storedBuffer = TokenCacheStorage . GetToken ( testAuthContext ) ;
130133 if ( index . ToString ( ) != Encoding . UTF8 . GetString ( storedBuffer ) )
131134 {
132135 failed = true ;
133136 }
134137
135- CleanTokenCache ( index . ToString ( ) ) ;
138+ CleanTokenCache ( testAuthContext ) ;
136139 Interlocked . Increment ( ref count ) ;
137140 } ) ;
138141
@@ -143,12 +146,12 @@ public void ShouldMakeParallelCallsToTokenCache()
143146
144147 public void Dispose ( )
145148 {
146- CleanTokenCache ( TestAppId1 ) ;
149+ CleanTokenCache ( _testAppContext1 ) ;
147150 }
148151
149- private void CleanTokenCache ( string appId )
152+ private void CleanTokenCache ( IAuthContext authContext )
150153 {
151- TokenCacheStorage . DeleteToken ( appId ) ;
154+ TokenCacheStorage . DeleteToken ( authContext ) ;
152155 }
153156 }
154157}
0 commit comments