fix(gift): 선물 API 개인정보 로깅을 차단한다
This commit is contained in:
@@ -93,6 +93,9 @@ import kr.co.vividnext.sodalive.home.HomeViewModel
|
||||
import kr.co.vividnext.sodalive.home.pushnotification.PushNotificationListViewModel
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftSendRequestViewModel
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftInboxViewModel
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftDetailViewModel
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftTrackingViewModel
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftCancelViewModel
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftApi
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
import kr.co.vividnext.sodalive.live.LiveApi
|
||||
@@ -272,6 +275,13 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
.build()
|
||||
}
|
||||
|
||||
single<OkHttpClient>(named("giftHttpClient")) {
|
||||
get<OkHttpClient>().newBuilder()
|
||||
.apply { interceptors().removeAll { it is HttpLoggingInterceptor } }
|
||||
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.NONE))
|
||||
.build()
|
||||
}
|
||||
|
||||
single<OkHttpClient>(named("agoraHttpClient")) {
|
||||
val logging = HttpLoggingInterceptor()
|
||||
|
||||
@@ -300,6 +310,12 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
.build()
|
||||
}
|
||||
|
||||
single<Retrofit>(named("giftRetrofit")) {
|
||||
get<Retrofit>().newBuilder()
|
||||
.client(get<OkHttpClient>(named("giftHttpClient")))
|
||||
.build()
|
||||
}
|
||||
|
||||
single<Retrofit>(named("agoraRetrofit")) {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(agoraBaseUrl)
|
||||
@@ -347,7 +363,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
single { ApiBuilder().build(get(), HomeCreatorRankingApi::class.java) }
|
||||
single { ApiBuilder().build(get(), HomeFollowingApi::class.java) }
|
||||
single { ApiBuilder().build(get(), HomeRecommendationApi::class.java) }
|
||||
single { ApiBuilder().build(get(), GiftApi::class.java) }
|
||||
single { ApiBuilder().build(get<Retrofit>(named("giftRetrofit")), GiftApi::class.java) }
|
||||
single { ApiBuilder().build(get(), CreatorChannelApi::class.java) }
|
||||
single { ApiBuilder().build(get(), HomeOnAirLiveApi::class.java) }
|
||||
single { ApiBuilder().build(get(), CharacterApi::class.java) }
|
||||
@@ -465,6 +481,9 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { HomeRecommendationViewModel(get()) }
|
||||
viewModel { GiftSendRequestViewModel(get()) }
|
||||
viewModel { GiftInboxViewModel(get()) }
|
||||
viewModel { GiftDetailViewModel(get()) }
|
||||
viewModel { GiftTrackingViewModel(get()) }
|
||||
viewModel { GiftCancelViewModel(get()) }
|
||||
viewModel { HomeOnAirLiveViewModel(get()) }
|
||||
viewModel { CreatorChannelHomeViewModel(get()) }
|
||||
viewModel { CreatorChannelLiveViewModel(get()) }
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift.data
|
||||
|
||||
import android.app.Application
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import java.lang.reflect.Proxy
|
||||
import kr.co.vividnext.sodalive.di.AppDI
|
||||
import kr.co.vividnext.sodalive.network.AcceptLanguageInterceptor
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Protocol
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotSame
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.get
|
||||
import org.koin.core.context.stopKoin
|
||||
import org.koin.core.qualifier.Qualifier
|
||||
import org.koin.core.qualifier.named
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.ShadowLog
|
||||
import retrofit2.Retrofit
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class GiftNetworkPrivacyTest : KoinComponent {
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
stopKoin()
|
||||
ShadowLog.clear()
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
stopKoin()
|
||||
ShadowLog.clear()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `디버그 선물 API는 전용 네트워크와 로그 차단을 사용하고 가상 개인정보를 남기지 않는다`() {
|
||||
startAppDi(isDebugMode = true)
|
||||
val sharedClient = resolve<OkHttpClient>()
|
||||
val sharedRetrofit = resolve<Retrofit>()
|
||||
val giftApi = resolve<GiftApi>()
|
||||
|
||||
assertNotSame(
|
||||
"GiftApi must not use the shared debug BODY Retrofit",
|
||||
sharedRetrofit,
|
||||
retrofitOf(giftApi)
|
||||
)
|
||||
|
||||
val giftClient = resolve<OkHttpClient>(named(GIFT_HTTP_CLIENT))
|
||||
val giftRetrofit = resolve<Retrofit>(named(GIFT_RETROFIT))
|
||||
val sharedLogger = sharedClient.loggingInterceptors().single()
|
||||
val giftLogger = giftClient.loggingInterceptors().single()
|
||||
|
||||
assertEquals(HttpLoggingInterceptor.Level.BODY, sharedLogger.level)
|
||||
assertEquals(HttpLoggingInterceptor.Level.NONE, giftLogger.level)
|
||||
assertNotSame(sharedLogger, giftLogger)
|
||||
assertSame(giftRetrofit, retrofitOf(giftApi))
|
||||
assertSame(sharedClient.authenticator, giftClient.authenticator)
|
||||
assertSame(sharedClient.acceptLanguageInterceptor(), giftClient.acceptLanguageInterceptor())
|
||||
assertEquals(sharedClient.connectTimeoutMillis, giftClient.connectTimeoutMillis)
|
||||
assertEquals(sharedClient.readTimeoutMillis, giftClient.readTimeoutMillis)
|
||||
assertEquals(sharedClient.writeTimeoutMillis, giftClient.writeTimeoutMillis)
|
||||
|
||||
val terminal = TerminalInterceptor()
|
||||
ShadowLog.clear()
|
||||
giftClient.newBuilder()
|
||||
.addInterceptor(terminal)
|
||||
.build()
|
||||
.newCall(virtualPiiRequest())
|
||||
.execute()
|
||||
.use { response -> assertEquals(200, response.code) }
|
||||
|
||||
assertEquals(1, terminal.callCount)
|
||||
val logs = ShadowLog.getLogs().mapNotNull { it.msg }
|
||||
assertTrue(logs.none { it.contains(VIRTUAL_PHONE) })
|
||||
assertTrue(logs.none { it.contains(VIRTUAL_ADDRESS) })
|
||||
assertTrue(logs.none { it.contains(VIRTUAL_TOKEN) })
|
||||
|
||||
stopKoin()
|
||||
startAppDi(isDebugMode = false)
|
||||
|
||||
assertEquals(
|
||||
HttpLoggingInterceptor.Level.NONE,
|
||||
resolve<OkHttpClient>().loggingInterceptors().single().level
|
||||
)
|
||||
assertEquals(
|
||||
HttpLoggingInterceptor.Level.NONE,
|
||||
resolve<OkHttpClient>(named(GIFT_HTTP_CLIENT)).loggingInterceptors().single().level
|
||||
)
|
||||
}
|
||||
|
||||
private fun startAppDi(isDebugMode: Boolean) {
|
||||
AppDI(ApplicationProvider.getApplicationContext(), isDebugMode)
|
||||
}
|
||||
|
||||
private inline fun <reified T : Any> resolve(qualifier: Qualifier? = null): T =
|
||||
getKoin().get(qualifier = qualifier)
|
||||
|
||||
private fun retrofitOf(api: GiftApi): Retrofit {
|
||||
val handler = Proxy.getInvocationHandler(api)
|
||||
val field = handler.javaClass.declaredFields.first { field ->
|
||||
Retrofit::class.java.isAssignableFrom(field.type)
|
||||
}
|
||||
field.isAccessible = true
|
||||
return field.get(handler) as Retrofit
|
||||
}
|
||||
|
||||
private fun OkHttpClient.loggingInterceptors(): List<HttpLoggingInterceptor> =
|
||||
interceptors.filterIsInstance<HttpLoggingInterceptor>()
|
||||
|
||||
private fun OkHttpClient.acceptLanguageInterceptor(): AcceptLanguageInterceptor =
|
||||
interceptors.filterIsInstance<AcceptLanguageInterceptor>().single()
|
||||
|
||||
private fun virtualPiiRequest(): Request = Request.Builder()
|
||||
.url("https://example.test/api/v2/gifts")
|
||||
.post(
|
||||
"""
|
||||
{"phone":"$VIRTUAL_PHONE","address":"$VIRTUAL_ADDRESS","token":"$VIRTUAL_TOKEN"}
|
||||
""".trimIndent().toRequestBody()
|
||||
)
|
||||
.build()
|
||||
|
||||
private class TerminalInterceptor : Interceptor {
|
||||
var callCount = 0
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
callCount += 1
|
||||
return Response.Builder()
|
||||
.request(chain.request())
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("OK")
|
||||
.body(virtualPiiResponse().toResponseBody(JSON_MEDIA_TYPE))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val GIFT_HTTP_CLIENT = "giftHttpClient"
|
||||
const val GIFT_RETROFIT = "giftRetrofit"
|
||||
const val VIRTUAL_PHONE = "010-5555-1234"
|
||||
const val VIRTUAL_ADDRESS = "가상시 가상로 1"
|
||||
const val VIRTUAL_TOKEN = "virtual-token"
|
||||
|
||||
val JSON_MEDIA_TYPE = "application/json".toMediaType()
|
||||
|
||||
fun virtualPiiResponse(): String =
|
||||
"""
|
||||
{"phone":"$VIRTUAL_PHONE","address":"$VIRTUAL_ADDRESS","token":"$VIRTUAL_TOKEN"}
|
||||
""".trimIndent()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user