-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_move_test.go
More file actions
197 lines (177 loc) · 5.42 KB
/
Copy pathemail_move_test.go
File metadata and controls
197 lines (177 loc) · 5.42 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package connector
import (
"context"
"fmt"
"net/smtp"
"testing"
"time"
imaplib "github.com/dvflw/mantle/internal/imap"
)
// TestEmailMoveParamValidation verifies that required parameters are enforced.
func TestEmailMoveParamValidation(t *testing.T) {
c := &EmailMoveConnector{}
t.Run("missing credential", func(t *testing.T) {
_, err := c.Execute(context.Background(), map[string]any{
"uid": uint32(1),
"target_folder": "Archive",
})
if err == nil {
t.Fatal("expected error for missing credential, got nil")
}
})
t.Run("missing uid", func(t *testing.T) {
_, err := c.Execute(context.Background(), map[string]any{
"target_folder": "Archive",
"_credential": map[string]string{
"host": "localhost", "port": "993",
"username": "u", "password": "p",
},
})
if err == nil {
t.Fatal("expected error for missing uid, got nil")
}
})
t.Run("missing target_folder", func(t *testing.T) {
_, err := c.Execute(context.Background(), map[string]any{
"uid": uint32(1),
"_credential": map[string]string{
"host": "localhost", "port": "993",
"username": "u", "password": "p",
},
})
if err == nil {
t.Fatal("expected error for missing target_folder, got nil")
}
})
t.Run("uid zero", func(t *testing.T) {
_, err := c.Execute(context.Background(), map[string]any{
"uid": uint32(0),
"target_folder": "Archive",
"_credential": map[string]string{
"host": "localhost", "port": "993",
"username": "u", "password": "p",
},
})
if err == nil {
t.Fatal("expected error for uid=0, got nil")
}
})
}
// TestEmailMove_MovesMessage is an integration test that:
// 1. Starts a GreenMail IMAP/SMTP container.
// 2. Delivers a test message via SMTP.
// 3. Calls EmailMoveConnector.Execute to move it to a target folder.
// 4. Verifies the move result.
func TestEmailMove_MovesMessage(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
host, imapPort, smtpPort := setupGreenMailFull(t)
const (
username = "mover@example.com"
password = "password"
)
// Deliver a test message via SMTP.
smtpAddr := fmt.Sprintf("%s:%s", host, smtpPort)
msg := []byte("From: sender@example.com\r\n" +
"To: mover@example.com\r\n" +
"Subject: Move Me\r\n" +
"\r\n" +
"Please move this message.\r\n")
if err := smtp.SendMail(smtpAddr, nil, "sender@example.com", []string{username}, msg); err != nil {
t.Fatalf("failed to send test email: %v", err)
}
// Fetch the UID of the delivered message.
uid := fetchFirstUID(t, host, imapPort, username, password, "INBOX")
// Create the target folder before moving (GreenMail does not auto-create folders).
createTargetFolder(t, host, imapPort, username, password, "INBOX.Archive")
// Move the message.
c := &EmailMoveConnector{}
result, err := c.Execute(context.Background(), map[string]any{
"uid": uid,
"source_folder": "INBOX",
"target_folder": "INBOX.Archive",
"_credential": map[string]string{
"host": host,
"port": imapPort,
"username": username,
"password": password,
"use_tls": "false",
},
})
if err != nil {
t.Fatalf("Execute() error: %v", err)
}
if moved, _ := result["moved"].(bool); !moved {
t.Errorf("moved = %v, want true", result["moved"])
}
if retUID, _ := result["uid"].(uint32); retUID != uid {
t.Errorf("uid = %v, want %d", result["uid"], uid)
}
if tf, _ := result["target_folder"].(string); tf != "INBOX.Archive" {
t.Errorf("target_folder = %q, want %q", tf, "INBOX.Archive")
}
}
// createTargetFolder creates an IMAP folder via a direct IMAP connection.
// Used in tests to ensure the target folder exists before moving messages.
func createTargetFolder(t *testing.T, host, imapPort, username, password, folder string) {
t.Helper()
cfg := &imaplib.Config{
Host: host,
Port: imapPort,
Username: username,
Password: password,
UseTLS: false,
}
client, err := imapDial(cfg)
if err != nil {
t.Fatalf("createTargetFolder: dial error: %v", err)
}
defer func() { _ = client.Close() }()
if err := client.Create(folder, nil).Wait(); err != nil {
t.Fatalf("createTargetFolder: CREATE %q error: %v", folder, err)
}
}
// fetchFirstUID retrieves the first UID from the given IMAP folder using the
// EmailReceiveConnector, to avoid duplicating IMAP client logic in tests.
// It retries up to 5 times with a 2-second pause to accommodate the brief
// delay between SMTP delivery and IMAP visibility in CI.
func fetchFirstUID(t *testing.T, host, imapPort, username, password, folder string) uint32 {
t.Helper()
recv := &EmailReceiveConnector{}
cred := map[string]string{
"host": host,
"port": imapPort,
"username": username,
"password": password,
"use_tls": "false",
}
for attempt := 0; attempt < 5; attempt++ {
if attempt > 0 {
time.Sleep(2 * time.Second)
}
result, err := recv.Execute(context.Background(), map[string]any{
"folder": folder,
"filter": "all",
"limit": 1,
"_credential": cred,
})
if err != nil {
t.Logf("fetchFirstUID attempt %d/5: receive error: %v", attempt+1, err)
continue
}
msgs, ok := result["messages"].([]map[string]any)
if !ok || len(msgs) == 0 {
t.Logf("fetchFirstUID attempt %d/5: no messages yet", attempt+1)
continue
}
uid, _ := msgs[0]["uid"].(uint32)
if uid == 0 {
t.Logf("fetchFirstUID attempt %d/5: uid is zero", attempt+1)
continue
}
return uid
}
t.Fatal("fetchFirstUID: no messages found after retries")
return 0
}