Пример #1
0
        public void ValidateRequest_NoCookie_TampredToken_RedirectToErrorPageWithHashError_DoNotStoreCookie()
        {
            Exception expectedException  = new Exception();
            var       cookieProviderMock = MockRepository.GenerateMock <IUserInQueueStateRepository>();

            var config = new EventConfig()
            {
                EventId              = "e1",
                QueueDomain          = "testDomain.com",
                CookieValidityMinute = 10,
                ExtendCookieValidity = false,
                Version              = 100
            };
            var customerKey = "4e1db821-a825-49da-acd0-5d376f2068db";
            var queueId     = "iopdb821-a825-49da-acd0-5d376f2068db";

            cookieProviderMock.Stub(stub => stub.GetState("", "")).IgnoreArguments().Return(new StateInfo(false, "", false));
            string hash = "";

            var queueitToken = QueueITTokenGenerator.GenerateToken(
                DateTime.UtcNow.AddHours(1),
                "e1",
                queueId,
                false,
                20,

                customerKey,
                out hash

                );

            queueitToken = queueitToken.Replace("False", "True");
            var currentUrl       = "http://test.test.com?b=h";
            var knownUserVersion = typeof(UserInQueueService).Assembly.GetName().Version.ToString();//queryStringList.Add($"ver=c{}");
            var expectedErrorUrl = $"https://testDomain.com/error/hash?c=testCustomer&e=e1" +
                                   $"&ver=v3-{knownUserVersion}"
                                   + $"&cver=100"
                                   + $"&queueittoken={queueitToken}"
                                   + $"&t={HttpUtility.UrlEncode(currentUrl)}";


            UserInQueueService testObject = new UserInQueueService(cookieProviderMock);
            var result = testObject.ValidateRequest(currentUrl, queueitToken, config, "testCustomer", customerKey);

            Assert.True(result.DoRedirect);

            var regex           = new Regex("&ts=[^&]*");
            var match           = regex.Match(result.RedirectUrl);
            var serverTimestamp = DateTimeHelper.GetUnixTimeStampAsDate(match.Value.Replace("&ts=", "").Replace("&", ""));

            Assert.True(DateTime.UtcNow.Subtract(serverTimestamp) < TimeSpan.FromSeconds(10));
            var redirectUrl = regex.Replace(result.RedirectUrl, "");

            Assert.True(redirectUrl.ToUpper() == expectedErrorUrl.ToUpper());
            Assert.True(config.EventId == result.EventId);
            cookieProviderMock.AssertWasNotCalled(stub => stub.Store("", "", true, "", 0, ""),
                                                  options => options.IgnoreArguments());
        }
        public void ValidateRequest_NoCookie_EventIdMismatch_RedirectToErrorPageWithEventIdMissMatchError_DoNotStoreCookie()
        {
            var cookieProviderMock = Substitute.For <IUserInQueueStateRepository>();
            var config             = new QueueEventConfig()
            {
                EventId              = "e2",
                QueueDomain          = "testDomain.com",
                CookieValidityMinute = 10,
                ExtendCookieValidity = false,
                Version              = 10,
                ActionName           = "QueueAction"
            };
            var customerKey = "4e1db821-a825-49da-acd0-5d376f2068db";
            var queueId     = "iopdb821-a825-49da-acd0-5d376f2068db";

            cookieProviderMock.GetState("", 10, "").ReturnsForAnyArgs(new StateInfo(false, false, "", null, ""));
            string hash = "";

            var queueitToken = QueueITTokenGenerator.GenerateToken(
                DateTime.UtcNow.AddHours(1),
                "e1",
                queueId,
                true,
                null,
                customerKey,
                out hash, "queue"
                );

            var targetUrl        = "http://test.test.com?b=h";
            var knownUserVersion = UserInQueueService.SDK_VERSION;
            var expectedMan      = Uri.EscapeDataString(config.ActionName);
            var expectedErrorUrl = $"https://testDomain.com/error/eventid/?c=testCustomer&e=e2" +
                                   $"&ver={knownUserVersion}" + "&cver=10"
                                   + $"&man=QueueAction"
                                   + $"&queueittoken={queueitToken}"
                                   + $"&t={Uri.EscapeDataString(targetUrl)}";

            UserInQueueService testObject = new UserInQueueService(cookieProviderMock);

            var result = testObject.ValidateQueueRequest(targetUrl, queueitToken, config, "testCustomer", customerKey);

            Assert.True(result.DoRedirect);
            var regex           = new Regex("&ts=[^&]*");
            var match           = regex.Match(result.RedirectUrl);
            var serverTimestamp = DateTimeHelper.GetDateTimeFromUnixTimeStamp(match.Value.Replace("&ts=", "").Replace("&", ""));

            Assert.True(DateTime.UtcNow.Subtract(serverTimestamp) < TimeSpan.FromSeconds(10));

            var redirectUrl = regex.Replace(result.RedirectUrl, "");

            Assert.True(redirectUrl.ToUpper() == expectedErrorUrl.ToUpper());
            Assert.True(config.EventId == result.EventId);
            Assert.Equal(config.ActionName, result.ActionName);
            cookieProviderMock.DidNotReceiveWithAnyArgs().Store("", "", null, "", null, "");
            cookieProviderMock.DidNotReceiveWithAnyArgs().CancelQueueCookie(config.EventId, config.CookieDomain);
        }
