-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
ApiKeyAuthorizationFilterTests.cs
148 lines (123 loc) · 5.21 KB
/
ApiKeyAuthorizationFilterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Api.Infrastructure.Configurations;
using HappyCode.NetCoreBoilerplate.Api.Infrastructure.Filters;
using HappyCode.NetCoreBoilerplate.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using Microsoft.FeatureManagement;
using Moq;
using Xunit;
namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Infrastructure.Filters
{
public class ApiKeyAuthorizationFilterTests
{
private const string _key = "test_secret_key";
private bool _headersAccessed = false;
private readonly ApiKeyAuthorizationFilter _filter;
private readonly Mock<IFeatureManager> _featureManagerMock;
public ApiKeyAuthorizationFilterTests()
{
var options = Options.Create(new ApiKeySettings { SecretKey = _key });
_featureManagerMock = new Mock<IFeatureManager>(MockBehavior.Strict);
_featureManagerMock.Setup(x => x.IsEnabledAsync(FeatureFlags.ApiKey.ToString()))
.ReturnsAsync(true);
_filter = new ApiKeyAuthorizationFilter(options, _featureManagerMock.Object);
}
[Fact]
public async Task When_feature_is_disabled_Then_should_immediately_returns()
{
//given
_featureManagerMock.Setup(x => x.IsEnabledAsync(FeatureFlags.ApiKey.ToString()))
.ReturnsAsync(false);
//when
var context = GetMockedContext(secretKey:null);
await _filter.OnAuthorizationAsync(context);
//then
context.Result.Should().BeNull();
_headersAccessed.Should().BeFalse();
_featureManagerMock.VerifyAll();
}
[Fact]
public async Task When_Authorization_header_not_presented_Then_should_return_Unauthorized()
{
//when
var context = GetMockedContext(secretKey: null);
await _filter.OnAuthorizationAsync(context);
//then
context.Result.Should().BeOfType<UnauthorizedObjectResult>()
.Which.StatusCode.Should().Be(StatusCodes.Status401Unauthorized);
_headersAccessed.Should().BeTrue();
_featureManagerMock.VerifyAll();
}
[Fact]
public async Task When_Authorization_header_is_empty_Then_should_return_Unauthorized()
{
//when
var context = GetMockedContext(secretKey: string.Empty);
await _filter.OnAuthorizationAsync(context);
//then
context.Result.Should().BeOfType<UnauthorizedObjectResult>()
.Which.StatusCode.Should().Be(StatusCodes.Status401Unauthorized);
_headersAccessed.Should().BeTrue();
}
[Fact]
public async Task When_Authorization_header_has_invalid_format_Then_should_return_Unauthorized()
{
//when
var context = GetMockedContext(secretKey: $"Key {_key}");
await _filter.OnAuthorizationAsync(context);
//then
context.Result.Should().BeOfType<UnauthorizedObjectResult>()
.Which.StatusCode.Should().Be(StatusCodes.Status401Unauthorized);
_headersAccessed.Should().BeTrue();
}
[Fact]
public async Task When_Authorization_header_do_not_match_Then_should_return_Unauthorized()
{
//when
var context = GetMockedContext(secretKey: "APIKey ABC");
await _filter.OnAuthorizationAsync(context);
//then
context.Result.Should().BeOfType<UnauthorizedObjectResult>()
.Which.StatusCode.Should().Be(StatusCodes.Status401Unauthorized);
_headersAccessed.Should().BeTrue();
}
[Fact]
public async Task When_Authorization_header_match_Then_result_should_be_null()
{
//when
var context = GetMockedContext(secretKey: $"APIKey {_key}");
await _filter.OnAuthorizationAsync(context);
//then
context.Result.Should().BeNull();
_headersAccessed.Should().BeTrue();
}
private AuthorizationFilterContext GetMockedContext(string secretKey)
{
var headers = new HeaderDictionary();
if (secretKey != null)
{
headers.Add("Authorization", new[] { secretKey });
}
var httpRequestMock = new Mock<HttpRequest>();
httpRequestMock.Setup(mock => mock.Headers)
.Returns(headers)
.Callback(() => _headersAccessed = true);
var httpContextMock = new Mock<HttpContext>();
httpContextMock.SetupGet(m => m.Request)
.Returns(httpRequestMock.Object);
var actionContext = new ActionContext(
httpContextMock.Object,
new Mock<RouteData>().Object,
new Mock<ActionDescriptor>().Object
);
return new AuthorizationFilterContext(actionContext, new List<IFilterMetadata>(0));
}
}
}