Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit06b27d2

Browse files
committed
Apply review suggestions
1 parentf7fa8e1 commit06b27d2

File tree

3 files changed

+93
-75
lines changed

3 files changed

+93
-75
lines changed

‎cli/exp_scaletest.go‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,21 +2178,16 @@ func (r *RootCmd) scaletestNotifications() *serpent.Command {
21782178

21792179
func (*RootCmd)scaletestSMTP()*serpent.Command {
21802180
var (
2181-
hoststring
2182-
portint64
2181+
hostAddressstring
2182+
smtpPortint64
21832183
apiPortint64
21842184
purgeAtCountint64
21852185
)
21862186
cmd:=&serpent.Command{
21872187
Use:"smtp",
21882188
Short:"Start a mock SMTP server for testing",
21892189
Long:`Start a mock SMTP server with an HTTP API server that can be used to purge
2190-
messages and get messages by email.
2191-
2192-
Coder deployment values required:
2193-
- CODER_EMAIL_FROM=noreply@coder.com
2194-
- CODER_EMAIL_SMARTHOST=localhost:33199
2195-
- CODER_EMAIL_HELLO=localhost`,
2190+
messages and get messages by email.`,
21962191
Handler:func(inv*serpent.Invocation)error {
21972192
ctx:=inv.Context()
21982193
notifyCtx,stop:=signal.NotifyContext(ctx,StopSignals...)
@@ -2201,10 +2196,10 @@ Coder deployment values required:
22012196

22022197
logger:=slog.Make(sloghuman.Sink(inv.Stderr)).Leveled(slog.LevelInfo)
22032198
srv:=smtpmock.New(smtpmock.Config{
2204-
Host:host,
2205-
SMTPPort:int(port),
2206-
APIPort:int(apiPort),
2207-
Logger:logger,
2199+
HostAddress:hostAddress,
2200+
SMTPPort:int(smtpPort),
2201+
APIPort:int(apiPort),
2202+
Logger:logger,
22082203
})
22092204

22102205
iferr:=srv.Start(ctx);err!=nil {
@@ -2226,7 +2221,7 @@ Coder deployment values required:
22262221
for {
22272222
select {
22282223
case<-ctx.Done():
2229-
_,_=fmt.Fprintf(inv.Stdout,"\nTotal messages received: %d\n",srv.MessageCount())
2224+
_,_=fmt.Fprintf(inv.Stdout,"\nTotal messages received since last purge: %d\n",srv.MessageCount())
22302225
returnnil
22312226
case<-ticker.C:
22322227
count:=srv.MessageCount()
@@ -2246,17 +2241,17 @@ Coder deployment values required:
22462241

22472242
cmd.Options= []serpent.Option{
22482243
{
2249-
Flag:"host",
2244+
Flag:"host-address",
22502245
Env:"CODER_SCALETEST_SMTP_HOST",
22512246
Default:"localhost",
22522247
Description:"Host to bind the mock SMTP and API servers.",
2253-
Value:serpent.StringOf(&host),
2248+
Value:serpent.StringOf(&hostAddress),
22542249
},
22552250
{
2256-
Flag:"port",
2251+
Flag:"smtp-port",
22572252
Env:"CODER_SCALETEST_SMTP_PORT",
22582253
Description:"Port for the mock SMTP server. Uses a random port if not specified.",
2259-
Value:serpent.Int64Of(&port),
2254+
Value:serpent.Int64Of(&smtpPort),
22602255
},
22612256
{
22622257
Flag:"api-port",

‎scaletest/smtpmock/server.go‎

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
10+
"mime/quotedprintable"
911
"net"
1012
"net/http"
13+
"net/mail"
1114
"regexp"
1215
"slices"
1316
"strings"
@@ -22,45 +25,45 @@ import (
2225

2326
// Server wraps the SMTP mock server and provides an HTTP API to retrieve emails.
2427
typeServerstruct {
25-
smtpServer*smtpmocklib.Server
26-
httpServer*http.Server
27-
listener net.Listener
28-
logger slog.Logger
29-
30-
hoststring
31-
smtpPortint
32-
apiPortint
28+
smtpServer*smtpmocklib.Server
29+
httpServer*http.Server
30+
httpListener net.Listener
31+
loggerslog.Logger
32+
33+
hostAddressstring
34+
smtpPortint
35+
apiPortint
3336
}
3437

3538
typeConfigstruct {
36-
Hoststring
37-
SMTPPortint
38-
APIPortint
39-
Logger slog.Logger
39+
HostAddressstring
40+
SMTPPortint
41+
APIPortint
42+
Loggerslog.Logger
4043
}
4144

4245
typeEmailSummarystruct {
43-
Subjectstring`json:"subject"`
44-
Date time.Time`json:"date"`
45-
NotificationID uuid.UUID`json:"notification_id,omitempty"`
46+
Subjectstring`json:"subject"`
47+
Datetime.Time`json:"date"`
48+
NotificationTemplateID uuid.UUID`json:"notification_template_id,omitempty"`
4649
}
4750

48-
varnotificationIDRegex=regexp.MustCompile(`notifications\?disabled=3D([a-f0-9-]+)`)
51+
varnotificationTemplateIDRegex=regexp.MustCompile(`notifications\?disabled=([a-f0-9-]+)`)
4952

5053
funcNew(cfgConfig)*Server {
5154
return&Server{
52-
host:cfg.Host,
53-
smtpPort:cfg.SMTPPort,
54-
apiPort:cfg.APIPort,
55-
logger:cfg.Logger,
55+
hostAddress:cfg.HostAddress,
56+
smtpPort:cfg.SMTPPort,
57+
apiPort:cfg.APIPort,
58+
logger:cfg.Logger,
5659
}
5760
}
5861

5962
func (s*Server)Start(ctx context.Context)error {
6063
s.smtpServer=smtpmocklib.New(smtpmocklib.ConfigurationAttr{
6164
LogToStdout:false,
6265
LogServerActivity:true,
63-
HostAddress:s.host,
66+
HostAddress:s.hostAddress,
6467
PortNumber:s.smtpPort,
6568
})
6669
iferr:=s.smtpServer.Start();err!=nil {
@@ -97,11 +100,11 @@ func (s *Server) Stop() error {
97100
}
98101

99102
func (s*Server)SMTPAddress()string {
100-
returnfmt.Sprintf("%s:%d",s.host,s.smtpPort)
103+
returnfmt.Sprintf("%s:%d",s.hostAddress,s.smtpPort)
101104
}
102105

103106
func (s*Server)APIAddress()string {
104-
returnfmt.Sprintf("http://%s:%d",s.host,s.apiPort)
107+
returnfmt.Sprintf("http://%s:%d",s.hostAddress,s.apiPort)
105108
}
106109

107110
func (s*Server)MessageCount()int {
@@ -127,11 +130,11 @@ func (s *Server) startAPIServer(ctx context.Context) error {
127130
ReadHeaderTimeout:10*time.Second,
128131
}
129132

130-
listener,err:=net.Listen("tcp",fmt.Sprintf("%s:%d",s.host,s.apiPort))
133+
listener,err:=net.Listen("tcp",fmt.Sprintf("%s:%d",s.hostAddress,s.apiPort))
131134
iferr!=nil {
132-
returnxerrors.Errorf("listen on %s:%d: %w",s.host,s.apiPort,err)
135+
returnxerrors.Errorf("listen on %s:%d: %w",s.hostAddress,s.apiPort,err)
133136
}
134-
s.listener=listener
137+
s.httpListener=listener
135138

136139
tcpAddr,valid:=listener.Addr().(*net.TCPAddr)
137140
if!valid {
@@ -187,13 +190,36 @@ func matchesRecipient(recipients [][]string, email string) bool {
187190
returntrue
188191
}
189192
returnslices.ContainsFunc(recipients,func(rcptPair []string)bool {
190-
returnlen(rcptPair)>0&&strings.Contains(rcptPair[0],email)
193+
iflen(rcptPair)==0 {
194+
returnfalse
195+
}
196+
197+
addrPart,ok:=strings.CutPrefix(rcptPair[0],"RCPT TO:")
198+
if!ok {
199+
returnfalse
200+
}
201+
202+
addr,err:=mail.ParseAddress(addrPart)
203+
iferr!=nil {
204+
returnfalse
205+
}
206+
207+
returnstrings.EqualFold(addr.Address,email)
191208
})
192209
}
193210

194-
funcparseEmailSummary(contentstring) (EmailSummary,error) {
211+
funcparseEmailSummary(messagestring) (EmailSummary,error) {
195212
varsummaryEmailSummary
196-
scanner:=bufio.NewScanner(strings.NewReader(content))
213+
214+
// Decode quoted-printable message
215+
reader:=quotedprintable.NewReader(strings.NewReader(message))
216+
content,err:=io.ReadAll(reader)
217+
iferr!=nil {
218+
returnsummary,xerrors.Errorf("decode email content: %w",err)
219+
}
220+
221+
contentStr:=string(content)
222+
scanner:=bufio.NewScanner(strings.NewReader(contentStr))
197223

198224
// Extract Subject and Date from headers.
199225
// Date is used to measure latency.
@@ -211,18 +237,15 @@ func parseEmailSummary(content string) (EmailSummary, error) {
211237
}
212238
}
213239

214-
// Extract notification ID from email content
240+
// Extract notification ID fromdecodedemail content
215241
// Notification ID is present in the email footer like this
216242
// <p><a href=3D"http://127.0.0.1:3000/settings/notifications?disabled=3D
217243
// =3D4e19c0ac-94e1-4532-9515-d1801aa283b2" style=3D"color: #2563eb; text-deco=
218244
// ration: none;">Stop receiving emails like this</a></p>
219-
replacer:=strings.NewReplacer("=\n","","=\r\n","")
220-
contentNormalized:=replacer.Replace(content)
221-
ifmatches:=notificationIDRegex.FindStringSubmatch(contentNormalized);len(matches)>1 {
222-
varerrerror
223-
summary.NotificationID,err=uuid.Parse(matches[1])
245+
ifmatches:=notificationTemplateIDRegex.FindStringSubmatch(contentStr);len(matches)>1 {
246+
summary.NotificationTemplateID,err=uuid.Parse(matches[1])
224247
iferr!=nil {
225-
returnsummary,xerrors.Errorf("failed toparse notification ID: %w",err)
248+
returnsummary,xerrors.Errorf("parse notification ID: %w",err)
226249
}
227250
}
228251

‎scaletest/smtpmock/server_test.go‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestServer_StartStop(t *testing.T) {
2323

2424
ctx:=context.Background()
2525
srv:=smtpmock.New(smtpmock.Config{
26-
Host:"127.0.0.1",
27-
SMTPPort:0,
28-
APIPort:0,
29-
Logger:slogtest.Make(t,nil),
26+
HostAddress:"127.0.0.1",
27+
SMTPPort:0,
28+
APIPort:0,
29+
Logger:slogtest.Make(t,nil),
3030
})
3131

3232
err:=srv.Start(ctx)
@@ -43,10 +43,10 @@ func TestServer_SendAndReceiveEmail(t *testing.T) {
4343

4444
ctx:=context.Background()
4545
srv:=smtpmock.New(smtpmock.Config{
46-
Host:"127.0.0.1",
47-
SMTPPort:0,
48-
APIPort:0,
49-
Logger:slogtest.Make(t,nil),
46+
HostAddress:"127.0.0.1",
47+
SMTPPort:0,
48+
APIPort:0,
49+
Logger:slogtest.Make(t,nil),
5050
})
5151

5252
err:=srv.Start(ctx)
@@ -82,10 +82,10 @@ func TestServer_FilterByEmail(t *testing.T) {
8282

8383
ctx:=context.Background()
8484
srv:=smtpmock.New(smtpmock.Config{
85-
Host:"127.0.0.1",
86-
SMTPPort:0,
87-
APIPort:0,
88-
Logger:slogtest.Make(t,nil),
85+
HostAddress:"127.0.0.1",
86+
SMTPPort:0,
87+
APIPort:0,
88+
Logger:slogtest.Make(t,nil),
8989
})
9090

9191
err:=srv.Start(ctx)
@@ -117,15 +117,15 @@ func TestServer_FilterByEmail(t *testing.T) {
117117
require.Equal(t,"Email for admin",summaries[0].Subject)
118118
}
119119

120-
funcTestServer_NotificationID(t*testing.T) {
120+
funcTestServer_NotificationTemplateID(t*testing.T) {
121121
t.Parallel()
122122

123123
ctx:=context.Background()
124124
srv:=smtpmock.New(smtpmock.Config{
125-
Host:"127.0.0.1",
126-
SMTPPort:0,
127-
APIPort:0,
128-
Logger:slogtest.Make(t,nil),
125+
HostAddress:"127.0.0.1",
126+
SMTPPort:0,
127+
APIPort:0,
128+
Logger:slogtest.Make(t,nil),
129129
})
130130

131131
err:=srv.Start(ctx)
@@ -154,18 +154,18 @@ func TestServer_NotificationID(t *testing.T) {
154154
err=json.NewDecoder(resp.Body).Decode(&summaries)
155155
require.NoError(t,err)
156156
require.Len(t,summaries,1)
157-
require.Equal(t,notificationID,summaries[0].NotificationID)
157+
require.Equal(t,notificationID,summaries[0].NotificationTemplateID)
158158
}
159159

160160
funcTestServer_Purge(t*testing.T) {
161161
t.Parallel()
162162

163163
ctx:=context.Background()
164164
srv:=smtpmock.New(smtpmock.Config{
165-
Host:"127.0.0.1",
166-
SMTPPort:0,
167-
APIPort:0,
168-
Logger:slogtest.Make(t,nil),
165+
HostAddress:"127.0.0.1",
166+
SMTPPort:0,
167+
APIPort:0,
168+
Logger:slogtest.Make(t,nil),
169169
})
170170

171171
err:=srv.Start(ctx)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp