Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit 8fe8ab9

Browse files
committed
new tests
1 parent 25fff4b commit 8fe8ab9

File tree

7 files changed

+348
-20
lines changed

7 files changed

+348
-20
lines changed

src/Backend/Jp.Domain/Validations/Client/ClientValidations.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected void ValidateGrantType()
2020

2121
private bool ValidateGrantCombination(ICollection<string> grantTypes, string message)
2222
{
23-
23+
2424

2525
// would allow response_type downgrade attack from code to token
2626
if (DisallowGrantTypeCombination(GrantType.Implicit, GrantType.AuthorizationCode, grantTypes))
@@ -64,5 +64,31 @@ protected void ValidateClientName()
6464
RuleFor(c => c.Client.ClientName).NotEmpty().WithMessage("Client Name must be set");
6565
}
6666

67+
protected void ValidateIdentityTokenLifetime()
68+
{
69+
RuleFor(c => c.Client.IdentityTokenLifetime).GreaterThan(0).WithMessage("Identity Token Lifetime must be greatter than 0");
70+
}
71+
72+
73+
protected void ValidateAccessTokenLifetime()
74+
{
75+
RuleFor(c => c.Client.AccessTokenLifetime).GreaterThan(0).WithMessage("Access Token Lifetime must be greatter than 0");
76+
}
77+
protected void ValidateAuthorizationCodeLifetime()
78+
{
79+
RuleFor(c => c.Client.AuthorizationCodeLifetime).GreaterThan(0).WithMessage("Authorization Code Lifetime must be greatter than 0");
80+
}
81+
protected void ValidateAbsoluteRefreshTokenLifetime()
82+
{
83+
RuleFor(c => c.Client.AbsoluteRefreshTokenLifetime).GreaterThan(0).WithMessage("Absolute Refresh Token Lifetime must be greatter than 0");
84+
}
85+
protected void ValidateSlidingRefreshTokenLifetime()
86+
{
87+
RuleFor(c => c.Client.SlidingRefreshTokenLifetime).GreaterThan(0).WithMessage("Sliding Refresh Token Lifetime must be greatter than 0");
88+
}
89+
protected void ValidateDeviceCodeLifetime()
90+
{
91+
RuleFor(c => c.Client.DeviceCodeLifetime).GreaterThan(0).WithMessage("Device Code Lifetime must be greatter than 0");
92+
}
6793
}
6894
}

src/Backend/Jp.Domain/Validations/Client/UpdateClientCommandValidation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public UpdateClientCommandValidation()
88
{
99
ValidateGrantType();
1010
ValidateOldClientId();
11+
ValidateIdentityTokenLifetime();
12+
ValidateAccessTokenLifetime();
13+
ValidateAuthorizationCodeLifetime();
14+
ValidateSlidingRefreshTokenLifetime();
15+
ValidateDeviceCodeLifetime();
16+
ValidateAbsoluteRefreshTokenLifetime();
1117
}
1218
}
1319
}

src/Frontend/Jp.AdminUI/src/app/shared/layout/header/header.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { UserblockService } from "../sidebar/userblock/userblock.service";
77
import { SettingsService } from "@core/settings/settings.service";
88
import { MenuService } from "@core/menu/menu.service";
99
import { Router } from "@angular/router";
10-
import { environment } from "@env/environment.prod";
10+
import { environment } from "@env/environment";
1111

1212

