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"]));
        }
        public void CookieValidateResultRepository_SetValidationResult_NotAccepted_NoCookie_Test()
        {
            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("acb");

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            EnqueueResult result = new EnqueueResult(this._queue, "http://q.queue-it.net/");

            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(0, this._response.Cookies.Count);
        }
        public void CookieValidateResultRepository_SetValidationResult_NotAccepted_NoCookie_Test()
        {
            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("acb");

            CookieValidateResultRepository repository = new CookieValidateResultRepository();

            EnqueueResult result = new EnqueueResult(this._queue, "http://q.queue-it.net/");

            repository.SetValidationResult(this._queue, result);

            Assert.AreEqual(0, this._response.Cookies.Count);
        }
        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"]));
        }