Skip to content

Commit ad12fba

Browse files
author
Donald Gray
committed
Throw exception from LimitKeyGenerator if no AuthProviders setup
1 parent 6a3873b commit ad12fba

File tree

6 files changed

+39
-11
lines changed

6 files changed

+39
-11
lines changed

src/ServiceStack.RateLimit.Redis/LimitKeyGenerator.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
namespace ServiceStack.RateLimit.Redis
55
{
6+
using System;
67
using System.Collections.Generic;
78
using Auth;
89
using Interfaces;
@@ -48,6 +49,12 @@ public virtual string GetRequestId(IRequest request)
4849

4950
public virtual string GetConsumerId(IRequest request)
5051
{
52+
if (AuthenticateService.AuthProviders == null)
53+
{
54+
throw new InvalidOperationException(
55+
"AuthService not initialized. This is required for generating default ConsumerId for RateLimitting.");
56+
}
57+
5158
IAuthSession userSession = request.GetSession();
5259

5360
// TODO This will need more love to authorize user rather than just verify authentication (not necessarily here but in general)

test/DemoService/AppHost.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public override void Configure(Container container)
3737

3838
private void SetupPlugins()
3939
{
40-
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
40+
/*Plugins.Add(new AuthFeature(() => new AuthUserSession(),
4141
new IAuthProvider[]
4242
{
4343
new BasicAuthProvider()
44-
}));
44+
}));*/
4545

46-
LimitKeyGenerator.Delimiter = ":";
47-
LimitKeyGenerator.Prefix = null;
46+
/*LimitKeyGenerator.Delimiter = ":";
47+
LimitKeyGenerator.Prefix = null;*/
4848
Plugins.Add(new RateLimitFeature(Container.Resolve<IRedisClientsManager>()));
4949
}
5050

test/DemoService/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DemoService : Service
2626
public object Any(DemoRequest demoRequest) => new DemoResponse { Message = "Response from Demo Service" };
2727
}
2828

29-
[Authenticate]
29+
//[Authenticate]
3030
public class DemoRequest : IReturn<DemoResponse>
3131
{
3232
}

test/ServiceStack.RateLimit.Redis.Tests/LimitKeyGeneratorTests.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
namespace ServiceStack.RateLimit.Redis.Tests
55
{
6+
using System;
67
using System.Linq;
78
using Auth;
89
using FakeItEasy;
@@ -20,7 +21,10 @@ public LimitKeyGeneratorTests()
2021
{
2122
if (ServiceStackHost.Instance == null)
2223
{
23-
new BasicAppHost().Init();
24+
var appHost = new BasicAppHost { TestMode = true }.Init();
25+
26+
// The GetConsumerId method requires an AuthUserSession.
27+
AuthenticateService.Init(() => new AuthUserSession(), new BasicAuthProvider(appHost.AppSettings));
2428
}
2529
}
2630

@@ -42,10 +46,11 @@ public void GetRequestId_ReturnsOperationName(string operationName)
4246
[Fact]
4347
public void GetConsumerId_ThrowsAuthenticationException_IfNotAuthenticated()
4448
{
45-
var request = new MockHttpRequest();
4649
var keyGenerator = GetGenerator();
4750

48-
Assert.Throws<AuthenticationException>(() => keyGenerator.GetConsumerId(request));
51+
Action action = () => keyGenerator.GetConsumerId(new MockHttpRequest());
52+
53+
action.ShouldThrow<AuthenticationException>();
4954
}
5055

5156
[Theory, AutoData]
@@ -174,4 +179,17 @@ private static IAuthSession SetupAuthenticatedSession(string userAuthId, IReques
174179
return authSession;
175180
}
176181
}
182+
183+
public class LimitKeyGeneratorHostlessTests
184+
{
185+
[Fact]
186+
public void GetConsumerId_ThrowsInvalidOperationException_IfNoAuthProviders()
187+
{
188+
var keyGenerator = new LimitKeyGenerator();
189+
190+
Action action = () => keyGenerator.GetConsumerId(new MockHttpRequest());
191+
192+
action.ShouldThrow<InvalidOperationException>();
193+
}
194+
}
177195
}

test/ServiceStack.RateLimit.Redis.Tests/LimitProviderBaseTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public LimitProviderBaseTests()
3232
[Fact]
3333
public void Ctor_ThrowsArgumentNullException_IfKeyGeneratorNull()
3434
{
35-
Assert.Throws<ArgumentNullException>(() => new LimitProviderBase(null, A.Fake<IAppSettings>()));
35+
Action action = () => new LimitProviderBase(null, A.Fake<IAppSettings>());
36+
action.ShouldThrow<ArgumentNullException>();
3637
}
3738

3839
[Fact]
3940
public void Ctor_ThrowsArgumentNullException_IfAppSettingNull()
4041
{
41-
Assert.Throws<ArgumentNullException>(() => new LimitProviderBase(A.Fake<ILimitKeyGenerator>(), null));
42+
Action action = () => new LimitProviderBase(A.Fake<ILimitKeyGenerator>(), null);
43+
action.ShouldThrow<ArgumentNullException>();
4244
}
4345

4446
[Theory, AutoData]

test/ServiceStack.RateLimit.Redis.Tests/RateLimitFeatureTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ private RateLimitFeature GetSut(bool setupDefaults = true)
5050
[Fact]
5151
public void Ctor_ThrowsArgumentNullException_IfRedisManagerNull()
5252
{
53-
Assert.Throws<ArgumentNullException>(() => new RateLimitFeature(null));
53+
Action action = () => new RateLimitFeature(null);
54+
action.ShouldThrow<ArgumentNullException>();
5455
}
5556

5657
[Fact]

0 commit comments

Comments
 (0)