1313
@Component({

tests/JpProject.Domain.Tests/ClientTests/ClientCommandHandlerTests.cs

Lines changed: 242 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using IdentityServer4.Models;
1+
using Bogus;
2+
using IdentityServer4.EntityFramework.Entities;
3+
using IdentityServer4.Models;
24
using Jp.Domain.CommandHandlers;
35
using Jp.Domain.Core.Bus;
46
using Jp.Domain.Core.Notifications;
@@ -26,9 +28,11 @@ public class ClientCommandHandlerTests
2628
private Mock<IClientPropertyRepository> _clientPropertyRepository;
2729
private Mock<IClientSecretRepository> _clientSecretRepository;
2830
private CancellationTokenSource _tokenSource;
31+
private Faker _faker;
2932

3033
public ClientCommandHandlerTests()
3134
{
35+
_faker = new Faker();
3236
_tokenSource = new CancellationTokenSource();
3337
_uow = new Mock<IUnitOfWork>();
3438
_mediator = new Mock<IMediatorHandler>();
@@ -108,6 +112,243 @@ public void ShouldNotAllowCombinationOfGrants(string a, string b)
108112
Assert.Throws<InvalidOperationException>(() => command.Client.AllowedGrantTypes = new List<string>() { a, b });
109113
}
110114

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+
}
111221

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+
}
112353
}
113354
}

tests/JpProject.Domain.Tests/ClientTests/Fakers/ClientCommandFaker.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,65 @@ public static Faker<CopyClientCommand> GenerateCopyClientCommand()
2626

2727
}
2828

29-
public static Faker<UpdateClientCommand> GenerateUpdateClientCommand()
29+
public static Faker<UpdateClientCommand> GenerateUpdateClientCommand(
30+
int? absoluteRefreshTokenLifetime = null,
31+
int? identityTokenLifetime = null,
32+
int? accessTokenLifetime = null,
33+
int? authorizationCodeLifetime = null,
34+
int? slidingRefreshTokenLifetime = null,
35+
int? deviceCodeLifetime = null)
3036
{
3137
return new Faker<UpdateClientCommand>().CustomInstantiator(f =>
3238
new UpdateClientCommand(
33-
ClientFaker.GenerateClient().Generate(),
39+
ClientFaker.GenerateClient(absoluteRefreshTokenLifetime,
40+
identityTokenLifetime,
41+
accessTokenLifetime,
42+
authorizationCodeLifetime,
43+
slidingRefreshTokenLifetime,
44+
deviceCodeLifetime).Generate(),
3445
f.Company.CompanyName()
3546
));
3647

3748
}
49+
50+
public static Faker<RemoveClientCommand> GenerateRemoveClientCommand()
51+
{
52+
return new Faker<RemoveClientCommand>().CustomInstantiator(f => new RemoveClientCommand(f.Lorem.Word()));
53+
}
54+
55+
public static Faker<RemoveClientSecretCommand> GenerateRemoveClientSecretCommand(int? id = null)
56+
{
57+
return new Faker<RemoveClientSecretCommand>().CustomInstantiator(f => new RemoveClientSecretCommand(id ?? f.Random.Int(0), f.Lorem.Word()));
58+
}
59+
60+
public static Faker<SaveClientSecretCommand> GenerateSaveClientSecretCommand()
61+
{
62+
return new Faker<SaveClientSecretCommand>().CustomInstantiator(f => new SaveClientSecretCommand(
63+
f.Lorem.Word(),
64+
f.Lorem.Sentence(),
65+
f.Lorem.Word(),
66+
f.Lorem.Word(),
67+
f.Date.Future(),
68+
f.Random.Int(0, 1)
69+
));
70+
}
71+
72+
73+
public static Faker<RemovePropertyCommand> GenerateRemovePropertyCommand(int? id = null)
74+
{
75+
return new Faker<RemovePropertyCommand>().CustomInstantiator(f => new RemovePropertyCommand(
76+
id ?? f.Random.Int(0),
77+
f.Random.Word()
78+
));
79+
}
80+
81+
public static Faker<SaveClientPropertyCommand> GenerateSavePropertyCommand()
82+
{
83+
return new Faker<SaveClientPropertyCommand>().CustomInstantiator(f => new SaveClientPropertyCommand(
84+
f.Random.Word(),
85+
f.Random.Word(),
86+
f.Random.Word()
87+
));
88+
}
3889
}
3990
}

0 commit comments

Comments
 (0)