From 2053b8f07f74281052350ea66909add1f6a2a6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 28 May 2023 02:27:15 +0200 Subject: [PATCH] Tests[ListMessages, ListMessagesPaginated, ListMessagesPaginatedInvalid] --- scnserver/api/handler/api.go | 4 +- scnserver/test/message_test.go | 145 ++++++++++++++++++++++++++++++++- scnserver/test/util/factory.go | 25 ++++++ 3 files changed, 171 insertions(+), 3 deletions(-) diff --git a/scnserver/api/handler/api.go b/scnserver/api/handler/api.go index 9c3bbc3..fea4947 100644 --- a/scnserver/api/handler/api.go +++ b/scnserver/api/handler/api.go @@ -890,7 +890,7 @@ func (h APIHandler) ListChannelMessages(g *gin.Context) ginresp.HTTPResponse { tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, "")) if err != nil { - return ginresp.APIError(g, 500, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err) + return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err) } filter := models.MessageFilter{ @@ -1379,7 +1379,7 @@ func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse { tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, "")) if err != nil { - return ginresp.APIError(g, 500, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err) + return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err) } err = h.database.UpdateUserLastRead(ctx, userid) diff --git a/scnserver/test/message_test.go b/scnserver/test/message_test.go index 7c8bb31..dfc5d94 100644 --- a/scnserver/test/message_test.go +++ b/scnserver/test/message_test.go @@ -185,4 +185,147 @@ func TestGetMessageFull(t *testing.T) { tt.AssertEqual(t, "msg.timestamp", time.Unix(ts, 0).In(timeext.TimezoneBerlin).Format(time.RFC3339Nano), msgIn["timestamp"]) } -//TODO test pagination +func TestListMessages(t *testing.T) { + ws, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + data := tt.InitDefaultData(t, ws) + + type msg struct { + ChannelId string `json:"channel_id"` + ChannelInternalName string `json:"channel_internal_name"` + Content string `json:"content"` + MessageId string `json:"message_id"` + OwnerUserId string `json:"owner_user_id"` + Priority int `json:"priority"` + SenderIp string `json:"sender_ip"` + SenderName string `json:"sender_name"` + SenderUserId string `json:"sender_user_id"` + Timestamp string `json:"timestamp"` + Title string `json:"title"` + Trimmed bool `json:"trimmed"` + UsrMessageId string `json:"usr_message_id"` + } + type mglist struct { + Messages []msg `json:"messages"` + } + + msgList := tt.RequestAuthGet[mglist](t, data.User[7].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages")) + tt.AssertEqual(t, "msgList.len", 6, len(msgList.Messages)) + tt.AssertEqual(t, "msgList[0]", "Server outage status", msgList.Messages[0].Title) + tt.AssertEqual(t, "msgList[1]", "Server maintenance reminder", msgList.Messages[1].Title) + tt.AssertEqual(t, "msgList[2]", "Server security alert", msgList.Messages[2].Title) + tt.AssertEqual(t, "msgList[3]", "Server traffic warning", msgList.Messages[3].Title) + tt.AssertEqual(t, "msgList[4]", "New server release update", msgList.Messages[4].Title) + tt.AssertEqual(t, "msgList[5]", "Server outage resolution update", msgList.Messages[5].Title) +} + +func TestListMessagesPaginated(t *testing.T) { + ws, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + data := tt.InitDefaultData(t, ws) + + type msg struct { + ChannelId string `json:"channel_id"` + ChannelInternalName string `json:"channel_internal_name"` + Content string `json:"content"` + MessageId string `json:"message_id"` + OwnerUserId string `json:"owner_user_id"` + Priority int `json:"priority"` + SenderIp string `json:"sender_ip"` + SenderName string `json:"sender_name"` + SenderUserId string `json:"sender_user_id"` + Timestamp string `json:"timestamp"` + Title string `json:"title"` + Trimmed bool `json:"trimmed"` + UsrMessageId string `json:"usr_message_id"` + } + type mglist struct { + Messages []msg `json:"messages"` + NPT string `json:"next_page_token"` + PageSize int `json:"page_size"` + } + { + msgList0 := tt.RequestAuthGet[mglist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages")) + tt.AssertEqual(t, "msgList.len", 16, len(msgList0.Messages)) + tt.AssertEqual(t, "msgList.len", 16, msgList0.PageSize) + } + npt := "" + { + msgList1 := tt.RequestAuthGet[mglist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages?page_size=%d&next_page_token=%s", 10, "@start")) + tt.AssertEqual(t, "msgList.len", 10, len(msgList1.Messages)) + tt.AssertEqual(t, "msgList.PageSize", 10, msgList1.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 23", msgList1.Messages[0].Title) + } + { + msgList1 := tt.RequestAuthGet[mglist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages?page_size=%d&next_page_token=%s", 10, "@START")) + tt.AssertEqual(t, "msgList.len", 10, len(msgList1.Messages)) + tt.AssertEqual(t, "msgList.PageSize", 10, msgList1.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 23", msgList1.Messages[0].Title) + } + { + msgList1 := tt.RequestAuthGet[mglist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages?page_size=%d&next_page_token=%s", 10, "")) + tt.AssertEqual(t, "msgList.len", 10, len(msgList1.Messages)) + tt.AssertEqual(t, "msgList.PageSize", 10, msgList1.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 23", msgList1.Messages[0].Title) + npt = msgList1.NPT + } + { + msgList2 := tt.RequestAuthGet[mglist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages?page_size=%d&next_page_token=%s", 10, npt)) + tt.AssertEqual(t, "msgList.len", 10, len(msgList2.Messages)) + tt.AssertEqual(t, "msgList.PageSize", 10, msgList2.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 13", msgList2.Messages[0].Title) + npt = msgList2.NPT + } + { + msgList3 := tt.RequestAuthGet[mglist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages?page_size=%d&next_page_token=%s", 10, npt)) + tt.AssertEqual(t, "msgList.len", 3, len(msgList3.Messages)) + tt.AssertEqual(t, "msgList.PageSize", 10, msgList3.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 03", msgList3.Messages[0].Title) + tt.AssertEqual(t, "msgList[0]", "@end", msgList3.NPT) + npt = msgList3.NPT + } +} + +func TestListMessagesPaginatedInvalid(t *testing.T) { + ws, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + data := tt.InitDefaultData(t, ws) + + tt.RequestAuthGetShouldFail(t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages?page_size=%d&next_page_token=%s", 10, "INVALID"), 400, apierr.PAGETOKEN_ERROR) +} + +func TestListMessagesZeroPagesize(t *testing.T) { + ws, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + data := tt.InitDefaultData(t, ws) + + type msg struct { + ChannelId string `json:"channel_id"` + ChannelInternalName string `json:"channel_internal_name"` + Content string `json:"content"` + MessageId string `json:"message_id"` + OwnerUserId string `json:"owner_user_id"` + Priority int `json:"priority"` + SenderIp string `json:"sender_ip"` + SenderName string `json:"sender_name"` + SenderUserId string `json:"sender_user_id"` + Timestamp string `json:"timestamp"` + Title string `json:"title"` + Trimmed bool `json:"trimmed"` + UsrMessageId string `json:"usr_message_id"` + } + type mglist struct { + Messages []msg `json:"messages"` + NPT string `json:"next_page_token"` + PageSize int `json:"page_size"` + } + + msgList1 := tt.RequestAuthGet[mglist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages?page_size=%d&next_page_token=%s", 0, "@start")) + tt.AssertEqual(t, "msgList.len", 1, len(msgList1.Messages)) + tt.AssertEqual(t, "msgList.PageSize", 1, msgList1.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 23", msgList1.Messages[0].Title) +} diff --git a/scnserver/test/util/factory.go b/scnserver/test/util/factory.go index 9a33d76..dd677eb 100644 --- a/scnserver/test/util/factory.go +++ b/scnserver/test/util/factory.go @@ -90,6 +90,7 @@ var userExamples = []userex{ {13, false, "EmptyUser", "", "", "", "", ""}, {14, true, "ChanTester1", "StarfireXX", "1.x", "IOS", "FCM_TOK_EX_012", ""}, {15, true, "ChanTester2", "StarfireXX", "1.x", "IOS", "FCM_TOK_EX_013", ""}, + {16, true, "PagTester1", "StarfireXX", "1.x", "ANDROID", "FCM_TOK_EX_016", ""}, } var clientExamples = []clientex{ @@ -277,6 +278,30 @@ var messageExamples = []msgex{ {15, "chan_other_nosub", "", P0, SKEY, "Account Suspended", "Please contact us", 0}, {15, "chan_other_request", "", P0, SKEY, "Invitation to Beta Test", "", 0}, {15, "chan_other_accepted", "", P0, SKEY, "New Blog Post", "Congratulations on your promotion! We are proud", 0}, + + {16, "Chan1", "", P2, SKEY, "Lorem Ipsum 01", Lipsum(30001, 1), 0}, + {16, "Chan2", "", P0, SKEY, "Lorem Ipsum 02", Lipsum(30002, 1), 0}, + {16, "Chan1", "", P2, SKEY, "Lorem Ipsum 03", Lipsum(30003, 1), 0}, + {16, "Chan1", "", P0, SKEY, "Lorem Ipsum 04", Lipsum(30004, 1), 0}, + {16, "Chan1", "", P2, SKEY, "Lorem Ipsum 05", Lipsum(30005, 1), 0}, + {16, "Chan1", "", P1, AKEY, "Lorem Ipsum 06", Lipsum(30006, 1), 0}, + {16, "Chan2", "", P1, AKEY, "Lorem Ipsum 07", Lipsum(30007, 1), 0}, + {16, "Chan2", "", P0, SKEY, "Lorem Ipsum 08", Lipsum(30008, 1), 0}, + {16, "Chan1", "", PX, AKEY, "Lorem Ipsum 09", Lipsum(30009, 1), 0}, + {16, "Chan1", "", P0, SKEY, "Lorem Ipsum 10", Lipsum(30010, 1), 0}, + {16, "Chan1", "", P2, SKEY, "Lorem Ipsum 11", Lipsum(30011, 1), 0}, + {16, "Chan2", "", PX, AKEY, "Lorem Ipsum 12", Lipsum(30012, 1), 0}, + {16, "Chan2", "", P2, SKEY, "Lorem Ipsum 13", Lipsum(30013, 1), 0}, + {16, "Chan2", "", P2, SKEY, "Lorem Ipsum 14", Lipsum(30014, 1), 0}, + {16, "Chan2", "", P0, SKEY, "Lorem Ipsum 15", Lipsum(30015, 1), 0}, + {16, "Chan3", "", P0, SKEY, "Lorem Ipsum 16", Lipsum(30016, 1), 0}, + {16, "Chan3", "", P0, SKEY, "Lorem Ipsum 17", Lipsum(30017, 1), 0}, + {16, "Chan3", "", P2, SKEY, "Lorem Ipsum 18", Lipsum(30018, 1), 0}, + {16, "Chan3", "", PX, AKEY, "Lorem Ipsum 19", Lipsum(30019, 1), 0}, + {16, "Chan3", "", P2, SKEY, "Lorem Ipsum 20", Lipsum(30020, 1), 0}, + {16, "Chan2", "", P2, SKEY, "Lorem Ipsum 21", Lipsum(30021, 1), 0}, + {16, "Chan2", "", P0, SKEY, "Lorem Ipsum 22", Lipsum(30022, 1), 0}, + {16, "Chan2", "", P0, SKEY, "Lorem Ipsum 23", Lipsum(30023, 1), 0}, } type DefData struct {