From 674714f0f3928ac139c814e88fc0b9da551f9904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 30 Jul 2023 16:53:46 +0200 Subject: [PATCH] Return more data in `/users/{uid}` --- scnserver/api/handler/apiChannel.go | 4 +- scnserver/api/handler/message.go | 8 ++-- scnserver/models/user.go | 64 ++++++++++++++++----------- scnserver/test/user_test.go | 67 +++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 31 deletions(-) diff --git a/scnserver/api/handler/apiChannel.go b/scnserver/api/handler/apiChannel.go index 9a853a9..8692485 100644 --- a/scnserver/api/handler/apiChannel.go +++ b/scnserver/api/handler/apiChannel.go @@ -349,8 +349,8 @@ func (h APIHandler) UpdateChannel(g *gin.Context) ginresp.HTTPResponse { descName = langext.Ptr(strings.TrimSpace(*b.DescriptionName)) } - if descName != nil && len(*descName) > user.MaxChannelDescriptionNameLength() { - return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelNameLength()), nil) + if descName != nil && len(*descName) > user.MaxChannelDescriptionLength() { + return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelDescriptionLength()), nil) } err := h.database.UpdateChannelDescriptionName(ctx, u.ChannelID, descName) diff --git a/scnserver/api/handler/message.go b/scnserver/api/handler/message.go index 12d4348..eb2315f 100644 --- a/scnserver/api/handler/message.go +++ b/scnserver/api/handler/message.go @@ -172,11 +172,11 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex if len(strings.TrimSpace(channelInternalName)) == 0 { return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.CHANNEL_NAME_EMPTY, hl.CHANNEL, fmt.Sprintf("Channel internalname cannot be empty"), nil)) } - if SenderName != nil && len(*SenderName) > user.MaxSenderName() { - return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.SENDERNAME_TOO_LONG, hl.SENDER_NAME, fmt.Sprintf("SenderName too long (max %d characters)", user.MaxSenderName()), nil)) + if SenderName != nil && len(*SenderName) > user.MaxSenderNameLength() { + return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.SENDERNAME_TOO_LONG, hl.SENDER_NAME, fmt.Sprintf("SenderName too long (max %d characters)", user.MaxSenderNameLength()), nil)) } - if UserMessageID != nil && len(*UserMessageID) > user.MaxUserMessageID() { - return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.USR_MSG_ID_TOO_LONG, hl.USER_MESSAGE_ID, fmt.Sprintf("MessageID too long (max %d characters)", user.MaxUserMessageID()), nil)) + if UserMessageID != nil && len(*UserMessageID) > user.MaxUserMessageIDLength() { + return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.USR_MSG_ID_TOO_LONG, hl.USER_MESSAGE_ID, fmt.Sprintf("MessageID too long (max %d characters)", user.MaxUserMessageIDLength()), nil)) } if SendTimestamp != nil && mathext.Abs(*SendTimestamp-float64(time.Now().Unix())) > timeext.FromHours(user.MaxTimestampDiffHours()).Seconds() { return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.TIMESTAMP_OUT_OF_RANGE, hl.NONE, fmt.Sprintf("The timestamp mus be within %d hours of now()", user.MaxTimestampDiffHours()), nil)) diff --git a/scnserver/models/user.go b/scnserver/models/user.go index c406c5b..0fd7753 100644 --- a/scnserver/models/user.go +++ b/scnserver/models/user.go @@ -23,17 +23,24 @@ type User struct { func (u User) JSON() UserJSON { return UserJSON{ - UserID: u.UserID, - Username: u.Username, - TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano), - TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano), - TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano), - MessagesSent: u.MessagesSent, - QuotaUsed: u.QuotaUsedToday(), - QuotaPerDay: u.QuotaPerDay(), - QuotaRemaining: u.QuotaRemainingToday(), - IsPro: u.IsPro, - DefaultChannel: u.DefaultChannel(), + UserID: u.UserID, + Username: u.Username, + TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano), + TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano), + TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano), + MessagesSent: u.MessagesSent, + QuotaUsed: u.QuotaUsedToday(), + QuotaPerDay: u.QuotaPerDay(), + QuotaRemaining: u.QuotaRemainingToday(), + IsPro: u.IsPro, + DefaultChannel: u.DefaultChannel(), + MaxBodySize: u.MaxContentLength(), + MaxTitleLength: u.MaxTitleLength(), + DefaultPriority: u.DefaultPriority(), + MaxChannelNameLength: u.MaxChannelNameLength(), + MaxChannelDescriptionLength: u.MaxChannelDescriptionLength(), + MaxSenderNameLength: u.MaxSenderNameLength(), + MaxUserMessageIDLength: u.MaxUserMessageIDLength(), } } @@ -92,15 +99,15 @@ func (u User) MaxChannelNameLength() int { return 120 } -func (u User) MaxChannelDescriptionNameLength() int { +func (u User) MaxChannelDescriptionLength() int { return 300 } -func (u User) MaxSenderName() int { +func (u User) MaxSenderNameLength() int { return 120 } -func (u User) MaxUserMessageID() int { +func (u User) MaxUserMessageIDLength() int { return 64 } @@ -109,17 +116,24 @@ func (u User) MaxTimestampDiffHours() int { } type UserJSON struct { - UserID UserID `json:"user_id"` - Username *string `json:"username"` - TimestampCreated string `json:"timestamp_created"` - TimestampLastRead *string `json:"timestamp_lastread"` - TimestampLastSent *string `json:"timestamp_lastsent"` - MessagesSent int `json:"messages_sent"` - QuotaUsed int `json:"quota_used"` - QuotaRemaining int `json:"quota_remaining"` - QuotaPerDay int `json:"quota_max"` - IsPro bool `json:"is_pro"` - DefaultChannel string `json:"default_channel"` + UserID UserID `json:"user_id"` + Username *string `json:"username"` + TimestampCreated string `json:"timestamp_created"` + TimestampLastRead *string `json:"timestamp_lastread"` + TimestampLastSent *string `json:"timestamp_lastsent"` + MessagesSent int `json:"messages_sent"` + QuotaUsed int `json:"quota_used"` + QuotaRemaining int `json:"quota_remaining"` + QuotaPerDay int `json:"quota_max"` + IsPro bool `json:"is_pro"` + DefaultChannel string `json:"default_channel"` + MaxBodySize int `json:"max_body_size"` + MaxTitleLength int `json:"max_title_length"` + DefaultPriority int `json:"default_priority"` + MaxChannelNameLength int `json:"max_channel_name_length"` + MaxChannelDescriptionLength int `json:"max_channel_description_length"` + MaxSenderNameLength int `json:"max_sender_name_length"` + MaxUserMessageIDLength int `json:"max_user_message_id_length"` } type UserJSONWithClientsAndKeys struct { diff --git a/scnserver/test/user_test.go b/scnserver/test/user_test.go index 67fe515..7e384c4 100644 --- a/scnserver/test/user_test.go +++ b/scnserver/test/user_test.go @@ -428,3 +428,70 @@ func TestUserMessageCounter(t *testing.T) { assertCounter(5) } + +func TestGetUserNoPro(t *testing.T) { + _, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{ + "no_client": true, + }) + + uid := fmt.Sprintf("%v", r0["user_id"]) + readtok := r0["read_key"].(string) + + r1 := tt.RequestAuthGet[gin.H](t, readtok, baseUrl, "/api/v2/users/"+uid) + + tt.AssertEqual(t, "user_id", uid, fmt.Sprintf("%v", r1["user_id"])) + tt.AssertEqual(t, "username", nil, r1["username"]) + tt.AssertNotEqual(t, "timestamp_created", nil, r1["timestamp_created"]) + tt.AssertEqual(t, "timestamp_lastread", nil, r1["timestamp_lastread"]) + tt.AssertEqual(t, "timestamp_lastsent", nil, r1["timestamp_lastsent"]) + tt.AssertEqual(t, "messages_sent", "0", fmt.Sprintf("%v", r1["messages_sent"])) + tt.AssertEqual(t, "quota_used", "0", fmt.Sprintf("%v", r1["quota_used"])) + tt.AssertEqual(t, "quota_remaining", "50", fmt.Sprintf("%v", r1["quota_remaining"])) + tt.AssertEqual(t, "quota_max", "50", fmt.Sprintf("%v", r1["quota_max"])) + tt.AssertEqual(t, "is_pro", "false", fmt.Sprintf("%v", r1["is_pro"])) + tt.AssertEqual(t, "default_channel", "main", fmt.Sprintf("%v", r1["default_channel"])) + tt.AssertEqual(t, "max_body_size", "2048", fmt.Sprintf("%v", r1["max_body_size"])) + tt.AssertEqual(t, "max_title_length", "120", fmt.Sprintf("%v", r1["max_title_length"])) + tt.AssertEqual(t, "default_priority", "1", fmt.Sprintf("%v", r1["default_priority"])) + tt.AssertEqual(t, "max_channel_name_length", "120", fmt.Sprintf("%v", r1["max_channel_name_length"])) + tt.AssertEqual(t, "max_channel_description_length", "300", fmt.Sprintf("%v", r1["max_channel_description_length"])) + tt.AssertEqual(t, "max_sender_name_length", "120", fmt.Sprintf("%v", r1["max_sender_name_length"])) + tt.AssertEqual(t, "max_user_message_id_length", "64", fmt.Sprintf("%v", r1["max_user_message_id_length"])) +} + +func TestGetUserPro(t *testing.T) { + _, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{ + "no_client": true, + "pro_token": "ANDROID|v2|PURCHASED:DUMMY_TOK_XX", + }) + + uid := fmt.Sprintf("%v", r0["user_id"]) + readtok := r0["read_key"].(string) + + r1 := tt.RequestAuthGet[gin.H](t, readtok, baseUrl, "/api/v2/users/"+uid) + + tt.AssertEqual(t, "user_id", uid, fmt.Sprintf("%v", r1["user_id"])) + tt.AssertEqual(t, "username", nil, r1["username"]) + tt.AssertNotEqual(t, "timestamp_created", nil, r1["timestamp_created"]) + tt.AssertEqual(t, "timestamp_lastread", nil, r1["timestamp_lastread"]) + tt.AssertEqual(t, "timestamp_lastsent", nil, r1["timestamp_lastsent"]) + tt.AssertEqual(t, "messages_sent", "0", fmt.Sprintf("%v", r1["messages_sent"])) + tt.AssertEqual(t, "quota_used", "0", fmt.Sprintf("%v", r1["quota_used"])) + tt.AssertEqual(t, "quota_remaining", "5000", fmt.Sprintf("%v", r1["quota_remaining"])) + tt.AssertEqual(t, "quota_max", "5000", fmt.Sprintf("%v", r1["quota_max"])) + tt.AssertEqual(t, "is_pro", "true", fmt.Sprintf("%v", r1["is_pro"])) + tt.AssertEqual(t, "default_channel", "main", fmt.Sprintf("%v", r1["default_channel"])) + tt.AssertEqual(t, "max_body_size", "2097152", fmt.Sprintf("%d", (int64)(r1["max_body_size"].(float64)))) + tt.AssertEqual(t, "max_title_length", "120", fmt.Sprintf("%v", r1["max_title_length"])) + tt.AssertEqual(t, "default_priority", "1", fmt.Sprintf("%v", r1["default_priority"])) + tt.AssertEqual(t, "max_channel_name_length", "120", fmt.Sprintf("%v", r1["max_channel_name_length"])) + tt.AssertEqual(t, "max_channel_description_length", "300", fmt.Sprintf("%v", r1["max_channel_description_length"])) + tt.AssertEqual(t, "max_sender_name_length", "120", fmt.Sprintf("%v", r1["max_sender_name_length"])) + tt.AssertEqual(t, "max_user_message_id_length", "64", fmt.Sprintf("%v", r1["max_user_message_id_length"])) +}