Skip to content

Commit 17202a5

Browse files
authored
Merge pull request #38 from Kros-sk/ApiBaseController
Add base controller for all api controllers + UT
2 parents d62182b + 1bd55a2 commit 17202a5

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace Kros.AspNetCore
5+
{
6+
/// <summary>
7+
/// A base class for all api controllers.
8+
/// </summary>
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
[Authorize]
12+
public abstract class ApiBaseController : ControllerBase
13+
{
14+
}
15+
}

src/Kros.AspNetCore/Kros.AspNetCore.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>1.1.5</Version>
5+
<Version>1.1.6</Version>
66
<Authors>KROS a.s.</Authors>
77
<Description>General utilities and helpers for building ASP.NET Core WEB API</Description>
88
<PackageProjectUrl>https://github.com/Kros-sk/Kros.AspNetCore/tree/master/src/Kros.AspNetCore</PackageProjectUrl>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using FluentAssertions;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace Kros.AspNetCore.Tests
9+
{
10+
public class ApiBaseControllerShould
11+
{
12+
#region Test classes
13+
14+
public class TestApiBaseController : ApiBaseController
15+
{ }
16+
17+
#endregion
18+
19+
[Fact]
20+
public void BeAuthorized()
21+
{
22+
GetControllerAttribute<AuthorizeAttribute>(new TestApiBaseController()).Should().NotBeNull();
23+
}
24+
25+
[Fact]
26+
public void BeApiController()
27+
{
28+
GetControllerAttribute<ApiControllerAttribute>(new TestApiBaseController()).Should().NotBeNull();
29+
}
30+
31+
[Fact]
32+
public void HasRouteAttribute()
33+
{
34+
GetControllerAttribute<RouteAttribute>(new TestApiBaseController()).Should().NotBeNull();
35+
}
36+
37+
private static T GetControllerAttribute<T>(ApiBaseController controller) where T : Attribute
38+
{
39+
Type type = controller.GetType();
40+
object[] attributes = type.BaseType.GetCustomAttributes(typeof(T), true);
41+
T attribute = attributes.Count() == 0 ? null : (T)attributes[0];
42+
return attribute;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)