/
manager.go
77 lines (68 loc) · 2 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package test
import (
"errors"
"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/policy"
"github.com/jhillyerd/inbucket/pkg/storage"
)
// ManagerStub is a test stub for message.Manager
type ManagerStub struct {
message.Manager
mailboxes map[string][]*message.Message
}
// NewManager creates a new ManagerStub.
func NewManager() *ManagerStub {
return &ManagerStub{
mailboxes: make(map[string][]*message.Message),
}
}
// AddMessage adds a message to the specified mailbox.
func (m *ManagerStub) AddMessage(mailbox string, msg *message.Message) {
messages := m.mailboxes[mailbox]
m.mailboxes[mailbox] = append(messages, msg)
}
// GetMessage gets a message by ID from the specified mailbox.
func (m *ManagerStub) GetMessage(mailbox, id string) (*message.Message, error) {
if mailbox == "messageerr" {
return nil, errors.New("internal error")
}
for _, msg := range m.mailboxes[mailbox] {
if msg.ID == id {
return msg, nil
}
}
return nil, storage.ErrNotExist
}
// GetMetadata gets all the metadata for the specified mailbox.
func (m *ManagerStub) GetMetadata(mailbox string) ([]*message.Metadata, error) {
if mailbox == "messageserr" {
return nil, errors.New("internal error")
}
messages := m.mailboxes[mailbox]
metas := make([]*message.Metadata, len(messages))
for i, msg := range messages {
metas[i] = &msg.Metadata
}
return metas, nil
}
// MailboxForAddress invokes policy.ParseMailboxName.
func (m *ManagerStub) MailboxForAddress(address string) (string, error) {
addrPolicy := &policy.Addressing{Config: &config.Root{
MailboxNaming: config.FullNaming,
}}
return addrPolicy.ExtractMailbox(address)
}
// MarkSeen marks a message as having been read.
func (m *ManagerStub) MarkSeen(mailbox, id string) error {
if mailbox == "messageerr" {
return errors.New("internal error")
}
for _, msg := range m.mailboxes[mailbox] {
if msg.ID == id {
msg.Metadata.Seen = true
return nil
}
}
return storage.ErrNotExist
}