refactor: variables name and use strings.Cut()
This commit is contained in:
@@ -10,9 +10,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"goyco/internal/config"
|
"goyco/internal/config"
|
||||||
"goyco/internal/database"
|
"goyco/internal/database"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestEmailServer struct {
|
type TestEmailServer struct {
|
||||||
@@ -47,95 +48,95 @@ func NewTestEmailServer() (*TestEmailServer, error) {
|
|||||||
closed: false,
|
closed: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := listener.Addr().(*net.TCPAddr)
|
address := listener.Addr().(*net.TCPAddr)
|
||||||
server.port = addr.Port
|
server.port = address.Port
|
||||||
|
|
||||||
go server.serve()
|
go server.serve()
|
||||||
|
|
||||||
return server, nil
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) serve() {
|
func (server *TestEmailServer) serve() {
|
||||||
for {
|
for {
|
||||||
if s.closed {
|
if server.closed {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := s.listener.Accept()
|
connection, err := server.listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !s.closed {
|
if !server.closed {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go s.handleConnection(conn)
|
go server.handleConnection(connection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) handleConnection(conn net.Conn) {
|
func (server *TestEmailServer) handleConnection(connection net.Conn) {
|
||||||
defer conn.Close()
|
defer connection.Close()
|
||||||
|
|
||||||
conn.Write([]byte("220 Test SMTP server ready\r\n"))
|
connection.Write([]byte("220 Test SMTP server ready\r\n"))
|
||||||
|
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
for {
|
for {
|
||||||
n, err := conn.Read(buffer)
|
n, err := connection.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
command := strings.TrimSpace(string(buffer[:n]))
|
command := strings.TrimSpace(string(buffer[:n]))
|
||||||
|
|
||||||
if s.delay > 0 {
|
if server.delay > 0 {
|
||||||
time.Sleep(s.delay)
|
time.Sleep(server.delay)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(command, "EHLO"), strings.HasPrefix(command, "HELO"):
|
case strings.HasPrefix(command, "EHLO"), strings.HasPrefix(command, "HELO"):
|
||||||
conn.Write([]byte("250-Hello\r\n250-AUTH PLAIN LOGIN\r\n250 OK\r\n"))
|
connection.Write([]byte("250-Hello\r\n250-AUTH PLAIN LOGIN\r\n250 OK\r\n"))
|
||||||
case strings.HasPrefix(command, "AUTH PLAIN"):
|
case strings.HasPrefix(command, "AUTH PLAIN"):
|
||||||
conn.Write([]byte("235 Authentication successful\r\n"))
|
connection.Write([]byte("235 Authentication successful\r\n"))
|
||||||
case strings.HasPrefix(command, "AUTH LOGIN"):
|
case strings.HasPrefix(command, "AUTH LOGIN"):
|
||||||
conn.Write([]byte("334 VXNlcm5hbWU6\r\n"))
|
connection.Write([]byte("334 VXNlcm5hbWU6\r\n"))
|
||||||
if _, err := conn.Read(buffer); err != nil {
|
if _, err := connection.Read(buffer); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Write([]byte("334 UGFzc3dvcmQ6\r\n"))
|
connection.Write([]byte("334 UGFzc3dvcmQ6\r\n"))
|
||||||
if _, err := conn.Read(buffer); err != nil {
|
if _, err := connection.Read(buffer); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Write([]byte("235 Authentication successful\r\n"))
|
connection.Write([]byte("235 Authentication successful\r\n"))
|
||||||
case strings.HasPrefix(command, "AUTH"):
|
case strings.HasPrefix(command, "AUTH"):
|
||||||
conn.Write([]byte("504 Unrecognized authentication type\r\n"))
|
connection.Write([]byte("504 Unrecognized authentication type\r\n"))
|
||||||
case strings.HasPrefix(command, "MAIL FROM"):
|
case strings.HasPrefix(command, "MAIL FROM"):
|
||||||
if s.shouldFail {
|
if server.shouldFail {
|
||||||
conn.Write([]byte("550 Mail from failed\r\n"))
|
connection.Write([]byte("550 Mail from failed\r\n"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Write([]byte("250 OK\r\n"))
|
connection.Write([]byte("250 OK\r\n"))
|
||||||
case strings.HasPrefix(command, "RCPT TO"):
|
case strings.HasPrefix(command, "RCPT TO"):
|
||||||
if s.shouldFail {
|
if server.shouldFail {
|
||||||
conn.Write([]byte("550 Rcpt to failed\r\n"))
|
connection.Write([]byte("550 Rcpt to failed\r\n"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Write([]byte("250 OK\r\n"))
|
connection.Write([]byte("250 OK\r\n"))
|
||||||
case command == "DATA":
|
case command == "DATA":
|
||||||
conn.Write([]byte("354 Start mail input; end with <CRLF>.<CRLF>\r\n"))
|
connection.Write([]byte("354 Start mail input; end with <CRLF>.<CRLF>\r\n"))
|
||||||
s.readEmailData(conn)
|
server.readEmailData(connection)
|
||||||
case command == "QUIT":
|
case command == "QUIT":
|
||||||
conn.Write([]byte("221 Bye\r\n"))
|
connection.Write([]byte("221 Bye\r\n"))
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
conn.Write([]byte("500 Unknown command\r\n"))
|
connection.Write([]byte("500 Unknown command\r\n"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) readEmailData(conn net.Conn) {
|
func (server *TestEmailServer) readEmailData(connection net.Conn) {
|
||||||
var emailData strings.Builder
|
var emailData strings.Builder
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := conn.Read(buffer)
|
n, err := connection.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -147,15 +148,15 @@ func (s *TestEmailServer) readEmailData(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
email := s.parseEmail(emailData.String())
|
email := server.parseEmail(emailData.String())
|
||||||
s.mu.Lock()
|
server.mu.Lock()
|
||||||
s.emails = append(s.emails, email)
|
server.emails = append(server.emails, email)
|
||||||
s.mu.Unlock()
|
server.mu.Unlock()
|
||||||
|
|
||||||
conn.Write([]byte("250 OK\r\n"))
|
connection.Write([]byte("250 OK\r\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) parseEmail(data string) TestEmail {
|
func (server *TestEmailServer) parseEmail(data string) TestEmail {
|
||||||
lines := strings.Split(data, "\r\n")
|
lines := strings.Split(data, "\r\n")
|
||||||
email := TestEmail{
|
email := TestEmail{
|
||||||
Headers: make(map[string]string),
|
Headers: make(map[string]string),
|
||||||
@@ -180,9 +181,9 @@ func (s *TestEmailServer) parseEmail(data string) TestEmail {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if line == "" {
|
} else if line == "" {
|
||||||
bodyStart := strings.Index(data, "\r\n\r\n")
|
_, after, ok := strings.Cut(data, "\r\n\r\n")
|
||||||
if bodyStart != -1 {
|
if ok {
|
||||||
email.Body = data[bodyStart+4:]
|
email.Body = after
|
||||||
email.Body = strings.TrimSuffix(email.Body, "\r\n.\r\n")
|
email.Body = strings.TrimSuffix(email.Body, "\r\n.\r\n")
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -192,56 +193,56 @@ func (s *TestEmailServer) parseEmail(data string) TestEmail {
|
|||||||
return email
|
return email
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) Close() error {
|
func (server *TestEmailServer) Close() error {
|
||||||
s.closed = true
|
server.closed = true
|
||||||
return s.listener.Close()
|
return server.listener.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) GetPort() int {
|
func (server *TestEmailServer) GetPort() int {
|
||||||
return s.port
|
return server.port
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) GetEmails() []TestEmail {
|
func (server *TestEmailServer) GetEmails() []TestEmail {
|
||||||
s.mu.RLock()
|
server.mu.RLock()
|
||||||
defer s.mu.RUnlock()
|
defer server.mu.RUnlock()
|
||||||
return s.emails
|
return server.emails
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) ClearEmails() {
|
func (server *TestEmailServer) ClearEmails() {
|
||||||
s.mu.Lock()
|
server.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer server.mu.Unlock()
|
||||||
s.emails = make([]TestEmail, 0)
|
server.emails = make([]TestEmail, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) SetShouldFail(shouldFail bool) {
|
func (server *TestEmailServer) SetShouldFail(shouldFail bool) {
|
||||||
s.shouldFail = shouldFail
|
server.shouldFail = shouldFail
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) SetDelay(delay time.Duration) {
|
func (server *TestEmailServer) SetDelay(delay time.Duration) {
|
||||||
s.delay = delay
|
server.delay = delay
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) GetEmailCount() int {
|
func (server *TestEmailServer) GetEmailCount() int {
|
||||||
s.mu.RLock()
|
server.mu.RLock()
|
||||||
defer s.mu.RUnlock()
|
defer server.mu.RUnlock()
|
||||||
return len(s.emails)
|
return len(server.emails)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) GetLastEmail() *TestEmail {
|
func (server *TestEmailServer) GetLastEmail() *TestEmail {
|
||||||
s.mu.RLock()
|
server.mu.RLock()
|
||||||
defer s.mu.RUnlock()
|
defer server.mu.RUnlock()
|
||||||
if len(s.emails) == 0 {
|
if len(server.emails) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &s.emails[len(s.emails)-1]
|
return &server.emails[len(server.emails)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestEmailServer) WaitForEmails(count int, timeout time.Duration) bool {
|
func (server *TestEmailServer) WaitForEmails(count int, timeout time.Duration) bool {
|
||||||
deadline := time.Now().Add(timeout)
|
deadline := time.Now().Add(timeout)
|
||||||
for time.Now().Before(deadline) {
|
for time.Now().Before(deadline) {
|
||||||
s.mu.RLock()
|
server.mu.RLock()
|
||||||
emailCount := len(s.emails)
|
emailCount := len(server.emails)
|
||||||
s.mu.RUnlock()
|
server.mu.RUnlock()
|
||||||
if emailCount >= count {
|
if emailCount >= count {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -338,33 +339,33 @@ func NewTestEmailBuilder() *TestEmailBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TestEmailBuilder) From(from string) *TestEmailBuilder {
|
func (builder *TestEmailBuilder) From(from string) *TestEmailBuilder {
|
||||||
b.email.From = from
|
builder.email.From = from
|
||||||
return b
|
return builder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TestEmailBuilder) To(to string) *TestEmailBuilder {
|
func (builder *TestEmailBuilder) To(to string) *TestEmailBuilder {
|
||||||
b.email.To = []string{to}
|
builder.email.To = []string{to}
|
||||||
return b
|
return builder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TestEmailBuilder) Subject(subject string) *TestEmailBuilder {
|
func (builder *TestEmailBuilder) Subject(subject string) *TestEmailBuilder {
|
||||||
b.email.Subject = subject
|
builder.email.Subject = subject
|
||||||
return b
|
return builder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TestEmailBuilder) Body(body string) *TestEmailBuilder {
|
func (builder *TestEmailBuilder) Body(body string) *TestEmailBuilder {
|
||||||
b.email.Body = body
|
builder.email.Body = body
|
||||||
return b
|
return builder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TestEmailBuilder) Header(key, value string) *TestEmailBuilder {
|
func (builder *TestEmailBuilder) Header(key, value string) *TestEmailBuilder {
|
||||||
b.email.Headers[key] = value
|
builder.email.Headers[key] = value
|
||||||
return b
|
return builder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TestEmailBuilder) Build() *TestEmail {
|
func (builder *TestEmailBuilder) Build() *TestEmail {
|
||||||
return b.email
|
return builder.email
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestEmailMatcher struct{}
|
type TestEmailMatcher struct{}
|
||||||
@@ -418,9 +419,9 @@ func (m *TestEmailMatcher) MatchEmail(email *TestEmail, criteria map[string]any)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *TestEmailMatcher) FindEmail(emails []TestEmail, criteria map[string]any) *TestEmail {
|
func (m *TestEmailMatcher) FindEmail(emails []TestEmail, criteria map[string]any) *TestEmail {
|
||||||
for i := range emails {
|
for idx := range emails {
|
||||||
if m.MatchEmail(&emails[i], criteria) {
|
if m.MatchEmail(&emails[idx], criteria) {
|
||||||
return &emails[i]
|
return &emails[idx]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -428,8 +429,8 @@ func (m *TestEmailMatcher) FindEmail(emails []TestEmail, criteria map[string]any
|
|||||||
|
|
||||||
func (m *TestEmailMatcher) CountMatchingEmails(emails []TestEmail, criteria map[string]any) int {
|
func (m *TestEmailMatcher) CountMatchingEmails(emails []TestEmail, criteria map[string]any) int {
|
||||||
count := 0
|
count := 0
|
||||||
for i := range emails {
|
for idx := range emails {
|
||||||
if m.MatchEmail(&emails[i], criteria) {
|
if m.MatchEmail(&emails[idx], criteria) {
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -515,11 +516,11 @@ func GetSMTPSenderFromEnv(t *testing.T) *SMTPSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
address := net.JoinHostPort(sender.Host, strconv.Itoa(sender.Port))
|
address := net.JoinHostPort(sender.Host, strconv.Itoa(sender.Port))
|
||||||
connexion, err := net.DialTimeout("tcp", address, 3*time.Second)
|
connection, err := net.DialTimeout("tcp", address, 3*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Skipf("Skipping SMTP integration tests: unable to reach %s: %v", address, err)
|
t.Skipf("Skipping SMTP integration tests: unable to reach %s: %v", address, err)
|
||||||
}
|
}
|
||||||
connexion.Close()
|
connection.Close()
|
||||||
|
|
||||||
return sender
|
return sender
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user