mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
Add REST call for purging an entire mailbox
This commit is contained in:
1
etc/mailbox-purge.sh
Executable file
1
etc/mailbox-purge.sh
Executable file
@@ -0,0 +1 @@
|
|||||||
|
curl -i -H "Accept: application/json" --noproxy localhost -X DELETE http://localhost:9000/mailbox/$1
|
||||||
@@ -15,6 +15,7 @@ type DataStore interface {
|
|||||||
type Mailbox interface {
|
type Mailbox interface {
|
||||||
GetMessages() ([]Message, error)
|
GetMessages() ([]Message, error)
|
||||||
GetMessage(id string) (Message, error)
|
GetMessage(id string) (Message, error)
|
||||||
|
Purge() error
|
||||||
NewMessage() Message
|
NewMessage() Message
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -178,6 +178,12 @@ func (mb *FileMailbox) GetMessage(id string) (Message, error) {
|
|||||||
return nil, fmt.Errorf("Message %s not in index", id)
|
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
|
// readIndex loads the mailbox index data from disk
|
||||||
func (mb *FileMailbox) readIndex() error {
|
func (mb *FileMailbox) readIndex() error {
|
||||||
// Clear message slice, open index
|
// Clear message slice, open index
|
||||||
|
|||||||
@@ -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
|
// Test message size calculation
|
||||||
func TestFSSize(t *testing.T) {
|
func TestFSSize(t *testing.T) {
|
||||||
ds := setupDataStore()
|
ds := setupDataStore()
|
||||||
|
|||||||
@@ -92,6 +92,11 @@ func (m *MockMailbox) GetMessage(id string) (Message, error) {
|
|||||||
return args.Get(0).(Message), args.Error(1)
|
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 {
|
func (m *MockMailbox) NewMessage() Message {
|
||||||
args := m.Called()
|
args := m.Called()
|
||||||
return args.Get(0).(Message)
|
return args.Get(0).(Message)
|
||||||
|
|||||||
@@ -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) {
|
func MailboxHtml(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
|
||||||
// Don't have to validate these aren't empty, Gorilla returns 404
|
// Don't have to validate these aren't empty, Gorilla returns 404
|
||||||
name := ctx.Vars["name"]
|
name := ctx.Vars["name"]
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ func setupRoutes(cfg config.WebConfig) {
|
|||||||
r.Path("/status").Handler(handler(RootStatus)).Name("RootStatus").Methods("GET")
|
r.Path("/status").Handler(handler(RootStatus)).Name("RootStatus").Methods("GET")
|
||||||
r.Path("/mailbox").Handler(handler(MailboxIndex)).Name("MailboxIndex").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(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}").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}/html").Handler(handler(MailboxHtml)).Name("MailboxHtml").Methods("GET")
|
||||||
r.Path("/mailbox/{name}/{id}/source").Handler(handler(MailboxSource)).Name("MailboxSource").Methods("GET")
|
r.Path("/mailbox/{name}/{id}/source").Handler(handler(MailboxSource)).Name("MailboxSource").Methods("GET")
|
||||||
|
|||||||
Reference in New Issue
Block a user