Merge pull request #3079 from philipwhiuk/reorderIdentityPriority
Test and re-order identity priority
This commit is contained in:
commit
b5cffe84e5
3 changed files with 177 additions and 162 deletions
|
@ -25,11 +25,34 @@ public class IdentityHelper {
|
|||
public static Identity getRecipientIdentityFromMessage(Account account, Message message) {
|
||||
Identity recipient = null;
|
||||
|
||||
for (Address address : message.getRecipients(Message.RecipientType.X_ORIGINAL_TO)) {
|
||||
Identity identity = account.findIdentity(address);
|
||||
if (identity != null) {
|
||||
recipient = identity;
|
||||
break;
|
||||
for (Address address : message.getRecipients(Message.RecipientType.TO)) {
|
||||
Identity identity = account.findIdentity(address);
|
||||
if (identity != null) {
|
||||
recipient = identity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (recipient == null) {
|
||||
Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC);
|
||||
if (ccAddresses.length > 0) {
|
||||
for (Address address : ccAddresses) {
|
||||
Identity identity = account.findIdentity(address);
|
||||
if (identity != null) {
|
||||
recipient = identity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (recipient == null) {
|
||||
for (Address address : message.getRecipients(Message.RecipientType.X_ORIGINAL_TO)) {
|
||||
Identity identity = account.findIdentity(address);
|
||||
if (identity != null) {
|
||||
recipient = identity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,29 +76,6 @@ public class IdentityHelper {
|
|||
}
|
||||
}
|
||||
|
||||
if (recipient == null) {
|
||||
for (Address address : message.getRecipients(Message.RecipientType.TO)) {
|
||||
Identity identity = account.findIdentity(address);
|
||||
if (identity != null) {
|
||||
recipient = identity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (recipient == null) {
|
||||
Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC);
|
||||
if (ccAddresses.length > 0) {
|
||||
for (Address address : ccAddresses) {
|
||||
Identity identity = account.findIdentity(address);
|
||||
if (identity != null) {
|
||||
recipient = identity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (recipient == null) {
|
||||
recipient = account.getIdentity(0);
|
||||
}
|
||||
|
|
|
@ -1,134 +0,0 @@
|
|||
package com.fsck.k9.helper;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.fsck.k9.Account;
|
||||
import com.fsck.k9.Identity;
|
||||
import com.fsck.k9.K9RobolectricTestRunner;
|
||||
import com.fsck.k9.mail.Message;
|
||||
import com.fsck.k9.mail.Address;
|
||||
import com.fsck.k9.mail.internet.MimeMessage;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(K9RobolectricTestRunner.class)
|
||||
public class IdentityHelperTest {
|
||||
|
||||
private Account account;
|
||||
private MimeMessage msg;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Context context = RuntimeEnvironment.application;
|
||||
createDummyAccount(context);
|
||||
msg = parseWithoutRecurse(toStream(
|
||||
"From: <adam@example.org>\r\n" +
|
||||
"To: <eva@example.org>\r\n" +
|
||||
"Subject: Testmail\r\n" +
|
||||
"MIME-Version: 1.0\r\n" +
|
||||
"Content-type: text/plain\r\n" +
|
||||
"Content-Transfer-Encoding: 7bit\r\n" +
|
||||
"\r\n" +
|
||||
"this is some test text."));
|
||||
}
|
||||
|
||||
|
||||
private static MimeMessage parseWithoutRecurse(InputStream data) throws Exception {
|
||||
return MimeMessage.parseMimeMessage(data, false);
|
||||
}
|
||||
|
||||
private static ByteArrayInputStream toStream(String rawMailData) throws Exception {
|
||||
return new ByteArrayInputStream(rawMailData.getBytes("ISO-8859-1"));
|
||||
}
|
||||
|
||||
private void createDummyAccount(Context context) {
|
||||
account = new DummyAccount(context);
|
||||
setIdentity();
|
||||
}
|
||||
|
||||
private void setIdentity() {
|
||||
Identity identity = new Identity();
|
||||
identity.setEmail("test@mail.com");
|
||||
identity.setName("test");
|
||||
Identity identity2 = new Identity();
|
||||
identity2.setEmail("test2@mail.com");
|
||||
identity2.setName("test2");
|
||||
Identity eva = new Identity();
|
||||
eva.setEmail("eva@example.org");
|
||||
eva.setName("Eva");
|
||||
|
||||
List<Identity> identityList = new ArrayList<>();
|
||||
identityList.add(identity);
|
||||
identityList.add(identity2);
|
||||
identityList.add(eva);
|
||||
account.setIdentities(identityList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXOriginalTo() throws Exception {
|
||||
Address[] addresses = {new Address("test2@mail.com")};
|
||||
msg.setRecipients(Message.RecipientType.X_ORIGINAL_TO, addresses);
|
||||
|
||||
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
|
||||
assertTrue(identity.getEmail().equalsIgnoreCase("test2@mail.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTo_withoutXOriginalTo() throws Exception {
|
||||
Identity eva = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
|
||||
assertTrue(eva.getEmail().equalsIgnoreCase("eva@example.org"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeliveredTo() throws Exception {
|
||||
Address[] addresses = {new Address("test2@mail.com")};
|
||||
msg.setRecipients(Message.RecipientType.DELIVERED_TO, addresses);
|
||||
msg.removeHeader("X-Original-To");
|
||||
|
||||
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
|
||||
assertTrue(identity.getEmail().equalsIgnoreCase("test2@mail.com"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXEnvelopeTo() throws Exception {
|
||||
Address[] addresses = {new Address("test@mail.com")};
|
||||
msg.setRecipients(Message.RecipientType.X_ENVELOPE_TO, addresses);
|
||||
msg.removeHeader("X-Original-To");
|
||||
msg.removeHeader("Delivered-To");
|
||||
|
||||
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
|
||||
assertTrue(identity.getEmail().equalsIgnoreCase("test@mail.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXEnvelopeTo_withXOriginalTo() throws Exception {
|
||||
Address[] addresses = {new Address("test@mail.com")};
|
||||
Address[] xoriginaltoaddresses = {new Address("test2@mail.com")};
|
||||
msg.setRecipients(Message.RecipientType.X_ENVELOPE_TO, addresses);
|
||||
msg.setRecipients(Message.RecipientType.X_ORIGINAL_TO, xoriginaltoaddresses);
|
||||
|
||||
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
|
||||
assertTrue(identity.getEmail().equalsIgnoreCase("test2@mail.com"));
|
||||
}
|
||||
|
||||
|
||||
static class DummyAccount extends Account {
|
||||
|
||||
protected DummyAccount(Context context) {
|
||||
super(context);
|
||||
}
|
||||
}
|
||||
}
|
149
k9mail/src/test/java/com/fsck/k9/helper/IdentityHelperTest.kt
Normal file
149
k9mail/src/test/java/com/fsck/k9/helper/IdentityHelperTest.kt
Normal file
|
@ -0,0 +1,149 @@
|
|||
package com.fsck.k9.helper
|
||||
|
||||
|
||||
import com.fsck.k9.Account
|
||||
import com.fsck.k9.Identity
|
||||
import com.fsck.k9.K9RobolectricTestRunner
|
||||
import com.fsck.k9.mail.Address
|
||||
import com.fsck.k9.mail.Message
|
||||
import com.fsck.k9.mail.Message.RecipientType
|
||||
import com.fsck.k9.mail.internet.MimeMessage
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
|
||||
@RunWith(K9RobolectricTestRunner::class)
|
||||
class IdentityHelperTest {
|
||||
private val account = createDummyAccount()
|
||||
|
||||
@Test
|
||||
fun getRecipientIdentityFromMessage_prefersToOverCc() {
|
||||
val message = messageWithRecipients(
|
||||
RecipientType.TO to IDENTITY_1_ADDRESS,
|
||||
RecipientType.CC to IDENTITY_2_ADDRESS,
|
||||
RecipientType.X_ORIGINAL_TO to IDENTITY_3_ADDRESS,
|
||||
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
|
||||
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
|
||||
|
||||
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
|
||||
|
||||
assertThat(identity.email).isEqualTo(IDENTITY_1_ADDRESS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getRecipientIdentityFromMessage_prefersCcOverXOriginalTo() {
|
||||
val message = messageWithRecipients(
|
||||
RecipientType.TO to "unrelated1@example.org",
|
||||
RecipientType.CC to IDENTITY_2_ADDRESS,
|
||||
RecipientType.X_ORIGINAL_TO to IDENTITY_3_ADDRESS,
|
||||
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
|
||||
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
|
||||
|
||||
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
|
||||
|
||||
assertThat(identity.email).isEqualTo(IDENTITY_2_ADDRESS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getRecipientIdentityFromMessage_prefersXOriginalToOverDeliveredTo() {
|
||||
val message = messageWithRecipients(
|
||||
RecipientType.TO to "unrelated1@example.org",
|
||||
RecipientType.CC to "unrelated2@example.org",
|
||||
RecipientType.X_ORIGINAL_TO to IDENTITY_3_ADDRESS,
|
||||
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
|
||||
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
|
||||
|
||||
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
|
||||
|
||||
assertThat(identity.email).isEqualTo(IDENTITY_3_ADDRESS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getRecipientIdentityFromMessage_prefersDeliveredToOverXEnvelopeTo() {
|
||||
val message = messageWithRecipients(
|
||||
RecipientType.TO to "unrelated1@example.org",
|
||||
RecipientType.CC to "unrelated2@example.org",
|
||||
RecipientType.X_ORIGINAL_TO to "unrelated3@example.org",
|
||||
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
|
||||
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
|
||||
|
||||
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
|
||||
|
||||
assertThat(identity.email).isEqualTo(IDENTITY_4_ADDRESS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getRecipientIdentityFromMessage_usesXEnvelopeToWhenPresent() {
|
||||
val message = messageWithRecipients(
|
||||
RecipientType.TO to "unrelated1@example.org",
|
||||
RecipientType.CC to "unrelated2@example.org",
|
||||
RecipientType.X_ORIGINAL_TO to "unrelated3@example.org",
|
||||
RecipientType.DELIVERED_TO to "unrelated4@example.org",
|
||||
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
|
||||
|
||||
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
|
||||
|
||||
assertThat(identity.email).isEqualTo(IDENTITY_5_ADDRESS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getRecipientIdentityFromMessage_withoutAnyIdentityAddresses_returnsFirstIdentity() {
|
||||
val message = messageWithRecipients(
|
||||
RecipientType.TO to "unrelated1@example.org",
|
||||
RecipientType.CC to "unrelated2@example.org",
|
||||
RecipientType.X_ORIGINAL_TO to "unrelated3@example.org",
|
||||
RecipientType.DELIVERED_TO to "unrelated4@example.org",
|
||||
RecipientType.X_ENVELOPE_TO to "unrelated5@example.org")
|
||||
|
||||
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
|
||||
|
||||
assertThat(identity.email).isEqualTo(DEFAULT_ADDRESS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getRecipientIdentityFromMessage_withNoApplicableHeaders_returnsFirstIdentity() {
|
||||
val emptyMessage = MimeMessage()
|
||||
|
||||
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, emptyMessage)
|
||||
|
||||
assertThat(identity.email).isEqualTo(DEFAULT_ADDRESS)
|
||||
}
|
||||
|
||||
|
||||
private fun createDummyAccount() = DummyAccount().apply {
|
||||
identities = listOf(
|
||||
newIdentity("Default", DEFAULT_ADDRESS),
|
||||
newIdentity("Identity 1", IDENTITY_1_ADDRESS),
|
||||
newIdentity("Identity 2", IDENTITY_2_ADDRESS),
|
||||
newIdentity("Identity 3", IDENTITY_3_ADDRESS),
|
||||
newIdentity("Identity 4", IDENTITY_4_ADDRESS),
|
||||
newIdentity("Identity 5", IDENTITY_5_ADDRESS)
|
||||
)
|
||||
}
|
||||
|
||||
private fun newIdentity(name: String, email: String) = Identity().apply {
|
||||
this.name = name
|
||||
this.email = email
|
||||
}
|
||||
|
||||
private fun messageWithRecipients(vararg recipients: Pair<RecipientType, String>): Message {
|
||||
return MimeMessage().apply {
|
||||
for ((recipientType, email) in recipients) {
|
||||
setRecipients(recipientType, arrayOf(Address(email)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val DEFAULT_ADDRESS = "default@example.org"
|
||||
const val IDENTITY_1_ADDRESS = "identity1@example.org"
|
||||
const val IDENTITY_2_ADDRESS = "identity2@example.org"
|
||||
const val IDENTITY_3_ADDRESS = "identity3@example.org"
|
||||
const val IDENTITY_4_ADDRESS = "identity4@example.org"
|
||||
const val IDENTITY_5_ADDRESS = "identity5@example.org"
|
||||
}
|
||||
|
||||
|
||||
class DummyAccount : Account(RuntimeEnvironment.application)
|
||||
}
|
Loading…
Reference in a new issue