|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Reactive.Linq; |
| 4 | +using System.Reactive.Subjects; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using ApplicationTemplate.Business; |
| 8 | +using ApplicationTemplate.DataAccess; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using NSubstitute; |
| 11 | +using Uno.Extensions; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace ApplicationTemplate.Tests.Unit; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Tests for the <see cref="AuthenticationService"/>. |
| 18 | +/// </summary> |
| 19 | +public sealed class AuthenticationServiceShould : IDisposable |
| 20 | +{ |
| 21 | + private readonly IAuthenticationService _authenticationService; |
| 22 | + private readonly IApplicationSettingsRepository _applicationSettingsRepository; |
| 23 | + private readonly IAuthenticationApiClient _authenticationRepository; |
| 24 | + private readonly BehaviorSubject<ApplicationSettings> _applicationSettingsSubject; |
| 25 | + |
| 26 | + public AuthenticationServiceShould() |
| 27 | + { |
| 28 | + _applicationSettingsRepository = Substitute.For<IApplicationSettingsRepository>(); |
| 29 | + _authenticationRepository = Substitute.For<IAuthenticationApiClient>(); |
| 30 | + |
| 31 | + _authenticationService = new AuthenticationService( |
| 32 | + Substitute.For<ILoggerFactory>(), |
| 33 | + _applicationSettingsRepository, |
| 34 | + _authenticationRepository |
| 35 | + ); |
| 36 | + |
| 37 | + _applicationSettingsSubject = new BehaviorSubject<ApplicationSettings>(CreateApplicationSettings()); |
| 38 | + |
| 39 | + _applicationSettingsRepository.GetAndObserveCurrent().Returns(_applicationSettingsSubject); |
| 40 | + _applicationSettingsRepository.GetCurrent(CancellationToken.None).Returns(Task.FromResult(_applicationSettingsSubject.Value)); |
| 41 | + |
| 42 | + _applicationSettingsRepository.When(x => x.SetAuthenticationData(Arg.Any<CancellationToken>(), Arg.Any<AuthenticationData>())) |
| 43 | + .Do(x => _applicationSettingsSubject.OnNext(CreateApplicationSettings(x.Arg<AuthenticationData>()))); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task ReturnAuthenticationData_WhenLoggingIn() |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var email = "email"; |
| 51 | + var password = "password"; |
| 52 | + var authenticationData = CreateAuthenticationData(email: email); |
| 53 | + |
| 54 | + _authenticationRepository.Login(Arg.Any<CancellationToken>(), Arg.Any<string>(), Arg.Any<string>()).Returns(authenticationData); |
| 55 | + |
| 56 | + // Act |
| 57 | + var result = await _authenticationService.Login(CancellationToken.None, email, password); |
| 58 | + |
| 59 | + // Assert |
| 60 | + Assert.Equal(authenticationData.AccessToken, result.AccessToken); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task ReturnAuthenticationData_WhenRefreshingTheToken() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var email = "email"; |
| 68 | + var password = "password"; |
| 69 | + var refreshedEmail = "RefreshedEmail"; |
| 70 | + var authenticationData = CreateAuthenticationData(email: email); |
| 71 | + var refreshAuthenticationData = CreateAuthenticationData(email: refreshedEmail); |
| 72 | + |
| 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); |
| 75 | + |
| 76 | + // Act |
| 77 | + var loginResult = await _authenticationService.Login(CancellationToken.None, email, password); |
| 78 | + var result = await _authenticationService.RefreshToken(CancellationToken.None, new HttpRequestMessage(), loginResult); |
| 79 | + |
| 80 | + // Assert |
| 81 | + Assert.Equal(result.AccessToken.Payload.Email, refreshedEmail); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task NotifySuscribersThatTheSessionExpired_WhenNotifySesionExpiredIsCalled() |
| 86 | + { |
| 87 | + // Arrange |
| 88 | + var notifySessionExpired = false; |
| 89 | + |
| 90 | + _authenticationService.ObserveSessionExpired().Subscribe(_ => notifySessionExpired = true); |
| 91 | + |
| 92 | + // Act |
| 93 | + await _authenticationService.NotifySessionExpired(CancellationToken.None, new HttpRequestMessage(), CreateAuthenticationData("email")); |
| 94 | + |
| 95 | + // Assert |
| 96 | + Assert.True(notifySessionExpired); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public async Task DiscardUserSettings_OnLogout() |
| 101 | + { |
| 102 | + // Arrange |
| 103 | + // Act |
| 104 | + await _authenticationService.Logout(CancellationToken.None); |
| 105 | + |
| 106 | + // Assert |
| 107 | + await _applicationSettingsRepository.Received().DiscardUserSettings(CancellationToken.None); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public async Task SignalThatTheUserIsLoggedOut_WhenThereIsNoAuthenticationDataInAppSettings() |
| 112 | + { |
| 113 | + // Arrange |
| 114 | + ResetApplicationSettings(); |
| 115 | + |
| 116 | + // Act |
| 117 | + var result = await _authenticationService.GetAndObserveIsAuthenticated().FirstAsync(); |
| 118 | + |
| 119 | + // Assert |
| 120 | + Assert.False(result); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public async Task RespondNull_WhenThereIsNoAuthenticationDataInAppSettings() |
| 125 | + { |
| 126 | + // Arrange |
| 127 | + ResetApplicationSettings(); |
| 128 | + |
| 129 | + // Act |
| 130 | + var result = await _authenticationService.GetAndObserveAuthenticationData().FirstAsync(); |
| 131 | + |
| 132 | + // Assert |
| 133 | + Assert.Null(result); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public async Task SignalThatUserIsLoggedIn_WhenTheSettingsContainsAuthenticationData() |
| 138 | + { |
| 139 | + // Arrange |
| 140 | + var authenticationData = CreateAuthenticationData("email"); |
| 141 | + |
| 142 | + _applicationSettingsSubject.OnNext(CreateApplicationSettings(authenticationData)); |
| 143 | + |
| 144 | + // Act |
| 145 | + var result = await _authenticationService.GetAndObserveIsAuthenticated().FirstAsync(); |
| 146 | + |
| 147 | + // Assert |
| 148 | + Assert.True(result); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public async Task GiveAuthData_WhenTheSettingsContainsAuthenticationData() |
| 153 | + { |
| 154 | + // Arrange |
| 155 | + var authenticationData = CreateAuthenticationData("email"); |
| 156 | + |
| 157 | + _applicationSettingsSubject.OnNext(CreateApplicationSettings(authenticationData)); |
| 158 | + |
| 159 | + // Act |
| 160 | + var result = await _authenticationService.GetAndObserveAuthenticationData().FirstAsync(); |
| 161 | + |
| 162 | + // Assert |
| 163 | + Assert.Equal(authenticationData.AccessToken, result.AccessToken); |
| 164 | + } |
| 165 | + |
| 166 | + public void Dispose() |
| 167 | + { |
| 168 | + _applicationSettingsSubject.Dispose(); |
| 169 | + } |
| 170 | + |
| 171 | + private void ResetApplicationSettings() |
| 172 | + { |
| 173 | + _applicationSettingsSubject.OnNext(CreateApplicationSettings()); |
| 174 | + } |
| 175 | + |
| 176 | + private AuthenticationData CreateAuthenticationData(string email, bool isAccessTokenNull = false) |
| 177 | + { |
| 178 | + return new AuthenticationData |
| 179 | + { |
| 180 | + AccessToken = isAccessTokenNull ? null : new JwtData<AuthenticationToken>(AuthenticationApiClientMock.CreateJsonWebToken(email: email, serializerOptions: SerializationConfiguration.DefaultJsonSerializerOptions)), |
| 181 | + RefreshToken = "RefreshToken", |
| 182 | + }; |
| 183 | + } |
| 184 | + |
| 185 | + private ApplicationSettings CreateApplicationSettings(AuthenticationData authenticationData = null) |
| 186 | + { |
| 187 | + return new ApplicationSettings() { AuthenticationData = authenticationData }; |
| 188 | + } |
| 189 | +} |
0 commit comments