@@ -18,6 +18,7 @@ internal class AuthorizedServiceCallerTests : AuthorizedServiceTestsBase
1818 public void SetUp ( )
1919 {
2020 TokenStorageMock = new Mock < ITokenStorage > ( ) ;
21+ KeyStorageMock = new Mock < IKeyStorage > ( ) ;
2122 }
2223
2324 [ Test ]
@@ -40,6 +41,23 @@ public async Task SendRequestAsync_WithoutData_WithValidAccessToken_WithSuccessR
4041 . Verify ( x => x . SaveToken ( It . IsAny < string > ( ) , It . IsAny < Token > ( ) ) , Times . Never ) ;
4142 }
4243
44+ [ Test ]
45+ public async Task SendRequestAsync_WithoutData_WithApiKeyFromStorage_WithSuccessReponse_ReturnsExpectedResponse ( )
46+ {
47+ // Arrange
48+ StoreApiKey ( ) ;
49+
50+ var path = "/api/test/" ;
51+ AuthorizedServiceCaller sut = CreateService ( HttpStatusCode . OK , authenticationMethod : AuthenticationMethod . ApiKey ) ;
52+
53+ // Act
54+ TestResponseData ? result = await sut . SendRequestAsync < TestResponseData > ( ServiceAlias , path , HttpMethod . Get ) ;
55+
56+ // Assert
57+ KeyStorageMock
58+ . Verify ( x => x . SaveKey ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
59+ }
60+
4361 [ Test ]
4462 public async Task SendRequestAsync_WithoutData_WithValidAccessToken_WithFaileReponse_ThrowsExpectedException ( )
4563 {
@@ -151,7 +169,7 @@ public async Task SendRequestAsync_WithExpiredAccessToken_WithRefreshTokenSucces
151169 public void GetApiKey_WithExistingApiKey_ReturnsApiKey ( )
152170 {
153171 // Arrange
154- AuthorizedServiceCaller sut = CreateService ( includeApiKey : true ) ;
172+ AuthorizedServiceCaller sut = CreateService ( authenticationMethod : AuthenticationMethod . ApiKey ) ;
155173
156174 // Act
157175 var result = sut . GetApiKey ( ServiceAlias ) ;
@@ -161,6 +179,21 @@ public void GetApiKey_WithExistingApiKey_ReturnsApiKey()
161179 result ! . Should ( ) . Be ( "test-api-key" ) ;
162180 }
163181
182+ [ Test ]
183+ public void GetApiKey_WithStoredApiKey_ReturnsStoredApiKey ( )
184+ {
185+ // Arrange
186+ StoreApiKey ( ) ;
187+ AuthorizedServiceCaller sut = CreateService ( authenticationMethod : AuthenticationMethod . ApiKey ) ;
188+
189+ // Act
190+ var result = sut . GetApiKey ( ServiceAlias ) ;
191+
192+ // Assert
193+ result . Should ( ) . NotBeNull ( ) ;
194+ result ! . Should ( ) . Be ( "stored-test-api-key" ) ;
195+ }
196+
164197 [ Test ]
165198 public void GetApiKey_WithoutExistingApiKey_ReturnsEmptyString ( )
166199 {
@@ -207,11 +240,17 @@ private void StoreToken(int daysUntilExpiry = 7) =>
207240 . Setup ( x => x . GetToken ( It . Is < string > ( y => y == ServiceAlias ) ) )
208241 . Returns ( new Token ( "abc" , "def" , DateTime . Now . AddDays ( daysUntilExpiry ) ) ) ;
209242
243+ private void StoreApiKey ( ) =>
244+ KeyStorageMock
245+ . Setup ( x => x . GetKey ( It . Is < string > ( y => y == ServiceAlias ) ) )
246+ . Returns ( "stored-test-api-key" ) ;
247+
248+
210249 private AuthorizedServiceCaller CreateService (
211250 HttpStatusCode statusCode = HttpStatusCode . OK ,
212251 string ? responseContent = null ,
213252 HttpStatusCode refreshTokenStatusCode = HttpStatusCode . OK ,
214- bool includeApiKey = false )
253+ AuthenticationMethod authenticationMethod = AuthenticationMethod . OAuth2AuthorizationCode )
215254 {
216255 var authorizationRequestSenderMock = new Mock < IAuthorizationRequestSender > ( ) ;
217256
@@ -225,7 +264,7 @@ private AuthorizedServiceCaller CreateService(
225264 . Setup ( x => x . SendRequest ( It . Is < ServiceDetail > ( y => y . Alias == ServiceAlias ) , It . Is < Dictionary < string , string > > ( y => y [ "grant_type" ] == "refresh_token" ) ) )
226265 . ReturnsAsync ( httpResponseMessage ) ;
227266
228- Mock < IOptionsMonitor < ServiceDetail > > optionsMonitorServiceDetailMock = CreateOptionsMonitorServiceDetail ( includeApiKey ) ;
267+ Mock < IOptionsMonitor < ServiceDetail > > optionsMonitorServiceDetailMock = CreateOptionsMonitorServiceDetail ( authenticationMethod ) ;
229268 var factory = new JsonSerializerFactory ( optionsMonitorServiceDetailMock . Object , new JsonNetSerializer ( ) ) ;
230269
231270 return new AuthorizedServiceCaller (
0 commit comments