github.com/jordwest/imap-server@v0.0.0-20200627020849-1cf758ba359f/mailstore/dummy_mailstore_test.go (about)

     1  package mailstore
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/jordwest/imap-server/types"
     7  )
     8  
     9  func getDefaultInbox(t *testing.T) *DummyMailbox {
    10  	m := NewDummyMailstore()
    11  	user, err := m.Authenticate("username", "password")
    12  	if err != nil {
    13  		t.Fatalf("Error getting user: %s\n", err)
    14  	}
    15  	mailbox, err := user.MailboxByName("INBOX")
    16  	if err != nil {
    17  		t.Fatalf("Error getting default mailbox: %s\n", err)
    18  	}
    19  	return mailbox.(*DummyMailbox)
    20  }
    21  
    22  func assertMessageUIDs(t *testing.T, msgs []Message, uids []uint32) {
    23  	if len(msgs) != len(uids) {
    24  		t.Errorf("Expecting %d messages, got %d messages\n", len(uids), len(msgs))
    25  		debugPrintMessages(msgs)
    26  		return
    27  	}
    28  
    29  	errorOccurred := false
    30  	for index, expected := range uids {
    31  		actual := msgs[index].UID()
    32  		if actual != expected {
    33  			t.Errorf("Expected msgs[%d].UID() == %d, got %d\n", index, expected, actual)
    34  			errorOccurred = true
    35  		}
    36  	}
    37  
    38  	if errorOccurred {
    39  		debugPrintMessages(msgs)
    40  	}
    41  }
    42  
    43  func TestMessageSetBySequenceNumber(t *testing.T) {
    44  	inbox := getDefaultInbox(t)
    45  	msgs := inbox.MessageSetBySequenceNumber(types.SequenceSet{
    46  		types.SequenceRange{Min: "1", Max: ""},
    47  		types.SequenceRange{Min: "4", Max: "*"},
    48  	})
    49  	assertMessageUIDs(t, msgs, []uint32{10})
    50  
    51  	msgs = inbox.MessageSetBySequenceNumber(types.SequenceSet{
    52  		types.SequenceRange{Min: "2", Max: "3"},
    53  	})
    54  	assertMessageUIDs(t, msgs, []uint32{11, 12})
    55  }
    56  
    57  func TestMessageSetByUID(t *testing.T) {
    58  	inbox := getDefaultInbox(t)
    59  	msgs := inbox.MessageSetByUID(types.SequenceSet{
    60  		types.SequenceRange{Min: "10", Max: "*"},
    61  	})
    62  	assertMessageUIDs(t, msgs, []uint32{10, 11, 12})
    63  
    64  	msgs = inbox.MessageSetByUID(types.SequenceSet{
    65  		types.SequenceRange{Min: "3", Max: "9"},
    66  	})
    67  	assertMessageUIDs(t, msgs, []uint32{})
    68  
    69  	msgs = inbox.MessageSetByUID(types.SequenceSet{
    70  		types.SequenceRange{Min: "11", Max: "12"},
    71  	})
    72  	assertMessageUIDs(t, msgs, []uint32{11, 12})
    73  
    74  	msgs = inbox.MessageSetByUID(types.SequenceSet{
    75  		types.SequenceRange{Min: "*", Max: ""},
    76  	})
    77  	assertMessageUIDs(t, msgs, []uint32{12})
    78  }