github.com/jordwest/imap-server@v0.0.0-20200627020849-1cf758ba359f/conn/command_copy_test.go (about) 1 package conn_test 2 3 import ( 4 "github.com/jordwest/imap-server/conn" 5 "github.com/jordwest/imap-server/mailstore" 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 ) 9 10 var _ = Describe("COPY Command", func() { 11 Context("When a mailbox is selected", func() { 12 BeforeEach(func() { 13 tConn.SetState(conn.StateSelected) 14 tConn.SetReadWrite() 15 tConn.User = mStore.User 16 tConn.SelectedMailbox = tConn.User.Mailboxes()[0] 17 }) 18 19 checkMsgEqual := func(actualMsg, expectMsg mailstore.Message) { 20 Expect(expectMsg.Body()).To(Equal(actualMsg.Body())) 21 Expect(expectMsg.Header()).To(Equal(actualMsg.Header())) 22 Expect(expectMsg.Flags()).To(Equal(actualMsg.Flags())) 23 } 24 25 It("should copy a message by sequence number", func() { 26 SendLine(`abcd.123 COPY 1:2 "Trash"`) 27 ExpectResponse("abcd.123 OK COPY Completed") 28 29 // Verify that everything matches. 30 trash, _ := tConn.User.MailboxByName("Trash") 31 checkMsgEqual(trash.MessageBySequenceNumber(1), 32 tConn.SelectedMailbox.MessageBySequenceNumber(1)) 33 checkMsgEqual(trash.MessageBySequenceNumber(2), 34 tConn.SelectedMailbox.MessageBySequenceNumber(2)) 35 }) 36 37 It("should copy a message by UID", func() { 38 SendLine(`abcd.123 uid COPY 11:12 "Trash"`) 39 ExpectResponse("abcd.123 OK UID COPY Completed") 40 41 // Verify that everything matches. 42 trash, _ := tConn.User.MailboxByName("Trash") 43 checkMsgEqual(trash.MessageByUID(10), 44 tConn.SelectedMailbox.MessageByUID(11)) 45 checkMsgEqual(trash.MessageByUID(11), 46 tConn.SelectedMailbox.MessageByUID(12)) 47 }) 48 49 It("shouldn't copy nonexistant message by sequence number", func() { 50 SendLine("abcd.125 uid COPY 50:* Trash") 51 ExpectResponse("abcd.125 NO no messages found") 52 }) 53 54 It("shouldn't copy nonexistant message by UID", func() { 55 SendLine("abcd.125 COPY 5:* Trash") 56 ExpectResponse("abcd.125 NO no messages found") 57 }) 58 }) 59 60 Context("When logged in but no mailbox is selected", func() { 61 BeforeEach(func() { 62 tConn.SetState(conn.StateAuthenticated) 63 tConn.User = mStore.User 64 }) 65 66 It("should return an error", func() { 67 SendLine("abcd.123 COPY 1 INBOX") 68 ExpectResponse("abcd.123 BAD not selected") 69 }) 70 }) 71 72 Context("When not logged in", func() { 73 BeforeEach(func() { 74 tConn.SetState(conn.StateNotAuthenticated) 75 }) 76 77 It("should return an error", func() { 78 SendLine("abcd.123 COPY 1 INBOX") 79 ExpectResponse("abcd.123 BAD not authenticated") 80 }) 81 }) 82 })