From 3d1e6cfa17d4ef2277433411fdcaf26072eb524c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 28 May 2023 02:50:55 +0200 Subject: [PATCH] Tests[ListSubscribedChannelMessages] --- scnserver/logic/permissions.go | 1 + scnserver/test/channel_test.go | 106 +++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/scnserver/logic/permissions.go b/scnserver/logic/permissions.go index bcae016..47e9254 100644 --- a/scnserver/logic/permissions.go +++ b/scnserver/logic/permissions.go @@ -52,6 +52,7 @@ func (ac *AppContext) CheckPermissionChanMessagesRead(channel models.Channel) *g if !sub.Confirmed { return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)) } + return nil // subscribed channel } } diff --git a/scnserver/test/channel_test.go b/scnserver/test/channel_test.go index da94527..8f145e7 100644 --- a/scnserver/test/channel_test.go +++ b/scnserver/test/channel_test.go @@ -560,6 +560,112 @@ func TestListChannelMessages(t *testing.T) { } } +func TestListSubscribedChannelMessages(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"` + } + + type chanobj struct { + ChannelId string `json:"channel_id"` + DescriptionName string `json:"description_name"` + DisplayName string `json:"display_name"` + InternalName string `json:"internal_name"` + MessagesSent int `json:"messages_sent"` + OwnerUserId string `json:"owner_user_id"` + SubscribeKey string `json:"subscribe_key"` + Subscription struct { + ChannelId string `json:"channel_id"` + ChannelInternalName string `json:"channel_internal_name"` + ChannelOwnerUserId string `json:"channel_owner_user_id"` + Confirmed bool `json:"confirmed"` + SubscriberUserId string `json:"subscriber_user_id"` + SubscriptionId string `json:"subscription_id"` + TimestampCreated string `json:"timestamp_created"` + } `json:"subscription"` + TimestampCreated string `json:"timestamp_created"` + TimestampLastsent string `json:"timestamp_lastsent"` + } + + type chanlist struct { + Channels []chanobj `json:"channels"` + } + + clist := tt.RequestAuthGet[chanlist](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data.User[16].UID)) + + chan1 := langext.ArrFirstOrNil(clist.Channels, func(v chanobj) bool { return v.InternalName == "chan1" }) + chan2 := langext.ArrFirstOrNil(clist.Channels, func(v chanobj) bool { return v.InternalName == "chan2" }) + chan3 := langext.ArrFirstOrNil(clist.Channels, func(v chanobj) bool { return v.InternalName == "chan3" }) + + { + sub1 := tt.RequestAuthPost[gin.H](t, data.User[1].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?chan_subscribe_key=%s", data.User[1].UID, chan1.SubscribeKey), gin.H{ + "channel_owner_user_id": data.User[16].UID, + "channel_internal_name": "chan1", + }) + sub2 := tt.RequestAuthPost[gin.H](t, data.User[1].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?chan_subscribe_key=%s", data.User[1].UID, chan2.SubscribeKey), gin.H{ + "channel_owner_user_id": data.User[16].UID, + "channel_internal_name": "chan2", + }) + sub3 := tt.RequestAuthPost[gin.H](t, data.User[1].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?chan_subscribe_key=%s", data.User[1].UID, chan3.SubscribeKey), gin.H{ + "channel_owner_user_id": data.User[16].UID, + "channel_internal_name": "chan3", + }) + + tt.RequestAuthPatch[gin.H](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions/%s", data.User[16].UID, sub1["subscription_id"]), gin.H{ + "confirmed": true, + }) + tt.RequestAuthPatch[gin.H](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions/%s", data.User[16].UID, sub2["subscription_id"]), gin.H{ + "confirmed": true, + }) + tt.RequestAuthPatch[gin.H](t, data.User[16].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions/%s", data.User[16].UID, sub3["subscription_id"]), gin.H{ + "confirmed": true, + }) + + } + + { + msgList0 := tt.RequestAuthGet[mglist](t, data.User[1].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s/messages", data.User[16].UID, chan1.ChannelId)) + tt.AssertEqual(t, "msgList.len", 8, len(msgList0.Messages)) + tt.AssertEqual(t, "PageSize", 16, msgList0.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 11", msgList0.Messages[0].Title) + } + + { + msgList0 := tt.RequestAuthGet[mglist](t, data.User[1].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s/messages", data.User[16].UID, chan2.ChannelId)) + tt.AssertEqual(t, "msgList.len", 10, len(msgList0.Messages)) + tt.AssertEqual(t, "PageSize", 16, msgList0.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 23", msgList0.Messages[0].Title) + } + + { + msgList0 := tt.RequestAuthGet[mglist](t, data.User[1].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s/messages", data.User[16].UID, chan3.ChannelId)) + tt.AssertEqual(t, "msgList.len", 5, len(msgList0.Messages)) + tt.AssertEqual(t, "PageSize", 16, msgList0.PageSize) + tt.AssertEqual(t, "msgList[0]", "Lorem Ipsum 20", msgList0.Messages[0].Title) + } +} + func TestListChannelSubscriptions(t *testing.T) { t.SkipNow() //TODO }