Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit b5cffe84 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #3079 from philipwhiuk/reorderIdentityPriority

Test and re-order identity priority
parents 38a8db27 11bd02a9
Loading
Loading
Loading
Loading
+20 −20
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ public class IdentityHelper {
    public static Identity getRecipientIdentityFromMessage(Account account, Message message) {
        Identity recipient = null;

        for (Address address : message.getRecipients(Message.RecipientType.X_ORIGINAL_TO)) {
            for (Address address : message.getRecipients(Message.RecipientType.TO)) {
                Identity identity = account.findIdentity(address);
                if (identity != null) {
                    recipient = identity;
@@ -34,7 +34,9 @@ public class IdentityHelper {
            }

        if (recipient == null) {
            for (Address address : message.getRecipients(Message.RecipientType.DELIVERED_TO)) {
            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;
@@ -42,9 +44,10 @@ public class IdentityHelper {
                    }
                }
            }
        }

        if (recipient == null) {
            for (Address address : message.getRecipients(Message.RecipientType.X_ENVELOPE_TO)) {
            for (Address address : message.getRecipients(Message.RecipientType.X_ORIGINAL_TO)) {
                Identity identity = account.findIdentity(address);
                if (identity != null) {
                    recipient = identity;
@@ -54,7 +57,7 @@ public class IdentityHelper {
        }

        if (recipient == null) {
            for (Address address : message.getRecipients(Message.RecipientType.TO)) {
            for (Address address : message.getRecipients(Message.RecipientType.DELIVERED_TO)) {
                Identity identity = account.findIdentity(address);
                if (identity != null) {
                    recipient = identity;
@@ -64,9 +67,7 @@ public class IdentityHelper {
        }

        if (recipient == null) {
            Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC);
            if (ccAddresses.length > 0) {
                for (Address address : ccAddresses) {
            for (Address address : message.getRecipients(Message.RecipientType.X_ENVELOPE_TO)) {
                Identity identity = account.findIdentity(address);
                if (identity != null) {
                    recipient = identity;
@@ -74,7 +75,6 @@ public class IdentityHelper {
                }
            }
        }
        }

        if (recipient == null) {
            recipient = account.getIdentity(0);
+0 −134
Original line number Diff line number Diff line
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 −0
Original line number Diff line number Diff line
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)
}