Пример #3
0
        public void ValidateRequest_NoCookie_EventIdMismatch_RedirectToErrorPageWithEventIdMissMatchError_DoNotStoreCookie()
        {
            var cookieProviderMock = MockRepository.GenerateMock <IUserInQueueStateRepository>();
            var config             = new QueueEventConfig()
            {
                EventId              = "e2",
                QueueDomain          = "testDomain.com",
                CookieValidityMinute = 10,
                ExtendCookieValidity = false,
                Version              = 10
            };
            var customerKey = "4e1db821-a825-49da-acd0-5d376f2068db";
            var queueId     = "iopdb821-a825-49da-acd0-5d376f2068db";

            cookieProviderMock.Stub(stub => stub.GetState("", 10, "")).IgnoreArguments().Return(new StateInfo(false, "", null, ""));
            string hash = "";

            var queueitToken = QueueITTokenGenerator.GenerateToken(
                DateTime.UtcNow.AddHours(1),
                "e1",
                queueId,
                true,
                null,
                customerKey,
                out hash, "queue"
                );

            var targetUrl        = "http://test.test.com?b=h";
            var knownUserVersion = UserInQueueService.SDK_VERSION;
            var expectedErrorUrl = $"https://testDomain.com/error/eventid/?c=testCustomer&e=e2" +
                                   $"&ver=v3-aspnet-{knownUserVersion}" + "&cver=10"
                                   + $"&queueittoken={queueitToken}"
                                   + $"&t={HttpUtility.UrlEncode(targetUrl)}";

            UserInQueueService testObject = new UserInQueueService(cookieProviderMock);

            var result = testObject.ValidateQueueRequest(targetUrl, queueitToken, config, "testCustomer", customerKey);

            Assert.True(result.DoRedirect);
            var regex           = new Regex("&ts=[^&]*");
            var match           = regex.Match(result.RedirectUrl);
            var serverTimestamp = DateTimeHelper.GetDateTimeFromUnixTimeStamp(match.Value.Replace("&ts=", "").Replace("&", ""));

            Assert.True(DateTime.UtcNow.Subtract(serverTimestamp) < TimeSpan.FromSeconds(10));

            var redirectUrl = regex.Replace(result.RedirectUrl, "");

            Assert.True(redirectUrl.ToUpper() == expectedErrorUrl.ToUpper());
            Assert.True(config.EventId == result.EventId);
            cookieProviderMock.AssertWasNotCalled(stub => stub.Store("", "", null, "", null, ""),
                                                  options => options.IgnoreArguments());
        }
        public void ValidateRequest_NoCookie_ValidToken_ExtendableCookie_DoNotRedirect_StoreExtendableCookie()
        {
            var cookieProviderMock = Substitute.For <IUserInQueueStateRepository>();

            var config = new QueueEventConfig()
            {
                EventId              = "e1",
                QueueDomain          = "testDomain.com",
                CookieValidityMinute = 10,
                ExtendCookieValidity = false,
                ActionName           = "QueueAction"
            };
            var customerKey = "4e1db821-a825-49da-acd0-5d376f2068db";

            var queueId = "iopdb821-a825-49da-acd0-5d376f2068db";

            cookieProviderMock.GetState("", 10, "").ReturnsForAnyArgs(new StateInfo(false, false, "", null, ""));
            string hash = "";

            var queueitToken = QueueITTokenGenerator.GenerateToken(
                DateTime.UtcNow.AddHours(1),
                "e1",
                queueId,
                true,
                null,
                customerKey,
                out hash,
                "queue");

            var targetUrl = "http://test.test.com?b=h";
            UserInQueueService testObject = new UserInQueueService(cookieProviderMock);

            var result = testObject.ValidateQueueRequest(targetUrl, queueitToken, config, "testCustomer", customerKey);

            Assert.True(!result.DoRedirect);

            cookieProviderMock.Received().Store(
                "e1",
                queueId,
                null,
                config.CookieDomain,
                "queue",
                customerKey);
            Assert.True(result.QueueId == queueId);
            Assert.True(result.RedirectType == "queue");
            Assert.True(config.EventId == result.EventId);
            cookieProviderMock.DidNotReceiveWithAnyArgs().CancelQueueCookie(config.EventId, config.CookieDomain);
        }
