Skip to content

Commit

Permalink
feat(multi-id): refresh token requests should be called separately fo…
Browse files Browse the repository at this point in the history
…r predict only and mobile engage case

SUITEDEV-35599

Co-authored-by: megamegax <[email protected]>
  • Loading branch information
matusekma and megamegax committed May 3, 2024
1 parent 07c39ac commit eb215c6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -110,7 +114,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() {

@Test
fun testOnSuccess_shouldExecuteOriginalRequest_whenPredictMultiIdRefreshContactTokenRequest_isSuccessful() {
val testUnauthorizedResponseModel = executeUnauthorizedPredictMultiIdSetContactTokenRequest()
val testUnauthorizedResponseModel = executeUnauthorizedPredictRequest()

val testResponseModel = ResponseModel(
200,
Expand Down Expand Up @@ -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,
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -356,7 +398,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() {
return testUnauthorizedResponseModel
}

private fun executeUnauthorizedPredictMultiIdSetContactTokenRequest(): ResponseModel {
private fun executeUnauthorizedPredictRequest(): ResponseModel {
val mockUnauthorizedRequestModel = mock<RequestModel>()
val testUnauthorizedResponseModel = ResponseModel(
401,
Expand All @@ -369,7 +411,7 @@ class CoreCompletionHandlerRefreshTokenProxyTest : AnnotationSpec() {
)
whenever(mockRefreshTokenStorage.get()).thenReturn(REFRESH_TOKEN)
whenever(
mockRequestModelHelper.isPredictMultiIdSetContactRequest(
mockRequestModelHelper.isPredictRequest(
testUnauthorizedResponseModel.requestModel
)
).thenReturn(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit eb215c6

Please sign in to comment.