Skip to content

Commit ffd04d0

Browse files
committed
add tests for smtp command
1 parent d29b785 commit ffd04d0

File tree

3 files changed

+313
-22
lines changed

3 files changed

+313
-22
lines changed

cmd/admin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ var (
5757
newMicrocmdAuthUpdateLdapBindDn(),
5858
newMicrocmdAuthAddLdapSimpleAuth(),
5959
newMicrocmdAuthUpdateLdapSimpleAuth(),
60-
microcmdAuthAddSMTP,
61-
microcmdAuthUpdateSMTP,
60+
microcmdAuthAddSMTP(),
61+
microcmdAuthUpdateSMTP(),
6262
microcmdAuthList,
6363
microcmdAuthDelete,
6464
},

cmd/admin_auth_smtp_test.go

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cmd
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
auth_model "code.gitea.io/gitea/models/auth"
11+
"code.gitea.io/gitea/services/auth/source/smtp"
12+
13+
"github.com/stretchr/testify/assert"
14+
"github.com/urfave/cli/v3"
15+
)
16+
17+
func TestAddSMTP(t *testing.T) {
18+
testCases := []struct {
19+
name string
20+
args []string
21+
source *auth_model.Source
22+
errMsg string
23+
}{
24+
{
25+
name: "missing name",
26+
args: []string{
27+
"--host", "localhost",
28+
"--port", "25",
29+
},
30+
errMsg: "name must be set",
31+
},
32+
{
33+
name: "missing host",
34+
args: []string{
35+
"--name", "test",
36+
"--port", "25",
37+
},
38+
errMsg: "host must be set",
39+
},
40+
{
41+
name: "missing port",
42+
args: []string{
43+
"--name", "test",
44+
"--host", "localhost",
45+
},
46+
errMsg: "port must be set",
47+
},
48+
{
49+
name: "valid config",
50+
args: []string{
51+
"--name", "test",
52+
"--host", "localhost",
53+
"--port", "25",
54+
},
55+
source: &auth_model.Source{
56+
Type: auth_model.SMTP,
57+
Name: "test",
58+
IsActive: true,
59+
Cfg: &smtp.Source{
60+
Auth: "PLAIN",
61+
Host: "localhost",
62+
Port: 25,
63+
// ForceSMTPS: true,
64+
// SkipVerify: true,
65+
},
66+
TwoFactorPolicy: "skip",
67+
},
68+
},
69+
{
70+
name: "valid config with options",
71+
args: []string{
72+
"--name", "test",
73+
"--host", "localhost",
74+
"--port", "25",
75+
"--auth-type", "LOGIN",
76+
"--force-smtps=false",
77+
"--skip-verify=false",
78+
"--helo-hostname", "example.com",
79+
"--disable-helo=false",
80+
"--allowed-domains", "example.com,example.org",
81+
"--skip-local-2fa=false",
82+
"--active=false",
83+
},
84+
source: &auth_model.Source{
85+
Type: auth_model.SMTP,
86+
Name: "test",
87+
IsActive: false,
88+
Cfg: &smtp.Source{
89+
Auth: "LOGIN",
90+
Host: "localhost",
91+
Port: 25,
92+
ForceSMTPS: false,
93+
SkipVerify: false,
94+
HeloHostname: "example.com",
95+
DisableHelo: false,
96+
AllowedDomains: "example.com,example.org",
97+
},
98+
TwoFactorPolicy: "",
99+
},
100+
},
101+
}
102+
103+
for _, tc := range testCases {
104+
t.Run(tc.name, func(t *testing.T) {
105+
a := &authService{
106+
initDB: func(ctx context.Context) error {
107+
return nil
108+
},
109+
createAuthSource: func(ctx context.Context, source *auth_model.Source) error {
110+
assert.Equal(t, tc.source, source)
111+
return nil
112+
},
113+
}
114+
115+
cmd := &cli.Command{
116+
Flags: microcmdAuthAddSMTP().Flags,
117+
Action: a.runAddSMTP,
118+
}
119+
120+
args := []string{"smtp-test"}
121+
args = append(args, tc.args...)
122+
123+
t.Log(args)
124+
err := cmd.Run(t.Context(), args)
125+
126+
if tc.errMsg != "" {
127+
assert.EqualError(t, err, tc.errMsg)
128+
} else {
129+
assert.NoError(t, err)
130+
}
131+
})
132+
}
133+
}
134+
135+
func TestUpdateSMTP(t *testing.T) {
136+
testCases := []struct {
137+
name string
138+
args []string
139+
existingAuthSource *auth_model.Source
140+
authSource *auth_model.Source
141+
errMsg string
142+
}{
143+
{
144+
name: "missing id",
145+
args: []string{
146+
"--name", "test",
147+
"--host", "localhost",
148+
"--port", "25",
149+
},
150+
errMsg: "--id flag is missing",
151+
},
152+
{
153+
name: "valid config",
154+
existingAuthSource: &auth_model.Source{
155+
ID: 1,
156+
Type: auth_model.SMTP,
157+
Name: "old name",
158+
IsActive: true,
159+
Cfg: &smtp.Source{
160+
Auth: "PLAIN",
161+
Host: "old host",
162+
Port: 26,
163+
ForceSMTPS: true,
164+
SkipVerify: true,
165+
},
166+
TwoFactorPolicy: "",
167+
},
168+
args: []string{
169+
"--id", "1",
170+
"--name", "test",
171+
"--host", "localhost",
172+
"--port", "25",
173+
},
174+
authSource: &auth_model.Source{
175+
ID: 1,
176+
Type: auth_model.SMTP,
177+
Name: "test",
178+
IsActive: true,
179+
Cfg: &smtp.Source{
180+
Auth: "PLAIN",
181+
Host: "localhost",
182+
Port: 25,
183+
ForceSMTPS: true,
184+
SkipVerify: true,
185+
},
186+
TwoFactorPolicy: "skip",
187+
},
188+
},
189+
{
190+
name: "valid config with options",
191+
existingAuthSource: &auth_model.Source{
192+
ID: 1,
193+
Type: auth_model.SMTP,
194+
Name: "old name",
195+
IsActive: true,
196+
Cfg: &smtp.Source{
197+
Auth: "PLAIN",
198+
Host: "old host",
199+
Port: 26,
200+
ForceSMTPS: true,
201+
SkipVerify: true,
202+
HeloHostname: "old.example.com",
203+
DisableHelo: false,
204+
AllowedDomains: "old.example.com",
205+
},
206+
TwoFactorPolicy: "",
207+
},
208+
args: []string{
209+
"--id", "1",
210+
"--name", "test",
211+
"--host", "localhost",
212+
"--port", "25",
213+
"--auth-type", "LOGIN",
214+
"--force-smtps=false",
215+
"--skip-verify=false",
216+
"--helo-hostname", "example.com",
217+
"--disable-helo=true",
218+
"--allowed-domains", "example.com,example.org",
219+
"--skip-local-2fa=true",
220+
"--active=false",
221+
},
222+
authSource: &auth_model.Source{
223+
ID: 1,
224+
Type: auth_model.SMTP,
225+
Name: "test",
226+
IsActive: false,
227+
Cfg: &smtp.Source{
228+
Auth: "LOGIN",
229+
Host: "localhost",
230+
Port: 25,
231+
ForceSMTPS: false,
232+
SkipVerify: false,
233+
HeloHostname: "example.com",
234+
DisableHelo: true,
235+
AllowedDomains: "example.com,example.org",
236+
},
237+
TwoFactorPolicy: "skip",
238+
},
239+
},
240+
}
241+
242+
for _, tc := range testCases {
243+
t.Run(tc.name, func(t *testing.T) {
244+
a := &authService{
245+
initDB: func(ctx context.Context) error {
246+
return nil
247+
},
248+
getAuthSourceByID: func(ctx context.Context, id int64) (*auth_model.Source, error) {
249+
return &auth_model.Source{
250+
ID: 1,
251+
Type: auth_model.SMTP,
252+
Name: "test",
253+
IsActive: true,
254+
Cfg: &smtp.Source{
255+
Auth: "PLAIN",
256+
SkipVerify: true,
257+
ForceSMTPS: true,
258+
},
259+
TwoFactorPolicy: "skip",
260+
}, nil
261+
262+
},
263+
264+
updateAuthSource: func(ctx context.Context, source *auth_model.Source) error {
265+
assert.Equal(t, tc.authSource, source)
266+
return nil
267+
},
268+
}
269+
270+
app := &cli.Command{
271+
Flags: microcmdAuthUpdateSMTP().Flags,
272+
Action: a.runUpdateSMTP,
273+
}
274+
args := []string{"smtp-tests"}
275+
args = append(args, tc.args...)
276+
277+
err := app.Run(t.Context(), args)
278+
279+
if tc.errMsg != "" {
280+
assert.EqualError(t, err, tc.errMsg)
281+
} else {
282+
assert.NoError(t, err)
283+
}
284+
})
285+
}
286+
}

0 commit comments

Comments
 (0)