Пример #5
0
        public void ValidateRequest_NoCookie_ValidToken_ExtendableCookie_DoNotRedirect_StoreExtendableCookie()
        {
            var cookieProviderMock = MockRepository.GenerateMock <IUserInQueueStateRepository>();

            var config = new QueueEventConfig()
            {
                EventId              = "e1",
                QueueDomain          = "testDomain.com",
                CookieValidityMinute = 10,
                ExtendCookieValidity = false
            };
            var customerKey = "4e1db821-a825-49da-acd0-5d376f2068db";

            var queueId = "iopdb821-a825-49da-acd0-5d376f2068db";

            cookieProviderMock.Stub(stub => stub.GetState("", 10, "")).IgnoreArguments().Return(new StateInfo(false, "", null, ""));
            string hash = "";

            var queueitToken = QueueITTokenGenerator.GenerateToken(
                DateTime.UtcNow.AddHours(1),
                "e1",
                queueId,
                true,
                null,
                customerKey,
                out hash,
                "queue");

            var targetUrl = "http://test.test.com?b=h";
            UserInQueueService testObject = new UserInQueueService(cookieProviderMock);

            var result = testObject.ValidateQueueRequest(targetUrl, queueitToken, config, "testCustomer", customerKey);

            Assert.True(!result.DoRedirect);

            cookieProviderMock.AssertWasCalled(stub => stub.Store(
                                                   Arg <string> .Is.Equal("e1"),
                                                   Arg <string> .Is.Equal(queueId),
                                                   Arg <int?> .Is.Equal(null),
                                                   Arg <string> .Is.Equal(config.CookieDomain),
                                                   Arg <string> .Is.Equal("queue"),
                                                   Arg <string> .Is.Equal(customerKey),
                                                   Arg <bool?> .Is.Equal(null)));
            Assert.True(result.QueueId == queueId);
            Assert.True(result.RedirectType == "queue");
            Assert.True(config.EventId == result.EventId);
        }