diff --git a/mail-common/domain/src/main/kotlin/ch/protonmail/android/mailcommon/domain/usecase/IsPaidMailUser.kt b/mail-common/domain/src/main/kotlin/ch/protonmail/android/mailcommon/domain/usecase/IsPaidMailUser.kt new file mode 100644 index 0000000000..398d29f73c --- /dev/null +++ b/mail-common/domain/src/main/kotlin/ch/protonmail/android/mailcommon/domain/usecase/IsPaidMailUser.kt @@ -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 . + */ + +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 { + val user = observeUser(userId).first() ?: return DataError.Local.Unknown.left() + return user.hasSubscriptionForMail().right() + } +} diff --git a/mail-common/domain/src/test/kotlin/ch/protonmail/android/mailcommon/domain/usecase/IsPaidMailUserTest.kt b/mail-common/domain/src/test/kotlin/ch/protonmail/android/mailcommon/domain/usecase/IsPaidMailUserTest.kt new file mode 100644 index 0000000000..305a55d004 --- /dev/null +++ b/mail-common/domain/src/test/kotlin/ch/protonmail/android/mailcommon/domain/usecase/IsPaidMailUserTest.kt @@ -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 . + */ + +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() + 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) + } +} diff --git a/test/test-data/src/main/kotlin/ch/protonmail/android/testdata/user/UserTestData.kt b/test/test-data/src/main/kotlin/ch/protonmail/android/testdata/user/UserTestData.kt index b87b50d27c..7dc63bc74c 100644 --- a/test/test-data/src/main/kotlin/ch/protonmail/android/testdata/user/UserTestData.kt +++ b/test/test-data/src/main/kotlin/ch/protonmail/android/testdata/user/UserTestData.kt @@ -51,6 +51,8 @@ object UserTestData { val freeUser = build(subscribed = 0) + val paidMailUser = build(subscribed = 1) + val paidUser = build(subscribed = 2) fun build(