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

Commit 5a1745e

Browse files
committed
Teste update
1 parent 8fe8ab9 commit 5a1745e

File tree

11 files changed

+456
-32
lines changed

11 files changed

+456
-32
lines changed

src/Backend/Jp.Domain/CommandHandlers/ApiResourceCommandHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public async Task<bool> Handle(RegisterApiResourceCommand request, CancellationT
6565
return false;
6666
}
6767

68-
6968
public async Task<bool> Handle(UpdateApiResourceCommand request, CancellationToken cancellationToken)
7069
{
7170
if (!request.IsValid())

src/Backend/Jp.UserManagement/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Sql Server connection
66
// "SSOConnection": "Server=.;Initial Catalog=JpProject;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
77
// MySql connection
8-
"SSOConnection": "Data Source=tcp:capturadb.database.windows.net,1433;Initial Catalog=jpproject;User [email protected];Password=8BIaspVxw1vOgSIDKdIn"
8+
"SSOConnection": "server=jpdatabase,port=3306;database=jpproject;user=jp;password=10203040"
99

1010
},
1111
"Logging": {

src/Frontend/Jp.UI.SSO/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Sql Server connection
66
// "SSOConnection": "Server=.;Initial Catalog=JpProject;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
77
// MySql connection
8-
"SSOConnection": "Data Source=tcp:capturadb.database.windows.net,1433;Initial Catalog=jpproject;User [email protected];Password=8BIaspVxw1vOgSIDKdIn"
8+
"SSOConnection": "server=jpdatabase,port=3306;database=jpproject;user=jp;password=10203040"
99
},
1010
"CertificateOptions": {
1111
"Type": "Temporary",

src/Frontend/Jp.UI.SSO/jpProject_sso_log.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17219,3 +17219,21 @@ Error Number:2714,State:6,Class:16
1721917219
2019-03-29 03:33:41.664 -03:00 [INF] Invoking IdentityServer endpoint: IdentityServer4.Endpoints.DiscoveryKeyEndpoint for /.well-known/openid-configuration/jwks
1722017220
2019-03-29 03:33:41.670 -03:00 [DBG] Start key discovery request
1722117221
2019-03-29 03:33:54.575 -03:00 [INF] Removing 0 grants
17222+
2019-03-30 00:30:38.774 -03:00 [INF] SigninCredentialExtension keyType is Temporary
17223+
2019-03-30 00:30:39.000 -03:00 [INF] SigninCredentialExtension added
17224+
2019-03-30 00:30:43.548 -03:00 [INF] Starting IdentityServer4 version 2.4.0.0
17225+
2019-03-30 00:30:43.587 -03:00 [INF] Using the default authentication scheme Identity.Application for IdentityServer
17226+
2019-03-30 00:30:43.589 -03:00 [DBG] Using Identity.Application as default ASP.NET Core scheme for authentication
17227+
2019-03-30 00:30:43.592 -03:00 [DBG] Using Identity.External as default ASP.NET Core scheme for sign-in
17228+
2019-03-30 00:30:43.594 -03:00 [DBG] Using Identity.External as default ASP.NET Core scheme for sign-out
17229+
2019-03-30 00:30:43.597 -03:00 [DBG] Using Identity.Application as default ASP.NET Core scheme for challenge
17230+
2019-03-30 00:30:43.600 -03:00 [DBG] Using Identity.Application as default ASP.NET Core scheme for forbid
17231+
2019-03-30 00:30:43.780 -03:00 [DBG] Starting grant removal
17232+
2019-03-30 00:30:53.984 -03:00 [DBG] Login Url: /Account/Login
17233+
2019-03-30 00:30:53.995 -03:00 [DBG] Login Return Url Parameter: ReturnUrl
17234+
2019-03-30 00:30:53.997 -03:00 [DBG] Logout Url: /Account/Logout
17235+
2019-03-30 00:30:54.000 -03:00 [DBG] ConsentUrl Url: /consent
17236+
2019-03-30 00:30:54.003 -03:00 [DBG] Consent Return Url Parameter: returnUrl
17237+
2019-03-30 00:30:54.006 -03:00 [DBG] Error Url: /home/error
17238+
2019-03-30 00:30:54.008 -03:00 [DBG] Error Id Parameter: errorId
17239+
2019-03-30 00:30:58.872 -03:00 [INF] Removing 0 grants
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using Bogus;
2+
using IdentityServer4.EntityFramework.Entities;
3+
using Jp.Domain.CommandHandlers;
4+
using Jp.Domain.Core.Bus;
5+
using Jp.Domain.Core.Notifications;
6+
using Jp.Domain.Interfaces;
7+
using JpProject.Domain.Tests.ApiResourceTests.Fakers;
8+
using Moq;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using Jp.Domain.Commands.ApiResource;
12+
using Xunit;
13+
14+
namespace JpProject.Domain.Tests.ApiResourceTests
15+
{
16+
public class ApiResourcsCommandHandlerTests
17+
{
18+
private readonly ApiResourceCommandHandler _commandHandler;
19+
private readonly Mock<DomainNotificationHandler> _notifications;
20+
private readonly Mock<IMediatorHandler> _mediator;
21+
private readonly Mock<IUnitOfWork> _uow;
22+
private readonly Mock<IApiResourceRepository> _apiResourceRepository;
23+
private readonly Mock<IApiScopeRepository> _apiScopeRepository;
24+
private readonly Mock<IApiSecretRepository> _apiSecretRepository;
25+
private readonly CancellationTokenSource _tokenSource;
26+
private readonly Faker _faker;
27+
public ApiResourcsCommandHandlerTests()
28+
{
29+
_faker = new Faker();
30+
_tokenSource = new CancellationTokenSource();
31+
_uow = new Mock<IUnitOfWork>();
32+
_mediator = new Mock<IMediatorHandler>();
33+
_notifications = new Mock<DomainNotificationHandler>();
34+
_apiResourceRepository = new Mock<IApiResourceRepository>();
35+
_apiSecretRepository = new Mock<IApiSecretRepository>();
36+
_apiScopeRepository = new Mock<IApiScopeRepository>();
37+
_commandHandler = new ApiResourceCommandHandler(_uow.Object, _mediator.Object, _notifications.Object, _apiResourceRepository.Object, _apiSecretRepository.Object, _apiScopeRepository.Object);
38+
}
39+
40+
41+
[Fact]
42+
public async Task ShouldNotSaveResourceWhenItAlreadyExist()
43+
{
44+
var command = ResourceCommandFaker.GenerateRegisterApiResourceCommand().Generate();
45+
46+
_apiResourceRepository.Setup(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name))).ReturnsAsync(EntityResourceFaker.GenerateResource().Generate());
47+
48+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
49+
50+
51+
Assert.False(result);
52+
_apiResourceRepository.Verify(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name)), Times.Once);
53+
_uow.Verify(v => v.Commit(), Times.Never);
54+
55+
}
56+
57+
[Fact]
58+
public async Task ShouldSaveResource()
59+
{
60+
var command = ResourceCommandFaker.GenerateRegisterApiResourceCommand().Generate();
61+
_apiResourceRepository.Setup(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name))).ReturnsAsync((ApiResource)null);
62+
_apiResourceRepository.Setup(s => s.Add(It.Is<ApiResource>(i => i.Name == command.Resource.Name)));
63+
_uow.Setup(s => s.Commit()).Returns(true);
64+
65+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
66+
67+
_apiResourceRepository.Verify(s => s.Add(It.IsAny<ApiResource>()), Times.Once);
68+
_apiResourceRepository.Verify(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name)), Times.Once);
69+
70+
Assert.True(result);
71+
}
72+
73+
74+
[Fact]
75+
public async Task ShouldNotUpdateResourceWhenItDoesntExist()
76+
{
77+
var command = ResourceCommandFaker.GenerateUpdateApiResourceCommand().Generate();
78+
79+
_apiResourceRepository.Setup(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name))).ReturnsAsync(EntityResourceFaker.GenerateResource().Generate());
80+
81+
82+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
83+
84+
85+
Assert.False(result);
86+
_apiResourceRepository.Verify(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name)), Times.Once);
87+
}
88+
89+
90+
[Fact]
91+
public async Task ShouldNotUpdateResourceWhenNameIsntProvided()
92+
{
93+
var command = new UpdateApiResourceCommand(new IdentityServer4.Models.ApiResource());
94+
95+
96+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
97+
98+
Assert.False(result);
99+
_uow.Verify(v => v.Commit(), Times.Never);
100+
101+
}
102+
103+
[Fact]
104+
public async Task ShouldUpdateResource()
105+
{
106+
var command = ResourceCommandFaker.GenerateUpdateApiResourceCommand().Generate();
107+
_apiResourceRepository.Setup(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name))).ReturnsAsync(EntityResourceFaker.GenerateResource().Generate());
108+
_apiResourceRepository.Setup(s => s.UpdateWithChildrens(It.Is<ApiResource>(i => i.Name == command.Resource.Name))).Returns(Task.CompletedTask);
109+
_uow.Setup(s => s.Commit()).Returns(true);
110+
111+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
112+
113+
_apiResourceRepository.Verify(s => s.UpdateWithChildrens(It.IsAny<ApiResource>()), Times.Once);
114+
_apiResourceRepository.Verify(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name)), Times.Once);
115+
116+
Assert.True(result);
117+
}
118+
119+
[Fact]
120+
public async Task ShouldNotRemoveResourceWhenNameIsntProvided()
121+
{
122+
var command = new RemoveApiResourceCommand(null);
123+
124+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
125+
126+
Assert.False(result);
127+
_uow.Verify(v => v.Commit(), Times.Never);
128+
129+
}
130+
131+
132+
[Fact]
133+
public async Task ShouldNotRemoveResourceWhenItDoesntExist()
134+
{
135+
var command = ResourceCommandFaker.GenerateUpdateApiResourceCommand().Generate();
136+
137+
_apiResourceRepository.Setup(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name))).ReturnsAsync((ApiResource)null);
138+
139+
140+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
141+
142+
143+
Assert.False(result);
144+
_uow.Verify(v => v.Commit(), Times.Never);
145+
_apiResourceRepository.Verify(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name)), Times.Once);
146+
}
147+
148+
[Fact]
149+
public async Task ShouldRemoveResource()
150+
{
151+
var command = ResourceCommandFaker.GenerateRemoveApiResourceCommand().Generate();
152+
_apiResourceRepository.Setup(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name))).ReturnsAsync(EntityResourceFaker.GenerateResource().Generate());
153+
_apiResourceRepository.Setup(s => s.Remove(It.IsAny<int>()));
154+
155+
_uow.Setup(s => s.Commit()).Returns(true);
156+
157+
var result = await _commandHandler.Handle(command, _tokenSource.Token);
158+
159+
_apiResourceRepository.Verify(s => s.GetResource(It.Is<string>(q => q == command.Resource.Name)),Times.Once);
160+
_apiResourceRepository.Verify(s => s.Remove(It.IsAny<int>()), Times.Once);
161+
162+
Assert.True(result);
163+
}
164+
}
165+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Linq;
2+
using Bogus;
3+
using IdentityServer4.Models;
4+
5+
namespace JpProject.Domain.Tests.ApiResourceTests.Fakers
6+
{
7+
public class ApiResourceFaker
8+
{
9+
public static Faker<ApiResource> GenerateApiResource()
10+
{
11+
return new Faker<ApiResource>()
12+
.RuleFor(a => a.ApiSecrets, f => GenerateSecret().Generate(f.Random.Int(0, 2)))
13+
.RuleFor(a => a.Scopes, f => GenerateScope().Generate(f.Random.Int(0, 2)))
14+
.RuleFor(a => a.Enabled, f => f.Random.Bool())
15+
.RuleFor(a => a.Name, f => f.Lorem.Word())
16+
.RuleFor(a => a.DisplayName, f => f.Lorem.Word())
17+
.RuleFor(a => a.Description, f => f.Lorem.Word())
18+
.RuleFor(a => a.UserClaims, f => f.PickRandom(IdentityHelpers.Claims, f.Random.Int(0, 3)).ToList());
19+
}
20+
21+
public static Faker<Secret> GenerateSecret()
22+
{
23+
return new Faker<Secret>()
24+
.RuleFor(s => s.Description, f => f.Lorem.Word())
25+
.RuleFor(s => s.Value, f => f.Lorem.Word())
26+
.RuleFor(s => s.Type, f => f.PickRandom(IdentityHelpers.SecretTypes));
27+
}
28+
public static Faker<Scope> GenerateScope()
29+
{
30+
return new Faker<Scope>()
31+
.RuleFor(s => s.Name, f => f.Lorem.Word())
32+
.RuleFor(s => s.DisplayName, f => f.Lorem.Word())
33+
.RuleFor(s => s.Description, f => f.Lorem.Word())
34+
.RuleFor(s => s.Required, f => f.Random.Bool())
35+
.RuleFor(s => s.Emphasize, f => f.Random.Bool())
36+
.RuleFor(s => s.ShowInDiscoveryDocument, f => f.Random.Bool())
37+
.RuleFor(s => s.UserClaims, f => f.PickRandom(IdentityHelpers.Claims, f.Random.Int(0, 3)).ToList());
38+
}
39+
}
40+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Bogus;
2+
using IdentityServer4.EntityFramework.Entities;
3+
4+
namespace JpProject.Domain.Tests.ApiResourceTests.Fakers
5+
{
6+
public class EntityResourceFaker
7+
{
8+
public static Faker<ApiResource> GenerateResource()
9+
{
10+
return new Faker<ApiResource>()
11+
.RuleFor(a => a.Id, f => f.Random.Int(0))
12+
.RuleFor(a => a.Enabled, f => f.Random.Bool())
13+
.RuleFor(a => a.Name, f => f.Lorem.Word())
14+
.RuleFor(a => a.DisplayName, f => f.Lorem.Word())
15+
.RuleFor(a => a.Description, f => f.Lorem.Word())
16+
.RuleFor(a => a.Secrets, f => GenerateSecrets().Generate(f.Random.Int(0, 3)))
17+
.RuleFor(a => a.Scopes, f => GenerateScopes().Generate(f.Random.Int(0, 3)))
18+
.RuleFor(a => a.UserClaims, f => GenerateUserClaims().Generate(f.Random.Int(0, 2)))
19+
.RuleFor(a => a.Properties, f => GenerateProperties().Generate(f.Random.Int(0, 3)))
20+
.RuleFor(a => a.NonEditable, f => f.Random.Bool());
21+
}
22+
23+
public static Faker<ApiSecret> GenerateSecrets()
24+
{
25+
return new Faker<ApiSecret>()
26+
.RuleFor(a => a.ApiResourceId, f => f.Random.Int(0))
27+
.RuleFor(a => a.Id, f => f.Random.Int(0))
28+
.RuleFor(a => a.Description, f => f.Lorem.Word())
29+
.RuleFor(a => a.Value, f => f.Lorem.Word())
30+
.RuleFor(a => a.Type, f => f.Lorem.Word())
31+
.RuleFor(a => a.Created, f => f.Date.Past());
32+
}
33+
public static Faker<ApiScope> GenerateScopes()
34+
{
35+
return new Faker<ApiScope>()
36+
.RuleFor(a => a.Id, f => f.Random.Int(0))
37+
.RuleFor(a => a.Name, f => f.Lorem.Word())
38+
.RuleFor(a => a.DisplayName, f => f.Lorem.Word())
39+
.RuleFor(a => a.Description, f => f.Lorem.Word())
40+
.RuleFor(a => a.Required, f => f.Random.Bool())
41+
.RuleFor(a => a.Emphasize, f => f.Random.Bool())
42+
.RuleFor(a => a.ShowInDiscoveryDocument, f => f.Random.Bool())
43+
.RuleFor(a => a.UserClaims, f => GenerateApiScopeClaim().Generate(f.Random.Int(0, 2)))
44+
.RuleFor(a => a.ApiResourceId, f => f.Random.Int(0));
45+
}
46+
public static Faker<ApiScopeClaim> GenerateApiScopeClaim()
47+
{
48+
return new Faker<ApiScopeClaim>()
49+
.RuleFor(a => a.ApiScopeId, f => f.Random.Int(0))
50+
.RuleFor(a => a.Id, f => f.Random.Int(0))
51+
.RuleFor(a => a.Type, f => f.Lorem.Word());
52+
}
53+
public static Faker<ApiResourceClaim> GenerateUserClaims()
54+
{
55+
return new Faker<ApiResourceClaim>()
56+
.RuleFor(a => a.ApiResourceId, f => f.Random.Int(0))
57+
.RuleFor(a => a.Id, f => f.Random.Int(0))
58+
.RuleFor(a => a.Type, f => f.Lorem.Word());
59+
}
60+
public static Faker<ApiResourceProperty> GenerateProperties()
61+
{
62+
return new Faker<ApiResourceProperty>()
63+
.RuleFor(a => a.ApiResourceId, f => f.Random.Int(0))
64+
.RuleFor(a => a.Id, f => f.Random.Int(0))
65+
.RuleFor(a => a.Key, f => f.Lorem.Word())
66+
.RuleFor(a => a.Value, f => f.Lorem.Word());
67+
}
68+
}
69+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Bogus;
5+
using IdentityServer4.Models;
6+
using Jp.Domain.Commands.ApiResource;
7+
8+
namespace JpProject.Domain.Tests.ApiResourceTests.Fakers
9+
{
10+
public class ResourceCommandFaker
11+
{
12+
public static Faker<RegisterApiResourceCommand> GenerateRegisterApiResourceCommand()
13+
{
14+
return new Faker<RegisterApiResourceCommand>().CustomInstantiator(faker =>
15+
new RegisterApiResourceCommand(ApiResourceFaker.GenerateApiResource().Generate())
16+
);
17+
18+
}
19+
public static Faker<UpdateApiResourceCommand> GenerateUpdateApiResourceCommand()
20+
{
21+
return new Faker<UpdateApiResourceCommand>().CustomInstantiator(faker =>
22+
new UpdateApiResourceCommand(ApiResourceFaker.GenerateApiResource().Generate())
23+
);
24+
25+
}
26+
public static Faker<RemoveApiResourceCommand> GenerateRemoveApiResourceCommand()
27+
{
28+
return new Faker<RemoveApiResourceCommand>().CustomInstantiator(faker =>
29+
new RemoveApiResourceCommand(faker.Random.Word())
30+
);
31+
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)