2020-08-04 13:18:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Proton Technologies AG
|
|
|
|
* This file is part of Proton Technologies AG and ProtonCore.
|
|
|
|
*
|
|
|
|
* ProtonCore 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.
|
|
|
|
*
|
|
|
|
* ProtonCore 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 ProtonCore. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package me.proton.core.network.data
|
|
|
|
|
2020-11-11 15:59:10 +00:00
|
|
|
import android.os.Build
|
2020-09-25 09:28:47 +00:00
|
|
|
import io.mockk.MockKAnnotations
|
2020-10-28 18:32:21 +00:00
|
|
|
import io.mockk.coEvery
|
2020-08-04 13:18:43 +00:00
|
|
|
import io.mockk.every
|
2020-09-25 09:28:47 +00:00
|
|
|
import io.mockk.impl.annotations.MockK
|
2020-08-04 13:18:43 +00:00
|
|
|
import io.mockk.mockk
|
|
|
|
import io.mockk.spyk
|
|
|
|
import io.mockk.verify
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
|
|
|
import me.proton.core.network.data.di.ApiFactory
|
2020-09-25 09:28:47 +00:00
|
|
|
import me.proton.core.network.data.util.MockApiClient
|
|
|
|
import me.proton.core.network.data.util.MockLogger
|
|
|
|
import me.proton.core.network.data.util.MockNetworkPrefs
|
|
|
|
import me.proton.core.network.data.util.MockSession
|
|
|
|
import me.proton.core.network.data.util.MockSessionListener
|
|
|
|
import me.proton.core.network.data.util.TestRetrofitApi
|
|
|
|
import me.proton.core.network.data.util.TestTLSHelper
|
|
|
|
import me.proton.core.network.data.util.prepareResponse
|
|
|
|
import me.proton.core.network.domain.ApiManager
|
|
|
|
import me.proton.core.network.domain.ApiResult
|
|
|
|
import me.proton.core.network.domain.NetworkManager
|
|
|
|
import me.proton.core.network.domain.NetworkPrefs
|
2020-08-04 13:18:43 +00:00
|
|
|
import me.proton.core.network.domain.humanverification.HumanVerificationHeaders
|
2020-09-25 09:28:47 +00:00
|
|
|
import me.proton.core.network.domain.session.Session
|
|
|
|
import me.proton.core.network.domain.session.SessionListener
|
|
|
|
import me.proton.core.network.domain.session.SessionProvider
|
2020-08-04 13:18:43 +00:00
|
|
|
import me.proton.core.util.kotlin.equalsNoCase
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import okhttp3.mockwebserver.MockWebServer
|
|
|
|
import org.junit.After
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
|
|
|
import org.robolectric.RobolectricTestRunner
|
2020-11-11 15:59:10 +00:00
|
|
|
import org.robolectric.annotation.Config
|
2020-08-04 13:18:43 +00:00
|
|
|
import retrofit2.converter.scalars.ScalarsConverterFactory
|
2020-09-25 09:28:47 +00:00
|
|
|
import kotlin.test.BeforeTest
|
2020-08-04 13:18:43 +00:00
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertNotNull
|
|
|
|
import kotlin.test.assertNull
|
|
|
|
import kotlin.test.assertTrue
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Human verification related tests.
|
|
|
|
*
|
|
|
|
* @author Dino Kadrikj.
|
|
|
|
*/
|
2020-11-11 15:59:10 +00:00
|
|
|
@Config(sdk = [Build.VERSION_CODES.M])
|
2020-08-04 13:18:43 +00:00
|
|
|
@RunWith(RobolectricTestRunner::class)
|
|
|
|
internal class HumanVerificationTests {
|
|
|
|
|
|
|
|
private val humanVerificationResponse =
|
|
|
|
"""
|
|
|
|
{
|
|
|
|
"Code": 9001,
|
|
|
|
"Error": "Human verification required",
|
|
|
|
"ErrorDescription": "",
|
|
|
|
"Details": {
|
|
|
|
"HumanVerificationMethods" : [
|
|
|
|
"captcha",
|
|
|
|
"email",
|
|
|
|
"sms"
|
|
|
|
],
|
|
|
|
"HumanVerificationToken": "test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""".trimIndent()
|
|
|
|
|
|
|
|
private val otherDetailsResponse =
|
|
|
|
"""
|
|
|
|
{
|
|
|
|
"Code": 9002,
|
|
|
|
"Error": "Something other happened",
|
|
|
|
"ErrorDescription": "",
|
|
|
|
"Details": {
|
|
|
|
"SomethingOther" : "something other"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""".trimIndent()
|
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
private val scope = CoroutineScope(TestCoroutineDispatcher())
|
2020-08-04 13:18:43 +00:00
|
|
|
|
|
|
|
private val testTlsHelper = TestTLSHelper()
|
2020-09-25 09:28:47 +00:00
|
|
|
private lateinit var apiFactory: ApiFactory
|
|
|
|
private lateinit var webServer: MockWebServer
|
2020-08-04 13:18:43 +00:00
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
private lateinit var backend: ProtonApiBackend<TestRetrofitApi>
|
|
|
|
private lateinit var logger: MockLogger
|
|
|
|
private lateinit var client: MockApiClient
|
2020-08-04 13:18:43 +00:00
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
private lateinit var session: Session
|
|
|
|
|
|
|
|
@MockK
|
|
|
|
private lateinit var sessionProvider: SessionProvider
|
|
|
|
private var sessionListener: SessionListener = MockSessionListener(
|
|
|
|
onTokenRefreshed = { session -> this.session = session }
|
|
|
|
)
|
2020-08-04 13:18:43 +00:00
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
private var isNetworkAvailable = true
|
|
|
|
private val networkManager = mockk<NetworkManager>()
|
2020-08-04 13:18:43 +00:00
|
|
|
|
|
|
|
private lateinit var prefs: NetworkPrefs
|
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
@BeforeTest
|
2020-08-04 13:18:43 +00:00
|
|
|
fun before() {
|
2020-09-25 09:28:47 +00:00
|
|
|
MockKAnnotations.init(this)
|
|
|
|
|
2020-08-04 13:18:43 +00:00
|
|
|
logger = MockLogger()
|
|
|
|
client = MockApiClient()
|
|
|
|
prefs = MockNetworkPrefs()
|
2020-09-25 09:28:47 +00:00
|
|
|
|
|
|
|
session = MockSession.getDefault()
|
2020-10-28 18:32:21 +00:00
|
|
|
coEvery { sessionProvider.getSessionId(any()) } returns session.sessionId
|
|
|
|
coEvery { sessionProvider.getSession(any()) } returns session
|
2020-09-25 09:28:47 +00:00
|
|
|
|
2020-08-04 13:18:43 +00:00
|
|
|
apiFactory =
|
2020-09-25 09:28:47 +00:00
|
|
|
ApiFactory(
|
|
|
|
"https://example.com/",
|
|
|
|
client,
|
|
|
|
logger,
|
|
|
|
networkManager,
|
|
|
|
prefs,
|
|
|
|
sessionProvider,
|
|
|
|
sessionListener,
|
|
|
|
scope
|
|
|
|
)
|
2020-08-04 13:18:43 +00:00
|
|
|
every { networkManager.isConnectedToNetwork() } returns isNetworkAvailable
|
|
|
|
|
|
|
|
isNetworkAvailable = true
|
|
|
|
webServer = testTlsHelper.createMockServer()
|
|
|
|
|
|
|
|
backend = createBackend {
|
|
|
|
testTlsHelper.initPinning(it, TestTLSHelper.TEST_PINS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createBackend(pinningInit: (OkHttpClient.Builder) -> Unit) =
|
|
|
|
ProtonApiBackend(
|
|
|
|
webServer.url("/").toString(),
|
|
|
|
client,
|
|
|
|
logger,
|
2020-09-25 09:28:47 +00:00
|
|
|
session.sessionId,
|
|
|
|
sessionProvider,
|
2020-08-04 13:18:43 +00:00
|
|
|
apiFactory.baseOkHttpClient,
|
|
|
|
listOf(
|
|
|
|
ScalarsConverterFactory.create(),
|
|
|
|
apiFactory.jsonConverter
|
|
|
|
),
|
|
|
|
TestRetrofitApi::class,
|
|
|
|
networkManager,
|
|
|
|
pinningInit
|
|
|
|
)
|
|
|
|
|
|
|
|
@After
|
|
|
|
fun after() {
|
|
|
|
webServer.shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test human verification returned`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
422,
|
|
|
|
humanVerificationResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Error.Http)
|
|
|
|
val data = result.proton
|
|
|
|
assertNotNull(data)
|
|
|
|
val humanVerification = data.humanVerification
|
|
|
|
assertNotNull(humanVerification)
|
|
|
|
assertNotNull(humanVerification.captchaVerificationToken)
|
|
|
|
assertEquals(3, humanVerification.verificationMethods.size)
|
|
|
|
assertTrue("captcha".equalsNoCase(humanVerification.verificationMethods[0].name))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test other details returned`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
422,
|
|
|
|
otherDetailsResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Error.Http)
|
|
|
|
val data = result.proton
|
|
|
|
assertNotNull(data)
|
|
|
|
assertNull(data.humanVerification)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test human verification headers`() = runBlocking {
|
|
|
|
|
|
|
|
webServer.prepareResponse(
|
|
|
|
422,
|
|
|
|
humanVerificationResponse
|
|
|
|
)
|
2020-09-25 09:28:47 +00:00
|
|
|
val humanVerificationHeaders = spyk(
|
|
|
|
HumanVerificationHeaders(
|
|
|
|
"captcha",
|
|
|
|
"captcha token"
|
|
|
|
)
|
|
|
|
)
|
2020-08-04 13:18:43 +00:00
|
|
|
|
2020-10-28 18:32:21 +00:00
|
|
|
coEvery { sessionProvider.getSession(any()) } returns MockSession.getWithHeader(
|
2020-09-25 09:28:47 +00:00
|
|
|
humanVerificationHeaders
|
|
|
|
)
|
2020-08-04 13:18:43 +00:00
|
|
|
|
|
|
|
backend(ApiManager.Call(0) { test() })
|
|
|
|
val headers = webServer.takeRequest().headers
|
|
|
|
verify(exactly = 1) {
|
|
|
|
humanVerificationHeaders.tokenCode
|
|
|
|
}
|
|
|
|
verify(exactly = 1) {
|
|
|
|
humanVerificationHeaders.tokenType
|
|
|
|
}
|
|
|
|
assertTrue(headers.contains(Pair("x-pm-human-verification-token-type", "captcha")))
|
|
|
|
assertTrue(headers.contains(Pair("x-pm-human-verification-token", "captcha token")))
|
|
|
|
}
|
|
|
|
}
|