Merge pull request #7115 from thundernest/fix_IdentityHeaderBuilder

Write empty string to identity header field if name is null
This commit is contained in:
cketti 2023-08-08 20:15:33 +02:00 committed by GitHub
commit f35603e14c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View file

@ -1,6 +1,8 @@
package com.fsck.k9.message; package com.fsck.k9.message;
import java.util.Objects;
import android.net.Uri; import android.net.Uri;
import android.net.Uri.Builder; import android.net.Uri.Builder;
@ -87,7 +89,7 @@ public class IdentityHeaderBuilder {
} }
if (identityChanged) { if (identityChanged) {
appendValue(IdentityField.NAME, identity.getName()); appendValue(IdentityField.NAME, Objects.requireNonNullElse(identity.getName(), ""));
appendValue(IdentityField.EMAIL, identity.getEmail()); appendValue(IdentityField.EMAIL, identity.getEmail());
} }

View file

@ -1,7 +1,10 @@
package com.fsck.k9.message package com.fsck.k9.message
import android.net.Uri
import app.k9mail.core.android.testing.RobolectricTest import app.k9mail.core.android.testing.RobolectricTest
import assertk.Assert
import assertk.assertThat import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isGreaterThan import assertk.assertions.isGreaterThan
import com.fsck.k9.Account.QuoteStyle import com.fsck.k9.Account.QuoteStyle
import com.fsck.k9.Identity import com.fsck.k9.Identity
@ -35,6 +38,21 @@ class IdentityHeaderBuilderTest : RobolectricTest() {
assertIsValidHeader(identityHeader) assertIsValidHeader(identityHeader)
} }
@Test
fun `identity header without identity name`() {
val identityHeader = IdentityHeaderBuilder()
.setIdentity(createIdentity(email = "test@domain.example", name = null))
.setIdentityChanged(true)
.setBody(TextBody("irrelevant"))
.setQuoteStyle(QuoteStyle.PREFIX)
.setMessageFormat(SimpleMessageFormat.TEXT)
.setQuoteTextMode(QuotedTextMode.NONE)
.build()
assertThat(identityHeader).containsParameter(IdentityField.EMAIL, "test@domain.example")
assertThat(identityHeader).containsParameter(IdentityField.NAME, "")
}
private fun assertIsValidHeader(identityHeader: String) { private fun assertIsValidHeader(identityHeader: String) {
try { try {
MimeHeaderChecker.checkHeader(IDENTITY_HEADER, identityHeader) MimeHeaderChecker.checkHeader(IDENTITY_HEADER, identityHeader)
@ -55,3 +73,11 @@ class IdentityHeaderBuilderTest : RobolectricTest() {
return Identity(description, name, email, signature, signatureUse, replyTo) return Identity(description, name, email, signature, signatureUse, replyTo)
} }
} }
private fun Assert<String>.containsParameter(identityField: IdentityField, value: String) = given { actual ->
assertThat("&${unfold(actual)}&").contains("&${identityField.value()}=${Uri.encode(value)}&")
}
private fun unfold(headerValue: String): String {
return headerValue.replace(Regex("\r?\n "), "")
}