ntfy/server/cache_test.go

223 lines
7.1 KiB
Go
Raw Normal View History

2021-12-07 17:45:15 +01:00
package server
import (
2022-01-13 21:17:30 +01:00
"github.com/stretchr/testify/require"
2021-12-07 17:45:15 +01:00
"testing"
"time"
)
func testCacheMessages(t *testing.T, c cache) {
m1 := newDefaultMessage("mytopic", "my message")
m1.Time = 1
m2 := newDefaultMessage("mytopic", "my other message")
m2.Time = 2
2022-01-13 21:17:30 +01:00
require.Nil(t, c.AddMessage(m1))
require.Nil(t, c.AddMessage(newDefaultMessage("example", "my example message")))
require.Nil(t, c.AddMessage(m2))
2021-12-07 17:45:15 +01:00
// Adding invalid
2022-01-13 21:17:30 +01:00
require.Equal(t, errUnexpectedMessageType, c.AddMessage(newKeepaliveMessage("mytopic"))) // These should not be added!
require.Equal(t, errUnexpectedMessageType, c.AddMessage(newOpenMessage("example"))) // These should not be added!
2021-12-07 17:45:15 +01:00
// mytopic: count
count, err := c.MessageCount("mytopic")
2022-01-13 21:17:30 +01:00
require.Nil(t, err)
require.Equal(t, 2, count)
2021-12-07 17:45:15 +01:00
// mytopic: since all
messages, _ := c.Messages("mytopic", sinceAllMessages, false)
2022-01-13 21:17:30 +01:00
require.Equal(t, 2, len(messages))
require.Equal(t, "my message", messages[0].Message)
require.Equal(t, "mytopic", messages[0].Topic)
require.Equal(t, messageEvent, messages[0].Event)
require.Equal(t, "", messages[0].Title)
require.Equal(t, 0, messages[0].Priority)
require.Nil(t, messages[0].Tags)
require.Equal(t, "my other message", messages[1].Message)
2021-12-07 17:45:15 +01:00
// mytopic: since none
messages, _ = c.Messages("mytopic", sinceNoMessages, false)
2022-01-13 21:17:30 +01:00
require.Empty(t, messages)
2021-12-07 17:45:15 +01:00
// mytopic: since 2
messages, _ = c.Messages("mytopic", sinceTime(time.Unix(2, 0)), false)
2022-01-13 21:17:30 +01:00
require.Equal(t, 1, len(messages))
require.Equal(t, "my other message", messages[0].Message)
2021-12-07 17:45:15 +01:00
// example: count
count, err = c.MessageCount("example")
2022-01-13 21:17:30 +01:00
require.Nil(t, err)
require.Equal(t, 1, count)
2021-12-07 17:45:15 +01:00
// example: since all
messages, _ = c.Messages("example", sinceAllMessages, false)
2022-01-13 21:17:30 +01:00
require.Equal(t, "my example message", messages[0].Message)
2021-12-07 17:45:15 +01:00
// non-existing: count
count, err = c.MessageCount("doesnotexist")
2022-01-13 21:17:30 +01:00
require.Nil(t, err)
require.Equal(t, 0, count)
2021-12-07 17:45:15 +01:00
// non-existing: since all
messages, _ = c.Messages("doesnotexist", sinceAllMessages, false)
2022-01-13 21:17:30 +01:00
require.Empty(t, messages)
2021-12-07 17:45:15 +01:00
}
2021-12-09 04:57:31 +01:00
func testCacheTopics(t *testing.T, c cache) {
2022-01-13 21:17:30 +01:00
require.Nil(t, c.AddMessage(newDefaultMessage("topic1", "my example message")))
require.Nil(t, c.AddMessage(newDefaultMessage("topic2", "message 1")))
require.Nil(t, c.AddMessage(newDefaultMessage("topic2", "message 2")))
require.Nil(t, c.AddMessage(newDefaultMessage("topic2", "message 3")))
2021-12-09 04:57:31 +01:00
topics, err := c.Topics()
if err != nil {
t.Fatal(err)
}
2022-01-13 21:17:30 +01:00
require.Equal(t, 2, len(topics))
require.Equal(t, "topic1", topics["topic1"].ID)
require.Equal(t, "topic2", topics["topic2"].ID)
2021-12-09 04:57:31 +01:00
}
func testCachePrune(t *testing.T, c cache) {
m1 := newDefaultMessage("mytopic", "my message")
m1.Time = 1
m2 := newDefaultMessage("mytopic", "my other message")
m2.Time = 2
m3 := newDefaultMessage("another_topic", "and another one")
m3.Time = 1
2022-01-13 21:17:30 +01:00
require.Nil(t, c.AddMessage(m1))
require.Nil(t, c.AddMessage(m2))
require.Nil(t, c.AddMessage(m3))
require.Nil(t, c.Prune(time.Unix(2, 0)))
2021-12-09 04:57:31 +01:00
count, err := c.MessageCount("mytopic")
2022-01-13 21:17:30 +01:00
require.Nil(t, err)
require.Equal(t, 1, count)
2021-12-09 04:57:31 +01:00
count, err = c.MessageCount("another_topic")
2022-01-13 21:17:30 +01:00
require.Nil(t, err)
require.Equal(t, 0, count)
2021-12-09 04:57:31 +01:00
messages, err := c.Messages("mytopic", sinceAllMessages, false)
2022-01-13 21:17:30 +01:00
require.Nil(t, err)
require.Equal(t, 1, len(messages))
require.Equal(t, "my other message", messages[0].Message)
2021-12-09 04:57:31 +01:00
}
2021-12-07 17:45:15 +01:00
func testCacheMessagesTagsPrioAndTitle(t *testing.T, c cache) {
m := newDefaultMessage("mytopic", "some message")
m.Tags = []string{"tag1", "tag2"}
m.Priority = 5
m.Title = "some title"
2022-01-13 21:17:30 +01:00
require.Nil(t, c.AddMessage(m))
2021-12-07 17:45:15 +01:00
messages, _ := c.Messages("mytopic", sinceAllMessages, false)
2022-01-13 21:17:30 +01:00
require.Equal(t, []string{"tag1", "tag2"}, messages[0].Tags)
require.Equal(t, 5, messages[0].Priority)
require.Equal(t, "some title", messages[0].Title)
2021-12-07 17:45:15 +01:00
}
func testCacheMessagesScheduled(t *testing.T, c cache) {
m1 := newDefaultMessage("mytopic", "message 1")
m2 := newDefaultMessage("mytopic", "message 2")
m2.Time = time.Now().Add(time.Hour).Unix()
m3 := newDefaultMessage("mytopic", "message 3")
m3.Time = time.Now().Add(time.Minute).Unix() // earlier than m2!
m4 := newDefaultMessage("mytopic2", "message 4")
m4.Time = time.Now().Add(time.Minute).Unix()
2022-01-13 21:17:30 +01:00
require.Nil(t, c.AddMessage(m1))
require.Nil(t, c.AddMessage(m2))
require.Nil(t, c.AddMessage(m3))
messages, _ := c.Messages("mytopic", sinceAllMessages, false) // exclude scheduled
2022-01-13 21:17:30 +01:00
require.Equal(t, 1, len(messages))
require.Equal(t, "message 1", messages[0].Message)
messages, _ = c.Messages("mytopic", sinceAllMessages, true) // include scheduled
2022-01-13 21:17:30 +01:00
require.Equal(t, 3, len(messages))
require.Equal(t, "message 1", messages[0].Message)
require.Equal(t, "message 3", messages[1].Message) // Order!
require.Equal(t, "message 2", messages[2].Message)
messages, _ = c.MessagesDue()
2022-01-13 21:17:30 +01:00
require.Empty(t, messages)
}
func testCacheAttachments(t *testing.T, c cache) {
expires1 := time.Now().Add(-4 * time.Hour).Unix()
m := newDefaultMessage("mytopic", "flower for you")
m.ID = "m1"
m.Attachment = &attachment{
Name: "flower.jpg",
Type: "image/jpeg",
Size: 5000,
Expires: expires1,
URL: "https://ntfy.sh/file/AbDeFgJhal.jpg",
Owner: "1.2.3.4",
}
require.Nil(t, c.AddMessage(m))
expires2 := time.Now().Add(2 * time.Hour).Unix() // Future
m = newDefaultMessage("mytopic", "sending you a car")
m.ID = "m2"
m.Attachment = &attachment{
Name: "car.jpg",
Type: "image/jpeg",
Size: 10000,
Expires: expires2,
URL: "https://ntfy.sh/file/aCaRURL.jpg",
Owner: "1.2.3.4",
}
require.Nil(t, c.AddMessage(m))
expires3 := time.Now().Add(1 * time.Hour).Unix() // Future
m = newDefaultMessage("another-topic", "sending you another car")
m.ID = "m3"
m.Attachment = &attachment{
Name: "another-car.jpg",
Type: "image/jpeg",
Size: 20000,
Expires: expires3,
URL: "https://ntfy.sh/file/zakaDHFW.jpg",
Owner: "1.2.3.4",
}
require.Nil(t, c.AddMessage(m))
messages, err := c.Messages("mytopic", sinceAllMessages, false)
require.Nil(t, err)
require.Equal(t, 2, len(messages))
require.Equal(t, "flower for you", messages[0].Message)
require.Equal(t, "flower.jpg", messages[0].Attachment.Name)
require.Equal(t, "image/jpeg", messages[0].Attachment.Type)
require.Equal(t, int64(5000), messages[0].Attachment.Size)
require.Equal(t, expires1, messages[0].Attachment.Expires)
require.Equal(t, "https://ntfy.sh/file/AbDeFgJhal.jpg", messages[0].Attachment.URL)
require.Equal(t, "1.2.3.4", messages[0].Attachment.Owner)
require.Equal(t, "sending you a car", messages[1].Message)
require.Equal(t, "car.jpg", messages[1].Attachment.Name)
require.Equal(t, "image/jpeg", messages[1].Attachment.Type)
require.Equal(t, int64(10000), messages[1].Attachment.Size)
require.Equal(t, expires2, messages[1].Attachment.Expires)
require.Equal(t, "https://ntfy.sh/file/aCaRURL.jpg", messages[1].Attachment.URL)
require.Equal(t, "1.2.3.4", messages[1].Attachment.Owner)
size, err := c.AttachmentsSize("1.2.3.4")
require.Nil(t, err)
require.Equal(t, int64(30000), size)
size, err = c.AttachmentsSize("5.6.7.8")
require.Nil(t, err)
require.Equal(t, int64(0), size)
ids, err := c.AttachmentsExpired()
require.Nil(t, err)
require.Equal(t, []string{"m1"}, ids)
}