diff --git a/emarsys/src/androidTest/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxyTest.kt b/emarsys/src/androidTest/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxyTest.kt index 4d1d0039..c6029220 100644 --- a/emarsys/src/androidTest/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxyTest.kt +++ b/emarsys/src/androidTest/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxyTest.kt @@ -1,7 +1,9 @@ package com.emarsys.request +import com.emarsys.common.feature.InnerFeature import com.emarsys.core.CoreCompletionHandler +import com.emarsys.core.feature.FeatureRegistry import com.emarsys.core.request.RestClient import com.emarsys.core.request.model.RequestMethod import com.emarsys.core.request.model.RequestModel @@ -45,6 +47,8 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { @Before fun setUp() { + FeatureRegistry.enableFeature(InnerFeature.MOBILE_ENGAGE) + FeatureRegistry.enableFeature(InnerFeature.PREDICT) mockRequestModel = mock { on { url } doReturn URL(CLIENT_HOST) } @@ -110,7 +114,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { @Test fun testOnSuccess_shouldExecuteOriginalRequest_whenPredictMultiIdRefreshContactTokenRequest_isSuccessful() { - val testUnauthorizedResponseModel = executeUnauthorizedPredictMultiIdSetContactTokenRequest() + val testUnauthorizedResponseModel = executeUnauthorizedPredictRequest() val testResponseModel = ResponseModel( 200, @@ -180,7 +184,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { @Test fun testOnError_shouldCall_completionHandler_withStatusCode418_whenAfterUnauthorizedRequest_PredictMultiIdRefreshContactTokenRequestFails() { - val testUnauthorizedResponseModel = executeUnauthorizedPredictMultiIdSetContactTokenRequest() + val testUnauthorizedResponseModel = executeUnauthorizedPredictRequest() val testResponseModel = ResponseModel( 400, @@ -204,9 +208,30 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { } @Test - fun testOnError_createRefreshTokenRequest_whenStatusCodeIs401_andPredictMultiIdSetContactRequest_andRefreshTokenIsAvailable() { + fun testOnError_createRefreshTokenRequest_whenStatusCodeIs401_andPredictRequest_andRefreshTokenIsAvailable_andMobileEngageEnabled() { + FeatureRegistry.enableFeature(InnerFeature.MOBILE_ENGAGE) + whenever(mockRequestModelHelper.isPredictRequest(mockRequestModel)).thenReturn( + true + ) + whenever(mockRequestModel.url).thenReturn(URL(CLIENT_HOST)) + whenever(mockResponseModel.statusCode).thenReturn(401) + whenever( + mockRequestModelFactory.createRefreshContactTokenRequest() + ).thenReturn( + mockRequestModel + ) + + proxy.onError(REQUEST_ID, mockResponseModel) + + verify(mockRestClient).execute(mockRequestModel, proxy) + } + + @Test + fun testOnError_createRefreshTokenRequest_whenStatusCodeIs401_andPredictRequest_andRefreshTokenIsAvailable_andMobileEngageDisabled() { + FeatureRegistry.disableFeature(InnerFeature.MOBILE_ENGAGE) + FeatureRegistry.enableFeature(InnerFeature.PREDICT) whenever(mockRefreshTokenStorage.get()).thenReturn(REFRESH_TOKEN) - whenever(mockRequestModelHelper.isPredictMultiIdSetContactRequest(mockRequestModel)).thenReturn( + whenever(mockRequestModelHelper.isPredictRequest(mockRequestModel)).thenReturn( true ) whenever(mockRequestModel.url).thenReturn(URL(CLIENT_HOST)) @@ -226,6 +251,8 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { @Test fun testOnError_shouldCall_coreCompletionHandler_whenStatusCodeIs401_andPredictMultiIdSetContactRequest_andRefreshTokenIsNotAvailable() { + FeatureRegistry.disableFeature(InnerFeature.MOBILE_ENGAGE) + FeatureRegistry.enableFeature(InnerFeature.PREDICT) whenever(mockRefreshTokenStorage.get()).thenReturn(null) whenever(mockRequestModelHelper.isPredictMultiIdSetContactRequest(mockRequestModel)).thenReturn( true @@ -239,7 +266,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { } @Test - fun testOnError_shouldCall_shouldGiveTheResponseToNextLevel_whenStatusCodeIs401_andNotMobileEngageRequest() { + fun testOnError_shouldGiveTheResponseToNextLevel_whenStatusCodeIs401_andNotMobileEngageRequest() { whenever(mockResponseModel.statusCode).thenReturn(401) whenever(mockRequestModelHelper.isMobileEngageRequest(any())).thenReturn(false) whenever(mockRequestModelFactory.createRefreshContactTokenRequest()).thenReturn( @@ -251,6 +278,21 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { verify(mockCoreCompletionHandler).onError(REQUEST_ID, mockResponseModel) } + @Test + fun testOnError_shouldGiveTheResponseToNextLevel_whenStatusCodeIs401_andMobileEngageAndPredictDisabled() { + FeatureRegistry.disableFeature(InnerFeature.MOBILE_ENGAGE) + FeatureRegistry.disableFeature(InnerFeature.PREDICT) + whenever(mockResponseModel.statusCode).thenReturn(401) + whenever(mockRequestModelHelper.isMobileEngageRequest(any())).thenReturn(true) + whenever(mockRequestModelFactory.createRefreshContactTokenRequest()).thenReturn( + mockRequestModel + ) + proxy.onError(REQUEST_ID, mockResponseModel) + + verify(mockRestClient, times(0)).execute(mockRequestModel, proxy) + verify(mockCoreCompletionHandler).onError(REQUEST_ID, mockResponseModel) + } + @Test fun testOnError_shouldGiveTheResponseToNextLevel_whenStatusCodeIsNot401() { whenever(mockResponseModel.statusCode).thenReturn(400) @@ -356,7 +398,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { return testUnauthorizedResponseModel } - private fun executeUnauthorizedPredictMultiIdSetContactTokenRequest(): ResponseModel { + private fun executeUnauthorizedPredictRequest(): ResponseModel { val mockUnauthorizedRequestModel = mock() val testUnauthorizedResponseModel = ResponseModel( 401, @@ -369,7 +411,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() { ) whenever(mockRefreshTokenStorage.get()).thenReturn(REFRESH_TOKEN) whenever( - mockRequestModelHelper.isPredictMultiIdSetContactRequest( + mockRequestModelHelper.isPredictRequest( testUnauthorizedResponseModel.requestModel ) ).thenReturn( diff --git a/emarsys/src/main/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxy.kt b/emarsys/src/main/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxy.kt index c90387a4..fd9012b2 100644 --- a/emarsys/src/main/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxy.kt +++ b/emarsys/src/main/java/com/emarsys/request/CoreCompletionHandlerRefreshTokenProxy.kt @@ -1,6 +1,8 @@ package com.emarsys.request +import com.emarsys.common.feature.InnerFeature import com.emarsys.core.CoreCompletionHandler +import com.emarsys.core.feature.FeatureRegistry import com.emarsys.core.request.RestClient import com.emarsys.core.response.ResponseModel import com.emarsys.core.storage.Storage @@ -50,18 +52,27 @@ class CoreCompletionHandlerRefreshTokenProxy( val response = originalResponseModel reset() coreCompletionHandler.onError(id, response!!.copy(statusCode = 418)) - } else if (isPredictSetContactTokenRequestUnauthorized(responseModel)) { - refreshPredictContactToken(id, responseModel) - } else if (isMobileEngageRequestUnauthorized(responseModel)) { + } else if (isPredictOrMobileEngageRequestUnauthorized(responseModel)) { refreshMobileEngageContactToken(responseModel) + } else if (isPredictOnlyPredictRequestUnauthorized(responseModel)) { + refreshPredictContactToken(id, responseModel) } else { callCompletionHandlerOnError(id, responseModel) } } - private fun isPredictSetContactTokenRequestUnauthorized(responseModel: ResponseModel) = + private fun isPredictOnlyPredictRequestUnauthorized(responseModel: ResponseModel) = + FeatureRegistry.isFeatureEnabled(InnerFeature.PREDICT) + && !FeatureRegistry.isFeatureEnabled(InnerFeature.MOBILE_ENGAGE) + && isPredictRequestUnauthorized(responseModel) + + private fun isPredictOrMobileEngageRequestUnauthorized(responseModel: ResponseModel) = + FeatureRegistry.isFeatureEnabled(InnerFeature.MOBILE_ENGAGE) + && (isPredictRequestUnauthorized(responseModel) || isMobileEngageRequestUnauthorized(responseModel)) + + private fun isPredictRequestUnauthorized(responseModel: ResponseModel) = (responseModel.statusCode == 401 - && requestModelHelper.isPredictMultiIdSetContactRequest(responseModel.requestModel)) + && requestModelHelper.isPredictRequest(responseModel.requestModel)) private fun isMobileEngageRequestUnauthorized(responseModel: ResponseModel) = (responseModel.statusCode == 401