def test_getState_validCookieFormat_nonExtendable(self): eventId = "event1" secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db" cookieDomain = ".test.com" isCookieHttpOnly = False isCookieSecure = False queueId = "queueId" cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId) wfHandler = HttpContextProviderMock() testObject = UserInQueueStateCookieRepository(wfHandler) issueTime = str(QueueitHelpers.getCurrentTime()) hashValue = UnitTestHelper.generateHash(eventId, queueId, 3, "idle", issueTime, secretKey) cookieValue = ("EventId=" + eventId + "&QueueId=" + queueId + "&FixedValidityMins=3" + "&RedirectType=idle" + "&IssueTime=" + issueTime + "&Hash=" + hashValue) wfHandler.setCookie(cookieKey, cookieValue, QueueitHelpers.getCookieExpirationDate(), cookieDomain, isCookieHttpOnly, isCookieSecure) state = testObject.getState(eventId, 10, secretKey, True) assert (not state.isStateExtendable()) assert (state.isValid) assert (state.isFound) assert (state.queueId == queueId) assert (state.redirectType == "idle")
def test_validateCancelRequest(self): cancelConfig = CancelEventConfig() cancelConfig.eventId = "e1" cancelConfig.queueDomain = "testDomain.com" cancelConfig.cookieDomain = "my-cookie-domain" cancelConfig.version = 10 cancelConfig.actionName = "TestCancelAction" url = "http://test.test.com?b=h" httpContextProviderMock = HttpContextProviderMock() userInQueueStateCookieRepositoryMock = UserInQueueStateCookieRepositoryMock( httpContextProviderMock) userInQueueStateCookieRepositoryMock.arrayReturns['getState'].append( StateInfo(True, True, "queueId", 3, "idle")) expectedUrl = ( "https://testDomain.com/cancel/testCustomer/e1/queueId?" + "c=testCustomer" + "&e=e1" + "&ver=" + UserInQueueService.SDK_VERSION + "&kupver=mock" + "&cver=10" + "&man=" + cancelConfig.actionName + "&r=" + QueueitHelpers.urlEncode(url)) testObject = UserInQueueService(httpContextProviderMock, userInQueueStateCookieRepositoryMock) result = testObject.validateCancelRequest(url, cancelConfig, "testCustomer", "key") assert (userInQueueStateCookieRepositoryMock.expectCall( 'cancelQueueCookie', 1, ["e1", 'my-cookie-domain', False, False])) assert (result.doRedirect()) assert (result.queueId == "queueId") assert (result.eventId == 'e1') assert (expectedUrl == result.redirectUrl) assert (cancelConfig.actionName == result.actionName)
def test_ValidateQueueRequest_NoCookie_ValidToken_ExtendableCookie_DoNotRedirect_StoreExtendableCookie( self): key = "4e1db821-a825-49da-acd0-5d376f2068db" queueConfig = QueueEventConfig() queueConfig.eventId = "e1" queueConfig.queueDomain = "testDomain.com" queueConfig.cookieValidityMinute = 10 queueConfig.cookieDomain = "testDomain" queueConfig.extendCookieValidity = True queueConfig.version = 11 queueConfig.actionName = "QueueAction" url = "http://test.test.com?b=h" httpContextProviderMock = HttpContextProviderMock() userInQueueStateCookieRepositoryMock = UserInQueueStateCookieRepositoryMock( httpContextProviderMock) userInQueueStateCookieRepositoryMock.arrayReturns['getState'].append( StateInfo(False, False, None, None, None)) token = TestHelper.generateHash( 'e1', 'queueId', str((QueueitHelpers.getCurrentTime() + (3 * 60))), 'true', None, 'queue', key) testObject = UserInQueueService(httpContextProviderMock, userInQueueStateCookieRepositoryMock) result = testObject.validateQueueRequest(url, token, queueConfig, "testCustomer", key) assert (not result.doRedirect()) assert (result.eventId == 'e1') assert (result.queueId == 'queueId') assert (result.redirectType == 'queue') assert (userInQueueStateCookieRepositoryMock.expectCall( 'store', 1, ["e1", 'queueId', None, 'testDomain', False, False, 'queue', key])) assert (queueConfig.actionName == result.actionName) assert (not userInQueueStateCookieRepositoryMock.expectCallAny( 'cancelQueueCookie'))
def test_store_hasValidState_nonExtendableCookie_CookieIsSaved(self): eventId = "event1" secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db" cookieDomain = ".test.com" isCookieHttpOnly = True isCookieSecure = True queueId = "queueId" cookieValidity = 3 cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId) wfHandler = HttpContextProviderMock() testObject = UserInQueueStateCookieRepository(wfHandler) testObject.store(eventId, queueId, cookieValidity, cookieDomain, isCookieHttpOnly, isCookieSecure, "Idle", secretKey) state = testObject.getState(eventId, cookieValidity, secretKey, True) assert (state.isValid) assert (state.queueId == queueId) assert (state.isStateExtendable() == False) assert (state.redirectType == 'Idle') assert (state.fixedCookieValidityMinutes == 3) expirationTimeDelta = wfHandler.cookieList[cookieKey][ "expiration"].replace( microsecond=0) - QueueitHelpers.getCookieExpirationDate( ).replace(microsecond=0) assert (str(expirationTimeDelta) == "0:00:00") self.assertEqual(wfHandler.cookieList[cookieKey]["cookieDomain"], cookieDomain) self.assertEqual(wfHandler.cookieList[cookieKey]["isHttpOnly"], isCookieHttpOnly) self.assertEqual(wfHandler.cookieList[cookieKey]["isSecure"], isCookieSecure)
def test_extendQueueCookie_cookieExist(self): eventId = "event1" secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db" cookieDomain = ".test.com" isCookieHttpOnly = True isCookieSecure = True queueId = "queueId" cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId) wfHandler = HttpContextProviderMock() testObject = UserInQueueStateCookieRepository(wfHandler) testObject.store(eventId, queueId, None, cookieDomain, isCookieHttpOnly, isCookieSecure, "Queue", secretKey) testObject.reissueQueueCookie(eventId, 12, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey) state = testObject.getState(eventId, 5, secretKey, True) assert (state.isValid) assert (state.queueId == queueId) assert (state.isStateExtendable()) expirationTimeDelta = wfHandler.cookieList[cookieKey][ "expiration"].replace( microsecond=0) - QueueitHelpers.getCookieExpirationDate( ).replace(microsecond=0) assert (str(expirationTimeDelta) == "0:00:00") assert ( wfHandler.cookieList[cookieKey]["cookieDomain"] == cookieDomain) assert ( wfHandler.cookieList[cookieKey]["isHttpOnly"] == isCookieHttpOnly) assert (wfHandler.cookieList[cookieKey]["isSecure"] == isCookieSecure)
def test_ValidateQueueRequest_NoCookie_TampredToken_RedirectToErrorPageWithHashError_DoNotStoreCookie( self): key = "4e1db821-a825-49da-acd0-5d376f2068db" queueConfig = QueueEventConfig() queueConfig.eventId = "e1" queueConfig.queueDomain = "testDomain.com" queueConfig.cookieValidityMinute = 10 queueConfig.extendCookieValidity = True queueConfig.version = 11 queueConfig.actionName = "Queue Action (._~-) !*|'\"" url = "http://test.test.com?b=h" httpContextProviderMock = HttpContextProviderMock() userInQueueStateCookieRepositoryMock = UserInQueueStateCookieRepositoryMock( httpContextProviderMock) userInQueueStateCookieRepositoryMock.arrayReturns['getState'].append( StateInfo(False, False, None, None, None)) token = TestHelper.generateHash( 'e1', 'queueId', str((QueueitHelpers.getCurrentTime() + (3 * 60))), 'False', None, 'idle', key) token = token.replace("False", 'True') expectedErrorUrl = "https://testDomain.com/error/hash/?c=testCustomer&e=e1" + \ "&ver=" + UserInQueueService.SDK_VERSION + \ "&kupver=mock" + \ "&cver=11" + \ "&man=" + QueueitHelpers.urlEncode(queueConfig.actionName) + \ "&queueittoken=" + token + \ "&t=" + QueueitHelpers.urlEncode(url) testObject = UserInQueueService(httpContextProviderMock, userInQueueStateCookieRepositoryMock) result = testObject.validateQueueRequest(url, token, queueConfig, "testCustomer", key) assert ( not userInQueueStateCookieRepositoryMock.expectCallAny('store')) assert (result.doRedirect()) assert (result.eventId == 'e1') matches = re.search("&ts=[^&]*", result.redirectUrl) timestamp = matches.group(0).replace("&ts=", "") timestamp = timestamp.replace("&", "") assert (QueueitHelpers.getCurrentTime() - int(timestamp) < 100) urlWithoutTimeStamp = re.sub("&ts=[^&]*", "", result.redirectUrl) assert (urlWithoutTimeStamp.upper() == expectedErrorUrl.upper()) assert (queueConfig.actionName == result.actionName) assert (queueConfig.actionName == result.actionName) assert (not userInQueueStateCookieRepositoryMock.expectCallAny( 'cancelQueueCookie')) assert ( not userInQueueStateCookieRepositoryMock.expectCallAny('store'))
def generateHash(eventId, queueId, timestamp, extendableCookie, cookieValidityMinutes, redirectType, secretKey): token = 'e_' + eventId + '~ts_' + timestamp + '~ce_' + extendableCookie + '~q_' + queueId if (cookieValidityMinutes is not None): token = token + '~cv_' + str(cookieValidityMinutes) if (redirectType is not None): token = token + '~rt_' + redirectType return token + '~h_' + QueueitHelpers.hmacSha256Encode( token, secretKey)
def test_NoCookie_NoValidToken_WithoutToken_RedirectToQueue(self): key = "4e1db821-a825-49da-acd0-5d376f2068db" queueConfig = QueueEventConfig() queueConfig.eventId = "e1" queueConfig.queueDomain = "testDomain.com" queueConfig.cookieValidityMinute = 10 queueConfig.extendCookieValidity = True queueConfig.version = 11 queueConfig.culture = 'en-US' queueConfig.layoutName = 'testlayout' queueConfig.actionName = "Queue Action (._~-) !*|'\"" url = "http://test.test.com?b=h" httpContextProviderMock = HttpContextProviderMock() userInQueueStateCookieRepositoryMock = UserInQueueStateCookieRepositoryMock( httpContextProviderMock) userInQueueStateCookieRepositoryMock.arrayReturns['getState'].append( StateInfo(False, False, None, None, None)) token = "" expectedRedirectUrl = "https://testDomain.com/?c=testCustomer&e=e1" + \ "&ver=" + UserInQueueService.SDK_VERSION + \ "&kupver=mock" + \ "&cver=11" + \ "&man=" + QueueitHelpers.urlEncode(queueConfig.actionName) + \ "&cid=en-US" + \ "&l=testlayout"+"&t=" + QueueitHelpers.urlEncode(url) testObject = UserInQueueService(httpContextProviderMock, userInQueueStateCookieRepositoryMock) result = testObject.validateQueueRequest(url, token, queueConfig, "testCustomer", key) assert ( not userInQueueStateCookieRepositoryMock.expectCallAny('store')) assert (result.doRedirect()) assert (result.eventId == 'e1') assert (result.queueId == None) assert (result.redirectUrl.upper() == expectedRedirectUrl.upper()) assert (queueConfig.actionName == result.actionName) assert (not userInQueueStateCookieRepositoryMock.expectCallAny( 'cancelQueueCookie'))
def test_validateRequest_InvalidCookie_WithoutToken_RedirectToQueue_CancelCookie( self): config = QueueEventConfig() config.eventId = "e1" config.queueDomain = "testDomain.com" config.cookieValidityMinute = 10 config.extendCookieValidity = False config.version = 11 config.culture = None config.layoutName = 'testlayout' config.actionName = "QueueAction" expectedUrl = "https://testDomain.com/?c=testCustomer&e=e1" + \ "&ver=" + UserInQueueService.SDK_VERSION + \ "&kupver=mock" + \ "&cver=11" + \ "&man=" + QueueitHelpers.urlEncode(config.actionName) + \ "&l=testlayout" httpContextProviderMock = HttpContextProviderMock() userInQueueStateCookieRepositoryMock = UserInQueueStateCookieRepositoryMock( httpContextProviderMock) userInQueueStateCookieRepositoryMock.arrayReturns['getState'].append( StateInfo(True, False, None, None, None)) testObject = UserInQueueService(httpContextProviderMock, userInQueueStateCookieRepositoryMock) result = testObject.validateQueueRequest(None, "", config, "testCustomer", "key") assert (result.doRedirect()) assert (result.redirectUrl.upper() == expectedUrl.upper()) assert ( not userInQueueStateCookieRepositoryMock.expectCallAny('store')) assert (result.eventId == config.eventId) assert (result.actionName == config.actionName) assert (userInQueueStateCookieRepositoryMock.expectCallAny( 'cancelQueueCookie'))
def generateHash(event_id, queue_id, fixed_cookie_validity_minutes, redirect_type, issue_time, secret_key): return QueueitHelpers.hmacSha256Encode( event_id + queue_id + Utils.toString(fixed_cookie_validity_minutes) + redirect_type + issue_time, secret_key)