-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add debug button send FCM token #WPB-9476 (#3050)
* feat: Add debug button send FCM token * reduce returns * fixed test * fix datadog report * after refactor fix * fix test
- Loading branch information
1 parent
089eca5
commit 8c2ad28
Showing
4 changed files
with
273 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
.../commonMain/kotlin/com/wire/kalium/logic/feature/notificationToken/SendFCMTokenUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.notificationToken | ||
|
||
import com.wire.kalium.logic.configuration.notification.NotificationTokenRepository | ||
import com.wire.kalium.logic.data.client.ClientRepository | ||
import com.wire.kalium.logic.data.id.CurrentClientIdProvider | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.fold | ||
import com.wire.kalium.logic.functional.getOrNull | ||
import com.wire.kalium.logic.functional.nullableFold | ||
|
||
/** | ||
* Sends to the API locally stored FCM push token | ||
*/ | ||
interface SendFCMTokenUseCase { | ||
suspend operator fun invoke(): Either<SendFCMTokenError, Unit> | ||
} | ||
|
||
data class SendFCMTokenError( | ||
val status: Reason, | ||
val error: String? = null, | ||
) { | ||
enum class Reason { | ||
CANT_GET_CLIENT_ID, CANT_GET_NOTIFICATION_TOKEN, CANT_REGISTER_TOKEN, | ||
} | ||
} | ||
|
||
class SendFCMTokenToAPIUseCaseImpl internal constructor( | ||
private val currentClientIdProvider: CurrentClientIdProvider, | ||
private val clientRepository: ClientRepository, | ||
private val notificationTokenRepository: NotificationTokenRepository, | ||
) : SendFCMTokenUseCase { | ||
|
||
override suspend fun invoke(): Either<SendFCMTokenError, Unit> { | ||
val clientIdResult = currentClientIdProvider() | ||
val notificationTokenResult = notificationTokenRepository.getNotificationToken() | ||
|
||
val error: SendFCMTokenError? = clientIdResult.nullableFold( | ||
{ SendFCMTokenError(SendFCMTokenError.Reason.CANT_GET_CLIENT_ID, it.toString()) }, | ||
{ | ||
notificationTokenResult.nullableFold( | ||
{ | ||
SendFCMTokenError( | ||
SendFCMTokenError.Reason.CANT_GET_NOTIFICATION_TOKEN, | ||
it.toString() | ||
) | ||
}, | ||
{ null } | ||
) | ||
} | ||
) | ||
|
||
if (error != null) { | ||
return Either.Left(error) | ||
} | ||
|
||
val clientId = clientIdResult.getOrNull()!!.value | ||
val notificationToken = notificationTokenResult.getOrNull()!! | ||
|
||
return clientRepository.registerToken( | ||
senderId = notificationToken.applicationId, | ||
client = clientId, | ||
token = notificationToken.token, | ||
transport = notificationToken.transport | ||
).fold( | ||
{ | ||
Either.Left( | ||
SendFCMTokenError( | ||
SendFCMTokenError.Reason.CANT_REGISTER_TOKEN, | ||
it.toString() | ||
) | ||
) | ||
}, | ||
{ Either.Right(Unit) } | ||
) | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
...st/kotlin/com/wire/kalium/logic/feature/notificationToken/SendFCMTokenToAPIUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.notificationToken | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.NetworkFailure | ||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.configuration.notification.NotificationToken | ||
import com.wire.kalium.logic.configuration.notification.NotificationTokenRepository | ||
import com.wire.kalium.logic.data.client.ClientRepository | ||
import com.wire.kalium.logic.data.conversation.ClientId | ||
import com.wire.kalium.logic.data.id.CurrentClientIdProvider | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.fold | ||
import io.mockative.Mock | ||
import io.mockative.any | ||
import io.mockative.coEvery | ||
import io.mockative.every | ||
import io.mockative.mock | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.fail | ||
|
||
class SendFCMTokenToAPIUseCaseTest { | ||
@Test | ||
fun whenInvokedAndSuccessfulResultInTokenRegistered() = runTest { | ||
|
||
val useCase = | ||
Arrangement() | ||
.withClientId() | ||
.withNotificationToken() | ||
.withClientRepositoryRegisterToken() | ||
.arrange() | ||
|
||
val result = useCase.invoke() | ||
assertEquals(Either.Right(Unit), result) | ||
} | ||
|
||
@Test | ||
fun whenInvokedAndFailureOnClientId() = runTest { | ||
|
||
val useCase = Arrangement() | ||
.withClientIdFailure() | ||
.withNotificationToken() | ||
.arrange() | ||
|
||
val failReason = useCase.invoke().fold( | ||
{ it.status }, | ||
{ fail("Expected failure, but got success") } | ||
) | ||
assertEquals(SendFCMTokenError.Reason.CANT_GET_CLIENT_ID, failReason) | ||
|
||
} | ||
|
||
@Test | ||
fun whenInvokedAndFailureOnNotificationToken() = runTest { | ||
|
||
val useCase = Arrangement() | ||
.withClientId() | ||
.withNotificationTokenFailure() | ||
.arrange() | ||
|
||
val failReason = useCase.invoke().fold( | ||
{ it.status }, | ||
{ fail("Expected failure, but got success") } | ||
) | ||
assertEquals(SendFCMTokenError.Reason.CANT_GET_NOTIFICATION_TOKEN, failReason) | ||
} | ||
|
||
@Test | ||
fun whenInvokedAndFailureOnClientRepositoryRegisterToken() = runTest { | ||
|
||
val useCase = Arrangement() | ||
.withClientId() | ||
.withNotificationToken() | ||
.withClientRepositoryRegisterTokenFailure() | ||
.arrange() | ||
|
||
val failReason = useCase.invoke().fold( | ||
{ it.status }, | ||
{ fail("Expected failure, but got success") } | ||
) | ||
assertEquals(SendFCMTokenError.Reason.CANT_REGISTER_TOKEN, failReason) | ||
} | ||
|
||
|
||
private class Arrangement { | ||
|
||
@Mock | ||
private val currentClientIdProvider: CurrentClientIdProvider = | ||
mock(CurrentClientIdProvider::class) | ||
|
||
@Mock | ||
private val clientRepository: ClientRepository = mock(ClientRepository::class) | ||
|
||
@Mock | ||
private val notificationTokenRepository: NotificationTokenRepository = | ||
mock(NotificationTokenRepository::class) | ||
|
||
|
||
fun arrange(): SendFCMTokenToAPIUseCaseImpl { | ||
return SendFCMTokenToAPIUseCaseImpl( | ||
currentClientIdProvider, clientRepository, notificationTokenRepository | ||
) | ||
} | ||
|
||
suspend fun withClientId() = apply { | ||
coEvery { | ||
currentClientIdProvider.invoke() | ||
}.returns(Either.Right(ClientId("clientId"))) | ||
} | ||
|
||
suspend fun withClientIdFailure() = apply { | ||
coEvery { | ||
currentClientIdProvider.invoke() | ||
}.returns(Either.Left(CoreFailure.MissingClientRegistration)) | ||
} | ||
|
||
fun withNotificationToken() = apply { | ||
every { | ||
notificationTokenRepository.getNotificationToken() | ||
}.returns(Either.Right(NotificationToken("applicationId", "token", "transport"))) | ||
} | ||
|
||
fun withNotificationTokenFailure() = apply { | ||
every { | ||
notificationTokenRepository.getNotificationToken() | ||
}.returns(Either.Left(StorageFailure.DataNotFound)) | ||
} | ||
|
||
suspend fun withClientRepositoryRegisterToken() = apply { | ||
coEvery { | ||
clientRepository.registerToken(any(), any(), any(), any()) | ||
}.returns(Either.Right(Unit)) | ||
} | ||
|
||
suspend fun withClientRepositoryRegisterTokenFailure() = apply { | ||
coEvery { | ||
clientRepository.registerToken(any(), any(), any(), any()) | ||
}.returns(Either.Left(NetworkFailure.FeatureNotSupported)) | ||
} | ||
|
||
} | ||
|
||
} |