-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CFODEV-472 throw an exception if not authorize attribute exists (or w…
…e do not explicitly allow anonymous). Tests added to break the build if these are detected at run time. (breaks the build as we have not yet added the authorise test to any command)
- Loading branch information
1 parent
bb4196a
commit 02c0e5b
Showing
4 changed files
with
112 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Cfo.Cats.Application.Common.Security; | ||
|
||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] | ||
public class AllowAnonymousAttribute : Attribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Cfo.Cats.Application.Common.Interfaces.Caching; | ||
using Cfo.Cats.Application.Common.Security; | ||
using Cfo.Cats.Domain.Common; | ||
using FluentAssertions; | ||
using MediatR; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; | ||
using NetArchTest.Rules; | ||
using NUnit.Framework; | ||
|
||
namespace Cfo.Cats.Domain.ArchitectureTests.ApplicationTests; | ||
|
||
public class RequestTests | ||
{ | ||
private static readonly Assembly ApplicationAssembly = typeof(Application.DependencyInjection).Assembly; | ||
|
||
[Test] | ||
public void Commands_Should_HaveAuthorizeAttribute() | ||
{ | ||
var result = Types.InAssembly(ApplicationAssembly) | ||
.That() | ||
.ImplementInterface(typeof(IRequest<>)) | ||
.Or() | ||
.ImplementInterface(typeof(ICacheableRequest<>)) | ||
.Or() | ||
.ImplementInterface(typeof(ICacheInvalidatorRequest<>)) | ||
.Should() | ||
.HaveCustomAttribute(typeof(AuthorAttribute)) | ||
.Or() | ||
.HaveCustomAttribute(typeof(AllowAnonymousAttribute)) | ||
.GetResult(); | ||
|
||
var failedTypes = result.FailingTypes?.Select(t => t.FullName).ToList(); | ||
|
||
var formattedFailedTypes = failedTypes == null ? "None" : string.Join("\n", failedTypes); | ||
|
||
result.IsSuccessful | ||
.Should() | ||
.BeTrue($"The following types failed the test:\n {formattedFailedTypes}"); | ||
|
||
} | ||
|
||
|
||
} |