public void SetUp() { _mocker = new AutoMocker(); var principleMock = new Mock <IPrincipal>(); var identityMock = new Mock <IIdentity>(); identityMock.SetupGet(x => x.Name).Returns("Test"); principleMock.SetupGet(x => x.Identity).Returns(identityMock.Object); _mocker.Use(principleMock.Object); _subject = _mocker.CreateInstance <RequestLimitService>(); }
public void ApiConcurrentRequestsIntervalLimit() { var tokens = new Dictionary <string, string>(); tokens["US"] = "token1"; _identityService.Setup(x => x.VerifyToken(tokens["US"])).Returns(new ClientContext { GeoLocation = "US", Token = tokens["US"] }); _resourceRepository.Setup(x => x.Get("order")) .Returns(new Resource { Key = "order", RateLimitRules = new List <LimitRule> { new LimitRule { GeoLocation = "US", Threshold = 1, TimeSpan = 2 }, } }); var tokenBucket = new InMemoryTokenBucket(_limitProvider); var limitService = new RequestLimitService(tokenBucket, _resourceRepository.Object, _limitRuleFilter); var api = new Api(_identityService.Object, _dateTimeService, limitService); var accepted = 0; var rejected = 0; Parallel.For(0, 20, (i) => { var result = api.ReceiveRequest("order", tokens["US"]); if (result == Api.Accepted) { Interlocked.Increment(ref accepted); } else { Interlocked.Increment(ref rejected); } }); Assert.AreEqual(1, accepted); Assert.AreEqual(19, rejected); Thread.Sleep(2000); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["US"])); }
public void SetUp() { _mocker = new AutoMocker(); var principleMock = new Mock <IPrincipal>(); var identityMock = new Mock <IIdentity>(); identityMock.SetupGet(x => x.Name).Returns("Test"); principleMock.SetupGet(x => x.Identity).Returns(identityMock.Object); var currentUser = new Mock <ICurrentUser>(); currentUser.Setup(x => x.Identity).Returns(identityMock.Object); currentUser.Setup(x => x.Username).Returns("Test"); currentUser.Setup(x => x.GetUser()).ReturnsAsync(new OmbiUser { UserName = "******", NormalizedUserName = "******", Id = "a" }); _mocker.Use(principleMock.Object); _mocker.Use(currentUser.Object); _subject = _mocker.CreateInstance <RequestLimitService>(); }
public void ApiMultiGeoLocationAccess() { var tokens = new Dictionary <string, string>(); tokens["US"] = "token1"; tokens["EU"] = "token2"; tokens["AU"] = "token3"; _identityService.Setup(x => x.VerifyToken(tokens["US"])).Returns(new ClientContext { GeoLocation = "US", Token = tokens["US"] }); _identityService.Setup(x => x.VerifyToken(tokens["EU"])).Returns(new ClientContext { GeoLocation = "EU", Token = tokens["EU"] }); _identityService.Setup(x => x.VerifyToken(tokens["AU"])).Returns(new ClientContext { GeoLocation = "AU", Token = tokens["AU"] }); _resourceRepository.Setup(x => x.Get("order")) .Returns(new Resource { Key = "order", RateLimitRules = new List <LimitRule> { new LimitRule { GeoLocation = "US", Threshold = 5, TimeSpan = 10 }, new LimitRule { GeoLocation = "AU", Threshold = 1, TimeSpan = 3 }, new LimitRule { GeoLocation = "AU", Threshold = 3, TimeSpan = 10 }, new LimitRule { Threshold = 1, TimeSpan = 10 }, } }); _resourceRepository.Setup(x => x.Get("product")) .Returns(new Resource { Key = "product" }); var tokenBucket = new InMemoryTokenBucket(_limitProvider); var limitService = new RequestLimitService(tokenBucket, _resourceRepository.Object, _limitRuleFilter); var api = new Api(_identityService.Object, _dateTimeService, limitService); api.ReceiveRequest("order", tokens["US"]); api.ReceiveRequest("order", tokens["AU"]); api.ReceiveRequest("order", tokens["EU"]); api.ReceiveRequest("product", tokens["AU"]); Thread.Sleep(2000); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["US"])); Assert.AreEqual(Api.Rejected, api.ReceiveRequest("order", tokens["AU"])); Assert.AreEqual(Api.Rejected, api.ReceiveRequest("order", tokens["EU"])); Thread.Sleep(2000); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["US"])); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["AU"])); Assert.AreEqual(Api.Rejected, api.ReceiveRequest("order", tokens["EU"])); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("product", tokens["AU"])); Thread.Sleep(2000); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["US"])); Assert.AreEqual(Api.Rejected, api.ReceiveRequest("order", tokens["AU"])); Assert.AreEqual(Api.Rejected, api.ReceiveRequest("order", tokens["EU"])); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("product", tokens["AU"])); Thread.Sleep(5000); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["US"])); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["AU"])); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("order", tokens["EU"])); Assert.AreEqual(Api.Accepted, api.ReceiveRequest("product", tokens["AU"])); }