public void SessionValidateResultRepository_SetValidationResult_WithExpiration_Test()
        {
            DateTime testOffest         = DateTime.UtcNow;
            string   sessionKey         = "QueueITAccepted-SDFrts345E-customerid-eventid";
            DateTime expectedExpiration = DateTime.UtcNow.AddMinutes(5);

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return("CustomerId");
            this._knownUser.Stub(knownUser => knownUser.EventId).Return("EventId");
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(Guid.NewGuid());
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return("http://original.url/");
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(5486);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(RedirectType.Idle);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(testOffest);

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            SessionValidateResultRepository repository = new SessionValidateResultRepository();

            repository.SetValidationResult(this._queue, result, expectedExpiration);

            var actualSessionState = HttpContext.Current.Session[sessionKey] as SessionStateModel;

            Assert.IsTrue(actualSessionState != null);
            Assert.IsTrue(actualSessionState.Expiration.HasValue);
            Assert.AreEqual(expectedExpiration, actualSessionState.Expiration.Value);
        }
        public void SessionValidateResultRepository_Cancel_Test()
        {
            string sessionKey = "QueueITAccepted-SDFrts345E-customerid-eventid";

            SessionStateModel model = new SessionStateModel()
            {
                OriginalUri  = "http://original.url/",
                PlaceInQueue = 5486,
                QueueId      = Guid.NewGuid(),
                RedirectType = RedirectType.Queue,
                TimeStamp    = DateTime.UtcNow
            };

            HttpContext.Current.Session[sessionKey] = model;

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            SessionValidateResultRepository repository = new SessionValidateResultRepository();

            AcceptedConfirmedResult actualValidationResult = repository.GetValidationResult(this._queue)
                                                             as AcceptedConfirmedResult;

            repository.Cancel(this._queue, actualValidationResult);

            Assert.IsNull(HttpContext.Current.Session[sessionKey]);
        }
        public void CookieValidateResultRepository_SetValidationResult_CookieDomain_Test()
        {
            string secretKey = "acb";

            string expectedCookieDomain = ".mydomain.com";

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return("CustomerId");
            this._knownUser.Stub(knownUser => knownUser.EventId).Return("EventId");
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(Guid.NewGuid());
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return("http://original.url/");
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(5486);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(RedirectType.Queue);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(DateTime.UtcNow);

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            CookieValidateResultRepository.Configure(expectedCookieDomain);
            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.AreEqual(expectedCookieDomain, this._response.Cookies[0].Domain);
        }
        public void SessionValidateResultRepository_SetValidationResult_Test()
        {
            DateTime testOffest = DateTime.UtcNow;
            string sessionKey = "QueueITAccepted-SDFrts345E-customerid-eventid";

            Guid expectedQueueId = Guid.NewGuid();

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return("CustomerId");
            this._knownUser.Stub(knownUser => knownUser.EventId).Return("EventId");
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(expectedQueueId);
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return("http://original.url/");
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(5486);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(RedirectType.Queue);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(testOffest);

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            SessionValidateResultRepository repository = new SessionValidateResultRepository();

            repository.SetValidationResult(this._queue, result);

            var actualSessionState = HttpContext.Current.Session[sessionKey] as SessionStateModel;

            Assert.IsTrue(actualSessionState != null);
            Assert.AreEqual("http://original.url/", actualSessionState.OriginalUri);
            Assert.AreEqual(5486, actualSessionState.PlaceInQueue);
            Assert.AreEqual(expectedQueueId, actualSessionState.QueueId);
            Assert.AreEqual(RedirectType.Queue, actualSessionState.RedirectType);
            Assert.AreEqual(testOffest, actualSessionState.TimeStamp);
            Assert.IsNull(actualSessionState.Expiration);
        }
        public void CookieValidateResultRepository_SetValidationResult_CookieExpiration_Test()
        {
            DateTime testOffest = DateTime.UtcNow;

            string secretKey = "acb";

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return("CustomerId");
            this._knownUser.Stub(knownUser => knownUser.EventId).Return("EventId");
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(Guid.NewGuid());
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return("http://original.url/");
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(5486);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(RedirectType.Queue);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(DateTime.UtcNow);

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository.Configure(cookieExpiration: TimeSpan.FromMinutes(5));
            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.IsTrue(this._response.Cookies[0].Expires >= testOffest.AddMinutes(5) &&
                          this._response.Cookies[0].Expires <= DateTime.UtcNow.AddMinutes(5));
        }
        protected override void OnValidated(ActionExecutingContext filterContext, IValidateResult result)
        {
            // Check if user must be enqueued
            if (result is EnqueueResult)
            {
                // Optional action
            }

            // Check if user has been through the queue (will be invoked for every page request after the user has been validated)
            if (result is AcceptedConfirmedResult)
            {
                AcceptedConfirmedResult confirmedResult = result as AcceptedConfirmedResult;

                if (!confirmedResult.IsInitialValidationRequest)
                {
                    return; // data has already been persisted
                }
                PersistModel model = new PersistModel(
                    confirmedResult.Queue.CustomerId,
                    confirmedResult.Queue.EventId,
                    confirmedResult.KnownUser.QueueId,
                    confirmedResult.KnownUser.PlaceInQueue,
                    confirmedResult.KnownUser.TimeStamp);

                model.Persist();
            }

            base.OnValidated(filterContext, result);
        }
        public void SessionValidateResultRepository_GetValidationResult_Test()
        {
            string sessionKey = "QueueITAccepted-SDFrts345E-customerid-eventid";

            SessionStateModel model = new SessionStateModel()
            {
                OriginalUri  = "http://original.url/",
                PlaceInQueue = 5486,
                QueueId      = Guid.NewGuid(),
                RedirectType = RedirectType.Queue,
                TimeStamp    = DateTime.UtcNow
            };

            HttpContext.Current.Session[sessionKey] = model;

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            SessionValidateResultRepository repository = new SessionValidateResultRepository();

            AcceptedConfirmedResult actualValidationResult = repository.GetValidationResult(this._queue)
                                                             as AcceptedConfirmedResult;

            Assert.IsNotNull(actualValidationResult);
            Assert.AreEqual(new Uri(model.OriginalUri), actualValidationResult.KnownUser.OriginalUrl);
            Assert.AreEqual(model.PlaceInQueue, actualValidationResult.KnownUser.PlaceInQueue);
            Assert.AreEqual(model.QueueId, actualValidationResult.KnownUser.QueueId);
            Assert.AreEqual(model.RedirectType, actualValidationResult.KnownUser.RedirectType);
            Assert.AreEqual(model.TimeStamp, actualValidationResult.KnownUser.TimeStamp);
            Assert.IsFalse(actualValidationResult.IsInitialValidationRequest);
            Assert.AreSame(this._queue, actualValidationResult.Queue);
        }
        public void CookieValidateResultRepository_GetValidationResult_ReadCookie_Test()
        {
            string secretKey = "acb";

            string       expectedCustomerId       = "CustomerId";
            string       expectedEventId          = "EventId";
            Guid         expectedQueueId          = new Guid(4567846, 35, 87, 3, 5, 8, 6, 4, 8, 2, 3);
            Uri          expectedOriginalUrl      = new Uri("http://original.url/");
            int          expectedPlaceInQueue     = 5486;
            RedirectType expectedRedirectType     = RedirectType.Queue;
            long         expectedSecondsSince1970 = 5465468;
            DateTime     expectedTimeStamp        = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(expectedSecondsSince1970);
            string       cookieName      = "QueueITAccepted-SDFrts345E-" + expectedCustomerId.ToLower() + "-" + expectedEventId.ToLower();
            DateTime     expectedExpires = DateTime.UtcNow.AddMinutes(2);
            string       expectedHash    = GenerateHash(
                expectedQueueId.ToString(),
                expectedOriginalUrl.AbsoluteUri,
                expectedPlaceInQueue.ToString(),
                expectedRedirectType,
                expectedSecondsSince1970.ToString(),
                expectedExpires,
                string.Empty,
                secretKey);

            this._queue.Stub(queue => queue.CustomerId).Return(expectedCustomerId);
            this._queue.Stub(queue => queue.EventId).Return(expectedEventId);

            HttpCookie cookie = new HttpCookie(cookieName);

            cookie.Values["QueueId"]      = expectedQueueId.ToString();
            cookie.Values["OriginalUrl"]  = expectedOriginalUrl.AbsoluteUri;
            cookie.Values["PlaceInQueue"] = Hashing.EncryptPlaceInQueue(expectedPlaceInQueue);
            cookie.Values["RedirectType"] = expectedRedirectType.ToString();
            cookie.Values["TimeStamp"]    = expectedSecondsSince1970.ToString();
            cookie.Values["Hash"]         = expectedHash;
            cookie.Values["Expires"]      = expectedExpires.ToString("o");
            cookie.HttpOnly = true;

            this._request.Cookies.Add(cookie);

            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult actualResult = repository.GetValidationResult(this._queue) as AcceptedConfirmedResult;

            Assert.IsNotNull(actualResult);
            Assert.AreEqual(this._queue, actualResult.Queue);
            Assert.AreEqual(expectedCustomerId, actualResult.KnownUser.CustomerId);
            Assert.AreEqual(expectedEventId, actualResult.KnownUser.EventId);
            Assert.AreEqual(expectedQueueId, actualResult.KnownUser.QueueId);
            Assert.AreEqual(expectedOriginalUrl, actualResult.KnownUser.OriginalUrl);
            Assert.AreEqual(expectedPlaceInQueue, actualResult.KnownUser.PlaceInQueue);
            Assert.AreEqual(expectedRedirectType, actualResult.KnownUser.RedirectType);
            Assert.AreEqual(expectedTimeStamp, actualResult.KnownUser.TimeStamp);
        }
        public void CookieValidateResultRepository_SetValidationResult_WriteCookie_Hash_Test()
        {
            string secretKey = "acb";

            string       expectedCustomerId       = "CustomerId";
            string       expectedEventId          = "EventId";
            Guid         expectedQueueId          = Guid.Empty;
            string       expectedOriginalUrl      = "http://original.url/";
            int          expectedPlaceInQueue     = 0;
            RedirectType expectedRedirectType     = RedirectType.Idle;
            long         expectedSecondsSince1970 = 0;
            DateTime     expectedTimeStamp        = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(expectedSecondsSince1970);
            string       expectedCookieName       = "QueueITAccepted-SDFrts345E-" + expectedCustomerId.ToLower() + "-" + expectedEventId.ToLower();
            DateTime     expectedExpires          = DateTime.UtcNow.AddMinutes(2);
            string       expectedHash             = GenerateHash(
                expectedQueueId.ToString(),
                expectedOriginalUrl,
                expectedPlaceInQueue.ToString(),
                expectedRedirectType,
                expectedSecondsSince1970.ToString(),
                expectedExpires,
                string.Empty,
                secretKey);

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return(expectedCustomerId);
            this._knownUser.Stub(knownUser => knownUser.EventId).Return(expectedEventId);
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(expectedQueueId);
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return(expectedOriginalUrl);
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(expectedPlaceInQueue);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(expectedRedirectType);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(expectedTimeStamp);

            this._queue.Stub(queue => queue.CustomerId).Return(expectedCustomerId);
            this._queue.Stub(queue => queue.EventId).Return(expectedEventId);

            CookieValidateResultRepository.Configure(null);
            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            repository.SetValidationResult(this._queue, result, expectedExpires);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.AreEqual(expectedCookieName, this._response.Cookies[0].Name);
            Assert.AreEqual(expectedHash, this._response.Cookies[0]["Hash"]);
        }
        public void CookieValidateResultRepository_SetValidationResult_WriteCookie_Test()
        {
            string secretKey = "acb";

            string       expectedCustomerId       = "CustomerId";
            string       expectedEventId          = "EventId";
            Guid         expectedQueueId          = new Guid(4567846, 35, 87, 3, 5, 8, 6, 4, 8, 2, 3);
            string       expectedOriginalUrl      = "http://original.url/";
            int          expectedPlaceInQueue     = 5486;
            RedirectType expectedRedirectType     = RedirectType.Queue;
            long         expectedSecondsSince1970 = 5465468;
            DateTime     expectedTimeStamp        = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(expectedSecondsSince1970);
            string       expectedCookieName       = "QueueITAccepted-SDFrts345E-" + expectedCustomerId.ToLower() + "-" + expectedEventId.ToLower();

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return(expectedCustomerId);
            this._knownUser.Stub(knownUser => knownUser.EventId).Return(expectedEventId);
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(expectedQueueId);
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return(expectedOriginalUrl);
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(expectedPlaceInQueue);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(expectedRedirectType);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(expectedTimeStamp);

            this._queue.Stub(queue => queue.CustomerId).Return(expectedCustomerId);
            this._queue.Stub(queue => queue.EventId).Return(expectedEventId);

            CookieValidateResultRepository.Configure(null);
            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.AreEqual(expectedCookieName, this._response.Cookies[0].Name);
            Assert.IsNull(this._response.Cookies[0].Domain);
            Assert.IsTrue(this._response.Cookies[0].HttpOnly);
            Assert.IsTrue(this._response.Cookies[0].Expires > DateTime.UtcNow.AddMinutes(19).AddSeconds(50));
            Assert.IsTrue(this._response.Cookies[0].Expires < DateTime.UtcNow.AddMinutes(20).AddSeconds(10));
            Assert.AreEqual(expectedQueueId.ToString(), this._response.Cookies[0]["QueueId"]);
            Assert.AreEqual(expectedSecondsSince1970.ToString(), this._response.Cookies[0]["TimeStamp"]);
            Assert.AreEqual(expectedRedirectType.ToString(), this._response.Cookies[0]["RedirectType"]);
            Assert.AreEqual(expectedPlaceInQueue, Hashing.DecryptPlaceInQueue(this._response.Cookies[0]["PlaceInQueue"]));
        }
        public void SessionValidationController_ValidateRequest_KnownUserAccepted_Test()
        {
            KnownUserFactory.Reset(false);
            KnownUserFactory.Configure(SharedSecreteEventKey);

            int    expectedPlaceInqueue  = 7810;
            Guid   expectedQueueId       = Guid.NewGuid();
            string placeInQueueEncrypted = Hashing.EncryptPlaceInQueue(expectedPlaceInqueue);
            long   unixTimestamp         = Hashing.GetTimestamp();

            string urlNoHash = "http://q.queue-it.net/inqueue.aspx?c=somecust&e=someevent&q=" + expectedQueueId +
                               "&p=" + placeInQueueEncrypted + "&ts=" + unixTimestamp + "&h=";
            Uri hashUri = new Uri(urlNoHash);

            string hash        = Hashing.GenerateMD5Hash(hashUri.AbsoluteUri, SharedSecreteEventKey);
            string querystring = "c=somecust&e=someevent&q=" + expectedQueueId +
                                 "&p=" + placeInQueueEncrypted + "&ts=" + unixTimestamp + "&h=" + hash;
            string url = urlNoHash + hash;

            HttpRequest httpRequest = new HttpRequest("inqueue.aspx", url, querystring);

            HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(null));

            AcceptedConfirmedResult firstResult = SessionValidationController.ValidateRequest(
                QueueFactory.CreateQueue("somecust", "someevent")) as AcceptedConfirmedResult;


            Assert.IsNotNull(firstResult);
            Assert.AreEqual(true, firstResult.IsInitialValidationRequest);
            Assert.AreEqual(expectedQueueId, firstResult.KnownUser.QueueId);

            AcceptedConfirmedResult secondResult = SessionValidationController.ValidateRequest(
                QueueFactory.CreateQueue("somecust", "someevent")) as AcceptedConfirmedResult;

            Assert.IsNotNull(secondResult);
            Assert.IsFalse(secondResult.IsInitialValidationRequest);
        }
        public void CookieValidateResultRepository_GetValidationResult_ModifiedCookie_Test()
        {
            string secretKey = "acb";

            string       expectedCustomerId       = "CustomerId";
            string       expectedEventId          = "EventId";
            Guid         expectedQueueId          = new Guid(4567846, 35, 87, 3, 5, 8, 6, 4, 8, 2, 3);
            Uri          expectedOriginalUrl      = new Uri("http://original.url/");
            int          expectedPlaceInQueue     = 5486;
            RedirectType expectedRedirectType     = RedirectType.Queue;
            long         expectedSecondsSince1970 = 5465468;
            DateTime     expectedTimeStamp        = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(expectedSecondsSince1970);
            string       cookieName   = "QueueITAccepted-SDFrts345E-" + expectedCustomerId.ToLower() + "-" + expectedEventId.ToLower();
            string       expectedHash = "D5-48-23-FE-D0-42-D0-59-88-39-AB-D0-CA-A0-18-5D-B8-21-2C-A7-62-A9-65-73-62-68-74-C5-1C-50-09-BA";

            this._queue.Stub(queue => queue.CustomerId).Return(expectedCustomerId);
            this._queue.Stub(queue => queue.EventId).Return(expectedEventId);

            HttpCookie cookie = new HttpCookie(cookieName);

            cookie.Values["QueueId"]      = expectedQueueId.ToString();
            cookie.Values["OriginalUrl"]  = expectedOriginalUrl.AbsoluteUri;
            cookie.Values["PlaceInQueue"] = Hashing.EncryptPlaceInQueue(expectedPlaceInQueue - 10);
            cookie.Values["RedirectType"] = expectedRedirectType.ToString();
            cookie.Values["TimeStamp"]    = expectedSecondsSince1970.ToString();
            cookie.Values["Hash"]         = expectedHash;

            this._request.Cookies.Add(cookie);

            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult actualResult = repository.GetValidationResult(this._queue) as AcceptedConfirmedResult;

            Assert.IsNull(actualResult);
        }
        public void SessionValidateResultRepository_SetValidationResult_IdleMode_WithExpiration_Test()
        {
            DateTime testOffest = DateTime.UtcNow;
            string sessionKey = "QueueITAccepted-SDFrts345E-customerid-eventid";

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return("CustomerId");
            this._knownUser.Stub(knownUser => knownUser.EventId).Return("EventId");
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(Guid.NewGuid());
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return("http://original.url/");
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(5486);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(RedirectType.Idle);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(testOffest);

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            SessionValidateResultRepository repository = new SessionValidateResultRepository();

            repository.SetValidationResult(this._queue, result);

            var actualSessionState = HttpContext.Current.Session[sessionKey] as SessionStateModel;

            Assert.IsTrue(actualSessionState != null);
            Assert.IsTrue(actualSessionState.Expiration.HasValue);
            Assert.IsTrue(testOffest.AddMinutes(3) <= actualSessionState.Expiration.Value);
            Assert.IsTrue(DateTime.UtcNow.AddMinutes(3) >= actualSessionState.Expiration.Value);
        }
        public void CookieValidateResultRepository_SetValidationResult_CookieExpiration_Test()
        {
            DateTime testOffest = DateTime.UtcNow;

            string secretKey = "acb";

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return("CustomerId");
            this._knownUser.Stub(knownUser => knownUser.EventId).Return("EventId");
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(Guid.NewGuid());
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return("http://original.url/");
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(5486);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(RedirectType.Queue);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(DateTime.UtcNow);

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository.Configure(cookieExpiration: TimeSpan.FromMinutes(5));
            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.IsTrue(this._response.Cookies[0].Expires >= testOffest.AddMinutes(5) &&
                this._response.Cookies[0].Expires <= DateTime.UtcNow.AddMinutes(5));
        }
        public void CookieValidateResultRepository_SetValidationResult_CookieDomain_Test()
        {
            string secretKey = "acb";

            string expectedCookieDomain = ".mydomain.com";

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return("CustomerId");
            this._knownUser.Stub(knownUser => knownUser.EventId).Return("EventId");
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(Guid.NewGuid());
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return("http://original.url/");
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(5486);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(RedirectType.Queue);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(DateTime.UtcNow);

            this._queue.Stub(queue => queue.CustomerId).Return("CustomerId");
            this._queue.Stub(queue => queue.EventId).Return("EventId");

            CookieValidateResultRepository.Configure(expectedCookieDomain);
            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.AreEqual(expectedCookieDomain, this._response.Cookies[0].Domain);
        }
        public void CookieValidateResultRepository_SetValidationResult_WriteCookie_Hash_Test()
        {
            string secretKey = "acb";

            string expectedCustomerId = "CustomerId";
            string expectedEventId = "EventId";
            Guid expectedQueueId = Guid.Empty;
            string expectedOriginalUrl = "http://original.url/";
            int expectedPlaceInQueue = 0;
            RedirectType expectedRedirectType = RedirectType.Idle;
            long expectedSecondsSince1970 = 0;
            DateTime expectedTimeStamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(expectedSecondsSince1970);
            string expectedCookieName = "QueueITAccepted-SDFrts345E-" + expectedCustomerId.ToLower() + "-" + expectedEventId.ToLower();
            DateTime expectedExpires = DateTime.UtcNow.AddMinutes(2);
            string expectedHash = GenerateHash(
                expectedQueueId.ToString(),
                expectedOriginalUrl,
                expectedPlaceInQueue.ToString(),
                expectedRedirectType,
                expectedSecondsSince1970.ToString(),
                expectedExpires,
                string.Empty,
                secretKey);

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return(expectedCustomerId);
            this._knownUser.Stub(knownUser => knownUser.EventId).Return(expectedEventId);
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(expectedQueueId);
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return(expectedOriginalUrl);
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(expectedPlaceInQueue);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(expectedRedirectType);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(expectedTimeStamp);

            this._queue.Stub(queue => queue.CustomerId).Return(expectedCustomerId);
            this._queue.Stub(queue => queue.EventId).Return(expectedEventId);

            CookieValidateResultRepository.Configure(null);
            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);

            repository.SetValidationResult(this._queue, result, expectedExpires);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.AreEqual(expectedCookieName, this._response.Cookies[0].Name);
            Assert.AreEqual(expectedHash, this._response.Cookies[0]["Hash"]);
        }
        public void CookieValidateResultRepository_SetValidationResult_WriteCookie_Test()
        {
            string secretKey = "acb";

            string expectedCustomerId = "CustomerId";
            string expectedEventId = "EventId";
            Guid expectedQueueId = new Guid(4567846,35,87,3,5,8,6,4,8,2,3);
            string expectedOriginalUrl = "http://original.url/";
            int expectedPlaceInQueue = 5486;
            RedirectType expectedRedirectType = RedirectType.Queue;
            long expectedSecondsSince1970 = 5465468;
            DateTime expectedTimeStamp = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc).AddSeconds(expectedSecondsSince1970);
            string expectedCookieName = "QueueITAccepted-SDFrts345E-" + expectedCustomerId.ToLower() + "-" + expectedEventId.ToLower();

            this._knownUser.Stub(knownUser => knownUser.CustomerId).Return(expectedCustomerId);
            this._knownUser.Stub(knownUser => knownUser.EventId).Return(expectedEventId);
            this._knownUser.Stub(knownUser => knownUser.QueueId).Return(expectedQueueId);
            this._knownUser.Stub(knownUser => knownUser.OriginalUrl).Return(expectedOriginalUrl);
            this._knownUser.Stub(knownUser => knownUser.PlaceInQueue).Return(expectedPlaceInQueue);
            this._knownUser.Stub(knownUser => knownUser.RedirectType).Return(expectedRedirectType);
            this._knownUser.Stub(knownUser => knownUser.TimeStamp).Return(expectedTimeStamp);

            this._queue.Stub(queue => queue.CustomerId).Return(expectedCustomerId);
            this._queue.Stub(queue => queue.EventId).Return(expectedEventId);

            CookieValidateResultRepository.Configure(null);
            KnownUserFactory.Configure(secretKey);

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            AcceptedConfirmedResult result = new AcceptedConfirmedResult(this._queue, this._knownUser, true);
            
            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(1, this._response.Cookies.Count);
            Assert.AreEqual(expectedCookieName, this._response.Cookies[0].Name);
            Assert.IsNull(this._response.Cookies[0].Domain);
            Assert.IsTrue(this._response.Cookies[0].HttpOnly);
            Assert.IsTrue(this._response.Cookies[0].Expires > DateTime.UtcNow.AddMinutes(19).AddSeconds(50));
            Assert.IsTrue(this._response.Cookies[0].Expires < DateTime.UtcNow.AddMinutes(20).AddSeconds(10));
            Assert.AreEqual(expectedQueueId.ToString(), this._response.Cookies[0]["QueueId"]);
            Assert.AreEqual(expectedSecondsSince1970.ToString(), this._response.Cookies[0]["TimeStamp"]);
            Assert.AreEqual(expectedRedirectType.ToString(), this._response.Cookies[0]["RedirectType"]);
            Assert.AreEqual(expectedPlaceInQueue, Hashing.DecryptPlaceInQueue(this._response.Cookies[0]["PlaceInQueue"]));
        }