package web import ( "bytes" "github.com/jhillyerd/go.enmime" "github.com/jhillyerd/inbucket/config" "github.com/jhillyerd/inbucket/smtpd" "github.com/stretchr/testify/mock" "io" "log" "net/http" "net/http/httptest" "net/mail" "os" "testing" "time" ) func TestRestMailboxList(t *testing.T) { // Create Mock Objects ds := &MockDataStore{} emptybox := &MockMailbox{} ds.On("MailboxFor", "empty").Return(emptybox, nil) emptybox.On("GetMessages").Return([]smtpd.Message{}, nil) logbuf := setupWebServer(ds) // Test invalid mailbox name w, err := testRestGet("http://localhost/mailbox/foo@bar") expectCode := 500 if err != nil { t.Fatal(err) } if w.Code != expectCode { t.Errorf("Expected code %v, got %v", expectCode, w.Code) } // Test empty mailbox w, err = testRestGet("http://localhost/mailbox/empty") expectCode = 200 if err != nil { t.Fatal(err) } if w.Code != expectCode { t.Errorf("Expected code %v, got %v", expectCode, w.Code) } if t.Failed() { // Wait for handler to finish logging time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } } func testRestGet(url string) (*httptest.ResponseRecorder, error) { req, err := http.NewRequest("GET", url, nil) req.Header.Add("Accept", "application/json") if err != nil { return nil, err } w := httptest.NewRecorder() Router.ServeHTTP(w, req) return w, nil } func setupWebServer(ds smtpd.DataStore) *bytes.Buffer { // Capture log output buf := new(bytes.Buffer) log.SetOutput(buf) cfg := config.WebConfig{ TemplateDir: "../themes/integral/templates", PublicDir: "../themes/integral/public", } Initialize(cfg, ds) return buf } // Mock DataStore object type MockDataStore struct { mock.Mock } func (m *MockDataStore) MailboxFor(name string) (smtpd.Mailbox, error) { args := m.Called(name) return args.Get(0).(smtpd.Mailbox), args.Error(1) } func (m *MockDataStore) AllMailboxes() ([]smtpd.Mailbox, error) { args := m.Called() return args.Get(0).([]smtpd.Mailbox), args.Error(1) } // Mock Mailbox object type MockMailbox struct { mock.Mock } func (m *MockMailbox) GetMessages() ([]smtpd.Message, error) { args := m.Called() return args.Get(0).([]smtpd.Message), args.Error(1) } func (m *MockMailbox) GetMessage(id string) (smtpd.Message, error) { args := m.Called(id) return args.Get(0).(smtpd.Message), args.Error(1) } func (m *MockMailbox) Purge() error { args := m.Called() return args.Error(0) } func (m *MockMailbox) NewMessage() (smtpd.Message, error) { args := m.Called() return args.Get(0).(smtpd.Message), args.Error(1) } func (m *MockMailbox) String() string { args := m.Called() return args.String(0) } // Mock Message object type MockMessage struct { mock.Mock } func (m *MockMessage) Id() string { args := m.Called() return args.String(0) } func (m *MockMessage) From() string { args := m.Called() return args.String(0) } func (m *MockMessage) Date() time.Time { args := m.Called() return args.Get(0).(time.Time) } func (m *MockMessage) Subject() string { args := m.Called() return args.String(0) } func (m *MockMessage) ReadHeader() (msg *mail.Message, err error) { args := m.Called() return args.Get(0).(*mail.Message), args.Error(1) } func (m *MockMessage) ReadBody() (body *enmime.MIMEBody, err error) { args := m.Called() return args.Get(0).(*enmime.MIMEBody), args.Error(1) } func (m *MockMessage) ReadRaw() (raw *string, err error) { args := m.Called() return args.Get(0).(*string), args.Error(1) } func (m *MockMessage) RawReader() (reader io.ReadCloser, err error) { args := m.Called() return args.Get(0).(io.ReadCloser), args.Error(1) } func (m *MockMessage) Size() int64 { args := m.Called() return int64(args.Int(0)) } func (m *MockMessage) Append(data []byte) error { // []byte arg seems to mess up testify/mock return nil } func (m *MockMessage) Close() error { args := m.Called() return args.Error(0) } func (m *MockMessage) Delete() error { args := m.Called() return args.Error(0) } func (m *MockMessage) String() string { args := m.Called() return args.String(0) }