Fix [TestChannelMessageCounter]

This commit is contained in:
Mike Schwörer 2023-06-17 20:08:39 +02:00
parent fb826919a6
commit d9a4c4ffd6
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 51 additions and 18 deletions

View File

@ -263,17 +263,17 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query subscriptions", err))
}
err = h.database.IncUserMessageCounter(ctx, user)
err = h.database.IncUserMessageCounter(ctx, &user)
if err != nil {
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to inc user msg-counter", err))
}
err = h.database.IncChannelMessageCounter(ctx, channel)
err = h.database.IncChannelMessageCounter(ctx, &channel)
if err != nil {
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to inc channel msg-counter", err))
}
err = h.database.IncKeyTokenMessageCounter(ctx, keytok.KeyTokenID)
err = h.database.IncKeyTokenMessageCounter(ctx, keytok)
if err != nil {
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to inc token msg-counter", err))
}

View File

@ -200,20 +200,25 @@ func (db *Database) GetChannel(ctx TxContext, userid models.UserID, channelid mo
return channel, nil
}
func (db *Database) IncChannelMessageCounter(ctx TxContext, channel models.Channel) error {
func (db *Database) IncChannelMessageCounter(ctx TxContext, channel *models.Channel) error {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return err
}
now := time.Now()
_, err = tx.Exec(ctx, "UPDATE channels SET messages_sent = messages_sent+1, timestamp_lastsent = :ts WHERE channel_id = :cid", sq.PP{
"cid": time2DB(time.Now()),
"ts": channel.ChannelID,
"ts": time2DB(now),
"cid": channel.ChannelID,
})
if err != nil {
return err
}
channel.MessagesSent += 1
channel.TimestampLastSent = &now
return nil
}

View File

@ -181,20 +181,25 @@ func (db *Database) UpdateKeyTokenChannels(ctx TxContext, keyTokenid models.KeyT
return nil
}
func (db *Database) IncKeyTokenMessageCounter(ctx TxContext, keyTokenid models.KeyTokenID) error {
func (db *Database) IncKeyTokenMessageCounter(ctx TxContext, keyToken *models.KeyToken) error {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return err
}
now := time.Now()
_, err = tx.Exec(ctx, "UPDATE keytokens SET messages_sent = messages_sent+1, timestamp_lastused = :ts WHERE keytoken_id = :tid", sq.PP{
"ts": time2DB(time.Now()),
"tid": keyTokenid,
"ts": time2DB(now),
"tid": keyToken.KeyTokenID,
})
if err != nil {
return err
}
keyToken.TimestampLastUsed = &now
keyToken.MessagesSent += 1
return nil
}

View File

@ -3,6 +3,7 @@ package primary
import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/models"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -102,18 +103,24 @@ func (db *Database) UpdateUserProToken(ctx TxContext, userid models.UserID, prot
return nil
}
func (db *Database) IncUserMessageCounter(ctx TxContext, user models.User) error {
func (db *Database) IncUserMessageCounter(ctx TxContext, user *models.User) error {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return err
}
now := time.Now()
quota := user.QuotaUsedToday() + 1
user.QuotaUsed = quota
user.QuotaUsedDay = langext.Ptr(scn.QuotaDayString())
user.TimestampLastSent = &now
_, err = tx.Exec(ctx, "UPDATE users SET timestamp_lastsent = :ts, messages_sent = messages_sent+1, quota_used = :qu, quota_used_day = :qd WHERE user_id = :uid", sq.PP{
"ts": time2DB(time.Now()),
"qu": quota,
"qd": scn.QuotaDayString(),
"ts": time2DB(now),
"qu": user.QuotaUsed,
"qd": user.QuotaUsedDay,
"uid": user.UserID,
})
if err != nil {

View File

@ -1091,7 +1091,7 @@ func TestListChannelMessagesOfRevokedConfirmation(t *testing.T) {
tt.RequestAuthGetShouldFail(t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s/messages", data1.UID, chan1.ChannelId), 401, apierr.USER_AUTH_FAILED)
}
func TestChannelMessageCounter(t *testing.T) { //TODO this fails!
func TestChannelMessageCounter(t *testing.T) {
_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
@ -1146,7 +1146,7 @@ func TestChannelMessageCounter(t *testing.T) { //TODO this fails!
"name": "Chan2",
})
assertCounter := func(c0 int, c1 int, c2 int) {
assertCounter := func(c0 float64, c1 float64, c2 float64) {
r1 := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/users/"+uid+"/channels/"+chan0.ChannelId)
tt.AssertStrRepEqual(t, "c0.messages_sent", c0, r1["messages_sent"])
@ -1186,8 +1186,24 @@ func TestChannelMessageCounter(t *testing.T) { //TODO this fails!
"title": tt.ShortLipsum(1005, 1),
})
assertCounter(5, 1, 2)
assertCounter(5, 1, 2)
assertCounter(2, 1, 2)
assertCounter(2, 1, 2)
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": admintok,
"user_id": uid,
"title": tt.ShortLipsum(2001, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": admintok,
"user_id": uid,
"title": tt.ShortLipsum(2002, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": admintok,
"user_id": uid,
"title": tt.ShortLipsum(2003, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": admintok,
@ -1201,7 +1217,7 @@ func TestChannelMessageCounter(t *testing.T) { //TODO this fails!
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": admintok,
"user_id": uid,
"title": tt.ShortLipsum(1002, 1),
"title": tt.ShortLipsum(4001, 1),
})
assertCounter(6, 1, 3)