Skip to content

Commit

Permalink
bugfix: commands were lacking authorizers
Browse files Browse the repository at this point in the history
  • Loading branch information
DejanMilicic committed Jul 7, 2021
1 parent 5713b6a commit f8885a7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/Digitalis/Infrastructure/Mediatr/AuthPipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace Digitalis.Infrastructure.Mediatr
{
internal class AuthPipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
internal class AuthPipelineQueryBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : AuthRequest<TResponse>
{
public AuthPipelineBehavior(Authenticator authenticator, IEnumerable<IAuth<TRequest, TResponse>> authorizers)
public AuthPipelineQueryBehavior(Authenticator authenticator,
IEnumerable<IAuth<TRequest, TResponse>> authorizers)
{
_ = authenticator.User;
}
Expand All @@ -19,4 +20,20 @@ public Task<TResponse> Handle(TRequest request, CancellationToken cancellationTo
return next();
}
}

internal class AuthPipelineCommandBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : AuthRequest
{
public AuthPipelineCommandBehavior(Authenticator authenticator,
IEnumerable<IAuth<TRequest>> authorizers)
{
_ = authenticator.User;
}

public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken,
RequestHandlerDelegate<TResponse> next)
{
return next();
}
}
}
3 changes: 2 additions & 1 deletion src/Digitalis/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Authenticator>();
Expand Down
42 changes: 42 additions & 0 deletions src/Specs/Features/CreateUser/Anon.cs
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", Claims = new Dictionary<string, string>()};

_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);
}
}
}

0 comments on commit f8885a7

Please sign in to comment.