Add UC to check if user is Paid without mail subscription

Needed by 1 click upselling logic.

MAILANDR-1587
This commit is contained in:
Niccolò Forlini
2024-04-19 13:41:32 +02:00
parent 1407f99836
commit e2f9d6f4de
3 changed files with 126 additions and 0 deletions
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2022 Proton Technologies AG
* This file is part of Proton Technologies AG and Proton Mail.
*
* Proton Mail 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.
*
* Proton Mail 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 Proton Mail. If not, see <https://www.gnu.org/licenses/>.
*/
package ch.protonmail.android.mailcommon.domain.usecase
import arrow.core.Either
import arrow.core.left
import arrow.core.right
import ch.protonmail.android.mailcommon.domain.model.DataError
import kotlinx.coroutines.flow.first
import me.proton.core.domain.entity.UserId
import me.proton.core.user.domain.extension.hasSubscriptionForMail
import javax.inject.Inject
class IsPaidMailUser @Inject constructor(
private val observeUser: ObserveUser
) {
suspend operator fun invoke(userId: UserId): Either<DataError, Boolean> {
val user = observeUser(userId).first() ?: return DataError.Local.Unknown.left()
return user.hasSubscriptionForMail().right()
}
}
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2022 Proton Technologies AG
* This file is part of Proton Technologies AG and Proton Mail.
*
* Proton Mail 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.
*
* Proton Mail 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 Proton Mail. If not, see <https://www.gnu.org/licenses/>.
*/
package ch.protonmail.android.mailcommon.domain.usecase
import arrow.core.left
import arrow.core.right
import ch.protonmail.android.mailcommon.domain.model.DataError
import ch.protonmail.android.testdata.user.UserIdTestData
import ch.protonmail.android.testdata.user.UserTestData
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.test.assertEquals
internal class IsPaidMailUserTest {
private val userId = UserIdTestData.userId
private val observeUser = mockk<ObserveUser>()
private val isPaidMailUser = IsPaidMailUser(observeUser)
@Test
fun `returns true when the user is valid and has a paid mail plan`() = runTest {
// Given
every { observeUser.invoke(userId) } returns flowOf(UserTestData.paidMailUser)
// When
val actual = isPaidMailUser(userId)
// Then
assertEquals(true.right(), actual)
}
@Test
fun `returns false when user is valid and is not paid`() = runTest {
// Given
every { observeUser.invoke(userId) } returns flowOf(UserTestData.freeUser)
// When
val actual = isPaidMailUser(userId)
// Then
assertEquals(false.right(), actual)
}
@Test
fun `returns false when user is valid and is a paid non-mail user`() = runTest {
// Given
every { observeUser.invoke(userId) } returns flowOf(UserTestData.paidUser)
// When
val actual = isPaidMailUser(userId)
// Then
assertEquals(false.right(), actual)
}
@Test
fun `returns error when user is not valid`() = runTest {
// Given
every { observeUser.invoke(userId) } returns flowOf(null)
// When
val actual = isPaidMailUser(userId)
// Then
assertEquals(DataError.Local.Unknown.left(), actual)
}
}
@@ -51,6 +51,8 @@ object UserTestData {
val freeUser = build(subscribed = 0)
val paidMailUser = build(subscribed = 1)
val paidUser = build(subscribed = 2)
fun build(