Skip to content

Commit

Permalink
add: unit test ensure login request is sent with external ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jinliu9508 committed Feb 3, 2025
1 parent 3cd9afb commit 29059ee
Showing 1 changed file with 136 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ class LoginUserOperationExecutorTests : FunSpec({
test("login anonymous user fails with retry when network condition exists") {
// Given
val mockUserBackendService = mockk<IUserBackendService>()
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } throws BackendException(408, "TIMEOUT", retryAfterSeconds = 10)
coEvery {
mockUserBackendService.createUser(
any(),
any(),
any(),
any(),
)
} throws BackendException(408, "TIMEOUT", retryAfterSeconds = 10)

val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()

Expand Down Expand Up @@ -130,7 +137,17 @@ class LoginUserOperationExecutorTests : FunSpec({
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()

val loginUserOperationExecutor =
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
LoginUserOperationExecutor(
mockIdentityOperationExecutor,
MockHelper.applicationService(),
MockHelper.deviceService(),
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockSubscriptionsModelStore,
MockHelper.configModelStore(),
MockHelper.languageContext(),
)
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, null, null))

// When
Expand All @@ -154,7 +171,17 @@ class LoginUserOperationExecutorTests : FunSpec({
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()

val loginUserOperationExecutor =
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
LoginUserOperationExecutor(
mockIdentityOperationExecutor,
MockHelper.applicationService(),
MockHelper.deviceService(),
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockSubscriptionsModelStore,
MockHelper.configModelStore(),
MockHelper.languageContext(),
)
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", null))

// When
Expand Down Expand Up @@ -220,7 +247,17 @@ class LoginUserOperationExecutorTests : FunSpec({
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()

val loginUserOperationExecutor =
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
LoginUserOperationExecutor(
mockIdentityOperationExecutor,
MockHelper.applicationService(),
MockHelper.deviceService(),
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockSubscriptionsModelStore,
MockHelper.configModelStore(),
MockHelper.languageContext(),
)
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))

// When
Expand Down Expand Up @@ -256,7 +293,17 @@ class LoginUserOperationExecutorTests : FunSpec({
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()

val loginUserOperationExecutor =
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
LoginUserOperationExecutor(
mockIdentityOperationExecutor,
MockHelper.applicationService(),
MockHelper.deviceService(),
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockSubscriptionsModelStore,
MockHelper.configModelStore(),
MockHelper.languageContext(),
)
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))

// When
Expand Down Expand Up @@ -292,7 +339,17 @@ class LoginUserOperationExecutorTests : FunSpec({
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()

val loginUserOperationExecutor =
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
LoginUserOperationExecutor(
mockIdentityOperationExecutor,
MockHelper.applicationService(),
MockHelper.deviceService(),
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockSubscriptionsModelStore,
MockHelper.configModelStore(),
MockHelper.languageContext(),
)
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))

// When
Expand Down Expand Up @@ -330,7 +387,17 @@ class LoginUserOperationExecutorTests : FunSpec({
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()

val loginUserOperationExecutor =
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
LoginUserOperationExecutor(
mockIdentityOperationExecutor,
MockHelper.applicationService(),
MockHelper.deviceService(),
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockSubscriptionsModelStore,
MockHelper.configModelStore(),
MockHelper.languageContext(),
)
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))

// When
Expand Down Expand Up @@ -448,7 +515,10 @@ class LoginUserOperationExecutorTests : FunSpec({
CreateUserResponse(
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
PropertiesObject(),
listOf(SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH), SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL)),
listOf(
SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH),
SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL),
),
)
// Given
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
Expand Down Expand Up @@ -534,7 +604,10 @@ class LoginUserOperationExecutorTests : FunSpec({
CreateUserResponse(
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
PropertiesObject(),
listOf(SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH), SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL)),
listOf(
SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH),
SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL),
),
)
// Given
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
Expand Down Expand Up @@ -620,7 +693,10 @@ class LoginUserOperationExecutorTests : FunSpec({
CreateUserResponse(
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
PropertiesObject(),
listOf(SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH), SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL)),
listOf(
SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH),
SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL),
),
)
// Given
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
Expand Down Expand Up @@ -669,7 +745,12 @@ class LoginUserOperationExecutorTests : FunSpec({

// Then
response.result shouldBe ExecutionResult.SUCCESS
response.idTranslations shouldBe mapOf(localOneSignalId to remoteOneSignalId, localSubscriptionId1 to remoteSubscriptionId1, localSubscriptionId2 to remoteSubscriptionId2)
response.idTranslations shouldBe
mapOf(
localOneSignalId to remoteOneSignalId,
localSubscriptionId1 to remoteSubscriptionId1,
localSubscriptionId2 to remoteSubscriptionId2,
)
coVerify(exactly = 1) {
mockUserBackendService.createUser(
appId,
Expand All @@ -679,4 +760,48 @@ class LoginUserOperationExecutorTests : FunSpec({
)
}
}

test(
"ensure login is sent with external ID when there is no other operations and there exists an external ID in the identity model store",
) {
// Note: this test covers
// Given
val mockUserBackendService = mockk<IUserBackendService>()
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())

val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
coEvery { mockIdentityOperationExecutor.execute(any()) } returns ExecutionResponse(ExecutionResult.FAIL_NORETRY)

val mockIdentityModelStore = MockHelper.identityModelStore()
coEvery { mockIdentityModelStore.model.externalId } returns "externalId"
val mockPropertiesModelStore = MockHelper.propertiesModelStore()
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()

val loginUserOperationExecutor =
LoginUserOperationExecutor(
mockIdentityOperationExecutor,
MockHelper.applicationService(),
MockHelper.deviceService(),
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockSubscriptionsModelStore,
MockHelper.configModelStore(),
MockHelper.languageContext(),
)
// set operation externalID to null but there exists an external ID in the identity model
var operation = LoginUserOperation(appId, localOneSignalId, null, null)
val operations = listOf<Operation>(operation)

// When
val response = loginUserOperationExecutor.execute(operations)

// Then
response.result shouldBeOneOf listOf(ExecutionResult.SUCCESS, ExecutionResult.SUCCESS_STARTING_ONLY)
// ensure user is created with the same externalID set in the identity model
coVerify(
exactly = 1,
) { mockUserBackendService.createUser(appId, mapOf(IdentityConstants.EXTERNAL_ID to "externalId"), any(), any()) }
}
})

0 comments on commit 29059ee

Please sign in to comment.