Inheritance: Dev2.Runtime.Security.ServerAuthorizationService
        public void ServerAuthorizationService_Constructor_PermissionsChangedEvent_ClearsCachedRequests()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock <ISecurityService>();

            securityService.SetupGet(p => p.Permissions).Returns(new List <WindowsGroupPermission>());

            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            var request = new Mock <IAuthorizationRequest>();

            request.Setup(r => r.User.IsInRole(It.IsAny <string>())).Returns(true);
            request.Setup(r => r.Key).Returns(new AuthorizationRequestKey("User", "Url", AuthorizationContext.Any));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.Unknown);
            request.Setup(r => r.QueryString[It.IsAny <string>()]).Returns(string.Empty);

            authorizationService.IsAuthorized(request.Object);
            Assert.AreEqual(1, authorizationService.CachedRequestCount);

            //------------Execute Test---------------------------
            securityService.Raise(m => m.PermissionsChanged += null, EventArgs.Empty);

            //------------Assert Results-------------------------
            Assert.AreEqual(0, authorizationService.CachedRequestCount);
        }
        public void ServerAuthorizationService_IsAuthorized_RequestWhenNotAllowedNoResultsPendingAndHubConnect_AuthorizationCalculatedAndNotCachedIsFalse()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock <ISecurityService>();

            securityService.SetupGet(p => p.Permissions).Returns(new List <WindowsGroupPermission>());

            var authorizationService = new TestServerAuthorizationService(securityService.Object);
            var request = new Mock <IAuthorizationRequest>();

            request.Setup(r => r.User.Identity.Name).Returns("TestUser");

            request.Setup(r => r.User.IsInRole(It.IsAny <string>())).Returns(false);
            request.Setup(r => r.Key).Returns(new AuthorizationRequestKey("User", "Url", AuthorizationContext.Any));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.HubConnect);
            request.Setup(r => r.QueryString[It.IsAny <string>()]).Returns(string.Empty);

            Assert.AreEqual(0, authorizationService.CachedRequestCount);

            //------------Execute Test---------------------------
            var result = authorizationService.IsAuthorized(request.Object);

            //------------Assert Results-------------------------
            securityService.VerifyGet(p => p.Permissions, Times.AtLeast(1));
            Assert.AreEqual(0, authorizationService.CachedRequestCount);
            Assert.IsFalse(result);
        }
        public void ServerAuthorizationService_IsAuthorized_WithinTimedOutPeriod_ShouldGetFromCache()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock <ISecurityService>();

            securityService.SetupGet(p => p.Permissions).Returns(new List <WindowsGroupPermission>());
            securityService.SetupGet(p => p.TimeOutPeriod).Returns(new TimeSpan(0, 1, 0));
            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            var request = new Mock <IAuthorizationRequest>();

            request.Setup(r => r.User.IsInRole(It.IsAny <string>())).Returns(true);
            request.Setup(r => r.Key).Returns(new AuthorizationRequestKey("User", "Url", AuthorizationContext.Any));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.WebGet);
            request.Setup(r => r.QueryString[It.IsAny <string>()]).Returns(string.Empty);
            authorizationService.IsAuthorized(request.Object);
            //-------------Assert Preconditions-------------------
            securityService.VerifyGet(p => p.Permissions, Times.AtLeast(1));
            Assert.AreEqual(1, authorizationService.CachedRequestCount);
            //------------Execute Test---------------------------
            authorizationService.IsAuthorized(request.Object);
            //------------Assert Results-------------------------
            securityService.VerifyGet(p => p.Permissions, Times.AtLeast(1));
            Assert.AreEqual(1, authorizationService.CachedRequestCount);
        }
        public void ServerAuthorizationService_IsAuthorized_RequestIsSecondTime_CachedAuthorizationUsed()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock <ISecurityService>();

            securityService.SetupGet(p => p.Permissions).Returns(new List <WindowsGroupPermission>());
            securityService.SetupGet(p => p.TimeOutPeriod).Returns(new TimeSpan(0, 2, 0));

            var authorizationService = new TestServerAuthorizationService(securityService.Object);
            var request = new Mock <IAuthorizationRequest>();

            request.Setup(r => r.User.IsInRole(It.IsAny <string>())).Returns(true);
            request.Setup(r => r.Key).Returns(new Tuple <string, string>("User", "Url"));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.WebGet);
            request.Setup(r => r.QueryString[It.IsAny <string>()]).Returns(string.Empty);

            authorizationService.IsAuthorized(request.Object);
            securityService.VerifyGet(p => p.Permissions, Times.Once());
            Assert.AreEqual(1, authorizationService.CachedRequestCount);

            //------------Execute Test---------------------------
            authorizationService.IsAuthorized(request.Object);

            //------------Assert Results-------------------------
            securityService.VerifyGet(p => p.Permissions, Times.Once());
            Assert.AreEqual(1, authorizationService.CachedRequestCount);
        }
        public void ServerAuthorizationService_IsAuthorized_RequestIsNull_ThrowsArgumentNullException()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock <ISecurityService>();

            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            //------------Execute Test---------------------------
            authorizationService.IsAuthorized(null);

            //------------Assert Results-------------------------
        }
        public void ServerAuthorizationService_IsAuthorized_RequestIsNull_ThrowsArgumentNullException()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock<ISecurityService>();

            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            //------------Execute Test---------------------------
            authorizationService.IsAuthorized(null);

            //------------Assert Results-------------------------
        }
        void Verify_IsAuthorized(WindowsGroupPermission configPermissions, TestAuthorizationRequest authorizationRequest)
        {
            //------------Setup for test--------------------------
            var allowedPermissions = AuthorizationHelpers.ToPermissions(authorizationRequest.AuthorizationContext);
            var expected           = authorizationRequest.UserIsInRole && (configPermissions.Permissions & allowedPermissions) != 0;

            var securityService = new Mock <ISecurityService>();

            securityService.SetupGet(p => p.Permissions).Returns(new[] { configPermissions });
            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            //------------Execute Test---------------------------
            var authorized = authorizationService.IsAuthorized(authorizationRequest);

            //------------Assert Results-------------------------
            Assert.AreEqual(expected, authorized, string.Format("\nUserIsInRole: {0}\nAllowed: {1}\nConfig: {2}\nIsServer: {3}\nURL: {4}",
                                                                authorizationRequest.UserIsInRole, allowedPermissions, configPermissions.Permissions, configPermissions.IsServer, authorizationRequest.Url));
        }
        public void ServerAuthorizationService_Constructor_PermissionsChangedEvent_ClearsCachedRequests()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock<ISecurityService>();
            securityService.SetupGet(p => p.Permissions).Returns(new List<WindowsGroupPermission>());

            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            var request = new Mock<IAuthorizationRequest>();
            request.Setup(r => r.User.IsInRole(It.IsAny<string>())).Returns(true);
            request.Setup(r => r.Key).Returns(new Tuple<string, string>("User", "Url"));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.Unknown);
            request.Setup(r => r.QueryString[It.IsAny<string>()]).Returns(string.Empty);

            authorizationService.IsAuthorized(request.Object);
            Assert.AreEqual(1, authorizationService.CachedRequestCount);

            //------------Execute Test---------------------------
            securityService.Raise(m => m.PermissionsChanged += null, EventArgs.Empty);

            //------------Assert Results-------------------------
            Assert.AreEqual(0, authorizationService.CachedRequestCount);
        }
        public void ServerAuthorizationService_IsAuthorized_RequestWhenNotAllowedButResultsPendingAndHubConnect_AuthorizationCalculatedAndNotCachedIsTrue()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock <ISecurityService>();

            securityService.SetupGet(p => p.Permissions).Returns(new List <WindowsGroupPermission>());

            var authorizationService = new TestServerAuthorizationService(securityService.Object);
            var reciept = new FutureReceipt {
                PartID = 0, RequestID = Guid.NewGuid(), User = "******"
            };

            ResultsCache.Instance.AddResult(reciept, "<x>hello world</x>");

            var request = new Mock <IAuthorizationRequest>();

            request.Setup(r => r.User.Identity.Name).Returns("TestUser");

            request.Setup(r => r.User.IsInRole(It.IsAny <string>())).Returns(false);
            request.Setup(r => r.Key).Returns(new AuthorizationRequestKey("User", "Url", AuthorizationContext.Any));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.HubConnect);
            request.Setup(r => r.QueryString[It.IsAny <string>()]).Returns(string.Empty);

            Assert.AreEqual(0, authorizationService.CachedRequestCount);

            //------------Execute Test---------------------------
            var result = authorizationService.IsAuthorized(request.Object);

            // Clear the cache out ;)
            ResultsCache.Instance.FetchResult(reciept);

            //------------Assert Results-------------------------
            securityService.VerifyGet(p => p.Permissions, Times.AtLeast(1));
            Assert.AreEqual(0, authorizationService.CachedRequestCount);
            Assert.IsTrue(result);
        }
        public void ServerAuthorizationService_IsAuthorized_RequestWhenNotAllowedButResultsPendingAndHubConnect_AuthorizationCalculatedAndNotCachedIsTrue()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock<ISecurityService>();
            securityService.SetupGet(p => p.Permissions).Returns(new List<WindowsGroupPermission>());

            var authorizationService = new TestServerAuthorizationService(securityService.Object);
            var reciept = new FutureReceipt { PartID = 0, RequestID = Guid.NewGuid(), User = "******" };
            ResultsCache.Instance.AddResult(reciept, "<x>hello world</x>");

            var request = new Mock<IAuthorizationRequest>();
            request.Setup(r => r.User.Identity.Name).Returns("TestUser");

            request.Setup(r => r.User.IsInRole(It.IsAny<string>())).Returns(false);
            request.Setup(r => r.Key).Returns(new Tuple<string, string>("User", "Url"));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.HubConnect);
            request.Setup(r => r.QueryString[It.IsAny<string>()]).Returns(string.Empty);

            Assert.AreEqual(0, authorizationService.CachedRequestCount);

            //------------Execute Test---------------------------
            var result = authorizationService.IsAuthorized(request.Object);

            // Clear the cache out ;)
            ResultsCache.Instance.FetchResult(reciept);

            //------------Assert Results-------------------------
            securityService.VerifyGet(p => p.Permissions, Times.Exactly(2));
            Assert.AreEqual(0, authorizationService.CachedRequestCount);
            Assert.IsTrue(result);
        }
        void Verify_IsAuthorized(WindowsGroupPermission configPermissions, TestAuthorizationRequest authorizationRequest)
        {
            //------------Setup for test--------------------------           
            var allowedPermissions = AuthorizationHelpers.ToPermissions(authorizationRequest.AuthorizationContext);
            var expected = authorizationRequest.UserIsInRole && (configPermissions.Permissions & allowedPermissions) != 0;

            var securityService = new Mock<ISecurityService>();
            securityService.SetupGet(p => p.Permissions).Returns(new[] { configPermissions });
            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            //------------Execute Test---------------------------
            var authorized = authorizationService.IsAuthorized(authorizationRequest);

            //------------Assert Results-------------------------
            Assert.AreEqual(expected, authorized, string.Format("\nUserIsInRole: {0}\nAllowed: {1}\nConfig: {2}\nIsServer: {3}\nURL: {4}",
                authorizationRequest.UserIsInRole, allowedPermissions, configPermissions.Permissions, configPermissions.IsServer, authorizationRequest.Url));
        }
        public void ServerAuthorizationService_IsAuthorized_WithinTimedOutPeriod_ShouldGetFromCache()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock<ISecurityService>();
            securityService.SetupGet(p => p.Permissions).Returns(new List<WindowsGroupPermission>());
            securityService.SetupGet(p => p.TimeOutPeriod).Returns(new TimeSpan(0, 1, 0));
            var authorizationService = new TestServerAuthorizationService(securityService.Object);

            var request = new Mock<IAuthorizationRequest>();
            request.Setup(r => r.User.IsInRole(It.IsAny<string>())).Returns(true);
            request.Setup(r => r.Key).Returns(new Tuple<string, string>("User", "Url"));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.WebGet);
            request.Setup(r => r.QueryString[It.IsAny<string>()]).Returns(string.Empty);
            authorizationService.IsAuthorized(request.Object);
            //-------------Assert Preconditions-------------------
            securityService.VerifyGet(p => p.Permissions, Times.Once());
            Assert.AreEqual(1, authorizationService.CachedRequestCount);
            //------------Execute Test---------------------------
            authorizationService.IsAuthorized(request.Object);
            //------------Assert Results-------------------------
            securityService.VerifyGet(p => p.Permissions, Times.Once());
            Assert.AreEqual(1, authorizationService.CachedRequestCount);
        }
        public void ServerAuthorizationService_IsAuthorized_RequestWhenNotAllowedNoResultsPendingAndHubConnect_AuthorizationCalculatedAndNotCachedIsFalse()
        {
            //------------Setup for test--------------------------
            var securityService = new Mock<ISecurityService>();
            securityService.SetupGet(p => p.Permissions).Returns(new List<WindowsGroupPermission>());

            var authorizationService = new TestServerAuthorizationService(securityService.Object);
            var request = new Mock<IAuthorizationRequest>();
            request.Setup(r => r.User.Identity.Name).Returns("TestUser");

            request.Setup(r => r.User.IsInRole(It.IsAny<string>())).Returns(false);
            request.Setup(r => r.Key).Returns(new Tuple<string, string>("User", "Url"));
            request.Setup(r => r.RequestType).Returns(WebServerRequestType.HubConnect);
            request.Setup(r => r.QueryString[It.IsAny<string>()]).Returns(string.Empty);

            Assert.AreEqual(0, authorizationService.CachedRequestCount);

            //------------Execute Test---------------------------
            var result = authorizationService.IsAuthorized(request.Object);

            //------------Assert Results-------------------------
            securityService.VerifyGet(p => p.Permissions, Times.Exactly(2));
            Assert.AreEqual(0, authorizationService.CachedRequestCount);
            Assert.IsFalse(result);
        }