Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ private val GLOBAL_SECURE_PATTERNS = listOf(
createKeyValueSecurePattern("postalCode"),
createKeyValueSecurePattern("stateOrProvinceCode"),
createKeyValueSecurePattern("phone"),
createKeyValueSecurePattern("name"),
createKeyValueSecurePattern("BillingPerson\\([^)]*name"),
createKeyValueSecurePattern("PersonInfo\\([^)]*name"),
createKeyValueSecurePattern("ShippingAddress\\([^)]*name"),
createKeyValueSecurePattern("contact"),
createKeyValueSecurePattern("note"),
createJsonSecurePattern("email"),
Expand Down
43 changes: 29 additions & 14 deletions src/test/kotlin/com/ecwid/apiclient/v3/MaskUtilsUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ecwid.apiclient.v3

import com.ecwid.apiclient.v3.util.SecurePattern

import com.ecwid.apiclient.v3.util.createSecurePatterns
import com.ecwid.apiclient.v3.util.maskLogString
import com.ecwid.apiclient.v3.util.maskSensitive
import org.junit.jupiter.api.Assertions.assertEquals
Expand All @@ -11,35 +12,49 @@ class MaskUtilsUnitTest {
@Test
fun testMaskLogKeyValueString() {
val logString =
"token=secret_RandomToken0jwOrgYc5sSKBYcvO0DbP; PasswordCredentials([email protected], password=123456)"
val securePatterns = listOf(
SecurePattern(Regex("token=(?:secret_|public_|)([^;,)]+)"), 6),
SecurePattern(Regex("email=([^;,)]+)"), 4),
SecurePattern(Regex("password=([^;,)]+)"), 2),
)
"token=secret_RandomToken0jwOrgYc5sSKBYcvO0DbP; PasswordCredentials([email protected], password=1234567890)"
val securePatterns = createSecurePatterns()

val maskedLogString = logString.maskLogString(securePatterns)
val expectedMaskedLogString = "token=secret_Ran***DbP; PasswordCredentials(email=te***om, password=1***6)"
val expectedMaskedLogString = "token=sec***DbP; PasswordCredentials(email=tes***com, password=12***890)"
assertEquals(expectedMaskedLogString, maskedLogString)
}

@Test
fun testMaskLogJsonString() {
val logString =
"""{"billingPerson":{"email":"[email protected]","firstName":"John","lastName":"Smith","phone":"123467890"}}"""
val securePatterns = listOf(
SecurePattern(Regex(""""email":\s*"([^"]*)""""), 6),
SecurePattern(Regex(""""firstName":\s*"([^"]*)""""), 6),
SecurePattern(Regex(""""lastName":\s*"([^"]*)""""), 6),
SecurePattern(Regex(""""phone":\s*"([^"]*)""""), 6),
)
val securePatterns = createSecurePatterns()

val maskedLogString = logString.maskLogString(securePatterns)
val expectedMaskedLogString =
"""{"billingPerson":{"email":"ale***com","firstName":"***","lastName":"***","phone":"***"}}"""
assertEquals(expectedMaskedLogString, maskedLogString)
}

@Test
fun testMaskLogKeyValueStringWithNameParameter() {
val logString =
"UpdatedProduct(name={unmasked}, " +
"attributes=[AttributeValue(name={unmasked})], " +
"options=[RadioOption(name={unmasked})], " +
"billingPerson=BillingPerson(name={unmasked}), " +
"shippingAddresses=[ShippingAddress(name={unmasked}), " +
"ShippingAddress(name={unmasked})], " +
"personInfo=PersonInfo(name={unmasked}))"
val securePatterns = createSecurePatterns()
val maskedLogString = logString.maskLogString(securePatterns)
val expectedMaskedLogString =
"UpdatedProduct(name={unmasked}, " +
"attributes=[AttributeValue(name={unmasked})], " +
"options=[RadioOption(name={unmasked})], " +
"billingPerson=BillingPerson(name={u***ed}), " +
"shippingAddresses=[ShippingAddress(name={u***ed}), " +
"ShippingAddress(name={u***ed})], " +
"personInfo=PersonInfo(name={u***ed}))"
assertEquals(expectedMaskedLogString, maskedLogString)
}

@Test
fun testMaskSensitive() {
assertEquals("te***ng", "test string".maskSensitive(4))
Expand Down