-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_test.go
160 lines (111 loc) · 4.55 KB
/
mail_test.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
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
package mailinabox
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUserService_GetUsers(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/users", testHandler("mail/mailusers.json", http.MethodGet, http.StatusOK))
records, err := client.Mail.GetUsers(context.Background())
require.NoError(t, err)
expected := []MailUsers{{
Domain: "example.com",
Users: []User{
{
Email: "user@example.com",
Privileges: []string{"admin"},
Status: "active",
Mailbox: "/home/user-data/mail/mailboxes/example.com/user",
},
},
}}
assert.Equal(t, expected, records)
}
func TestUserService_AddUser(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/users/add", testHandler("mail/adduser.html", http.MethodPost, http.StatusOK))
resp, err := client.Mail.AddUser(context.Background(), "user@example.com", "secret", "admin")
require.NoError(t, err)
assert.Equal(t, "mail user added\nupdated DNS: OpenDKIM configuration", resp)
}
func TestUserService_RemoveUser(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/users/remove", testHandler("mail/removeuser.html", http.MethodPost, http.StatusOK))
resp, err := client.Mail.RemoveUser(context.Background(), "user@example.com")
require.NoError(t, err)
assert.Equal(t, "OK", resp)
}
func TestUserService_AddUserPrivilege(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/users/privileges/add", testHandler("mail/adduserprivilege.html", http.MethodPost, http.StatusOK))
resp, err := client.Mail.AddUserPrivilege(context.Background(), "user@example.com", "admin")
require.NoError(t, err)
assert.Equal(t, "OK", resp)
}
func TestUserService_RemoveUserPrivilege(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/users/privileges/remove", testHandler("mail/removeuserprivilege.html", http.MethodPost, http.StatusOK))
resp, err := client.Mail.RemoveUserPrivilege(context.Background(), "user@example.com", "admin")
require.NoError(t, err)
assert.Equal(t, "OK", resp)
}
func TestUserService_SetUserPassword(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/users/password", testHandler("mail/setuserpassword.html", http.MethodPost, http.StatusOK))
resp, err := client.Mail.SetUserPassword(context.Background(), "user@example.com", "secret")
require.NoError(t, err)
assert.Equal(t, "OK", resp)
}
func TestUserService_GetUserPrivileges(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/users/privileges", testHandler("mail/getMailUserPrivileges.html", http.MethodGet, http.StatusOK))
domains, err := client.Mail.GetUserPrivileges(context.Background(), "user@example.com")
require.NoError(t, err)
assert.Equal(t, "admin", domains)
}
func TestUserService_GetDomains(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/domains", testHandler("mail/getMailDomains.html", http.MethodGet, http.StatusOK))
domains, err := client.Mail.GetDomains(context.Background())
require.NoError(t, err)
expected := []string{"example1.com", "example2.com"}
assert.Equal(t, expected, domains)
}
func TestUserService_GetAliases(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/aliases", testHandler("mail/getMailAliases.json", http.MethodGet, http.StatusOK))
aliases, err := client.Mail.GetAliases(context.Background())
require.NoError(t, err)
expected := []MailAliases{
{
Domain: "example.com",
Aliases: []Alias{
{
Address: "user@example.com",
AddressDisplay: "user@example.com",
ForwardsTo: []string{"user@example.com"},
PermittedSenders: []string{"user@example.com"},
Required: true,
},
},
},
}
assert.Equal(t, expected, aliases)
}
func TestUserService_UpsertAlias(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/aliases/add", testHandler("mail/upsertMailAlias.html", http.MethodPost, http.StatusOK))
resp, err := client.Mail.UpsertAlias(context.Background(), true, "user@example.com", []string{"user@example.com"}, []string{"user@example.com"})
require.NoError(t, err)
assert.Equal(t, "alias updated", resp)
}
func TestUserService_RemoveAliases(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/admin/mail/aliases/remove", testHandler("mail/removeMailAlias.html", http.MethodPost, http.StatusOK))
resp, err := client.Mail.RemoveAliases(context.Background(), "user@example.com")
require.NoError(t, err)
assert.Equal(t, "alias updated", resp)
}