1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-27 12:12:28 +01:00

Fix delayed message sending from authenticated users, closes #679

This commit is contained in:
binwiederhier 2023-03-22 15:30:20 -04:00
parent b8836d674a
commit 502d0a0abd
3 changed files with 52 additions and 9 deletions

View file

@ -1135,3 +1135,9 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release
**Additional languages:** **Additional languages:**
* Swedish (thanks to [@hellbown](https://hosted.weblate.org/user/hellbown/)) * Swedish (thanks to [@hellbown](https://hosted.weblate.org/user/hellbown/))
## ntfy server v2.3.0 (UNRELEASED)
**Bug fixes + maintenance:**
* Fix delayed message sending from authenticated users ([#679](https://github.com/binwiederhier/ntfy/issues/679))

View file

@ -1541,7 +1541,7 @@ func (s *Server) sendDelayedMessages() error {
for _, m := range messages { for _, m := range messages {
var u *user.User var u *user.User
if s.userManager != nil && m.User != "" { if s.userManager != nil && m.User != "" {
u, err = s.userManager.User(m.User) u, err = s.userManager.UserByID(m.User)
if err != nil { if err != nil {
log.With(m).Err(err).Warn("Error sending delayed message") log.With(m).Err(err).Warn("Error sending delayed message")
continue continue

View file

@ -327,13 +327,10 @@ func TestServer_PublishNoCache(t *testing.T) {
func TestServer_PublishAt(t *testing.T) { func TestServer_PublishAt(t *testing.T) {
t.Parallel() t.Parallel()
c := newTestConfig(t) s := newTestServer(t, newTestConfig(t))
c.MinDelay = time.Second
c.DelayedSenderInterval = 100 * time.Millisecond
s := newTestServer(t, c)
response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{ response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
"In": "1s", "In": "1h",
}) })
require.Equal(t, 200, response.Code) require.Equal(t, 200, response.Code)
@ -341,22 +338,62 @@ func TestServer_PublishAt(t *testing.T) {
messages := toMessages(t, response.Body.String()) messages := toMessages(t, response.Body.String())
require.Equal(t, 0, len(messages)) require.Equal(t, 0, len(messages))
time.Sleep(time.Second) // Update message time to the past
require.Nil(t, s.sendDelayedMessages()) fakeTime := time.Now().Add(-10 * time.Second).Unix()
_, err := s.messageCache.db.Exec(`UPDATE messages SET time=?`, fakeTime)
require.Nil(t, err)
// Trigger delayed message sending
require.Nil(t, s.sendDelayedMessages())
response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil) response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
messages = toMessages(t, response.Body.String()) messages = toMessages(t, response.Body.String())
require.Equal(t, 1, len(messages)) require.Equal(t, 1, len(messages))
require.Equal(t, "a message", messages[0].Message) require.Equal(t, "a message", messages[0].Message)
require.Equal(t, netip.Addr{}, messages[0].Sender) // Never return the sender! require.Equal(t, netip.Addr{}, messages[0].Sender) // Never return the sender!
messages, err := s.messageCache.Messages("mytopic", sinceAllMessages, true) messages, err = s.messageCache.Messages("mytopic", sinceAllMessages, true)
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, 1, len(messages)) require.Equal(t, 1, len(messages))
require.Equal(t, "a message", messages[0].Message) require.Equal(t, "a message", messages[0].Message)
require.Equal(t, "9.9.9.9", messages[0].Sender.String()) // It's stored in the DB though! require.Equal(t, "9.9.9.9", messages[0].Sender.String()) // It's stored in the DB though!
} }
func TestServer_PublishAt_FromUser(t *testing.T) {
t.Parallel()
s := newTestServer(t, newTestConfigWithAuthFile(t))
require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleAdmin))
response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{
"Authorization": util.BasicAuth("phil", "phil"),
"In": "1h",
})
require.Equal(t, 200, response.Code)
// Message doesn't show up immediately
response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
messages := toMessages(t, response.Body.String())
require.Equal(t, 0, len(messages))
// Update message time to the past
fakeTime := time.Now().Add(-10 * time.Second).Unix()
_, err := s.messageCache.db.Exec(`UPDATE messages SET time=?`, fakeTime)
require.Nil(t, err)
// Trigger delayed message sending
require.Nil(t, s.sendDelayedMessages())
response = request(t, s, "GET", "/mytopic/json?poll=1", "", nil)
messages = toMessages(t, response.Body.String())
require.Equal(t, 1, len(messages))
require.Equal(t, fakeTime, messages[0].Time)
require.Equal(t, "a message", messages[0].Message)
messages, err = s.messageCache.Messages("mytopic", sinceAllMessages, true)
require.Nil(t, err)
require.Equal(t, 1, len(messages))
require.Equal(t, "a message", messages[0].Message)
require.True(t, strings.HasPrefix(messages[0].User, "u_"))
}
func TestServer_PublishAt_Expires(t *testing.T) { func TestServer_PublishAt_Expires(t *testing.T) {
s := newTestServer(t, newTestConfig(t)) s := newTestServer(t, newTestConfig(t))