|
1 | | -using IdentityServer4.Models; |
| 1 | +using Bogus; |
| 2 | +using IdentityServer4.EntityFramework.Entities; |
| 3 | +using IdentityServer4.Models; |
2 | 4 | using Jp.Domain.CommandHandlers; |
3 | 5 | using Jp.Domain.Core.Bus; |
4 | 6 | using Jp.Domain.Core.Notifications; |
@@ -26,9 +28,11 @@ public class ClientCommandHandlerTests |
26 | 28 | private Mock<IClientPropertyRepository> _clientPropertyRepository; |
27 | 29 | private Mock<IClientSecretRepository> _clientSecretRepository; |
28 | 30 | private CancellationTokenSource _tokenSource; |
| 31 | + private Faker _faker; |
29 | 32 |
|
30 | 33 | public ClientCommandHandlerTests() |
31 | 34 | { |
| 35 | + _faker = new Faker(); |
32 | 36 | _tokenSource = new CancellationTokenSource(); |
33 | 37 | _uow = new Mock<IUnitOfWork>(); |
34 | 38 | _mediator = new Mock<IMediatorHandler>(); |
@@ -108,6 +112,243 @@ public void ShouldNotAllowCombinationOfGrants(string a, string b) |
108 | 112 | Assert.Throws<InvalidOperationException>(() => command.Client.AllowedGrantTypes = new List<string>() { a, b }); |
109 | 113 | } |
110 | 114 |
|
| 115 | + [Fact] |
| 116 | + public async Task ShouldNotAcceptNegativeAbsoluteRefreshTokenLifetime() |
| 117 | + { |
| 118 | + var command = ClientCommandFaker.GenerateUpdateClientCommand(absoluteRefreshTokenLifetime: _faker.Random.Int(max: 0)).Generate(); |
| 119 | + |
| 120 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 121 | + |
| 122 | + Assert.False(result); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public async Task ShouldNotAcceptNegativeIdentityTokenLifetime() |
| 127 | + { |
| 128 | + var command = ClientCommandFaker.GenerateUpdateClientCommand(identityTokenLifetime: _faker.Random.Int(max: 0)).Generate(); |
| 129 | + |
| 130 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 131 | + |
| 132 | + Assert.False(result); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public async Task ShouldNotAcceptNegativeAccessTokenLifetime() |
| 137 | + { |
| 138 | + var command = ClientCommandFaker.GenerateUpdateClientCommand(accessTokenLifetime: _faker.Random.Int(max: 0)).Generate(); |
| 139 | + |
| 140 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 141 | + |
| 142 | + Assert.False(result); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public async Task ShouldNotAcceptNegativeAuthorizationCodeLifetime() |
| 147 | + { |
| 148 | + var command = ClientCommandFaker.GenerateUpdateClientCommand(authorizationCodeLifetime: _faker.Random.Int(max: 0)).Generate(); |
| 149 | + |
| 150 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 151 | + |
| 152 | + Assert.False(result); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public async Task ShouldNotAcceptNegativeSlidingRefreshTokenLifetime() |
| 157 | + { |
| 158 | + var command = ClientCommandFaker.GenerateUpdateClientCommand(slidingRefreshTokenLifetime: _faker.Random.Int(max: 0)).Generate(); |
| 159 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 160 | + |
| 161 | + Assert.False(result); |
| 162 | + } |
| 163 | + |
| 164 | + [Fact] |
| 165 | + public async Task ShouldNotAcceptNegativeDeviceCodeLifetime() |
| 166 | + { |
| 167 | + var command = ClientCommandFaker.GenerateUpdateClientCommand(deviceCodeLifetime: _faker.Random.Int(max: 0)).Generate(); |
| 168 | + |
| 169 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 170 | + |
| 171 | + Assert.False(result); |
| 172 | + } |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public async Task ShouldUpdateClient() |
| 176 | + { |
| 177 | + var command = ClientCommandFaker.GenerateUpdateClientCommand().Generate(); |
| 178 | + _clientRepository.Setup(s => s.UpdateWithChildrens(It.Is<Client>(a => a.ClientId == command.Client.ClientId))).Returns(Task.CompletedTask); |
| 179 | + _clientRepository.Setup(s => s.GetClient(It.Is<string>(a => a == command.Client.ClientId))).ReturnsAsync(EntityClientFaker.GenerateClient().Generate()); |
| 180 | + _uow.Setup(s => s.Commit()).Returns(true); |
| 181 | + |
| 182 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 183 | + |
| 184 | + Assert.True(result); |
| 185 | + } |
| 186 | + |
| 187 | + [Fact] |
| 188 | + public async Task ShouldRemoveClient() |
| 189 | + { |
| 190 | + var command = ClientCommandFaker.GenerateRemoveClientCommand().Generate(); |
| 191 | + _clientRepository.Setup(s => s.Remove(It.Is<Client>(a => a.ClientId == command.Client.ClientId))); |
| 192 | + _clientRepository.Setup(s => s.GetByClientId(It.Is<string>(a => a == command.Client.ClientId))).ReturnsAsync(EntityClientFaker.GenerateClient().Generate()); |
| 193 | + _uow.Setup(s => s.Commit()).Returns(true); |
| 194 | + |
| 195 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 196 | + |
| 197 | + Assert.True(result); |
| 198 | + } |
| 199 | + |
| 200 | + [Fact] |
| 201 | + public async Task ShouldNotRemoveSecretWhenClientDoesntExist() |
| 202 | + { |
| 203 | + var command = ClientCommandFaker.GenerateRemoveClientSecretCommand().Generate(); |
| 204 | + //_clientRepository.Setup(s => s.GetClient(It.Is<string>(a => a == command.ClientId))).ReturnsAsync(null); |
| 205 | + |
| 206 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 207 | + |
| 208 | + Assert.False(result); |
| 209 | + } |
| 210 | + |
| 211 | + [Fact] |
| 212 | + public async Task ShouldNotRemoveSecretWhenSecretIdIsDifferent() |
| 213 | + { |
| 214 | + var command = ClientCommandFaker.GenerateRemoveClientSecretCommand().Generate(); |
| 215 | + _clientRepository.Setup(s => s.GetClient(It.Is<string>(a => a == command.ClientId))).ReturnsAsync(EntityClientFaker.GenerateClient().Generate()); |
| 216 | + |
| 217 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 218 | + |
| 219 | + Assert.False(result); |
| 220 | + } |
111 | 221 |
|
| 222 | + [Fact] |
| 223 | + public async Task ShouldRemoveClientSecret() |
| 224 | + { |
| 225 | + var clientSecret = EntityClientFaker.GenerateClient(clientSecrets: _faker.Random.Int(1, 3)).Generate(); |
| 226 | + var command = ClientCommandFaker.GenerateRemoveClientSecretCommand(_faker.PickRandom(clientSecret.ClientSecrets).Id).Generate(); |
| 227 | + |
| 228 | + _uow.Setup(s => s.Commit()).Returns(true); |
| 229 | + _clientRepository.Setup(s => s.GetClient(It.Is<string>(a => a == command.ClientId))).ReturnsAsync(clientSecret); |
| 230 | + |
| 231 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 232 | + |
| 233 | + Assert.True(result); |
| 234 | + } |
| 235 | + |
| 236 | + [Fact] |
| 237 | + public async Task ShouldNotSaveClientSecretWhenClientDoesntExist() |
| 238 | + { |
| 239 | + var command = ClientCommandFaker.GenerateSaveClientSecretCommand().Generate(); |
| 240 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 241 | + |
| 242 | + Assert.False(result); |
| 243 | + } |
| 244 | + |
| 245 | + [Fact] |
| 246 | + public async Task ShouldEncryptedValueBeCorrect() |
| 247 | + { |
| 248 | + var command = ClientCommandFaker.GenerateSaveClientSecretCommand().Generate(); |
| 249 | + var valueEncryptedMustBe = command.GetValue(); |
| 250 | + |
| 251 | + _clientRepository.Setup(s => s.GetByClientId(It.Is<string>(a => a == command.ClientId))).ReturnsAsync(EntityClientFaker.GenerateClient().Generate()); |
| 252 | + _clientSecretRepository.Setup(s => s.Add(It.Is<ClientSecret>(cs => cs.Value == valueEncryptedMustBe))); |
| 253 | + _uow.Setup(s => s.Commit()).Returns(true); |
| 254 | + |
| 255 | + |
| 256 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 257 | + |
| 258 | + Assert.True(result); |
| 259 | + _clientRepository.Verify(s => s.GetByClientId(It.Is<string>(a => a == command.ClientId)), Times.Once); |
| 260 | + _clientSecretRepository.Verify(s => s.Add(It.Is<ClientSecret>(cs => cs.Value == valueEncryptedMustBe)), Times.Once); |
| 261 | + _uow.Verify(s => s.Commit(), Times.Once); |
| 262 | + } |
| 263 | + |
| 264 | + [Fact] |
| 265 | + public async Task ShouldNotEncryptedValueBeCorrect() |
| 266 | + { |
| 267 | + var command = ClientCommandFaker.GenerateSaveClientSecretCommand().Generate(); |
| 268 | + var valueEncryptedMustBe = command.GetValue(); |
| 269 | + |
| 270 | + _clientRepository.Setup(s => s.GetByClientId(It.Is<string>(a => a == command.ClientId))).ReturnsAsync(EntityClientFaker.GenerateClient().Generate()); |
| 271 | + _clientSecretRepository.Setup(s => s.Add(It.Is<ClientSecret>(cs => cs.Value == valueEncryptedMustBe))); |
| 272 | + _uow.Setup(s => s.Commit()).Returns(true); |
| 273 | + |
| 274 | + |
| 275 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 276 | + |
| 277 | + Assert.True(result); |
| 278 | + _clientRepository.Verify(s => s.GetByClientId(It.Is<string>(a => a == command.ClientId)), Times.Once); |
| 279 | + _clientSecretRepository.Verify(s => s.Add(It.Is<ClientSecret>(cs => cs.Value == valueEncryptedMustBe)), Times.Once); |
| 280 | + _uow.Verify(s => s.Commit(), Times.Once); |
| 281 | + } |
| 282 | + |
| 283 | + [Fact] |
| 284 | + public async Task ShouldNotRemovePropertyWhenClientDoesntExist() |
| 285 | + { |
| 286 | + var command = ClientCommandFaker.GenerateRemovePropertyCommand().Generate(); |
| 287 | + |
| 288 | + |
| 289 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 290 | + |
| 291 | + Assert.False(result); |
| 292 | + _clientRepository.Verify(s => s.GetClient(It.Is<string>(a => a == command.ClientId)), Times.Once); |
| 293 | + } |
| 294 | + |
| 295 | + |
| 296 | + [Fact] |
| 297 | + public async Task ShouldNotRemovePropertyWhenIdIsDifferent() |
| 298 | + { |
| 299 | + var command = ClientCommandFaker.GenerateRemovePropertyCommand().Generate(); |
| 300 | + _clientRepository.Setup(s => s.GetClient(It.Is<string>(a => a == command.ClientId))).ReturnsAsync(EntityClientFaker.GenerateClient().Generate()); |
| 301 | + |
| 302 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 303 | + |
| 304 | + Assert.False(result); |
| 305 | + _clientRepository.Verify(s => s.GetClient(It.Is<string>(a => a == command.ClientId)), Times.Once); |
| 306 | + } |
| 307 | + |
| 308 | + [Fact] |
| 309 | + public async Task ShouldRemoveProperty() |
| 310 | + { |
| 311 | + var properties = EntityClientFaker.GenerateClient(clientProperties: _faker.Random.Int(1, 3)).Generate(); |
| 312 | + var command = ClientCommandFaker.GenerateRemovePropertyCommand(_faker.PickRandom(properties.Properties).Id).Generate(); |
| 313 | + |
| 314 | + _uow.Setup(s => s.Commit()).Returns(true); |
| 315 | + _clientRepository.Setup(s => s.GetClient(It.Is<string>(a => a == command.ClientId))).ReturnsAsync(properties); |
| 316 | + _clientPropertyRepository.Setup(s => s.Remove(It.Is<int>(a => a == command.Id))); |
| 317 | + |
| 318 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 319 | + |
| 320 | + Assert.True(result); |
| 321 | + _clientRepository.Verify(s => s.GetClient(It.Is<string>(a => a == command.ClientId)), Times.Once); |
| 322 | + _clientPropertyRepository.Verify(s => s.Remove(It.Is<int>(a => a == command.Id)), Times.Once); |
| 323 | + } |
| 324 | + |
| 325 | + |
| 326 | + [Fact] |
| 327 | + public async Task ShouldNotSavePropertyWhenClientDoesntExist() |
| 328 | + { |
| 329 | + var command = ClientCommandFaker.GenerateSavePropertyCommand().Generate(); |
| 330 | + _clientRepository.Setup(s => s.GetByClientId(It.Is<string>(q => q == command.ClientId))).ReturnsAsync((Client)null); |
| 331 | + |
| 332 | + |
| 333 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 334 | + |
| 335 | + |
| 336 | + Assert.False(result); |
| 337 | + _clientRepository.Verify(s => s.GetByClientId(It.Is<string>(q => q == command.ClientId)), Times.Once); |
| 338 | + } |
| 339 | + |
| 340 | + [Fact] |
| 341 | + public async Task ShouldSaveProperty() |
| 342 | + { |
| 343 | + var command = ClientCommandFaker.GenerateSavePropertyCommand().Generate(); |
| 344 | + _clientRepository.Setup(s => s.GetByClientId(It.Is<string>(q => q == command.ClientId))).ReturnsAsync(EntityClientFaker.GenerateClient().Generate()).Verifiable(); |
| 345 | + _clientPropertyRepository.Setup(s => s.Add(It.IsAny<ClientProperty>())); |
| 346 | + |
| 347 | + var result = await _commandHandler.Handle(command, _tokenSource.Token); |
| 348 | + |
| 349 | + _clientPropertyRepository.Verify(s => s.Add(It.IsAny<ClientProperty>()), Times.Once); |
| 350 | + _clientRepository.Verify(s => s.GetByClientId(It.Is<string>(q => q == command.ClientId)), Times.Once); |
| 351 | + Assert.False(result); |
| 352 | + } |
112 | 353 | } |
113 | 354 | } |
0 commit comments