From 6b4971786f8d15a53ce905b74fdf7b44ceccd0c7 Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Sun, 12 Feb 2023 12:08:56 -0500 Subject: [PATCH] Fix intermittent test failure; add test for expiring messages after reservation removal --- server/server.go | 1 - server/server_account_test.go | 75 +++++++++++++++++++++++++++++++++++ server/server_test.go | 2 +- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/server/server.go b/server/server.go index e410f528..c96d8f79 100644 --- a/server/server.go +++ b/server/server.go @@ -39,7 +39,6 @@ import ( - tiers - api - tokens -- MEDIUM: Test for expiring messages after reservation removal - MEDIUM: Test new token endpoints & never-expiring token - LOW: UI: Flickering upgrade banner when logging in - LOW: get rid of reservation id, replace with DELETE X-Topic: ... diff --git a/server/server_account_test.go b/server/server_account_test.go index c5653055..21947645 100644 --- a/server/server_account_test.go +++ b/server/server_account_test.go @@ -8,6 +8,7 @@ import ( "heckel.io/ntfy/util" "io" "net/netip" + "path/filepath" "strings" "testing" "time" @@ -599,6 +600,80 @@ func TestAccount_Reservation_PublishByAnonymousFails(t *testing.T) { require.Equal(t, 403, rr.Code) } +func TestAccount_Reservation_Delete_Messages_And_Attachments(t *testing.T) { + conf := newTestConfigWithAuthFile(t) + conf.AuthDefault = user.PermissionReadWrite + s := newTestServer(t, conf) + + // Create user with tier + require.Nil(t, s.userManager.AddUser("phil", "mypass", user.RoleUser)) + require.Nil(t, s.userManager.AddTier(&user.Tier{ + Code: "pro", + MessageLimit: 20, + MessageExpiryDuration: time.Hour, + ReservationLimit: 2, + AttachmentTotalSizeLimit: 10000, + AttachmentFileSizeLimit: 10000, + AttachmentExpiryDuration: time.Hour, + AttachmentBandwidthLimit: 10000, + })) + require.Nil(t, s.userManager.ChangeTier("phil", "pro")) + + // Reserve two topics "mytopic1" and "mytopic2" + rr := request(t, s, "POST", "/v1/account/reservation", `{"topic": "mytopic1", "everyone":"deny-all"}`, map[string]string{ + "Authorization": util.BasicAuth("phil", "mypass"), + }) + require.Equal(t, 200, rr.Code) + + rr = request(t, s, "POST", "/v1/account/reservation", `{"topic": "mytopic2", "everyone":"deny-all"}`, map[string]string{ + "Authorization": util.BasicAuth("phil", "mypass"), + }) + require.Equal(t, 200, rr.Code) + + // Publish a message with attachment to each topic + rr = request(t, s, "POST", "/mytopic1?f=attach.txt", `Howdy`, map[string]string{ + "Authorization": util.BasicAuth("phil", "mypass"), + }) + require.Equal(t, 200, rr.Code) + m1 := toMessage(t, rr.Body.String()) + require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, m1.ID)) + + rr = request(t, s, "POST", "/mytopic2?f=attach.txt", `Howdy`, map[string]string{ + "Authorization": util.BasicAuth("phil", "mypass"), + }) + require.Equal(t, 200, rr.Code) + m2 := toMessage(t, rr.Body.String()) + require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, m2.ID)) + + // Delete reservation + rr = request(t, s, "DELETE", "/v1/account/reservation/mytopic1", ``, map[string]string{ + "X-Delete-Messages": "true", + "Authorization": util.BasicAuth("phil", "mypass"), + }) + require.Equal(t, 200, rr.Code) + + rr = request(t, s, "DELETE", "/v1/account/reservation/mytopic2", ``, map[string]string{ + "X-Delete-Messages": "false", + "Authorization": util.BasicAuth("phil", "mypass"), + }) + require.Equal(t, 200, rr.Code) + + // Verify that messages and attachments were deleted + time.Sleep(time.Second) + s.execManager() + + ms, err := s.messageCache.Messages("mytopic1", sinceAllMessages, false) + require.Nil(t, err) + require.Equal(t, 0, len(ms)) + require.NoFileExists(t, filepath.Join(s.config.AttachmentCacheDir, m1.ID)) + + ms, err = s.messageCache.Messages("mytopic2", sinceAllMessages, false) + require.Nil(t, err) + require.Equal(t, 1, len(ms)) + require.Equal(t, m2.ID, ms[0].ID) + require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, m2.ID)) +} + func TestAccount_Reservation_Add_Kills_Other_Subscribers(t *testing.T) { conf := newTestConfigWithAuthFile(t) conf.AuthDefault = user.PermissionReadWrite diff --git a/server/server_test.go b/server/server_test.go index 6fb614bf..375a5905 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -1339,7 +1339,7 @@ func TestServer_PublishWithTierBasedMessageLimitAndExpiry(t *testing.T) { } func TestServer_PublishAttachment(t *testing.T) { - content := util.RandomString(5000) // > 4096 + content := "text file!" + util.RandomString(4990) // > 4096 s := newTestServer(t, newTestConfig(t)) response := request(t, s, "PUT", "/mytopic", content, nil) msg := toMessage(t, response.Body.String())