public void GetState_ValidCookieFormat_NonExtendable()
        {
            var eventId   = "event1";
            var secretKey = "4e1db821-a825-49da-acd0-5d376f2068db";

            var queueId   = "f8757c2d-34c2-4639-bef2-1736cdd30bbb";
            var cookieKey = UserInQueueStateCookieRepository.GetCookieKey(eventId);

            var fakeContext = MockRepository.GenerateMock <HttpContextBase>();

            var issueTime = DateTimeHelper.GetUnixTimeStampFromDate(DateTime.UtcNow);
            var hash      = QueueITTokenGenerator.GetSHA256Hash(eventId.ToLower() + queueId + "3" + "idle" + issueTime.ToString(),
                                                                secretKey);
            var cookieValue = HttpUtility.UrlEncode($"EventId={eventId}&QueueId={queueId}&{_FixedCookieValidityMinutesKey}=3&RedirectType=idle&IssueTime={issueTime}&Hash={hash}");

            var fakeRequest = MockRepository.GenerateMock <HttpRequestBase>();

            fakeContext.Stub(stub => stub.Request).Return(fakeRequest);
            var cookies = new HttpCookieCollection()
            {
                new HttpCookie("key1")
                {
                    Value = "test"
                },
                new HttpCookie("a")
                {
                    Value = "test"
                },
                new HttpCookie("b")
                {
                    Value = "test"
                },
                new HttpCookie(cookieKey)
                {
                    Value = cookieValue
                }
            };

            fakeRequest.Stub(stub => stub.Cookies).Return(cookies);

            var testObject = new UserInQueueStateCookieRepository(fakeContext);

            var state = testObject.GetState(eventId, 10, secretKey);

            Assert.False(state.IsStateExtendable);
            Assert.True(state.IsValid);
            Assert.True(state.QueueId == queueId);
            Assert.True(state.RedirectType == "idle");
        }
コード例 #2
0
        public void ExtendQueueCookie_CookieExist_Test()
        {
            var eventId   = "event1";
            var secretKey = "4e1db821-a825-49da-acd0-5d376f2068db";

            var queueId   = "f8757c2d-34c2-4639-bef2-1736cdd30bbb";
            var cookieKey = UserInQueueStateCookieRepository.GetCookieKey(eventId);
            var issueTime = DateTimeHelper.GetUnixTimeStampFromDate(DateTime.UtcNow.AddMinutes(-1));
            var hash      = QueueITTokenGenerator.GetSHA256Hash(eventId.ToLower() + queueId + "3" + "idle" + issueTime.ToString(),
                                                                secretKey);
            var cookieValue = $"EventId={eventId}&QueueId={queueId}&{_FixedCookieValidityMinutesKey}=3&RedirectType=idle&IssueTime={issueTime}&Hash={hash}";

            KnownUserTest.HttpContextMock fakeContext = new KnownUserTest.HttpContextMock();
            var fakeRequest = new KnownUserTest.MockHttpRequest()
            {
                CookiesValue = new NameValueCollection()
                {
                    { cookieKey, cookieValue }
                }
            };

            fakeContext.HttpRequest = fakeRequest;
            var fakeResponse = new KnownUserTest.MockHttpResponse()
            {
            };

            fakeContext.HttpResponse = fakeResponse;

            var testObject = new UserInQueueStateCookieRepository(fakeContext);

            testObject.ReissueQueueCookie(eventId, 12, "testdomain", secretKey);

            var newIssueTime = DateTimeHelper.GetDateTimeFromUnixTimeStamp(CookieHelper.ToNameValueCollectionFromValue(fakeResponse.CookiesValue[cookieKey]["cookieValue"].ToString())["IssueTime"]);

            Assert.True(newIssueTime.Subtract(DateTime.UtcNow) < TimeSpan.FromSeconds(2));
            Assert.True(fakeResponse.CookiesValue[cookieKey]["domain"].ToString() == "testdomain");

            var state = testObject.GetState(eventId, 5, secretKey);

            Assert.True(state.IsValid);
            Assert.True(!state.IsStateExtendable);
            Assert.True(state.QueueId == queueId);
            Assert.True(state.RedirectType == "idle");
        }
コード例 #3
0
        public void GetState_ValidCookieFormat_Extendable()
        {
            var eventId   = "event1";
            var secretKey = "4e1db821-a825-49da-acd0-5d376f2068db";

            var queueId   = "f8757c2d-34c2-4639-bef2-1736cdd30bbb";
            var cookieKey = UserInQueueStateCookieRepository.GetCookieKey(eventId);



            var issueTime = DateTimeHelper.GetUnixTimeStampFromDate(DateTime.UtcNow);
            var hash      = QueueITTokenGenerator.GetSHA256Hash(eventId.ToLower() + queueId + "queue" + issueTime.ToString(),
                                                                secretKey);
            var cookieValue = $"EventId={eventId}&QueueId={queueId}&RedirectType=queue&IssueTime={issueTime}&Hash={hash}";

            KnownUserTest.HttpContextMock fakeContext = new KnownUserTest.HttpContextMock();
            var fakeRequest = new KnownUserTest.MockHttpRequest()
            {
                CookiesValue = new NameValueCollection()
                {
                    { cookieKey, cookieValue }
                }
            };

            fakeContext.HttpRequest = fakeRequest;
            var fakeResponse = new KnownUserTest.MockHttpResponse()
            {
            };

            fakeContext.HttpResponse = fakeResponse;


            var testObject = new UserInQueueStateCookieRepository(fakeContext);

            var state = testObject.GetState(eventId, 10, secretKey);

            Assert.True(state.IsStateExtendable);
            Assert.True(state.IsValid);
            Assert.True(state.IsFound);
            Assert.True(state.QueueId == queueId);
            Assert.True(state.RedirectType == "queue");
        }
