1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

Add REST call for purging an entire mailbox

This commit is contained in:
James Hillyerd
2013-10-14 15:35:09 -07:00
parent 46d4f7be1d
commit 1e85699ccf
7 changed files with 77 additions and 0 deletions

1
etc/mailbox-purge.sh Executable file
View File

@@ -0,0 +1 @@
curl -i -H "Accept: application/json" --noproxy localhost -X DELETE http://localhost:9000/mailbox/$1

View File

@@ -15,6 +15,7 @@ type DataStore interface {
type Mailbox interface {
GetMessages() ([]Message, error)
GetMessage(id string) (Message, error)
Purge() error
NewMessage() Message
String() string
}

View File

@@ -178,6 +178,12 @@ func (mb *FileMailbox) GetMessage(id string) (Message, error) {
return nil, fmt.Errorf("Message %s not in index", id)
}
// Delete all messages in this mailbox
func (mb *FileMailbox) Purge() error {
mb.messages = mb.messages[:0]
return mb.writeIndex()
}
// readIndex loads the mailbox index data from disk
func (mb *FileMailbox) readIndex() error {
// Clear message slice, open index

View File

@@ -218,6 +218,47 @@ func TestFSDelete(t *testing.T) {
}
// Test purging a mailbox
func TestFSPurge(t *testing.T) {
ds := setupDataStore()
defer teardownDataStore(ds)
mbName := "fred"
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
for _, subj := range subjects {
// Add a message
deliverMessage(ds, mbName, subj, time.Now())
}
mb, err := ds.MailboxFor(mbName)
if err != nil {
panic(err)
}
msgs, err := mb.GetMessages()
if err != nil {
panic(err)
}
assert.Equal(t, len(subjects), len(msgs), "Expected %v message(s), but got %v",
len(subjects), len(msgs))
// Purge mailbox
err = mb.Purge()
assert.Nil(t, err)
// Confirm deletion
mb, err = ds.MailboxFor(mbName)
if err != nil {
panic(err)
}
msgs, err = mb.GetMessages()
if err != nil {
panic(err)
}
assert.Equal(t, len(msgs), 0, "Expected mailbox to have zero messages, got %v", len(msgs))
}
// Test message size calculation
func TestFSSize(t *testing.T) {
ds := setupDataStore()

View File

@@ -92,6 +92,11 @@ func (m *MockMailbox) GetMessage(id string) (Message, error) {
return args.Get(0).(Message), args.Error(1)
}
func (m *MockMailbox) Purge() error {
args := m.Called()
return args.Error(0)
}
func (m *MockMailbox) NewMessage() Message {
args := m.Called()
return args.Get(0).(Message)

View File

@@ -94,6 +94,28 @@ func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *Context) (err er
})
}
func MailboxPurge(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
// Don't have to validate these aren't empty, Gorilla returns 404
name := ctx.Vars["name"]
mb, err := ctx.DataStore.MailboxFor(name)
if err != nil {
return fmt.Errorf("MailboxFor('%v'): %v", name, err)
}
if err := mb.Purge(); err != nil {
return fmt.Errorf("Mailbox(%q) Purge: %v", name, err)
}
log.LogTrace("Purged mailbox for %q", name)
if ctx.IsJson {
return RenderJson(w, "OK")
}
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "OK")
return nil
}
func MailboxHtml(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
// Don't have to validate these aren't empty, Gorilla returns 404
name := ctx.Vars["name"]

View File

@@ -36,6 +36,7 @@ func setupRoutes(cfg config.WebConfig) {
r.Path("/status").Handler(handler(RootStatus)).Name("RootStatus").Methods("GET")
r.Path("/mailbox").Handler(handler(MailboxIndex)).Name("MailboxIndex").Methods("GET")
r.Path("/mailbox/{name}").Handler(handler(MailboxList)).Name("MailboxList").Methods("GET")
r.Path("/mailbox/{name}").Handler(handler(MailboxPurge)).Name("MailboxPurge").Methods("DELETE")
r.Path("/mailbox/{name}/{id}").Handler(handler(MailboxShow)).Name("MailboxShow").Methods("GET")
r.Path("/mailbox/{name}/{id}/html").Handler(handler(MailboxHtml)).Name("MailboxHtml").Methods("GET")
r.Path("/mailbox/{name}/{id}/source").Handler(handler(MailboxSource)).Name("MailboxSource").Methods("GET")