1package config
2
3import (
4 "reflect"
5 "testing"
6
7 "github.com/zalando/go-keyring"
8)
9
10// TestSaveAndLoadConfig verifies that the config can be saved to and loaded from a file correctly.
11func TestSaveAndLoadConfig(t *testing.T) {
12 // Use an in-memory mock keyring so tests do not interact with the host OS keyring
13 keyring.MockInit()
14
15 // Create a temporary directory for the test to avoid interfering with actual user config.
16 tempDir := t.TempDir()
17
18 // Temporarily override the user home directory to our temp directory.
19 // This ensures that our config file is written to a predictable, temporary location.
20 t.Setenv("HOME", tempDir)
21
22 // Define a sample configuration to save with multiple accounts.
23 expectedConfig := &Config{
24 Accounts: []Account{
25 {
26 ID: "test-id-1",
27 Name: "Test User",
28 Email: "test@example.com",
29 Password: "supersecret",
30 ServiceProvider: "gmail",
31 SendAsEmail: "alias@example.com",
32 },
33 {
34 ID: "test-id-2",
35 Name: "Custom User",
36 Email: "custom@example.com",
37 Password: "customsecret",
38 ServiceProvider: "custom",
39 IMAPServer: "imap.custom.com",
40 IMAPPort: 993,
41 SMTPServer: "smtp.custom.com",
42 SMTPPort: 587,
43 },
44 },
45 }
46
47 // Attempt to save the configuration.
48 err := SaveConfig(expectedConfig)
49 if err != nil {
50 t.Fatalf("SaveConfig() failed: %v", err)
51 }
52
53 // Attempt to load the configuration back.
54 loadedConfig, err := LoadConfig()
55 if err != nil {
56 t.Fatalf("LoadConfig() failed: %v", err)
57 }
58
59 // Compare the loaded configuration with the original one.
60 // reflect.DeepEqual is used for a deep comparison of the structs.
61 if !reflect.DeepEqual(loadedConfig, expectedConfig) {
62 t.Errorf("Loaded config does not match expected config.\nGot: %+v\nWant: %+v", loadedConfig, expectedConfig)
63 }
64}
65
66// TestAccountGetIMAPServer tests the logic that determines the IMAP server address.
67func TestAccountGetIMAPServer(t *testing.T) {
68 testCases := []struct {
69 name string
70 account Account
71 want string
72 }{
73 {"Gmail", Account{ServiceProvider: "gmail"}, "imap.gmail.com"},
74 {"iCloud", Account{ServiceProvider: "icloud"}, "imap.mail.me.com"},
75 {"Custom", Account{ServiceProvider: "custom", IMAPServer: "imap.custom.com"}, "imap.custom.com"},
76 {"Unsupported", Account{ServiceProvider: "yahoo"}, ""},
77 {"Empty", Account{ServiceProvider: ""}, ""},
78 }
79
80 for _, tc := range testCases {
81 t.Run(tc.name, func(t *testing.T) {
82 got := tc.account.GetIMAPServer()
83 if got != tc.want {
84 t.Errorf("GetIMAPServer() = %q, want %q", got, tc.want)
85 }
86 })
87 }
88}
89
90// TestAccountGetSMTPServer tests the logic that determines the SMTP server address.
91func TestAccountGetSMTPServer(t *testing.T) {
92 testCases := []struct {
93 name string
94 account Account
95 want string
96 }{
97 {"Gmail", Account{ServiceProvider: "gmail"}, "smtp.gmail.com"},
98 {"iCloud", Account{ServiceProvider: "icloud"}, "smtp.mail.me.com"},
99 {"Custom", Account{ServiceProvider: "custom", SMTPServer: "smtp.custom.com"}, "smtp.custom.com"},
100 {"Unsupported", Account{ServiceProvider: "yahoo"}, ""},
101 {"Empty", Account{ServiceProvider: ""}, ""},
102 }
103
104 for _, tc := range testCases {
105 t.Run(tc.name, func(t *testing.T) {
106 got := tc.account.GetSMTPServer()
107 if got != tc.want {
108 t.Errorf("GetSMTPServer() = %q, want %q", got, tc.want)
109 }
110 })
111 }
112}
113
114// TestConfigAddRemoveAccount tests adding and removing accounts from config.
115func TestConfigAddRemoveAccount(t *testing.T) {
116 // Use an in-memory mock keyring to test the deletion step cleanly
117 keyring.MockInit()
118
119 cfg := &Config{}
120
121 // Add an account
122 account := Account{
123 Name: "Test",
124 Email: "test@example.com",
125 ServiceProvider: "gmail",
126 }
127 cfg.AddAccount(account)
128
129 if len(cfg.Accounts) != 1 {
130 t.Fatalf("Expected 1 account, got %d", len(cfg.Accounts))
131 }
132
133 // Check that ID was auto-generated
134 if cfg.Accounts[0].ID == "" {
135 t.Error("Expected account ID to be auto-generated")
136 }
137
138 // Remove the account
139 accountID := cfg.Accounts[0].ID
140 removed := cfg.RemoveAccount(accountID)
141 if !removed {
142 t.Error("RemoveAccount should return true when account exists")
143 }
144
145 if len(cfg.Accounts) != 0 {
146 t.Fatalf("Expected 0 accounts after removal, got %d", len(cfg.Accounts))
147 }
148
149 // Try to remove non-existent account
150 removed = cfg.RemoveAccount("non-existent")
151 if removed {
152 t.Error("RemoveAccount should return false for non-existent account")
153 }
154}
155
156// TestConfigGetAccountByID tests retrieving accounts by ID.
157func TestConfigGetAccountByID(t *testing.T) {
158 cfg := &Config{
159 Accounts: []Account{
160 {ID: "id-1", Email: "test1@example.com"},
161 {ID: "id-2", Email: "test2@example.com"},
162 },
163 }
164
165 account := cfg.GetAccountByID("id-1")
166 if account == nil {
167 t.Fatal("Expected to find account with id-1")
168 }
169 if account.Email != "test1@example.com" {
170 t.Errorf("Expected email test1@example.com, got %s", account.Email)
171 }
172
173 // Non-existent ID
174 account = cfg.GetAccountByID("non-existent")
175 if account != nil {
176 t.Error("Expected nil for non-existent account ID")
177 }
178}
179
180// TestConfigGetAccountByEmail tests retrieving accounts by email.
181func TestConfigGetAccountByEmail(t *testing.T) {
182 cfg := &Config{
183 Accounts: []Account{
184 {ID: "id-1", Email: "test1@example.com"},
185 {ID: "id-2", Email: "test2@example.com"},
186 },
187 }
188
189 account := cfg.GetAccountByEmail("test2@example.com")
190 if account == nil {
191 t.Fatal("Expected to find account with test2@example.com")
192 }
193 if account.ID != "id-2" {
194 t.Errorf("Expected ID id-2, got %s", account.ID)
195 }
196
197 // Non-existent email
198 account = cfg.GetAccountByEmail("nonexistent@example.com")
199 if account != nil {
200 t.Error("Expected nil for non-existent account email")
201 }
202}
203
204// TestConfigHasAccounts tests the HasAccounts method.
205func TestConfigHasAccounts(t *testing.T) {
206 cfg := &Config{}
207 if cfg.HasAccounts() {
208 t.Error("Expected HasAccounts to return false for empty config")
209 }
210
211 cfg.AddAccount(Account{Email: "test@example.com"})
212 if !cfg.HasAccounts() {
213 t.Error("Expected HasAccounts to return true after adding account")
214 }
215}
216
217// TestAccountGetPorts tests the port retrieval methods.
218func TestAccountGetPorts(t *testing.T) {
219 // Gmail account should use default ports
220 gmailAccount := Account{ServiceProvider: "gmail"}
221 if gmailAccount.GetIMAPPort() != 993 {
222 t.Errorf("Expected Gmail IMAP port 993, got %d", gmailAccount.GetIMAPPort())
223 }
224 if gmailAccount.GetSMTPPort() != 587 {
225 t.Errorf("Expected Gmail SMTP port 587, got %d", gmailAccount.GetSMTPPort())
226 }
227
228 // Custom account with custom ports
229 customAccount := Account{
230 ServiceProvider: "custom",
231 IMAPPort: 1993,
232 SMTPPort: 1587,
233 }
234 if customAccount.GetIMAPPort() != 1993 {
235 t.Errorf("Expected custom IMAP port 1993, got %d", customAccount.GetIMAPPort())
236 }
237 if customAccount.GetSMTPPort() != 1587 {
238 t.Errorf("Expected custom SMTP port 1587, got %d", customAccount.GetSMTPPort())
239 }
240
241 // Custom account with default ports (0 means use default)
242 customDefaultAccount := Account{ServiceProvider: "custom"}
243 if customDefaultAccount.GetIMAPPort() != 993 {
244 t.Errorf("Expected default IMAP port 993 for custom with no port, got %d", customDefaultAccount.GetIMAPPort())
245 }
246 if customDefaultAccount.GetSMTPPort() != 587 {
247 t.Errorf("Expected default SMTP port 587 for custom with no port, got %d", customDefaultAccount.GetSMTPPort())
248 }
249}
250
251func TestAccountSendIdentityHelpers(t *testing.T) {
252 t.Run("send as takes precedence", func(t *testing.T) {
253 account := Account{
254 Name: "Alias User",
255 Email: "login@gmail.com",
256 FetchEmail: "inbox@gmail.com",
257 SendAsEmail: "alias@example.com",
258 }
259
260 if got := account.GetFetchEmail(); got != "inbox@gmail.com" {
261 t.Fatalf("GetFetchEmail() = %q, want %q", got, "inbox@gmail.com")
262 }
263 if got := account.GetSendAsEmail(); got != "alias@example.com" {
264 t.Fatalf("GetSendAsEmail() = %q, want %q", got, "alias@example.com")
265 }
266 if got := account.FormatFromHeader(); got != "Alias User <alias@example.com>" {
267 t.Fatalf("FormatFromHeader() = %q, want %q", got, "Alias User <alias@example.com>")
268 }
269 })
270
271 t.Run("send as falls back to fetch then login", func(t *testing.T) {
272 account := Account{
273 Name: "Fallback User",
274 Email: "login@gmail.com",
275 FetchEmail: "inbox@gmail.com",
276 }
277 if got := account.GetSendAsEmail(); got != "inbox@gmail.com" {
278 t.Fatalf("GetSendAsEmail() = %q, want %q", got, "inbox@gmail.com")
279 }
280
281 account.FetchEmail = ""
282 if got := account.GetSendAsEmail(); got != "login@gmail.com" {
283 t.Fatalf("GetSendAsEmail() = %q, want %q", got, "login@gmail.com")
284 }
285 })
286}