@@ -11,7 +11,10 @@ import org.dexpace.sdk.core.http.common.HttpHeaderName
1111import kotlin.test.Test
1212import kotlin.test.assertEquals
1313import kotlin.test.assertFailsWith
14+ import kotlin.test.assertFalse
15+ import kotlin.test.assertNotEquals
1416import kotlin.test.assertNull
17+ import kotlin.test.assertTrue
1518
1619class CredentialTest {
1720 // ----------------- KeyCredential -----------------
@@ -54,6 +57,31 @@ class CredentialTest {
5457 assertEquals(" k" , (cred as KeyCredential ).apiKey)
5558 }
5659
60+ @Test
61+ fun `KeyCredential toString redacts the apiKey but keeps non-secret fields` () {
62+ val cred = KeyCredential (" super-secret-key" , prefix = " SharedAccessKey" )
63+ val rendered = cred.toString()
64+ assertFalse(rendered.contains(" super-secret-key" ), " toString must not contain the raw apiKey" )
65+ assertTrue(rendered.contains(" apiKey=***" ), " toString must redact the apiKey" )
66+ assertTrue(rendered.contains(" SharedAccessKey" ), " toString should keep the non-secret prefix for diagnostics" )
67+ assertTrue(
68+ rendered.contains(HttpHeaderName .AUTHORIZATION .toString()),
69+ " toString should keep the non-secret headerName for diagnostics" ,
70+ )
71+ }
72+
73+ @Test
74+ fun `KeyCredential toString redacts the apiKey with default fields` () {
75+ val cred = KeyCredential (" another-secret" )
76+ val rendered = cred.toString()
77+ assertFalse(rendered.contains(" another-secret" ), " toString must not contain the raw apiKey" )
78+ assertTrue(rendered.contains(" apiKey=***" ), " toString must redact the apiKey" )
79+ assertTrue(
80+ rendered.contains(HttpHeaderName .AUTHORIZATION .toString()),
81+ " toString should keep the default headerName for diagnostics" ,
82+ )
83+ }
84+
5785 // ----------------- NamedKeyCredential -----------------
5886
5987 @Test
@@ -92,4 +120,30 @@ class CredentialTest {
92120 val cred: Credential = NamedKeyCredential (" acct" , " secret" )
93121 assertEquals(" acct" , (cred as NamedKeyCredential ).name)
94122 }
123+
124+ @Test
125+ fun `NamedKeyCredential toString redacts the key but keeps the name` () {
126+ val cred = NamedKeyCredential (" acct-name" , " super-secret-key" )
127+ val rendered = cred.toString()
128+ assertFalse(rendered.contains(" super-secret-key" ), " toString must not contain the raw key" )
129+ assertTrue(rendered.contains(" key=***" ), " toString must redact the key" )
130+ assertTrue(rendered.contains(" acct-name" ), " toString should keep the non-secret name for diagnostics" )
131+ }
132+
133+ @Test
134+ fun `NamedKeyCredential keeps identity equality - redaction is toString-only` () {
135+ // These are reference types (not data classes): equality is identity-based and the
136+ // redacting toString override must not change that. Same instance equals itself;
137+ // distinct instances with the same fields are not equal.
138+ val cred = NamedKeyCredential (" acct" , " secret" )
139+ assertEquals(cred, cred)
140+ assertNotEquals<NamedKeyCredential >(cred, NamedKeyCredential (" acct" , " secret" ))
141+ }
142+
143+ @Test
144+ fun `KeyCredential keeps identity equality - redaction is toString-only` () {
145+ val cred = KeyCredential (" secret-key" )
146+ assertEquals(cred, cred)
147+ assertNotEquals<KeyCredential >(cred, KeyCredential (" secret-key" ))
148+ }
95149}
0 commit comments