diff --git a/src/Digitalis/Infrastructure/Mediatr/AuthPipelineBehavior.cs b/src/Digitalis/Infrastructure/Mediatr/AuthPipelineBehavior.cs index e6091bb..7757f24 100644 --- a/src/Digitalis/Infrastructure/Mediatr/AuthPipelineBehavior.cs +++ b/src/Digitalis/Infrastructure/Mediatr/AuthPipelineBehavior.cs @@ -6,10 +6,11 @@ namespace Digitalis.Infrastructure.Mediatr { - internal class AuthPipelineBehavior : IPipelineBehavior + internal class AuthPipelineQueryBehavior : IPipelineBehavior where TRequest : AuthRequest { - public AuthPipelineBehavior(Authenticator authenticator, IEnumerable> authorizers) + public AuthPipelineQueryBehavior(Authenticator authenticator, + IEnumerable> authorizers) { _ = authenticator.User; } @@ -19,4 +20,20 @@ public Task Handle(TRequest request, CancellationToken cancellationTo return next(); } } + + internal class AuthPipelineCommandBehavior : IPipelineBehavior + where TRequest : AuthRequest + { + public AuthPipelineCommandBehavior(Authenticator authenticator, + IEnumerable> authorizers) + { + _ = authenticator.User; + } + + public Task Handle(TRequest request, CancellationToken cancellationToken, + RequestHandlerDelegate next) + { + return next(); + } + } } diff --git a/src/Digitalis/Startup.cs b/src/Digitalis/Startup.cs index 40031ae..88e6a1e 100644 --- a/src/Digitalis/Startup.cs +++ b/src/Digitalis/Startup.cs @@ -98,7 +98,8 @@ public void ConfigureServices(IServiceCollection services) services.AddMediatR(typeof(Startup)); services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingPipelineBehavior<,>)); - services.AddTransient(typeof(IPipelineBehavior<,>), typeof(AuthPipelineBehavior<,>)); + services.AddTransient(typeof(IPipelineBehavior<,>), typeof(AuthPipelineQueryBehavior<,>)); + services.AddTransient(typeof(IPipelineBehavior<,>), typeof(AuthPipelineCommandBehavior<,>)); services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorPipelineBehavior<,>)); services.AddScoped(); diff --git a/src/Specs/Features/CreateUser/Anon.cs b/src/Specs/Features/CreateUser/Anon.cs new file mode 100644 index 0000000..8bb17cb --- /dev/null +++ b/src/Specs/Features/CreateUser/Anon.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Digitalis.Features; +using Digitalis.Models; +using FakeItEasy; +using FluentAssertions; +using Raven.Client.Documents.Session; +using Specs.Infrastructure; +using Xunit; + +namespace Specs.Features.CreateUser +{ + [Trait("Add New User", "Anon User")] + public class AnonUser : Fixture + { + private readonly HttpResponseMessage _response; + private readonly Digitalis.Features.CreateUser.Command _newUser; + + public AnonUser() + { + var client = Client(); + + _newUser = new Digitalis.Features.CreateUser.Command{Email = "john@doe.com", Claims = new Dictionary()}; + + _response = client.PostAsync("/user", + Serialize(_newUser)).Result; + + WaitForIndexing(Store); + WaitForUserToContinueTheTest(Store); + } + + [Fact(DisplayName = "1. Status 401 is returned")] + public void StatusReturned() + { + _response.StatusCode.Should().Be(401); + } + } +}