diff --git a/server/api/apierr/enums.go b/server/api/apierr/enums.go index 6303670..1ff7709 100644 --- a/server/api/apierr/enums.go +++ b/server/api/apierr/enums.go @@ -26,6 +26,7 @@ const ( USR_MSG_ID_TOO_LONG APIError = 1204 TIMESTAMP_OUT_OF_RANGE APIError = 1205 SENDERNAME_TOO_LONG APIError = 1206 + CHANNEL_TOO_LONG APIError = 1207 USER_NOT_FOUND APIError = 1301 CLIENT_NOT_FOUND APIError = 1302 diff --git a/server/api/handler/api.go b/server/api/handler/api.go index 489cd68..d4588fa 100644 --- a/server/api/handler/api.go +++ b/server/api/handler/api.go @@ -2,12 +2,14 @@ package handler import ( "blackforestbytes.com/simplecloudnotifier/api/apierr" + hl "blackforestbytes.com/simplecloudnotifier/api/apihighlight" "blackforestbytes.com/simplecloudnotifier/common/ginresp" "blackforestbytes.com/simplecloudnotifier/db" "blackforestbytes.com/simplecloudnotifier/db/cursortoken" "blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/models" "database/sql" + "fmt" "github.com/gin-gonic/gin" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/mathext" @@ -658,6 +660,18 @@ func (h APIHandler) CreateChannel(g *gin.Context) ginresp.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err) } + user, err := h.database.GetUser(ctx, u.UserID) + if err == sql.ErrNoRows { + return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found", nil) + } + if err != nil { + return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query user", err) + } + + if len(channelName) > user.MaxChannelNameLength() { + return ginresp.SendAPIError(g, 400, apierr.CHANNEL_TOO_LONG, hl.CHANNEL, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil) + } + if channelExisting != nil { return ginresp.APIError(g, 409, apierr.CHANNEL_ALREADY_EXISTS, "Channel with this name already exists", nil) } diff --git a/server/api/handler/message.go b/server/api/handler/message.go index 68195a0..0e6f79a 100644 --- a/server/api/handler/message.go +++ b/server/api/handler/message.go @@ -178,7 +178,7 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex return ginresp.SendAPIError(g, 400, apierr.CONTENT_TOO_LONG, hl.CONTENT, fmt.Sprintf("Content too long (%d characters; max := %d characters)", len(*Content), user.MaxContentLength()), nil) } if len(channelName) > user.MaxChannelNameLength() { - return ginresp.SendAPIError(g, 400, apierr.CONTENT_TOO_LONG, hl.CHANNEL, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil) + return ginresp.SendAPIError(g, 400, apierr.CHANNEL_TOO_LONG, hl.CHANNEL, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil) } if SenderName != nil && len(*SenderName) > user.MaxSenderName() { return ginresp.SendAPIError(g, 400, apierr.SENDERNAME_TOO_LONG, hl.SENDER_NAME, fmt.Sprintf("SenderName too long (max %d characters)", user.MaxSenderName()), nil) diff --git a/server/test/send_test.go b/server/test/send_test.go index 0248ccb..8e3771d 100644 --- a/server/test/send_test.go +++ b/server/test/send_test.go @@ -1188,11 +1188,43 @@ func TestSendToManualChannel(t *testing.T) { } } -//TODO post to channel +func TestSendToTooLongChannel(t *testing.T) { + ws, stop := tt.StartSimpleWebserver(t) + defer stop() -//TODO post to newly-created-channel + baseUrl := "http://127.0.0.1:" + ws.Port -//TODO post to existing-channel + r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/users", gin.H{ + "agent_model": "DUMMY_PHONE", + "agent_version": "4X", + "client_type": "ANDROID", + "fcm_token": "DUMMY_FCM", + }) + + uid := int(r0["user_id"].(float64)) + sendtok := r0["send_key"].(string) + + tt.RequestPost[tt.Void](t, baseUrl, "/", gin.H{ + "user_key": sendtok, + "user_id": uid, + "title": "M3", + "channel": "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", + }) + + tt.RequestPost[tt.Void](t, baseUrl, "/", gin.H{ + "user_key": sendtok, + "user_id": uid, + "title": "M3", + "channel": "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", + }) + + tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ + "user_key": sendtok, + "user_id": uid, + "title": "M3", + "channel": "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901", + }, 400, apierr.CHANNEL_TOO_LONG) +} //TODO post to foreign channel via send-key