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

     1  package conn_test
     2  
     3  import (
     4  	"github.com/jordwest/imap-server/conn"
     5  	"github.com/jordwest/imap-server/types"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("EXPUNGE 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  		It("should delete the second message", func() {
    20  			// Mark the last message as deleted.
    21  			_, err := tConn.SelectedMailbox.MessageBySequenceNumber(2).
    22  				AddFlags(types.FlagDeleted).Save()
    23  			Expect(err).ToNot(HaveOccurred())
    24  
    25  			// Expunge the e-mail.
    26  			SendLine("abc.123 EXPUNGE")
    27  			ExpectResponse("* 2 EXPUNGE")
    28  			ExpectResponse("abc.123 OK EXPUNGE completed")
    29  
    30  			// Make sure that it was removed from the mailbox.
    31  			Expect(tConn.SelectedMailbox.Messages()).To(Equal(uint32(2)))
    32  
    33  			// Make sure that sequence numbers were properly arranged.
    34  			for i := uint32(1); i <= 2; i++ {
    35  				msg := tConn.SelectedMailbox.MessageBySequenceNumber(i)
    36  				Expect(msg.SequenceNumber()).To(Equal(i))
    37  			}
    38  		})
    39  
    40  		It("should delete the first and third messages", func() {
    41  			// Mark the first message as deleted.
    42  			_, err := tConn.SelectedMailbox.MessageBySequenceNumber(1).
    43  				AddFlags(types.FlagDeleted).Save()
    44  			Expect(err).ToNot(HaveOccurred())
    45  
    46  			// Mark the second message as deleted.
    47  			_, err = tConn.SelectedMailbox.MessageBySequenceNumber(3).
    48  				AddFlags(types.FlagDeleted).Save()
    49  			Expect(err).ToNot(HaveOccurred())
    50  
    51  			// Expunge the e-mail.
    52  			SendLine("abc.234 EXPUNGE")
    53  			ExpectResponse("* 1 EXPUNGE")
    54  			ExpectResponse("* 3 EXPUNGE")
    55  			ExpectResponse("abc.234 OK EXPUNGE completed")
    56  
    57  			// Make sure that the two messages were deleted from the mailbox.
    58  			Expect(tConn.SelectedMailbox.Messages()).To(Equal(uint32(1)))
    59  
    60  			// Make sure that sequence numbers were properly arranged.
    61  			for i := uint32(1); i <= 1; i++ {
    62  				msg := tConn.SelectedMailbox.MessageBySequenceNumber(i)
    63  				Expect(msg.SequenceNumber()).To(Equal(i))
    64  			}
    65  		})
    66  
    67  	})
    68  
    69  	Context("When logged in but no mailbox is selected", func() {
    70  		BeforeEach(func() {
    71  			tConn.SetState(conn.StateAuthenticated)
    72  			tConn.User = mStore.User
    73  		})
    74  
    75  		It("should return an error", func() {
    76  			SendLine("abcd.123 EXPUNGE")
    77  			ExpectResponse("abcd.123 BAD not selected")
    78  		})
    79  	})
    80  
    81  	Context("When not logged in", func() {
    82  		BeforeEach(func() {
    83  			tConn.SetState(conn.StateNotAuthenticated)
    84  		})
    85  
    86  		It("should return an error", func() {
    87  			SendLine("abcd.123 EXPUNGE")
    88  			ExpectResponse("abcd.123 BAD not authenticated")
    89  		})
    90  	})
    91  })