コード例 #4
0
        public void GetState_OldCookie_InValid_ExpiredCookie_NonExtendable()
        {
            var eventId   = "event1";
            var secretKey = "4e1db821-a825-49da-acd0-5d376f2068db";

            var queueId   = "f8757c2d-34c2-4639-bef2-1736cdd30bbb";
            var cookieKey = UserInQueueStateCookieRepository.GetCookieKey(eventId);

            var issueTime = DateTimeHelper.GetUnixTimeStampFromDate(DateTime.UtcNow.AddMinutes(-4));
            var hash      = QueueITTokenGenerator.GetSHA256Hash(eventId.ToLower() + queueId + "3" + "idle" + issueTime.ToString(),
                                                                secretKey);
            var cookieValue = Uri.EscapeDataString($"EventId={eventId}&QueueId={queueId}&{_FixedCookieValidityMinutesKey}=3&RedirectType=idle&IssueTime={issueTime}&Hash={hash}");

            KnownUserTest.HttpContextMock fakeContext = new KnownUserTest.HttpContextMock();
            var fakeRequest = new KnownUserTest.MockHttpRequest()
            {
                CookiesValue = new NameValueCollection()
                {
                    { cookieKey, cookieValue }
                }
            };

            fakeContext.HttpRequest = fakeRequest;
            var fakeResponse = new KnownUserTest.MockHttpResponse()
            {
            };

            fakeContext.HttpResponse = fakeResponse;

            var testObject = new UserInQueueStateCookieRepository(fakeContext);

            var state = testObject.GetState(eventId, 10, secretKey);

            Assert.True(!state.IsValid);
            Assert.True(state.IsFound);
        }
        public void ExtendQueueCookie_CookieExist_Test()
        {
            var eventId   = "event1";
            var secretKey = "4e1db821-a825-49da-acd0-5d376f2068db";

            var queueId   = "f8757c2d-34c2-4639-bef2-1736cdd30bbb";
            var cookieKey = UserInQueueStateCookieRepository.GetCookieKey(eventId);

            var fakeContext = MockRepository.GenerateMock <HttpContextBase>();

            var issueTime = DateTimeHelper.GetUnixTimeStampFromDate(DateTime.UtcNow.AddMinutes(-1));
            var hash      = QueueITTokenGenerator.GetSHA256Hash(eventId.ToLower() + queueId + "3" + "idle" + issueTime.ToString(),
                                                                secretKey);
            var cookieValue = $"EventId={eventId}&QueueId={queueId}&{_FixedCookieValidityMinutesKey}=3&RedirectType=idle&IssueTime={issueTime}&Hash={hash}";

            var fakeRequest = MockRepository.GenerateMock <HttpRequestBase>();

            fakeContext.Stub(stub => stub.Request).Return(fakeRequest);
            var cookies = new HttpCookieCollection()
            {
                new HttpCookie("key1")
                {
                    Value = "test"
                },
                new HttpCookie("a")
                {
                    Value = "test"
                },
                new HttpCookie("b")
                {
                    Value = "test"
                },
                new HttpCookie(cookieKey)
                {
                    Value = cookieValue, Domain = "testdomain"
                }
            };

            fakeRequest.Stub(stub => stub.Cookies).Return(cookies);

            var fakeResponse = MockRepository.GenerateMock <HttpResponseBase>();

            fakeContext.Stub(stub => stub.Response).Return(fakeResponse);
            fakeResponse.Stub(stub => stub.Cookies).Return(cookies);

            var testObject = new UserInQueueStateCookieRepository(fakeContext);

            testObject.ReissueQueueCookie(eventId, 12, secretKey);

            var newIssueTime = DateTimeHelper.GetDateTimeFromUnixTimeStamp(CookieHelper.ToNameValueCollectionFromValue(cookies[cookieKey].Value)["IssueTime"]);

            Assert.True(newIssueTime.Subtract(DateTime.UtcNow) < TimeSpan.FromSeconds(2));
            Assert.True(cookies[cookieKey].Domain == "testdomain");

            var state = testObject.GetState(eventId, 5, secretKey);

            Assert.True(state.IsValid);
            Assert.True(!state.IsStateExtendable);
            Assert.True(state.QueueId == queueId);
            Assert.True(state.RedirectType == "idle");
        }