@@ -20,18 +20,18 @@ public sealed class AuthenticationServiceShould : IDisposable
20
20
{
21
21
private readonly IAuthenticationService _authenticationService ;
22
22
private readonly IApplicationSettingsRepository _applicationSettingsRepository ;
23
- private readonly IAuthenticationApiClient _authenticationRepository ;
23
+ private readonly IAuthenticationApiClient _authenticationApiClient ;
24
24
private readonly BehaviorSubject < ApplicationSettings > _applicationSettingsSubject ;
25
25
26
26
public AuthenticationServiceShould ( )
27
27
{
28
28
_applicationSettingsRepository = Substitute . For < IApplicationSettingsRepository > ( ) ;
29
- _authenticationRepository = Substitute . For < IAuthenticationApiClient > ( ) ;
29
+ _authenticationApiClient = Substitute . For < IAuthenticationApiClient > ( ) ;
30
30
31
31
_authenticationService = new AuthenticationService (
32
32
Substitute . For < ILoggerFactory > ( ) ,
33
33
_applicationSettingsRepository ,
34
- _authenticationRepository
34
+ _authenticationApiClient
35
35
) ;
36
36
37
37
_applicationSettingsSubject = new BehaviorSubject < ApplicationSettings > ( CreateApplicationSettings ( ) ) ;
@@ -51,7 +51,7 @@ public async Task ReturnAuthenticationData_WhenLoggingIn()
51
51
var password = "password" ;
52
52
var authenticationData = CreateAuthenticationData ( email : email ) ;
53
53
54
- _authenticationRepository . Login ( Arg . Any < CancellationToken > ( ) , Arg . Any < string > ( ) , Arg . Any < string > ( ) ) . Returns ( authenticationData ) ;
54
+ _authenticationApiClient . Login ( Arg . Any < CancellationToken > ( ) , Arg . Any < string > ( ) , Arg . Any < string > ( ) ) . Returns ( authenticationData ) ;
55
55
56
56
// Act
57
57
var result = await _authenticationService . Login ( CancellationToken . None , email , password ) ;
@@ -66,19 +66,22 @@ public async Task ReturnAuthenticationData_WhenRefreshingTheToken()
66
66
// Arrange
67
67
var email = "email" ;
68
68
var password = "password" ;
69
- var refreshedEmail = "RefreshedEmail" ;
70
- var authenticationData = CreateAuthenticationData ( email : email ) ;
71
- var refreshAuthenticationData = CreateAuthenticationData ( email : refreshedEmail ) ;
69
+ var now = DateTimeOffset . Now ;
70
+ var authenticationData = CreateAuthenticationData ( email : email , now : now ) ;
71
+
72
+ now = now . Add ( TimeSpan . FromMinutes ( 1 ) ) ; // Advance the time to simulate the token expiration.
73
+
74
+ var refreshAuthenticationData = CreateAuthenticationData ( email : email , now : now ) ;
72
75
73
- _authenticationRepository . Login ( Arg . Any < CancellationToken > ( ) , Arg . Any < string > ( ) , Arg . Any < string > ( ) ) . Returns ( authenticationData ) ;
74
- _authenticationRepository . RefreshToken ( Arg . Any < CancellationToken > ( ) , Arg . Any < AuthenticationData > ( ) ) . Returns ( refreshAuthenticationData ) ;
76
+ _authenticationApiClient . Login ( Arg . Any < CancellationToken > ( ) , Arg . Any < string > ( ) , Arg . Any < string > ( ) ) . Returns ( authenticationData ) ;
77
+ _authenticationApiClient . RefreshToken ( Arg . Any < CancellationToken > ( ) , Arg . Any < AuthenticationData > ( ) ) . Returns ( refreshAuthenticationData ) ;
75
78
76
79
// Act
77
80
var loginResult = await _authenticationService . Login ( CancellationToken . None , email , password ) ;
78
81
var result = await _authenticationService . RefreshToken ( CancellationToken . None , new HttpRequestMessage ( ) , loginResult ) ;
79
82
80
83
// Assert
81
- Assert . Equal ( result . AccessToken . Payload . Email , refreshedEmail ) ;
84
+ Assert . True ( result . AccessToken . Payload . IssuedAt > loginResult . AccessToken . Payload . IssuedAt ) ;
82
85
}
83
86
84
87
[ Fact ]
@@ -173,11 +176,11 @@ private void ResetApplicationSettings()
173
176
_applicationSettingsSubject . OnNext ( CreateApplicationSettings ( ) ) ;
174
177
}
175
178
176
- private AuthenticationData CreateAuthenticationData ( string email , bool isAccessTokenNull = false )
179
+ private AuthenticationData CreateAuthenticationData ( string email , DateTimeOffset ? now = null )
177
180
{
178
181
return new AuthenticationData
179
182
{
180
- AccessToken = isAccessTokenNull ? null : new JwtData < AuthenticationToken > ( AuthenticationApiClientMock . CreateJsonWebToken ( email : email , serializerOptions : SerializationConfiguration . DefaultJsonSerializerOptions ) ) ,
183
+ AccessToken = new JwtData < AuthenticationToken > ( AuthenticationApiClientMock . CreateJsonWebToken ( email : email , now : now , serializerOptions : SerializationConfiguration . DefaultJsonSerializerOptions ) ) ,
181
184
RefreshToken = "RefreshToken" ,
182
185
} ;
183
186
}
0 commit comments