1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-11-05 07:20:26 +01:00
This commit is contained in:
binwiederhier 2024-09-29 13:12:51 -04:00
parent fc3624cd50
commit d38c149263
7 changed files with 55 additions and 19 deletions

View file

@ -1277,6 +1277,7 @@ func TestServer_PublishEmailNoMailer_Fail(t *testing.T) {
func TestServer_PublishAndExpungeTopicAfter16Hours(t *testing.T) {
t.Parallel()
s := newTestServer(t, newTestConfig(t))
defer s.messageCache.Close()
subFn := func(v *visitor, msg *message) error {
return nil
@ -1288,13 +1289,22 @@ func TestServer_PublishAndExpungeTopicAfter16Hours(t *testing.T) {
})
require.Equal(t, 200, response.Code)
waitFor(t, func() bool {
s.mu.Lock()
tp, exists := s.topics["mytopic"]
s.mu.Unlock()
if !exists {
return false
}
// .lastAccess set in t.Publish() -> t.Keepalive() in Goroutine
s.topics["mytopic"].mu.RLock()
defer s.topics["mytopic"].mu.RUnlock()
return s.topics["mytopic"].lastAccess.Unix() >= time.Now().Unix()-2 &&
s.topics["mytopic"].lastAccess.Unix() <= time.Now().Unix()+2
tp.mu.RLock()
defer tp.mu.RUnlock()
return tp.lastAccess.Unix() >= time.Now().Unix()-2 &&
tp.lastAccess.Unix() <= time.Now().Unix()+2
})
// Hack!
time.Sleep(time.Second)
// Topic won't get pruned
s.execManager()
require.NotNil(t, s.topics["mytopic"])

View file

@ -110,7 +110,7 @@ func formatMail(baseURL, senderIP, from, to string, m *message) (string, error)
if trailer != "" {
message += "\n\n" + trailer
}
date := time.Unix(m.Time, 0).Format(time.RFC1123Z)
date := time.Unix(m.Time, 0).UTC().Format(time.RFC1123Z)
subject = mime.BEncoding.Encode("utf-8", subject)
body := `From: "{shortTopicURL}" <{from}>
To: {to}

View file

@ -15,6 +15,7 @@ func TestFormatMail_Basic(t *testing.T) {
})
expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
To: phil@example.com
Date: Fri, 24 Dec 2021 21:43:24 +0000
Subject: A simple message
Content-Type: text/plain; charset="utf-8"
@ -36,6 +37,7 @@ func TestFormatMail_JustEmojis(t *testing.T) {
})
expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
To: phil@example.com
Date: Fri, 24 Dec 2021 21:43:24 +0000
Subject: =?utf-8?b?8J+YgCBBIHNpbXBsZSBtZXNzYWdl?=
Content-Type: text/plain; charset="utf-8"
@ -57,6 +59,7 @@ func TestFormatMail_JustOtherTags(t *testing.T) {
})
expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
To: phil@example.com
Date: Fri, 24 Dec 2021 21:43:24 +0000
Subject: A simple message
Content-Type: text/plain; charset="utf-8"
@ -80,6 +83,7 @@ func TestFormatMail_JustPriority(t *testing.T) {
})
expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
To: phil@example.com
Date: Fri, 24 Dec 2021 21:43:24 +0000
Subject: A simple message
Content-Type: text/plain; charset="utf-8"
@ -103,6 +107,7 @@ func TestFormatMail_UTF8Subject(t *testing.T) {
})
expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
To: phil@example.com
Date: Fri, 24 Dec 2021 21:43:24 +0000
Subject: =?utf-8?b?IDo6IEEgbm90IHNvIHNpbXBsZSB0aXRsZSDDtsOkw7zDnyDCoUhvbGEsIHNl?= =?utf-8?b?w7FvciE=?=
Content-Type: text/plain; charset="utf-8"
@ -126,6 +131,7 @@ func TestFormatMail_WithAllTheThings(t *testing.T) {
})
expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
To: phil@example.com
Date: Fri, 24 Dec 2021 21:43:24 +0000
Subject: =?utf-8?b?4pqg77iPIPCfkoAgT2ggbm8g8J+ZiCBUaGlzIGlzIGEgbWVzc2FnZSBhY3Jv?= =?utf-8?b?c3MgbXVsdGlwbGUgbGluZXM=?=
Content-Type: text/plain; charset="utf-8"

View file

@ -70,19 +70,18 @@ func (b *smtpBackend) Counts() (total int64, success int64, failure int64) {
// smtpSession is returned after EHLO.
type smtpSession struct {
backend *smtpBackend
conn *smtp.Conn
topic string
token string
mu sync.Mutex
basic_auth string
backend *smtpBackend
conn *smtp.Conn
topic string
token string // If email address contains token, e.g. topic+token@domain
basicAuth string // If SMTP AUTH PLAIN was used
mu sync.Mutex
}
func (s *smtpSession) AuthPlain(username, password string) error {
logem(s.conn).Field("smtp_username", username).Debug("AUTH PLAIN (with username %s)", username)
basic_auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
s.mu.Lock()
s.basic_auth = basic_auth
s.basicAuth = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
s.mu.Unlock()
return nil
}
@ -203,8 +202,8 @@ func (s *smtpSession) publishMessage(m *message) error {
}
if s.token != "" {
req.Header.Add("Authorization", "Bearer "+s.token)
} else if s.basic_auth != "" {
req.Header.Add("Authorization", "Basic "+s.basic_auth)
} else if s.basicAuth != "" {
req.Header.Add("Authorization", "Basic "+s.basicAuth)
}
rr := httptest.NewRecorder()
s.backend.handler(rr, req)
@ -222,7 +221,7 @@ func (s *smtpSession) Reset() {
func (s *smtpSession) Logout() error {
s.mu.Lock()
s.basic_auth = ""
s.basicAuth = ""
s.mu.Unlock()
return nil
}

View file

@ -10,8 +10,6 @@ import (
)
func TestTopic_CancelSubscribersExceptUser(t *testing.T) {
t.Parallel()
subFn := func(v *visitor, msg *message) error {
return nil
}