SimpleCloudNotifier/scnserver/test/send_test.go

1810 lines
58 KiB
Go
Raw Permalink Normal View History

2022-12-09 00:13:10 +01:00
package test
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/models"
2022-12-09 00:13:10 +01:00
"blackforestbytes.com/simplecloudnotifier/push"
tt "blackforestbytes.com/simplecloudnotifier/test/util"
"fmt"
"github.com/gin-gonic/gin"
"math/rand/v2"
2022-12-09 00:13:10 +01:00
"net/url"
2023-07-30 16:37:39 +02:00
"strings"
2022-12-09 00:13:10 +01:00
"testing"
"time"
)
func TestSendSimpleMessageJSON(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
admintok := r0["admin_key"].(string)
readtok := r0["read_key"].(string)
sendtok := r0["send_key"].(string)
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_001",
2022-12-09 00:13:10 +01:00
})
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": readtok,
"user_id": uid,
"title": "HelloWorld_001",
2022-12-09 00:13:10 +01:00
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": "asdf",
"user_id": uid,
"title": "HelloWorld_001",
2022-12-09 00:13:10 +01:00
}, 401, apierr.USER_AUTH_FAILED)
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_001", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", nil, pusher.Last().Message.Content)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_001", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
}
func TestSendSimpleMessageQuery(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
admintok := r0["admin_key"].(string)
sendtok := r0["send_key"].(string)
2023-04-21 21:45:16 +02:00
msg1 := tt.RequestPost[gin.H](t, baseUrl, fmt.Sprintf("/?user_id=%s&key=%s&title=%s", uid, sendtok, url.QueryEscape("Hello World 2134")), nil)
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "Hello World 2134", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", nil, pusher.Last().Message.Content)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "Hello World 2134", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
}
func TestSendSimpleMessageForm(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
admintok := r0["admin_key"].(string)
sendtok := r0["send_key"].(string)
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "Hello World 9999 [$$$]",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "Hello World 9999 [$$$]", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", nil, pusher.Last().Message.Content)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "Hello World 9999 [$$$]", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
}
func TestSendSimpleMessageFormAndQuery(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
2023-04-21 21:45:16 +02:00
msg1 := tt.RequestPost[gin.H](t, baseUrl, fmt.Sprintf("/?user_id=%s&key=%s&title=%s", uid, sendtok, url.QueryEscape("1111111")), tt.FormData{
"key": "ERR",
"user_id": "999999",
"title": "2222222",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "1111111", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
}
func TestSendSimpleMessageJSONAndQuery(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
// query overwrite body
2023-04-21 21:45:16 +02:00
msg1 := tt.RequestPost[gin.H](t, baseUrl, fmt.Sprintf("/?user_id=%s&key=%s&title=%s", uid, sendtok, url.QueryEscape("1111111")), gin.H{
"key": "ERR",
"user_id": models.NewUserID(),
"title": "2222222",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "1111111", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
}
func TestSendSimpleMessageAlt1(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
admintok := r0["admin_key"].(string)
readtok := r0["read_key"].(string)
sendtok := r0["send_key"].(string)
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/send", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_001",
2022-12-09 00:13:10 +01:00
})
tt.RequestPostShouldFail(t, baseUrl, "/send", gin.H{
2023-04-21 21:45:16 +02:00
"key": readtok,
"user_id": uid,
"title": "HelloWorld_001",
2022-12-09 00:13:10 +01:00
}, 401, apierr.USER_AUTH_FAILED)
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_001", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", nil, pusher.Last().Message.Content)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_001", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
}
func TestSendContentMessage(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
admintok := r0["admin_key"].(string)
sendtok := r0["send_key"].(string)
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_042",
"content": "I am Content\nasdf",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", "I am Content\nasdf", pusher.Last().Message.Content)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", msgList1.Messages[0]["title"])
tt.AssertStrRepEqual(t, "msg.content", "I am Content\nasdf", msgList1.Messages[0]["content"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msgList1.Messages[0]["channel_internal_name"])
2022-12-09 00:13:10 +01:00
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.content", "I am Content\nasdf", msg1Get["content"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
}
func TestSendWithSendername(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
admintok := r0["admin_key"].(string)
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "HelloWorld_xyz",
"content": "Unicode: 日本 - yäy\000\n\t\x00...",
"sender_name": "localhorst",
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_xyz", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", "Unicode: 日本 - yäy\000\n\t\x00...", pusher.Last().Message.Content)
tt.AssertStrRepEqual(t, "msg.SenderName", "localhorst", pusher.Last().Message.SenderName)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_xyz", msgList1.Messages[0]["title"])
tt.AssertStrRepEqual(t, "msg.content", "Unicode: 日本 - yäy\000\n\t\x00...", msgList1.Messages[0]["content"])
tt.AssertStrRepEqual(t, "msg.sender_name", "localhorst", msgList1.Messages[0]["sender_name"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msgList1.Messages[0]["channel_internal_name"])
2022-12-09 00:13:10 +01:00
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_xyz", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.content", "Unicode: 日本 - yäy\000\n\t\x00...", msg1Get["content"])
tt.AssertStrRepEqual(t, "msg.sender_name", "localhorst", msg1Get["sender_name"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
}
func TestSendLongContent(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
admintok := r0["admin_key"].(string)
sendtok := r0["send_key"].(string)
longContent := ""
for i := 0; i < 200; i++ {
longContent += "123456789\n" // 10 * 200 = 2_000
}
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_042",
"content": longContent,
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", longContent, pusher.Last().Message.Content)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", msgList1.Messages[0]["title"])
tt.AssertNotStrRepEqual(t, "msg.content", longContent, msgList1.Messages[0]["content"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msgList1.Messages[0]["channel_internal_name"])
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.trimmmed", true, msgList1.Messages[0]["trimmed"])
msgList2 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages?trimmed=false")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList2.Messages))
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", msgList2.Messages[0]["title"])
tt.AssertStrRepEqual(t, "msg.content", longContent, msgList2.Messages[0]["content"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msgList2.Messages[0]["channel_internal_name"])
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.trimmmed", false, msgList2.Messages[0]["trimmed"])
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.titcontentle", longContent, msg1Get["content"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.trimmmed", false, msg1Get["trimmed"])
}
func TestSendTooLongContent(t *testing.T) {
2022-12-14 17:02:18 +01:00
_, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
longContent := ""
for i := 0; i < 400; i++ {
longContent += "123456789\n" // 10 * 400 = 4_000
}
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_042",
"content": longContent,
2022-12-09 00:13:10 +01:00
}, 400, apierr.CONTENT_TOO_LONG)
}
2022-12-14 18:18:02 +01:00
func TestSendLongContentPro(t *testing.T) {
_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-14 18:18:02 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
"pro_token": "ANDROID|v2|PURCHASED:DUMMY_TOK_XX",
})
uid := r0["user_id"].(string)
2022-12-14 18:18:02 +01:00
sendtok := r0["send_key"].(string)
2023-07-30 16:37:39 +02:00
str1k := strings.Repeat(".", 1000)
str100k := strings.Repeat(":", 100_000)
2022-12-14 18:18:02 +01:00
{
longContent := ""
for i := 0; i < 400; i++ {
2023-07-30 16:37:39 +02:00
longContent += "123456789\n" // 10 * 400 = 4_000 (max = 2_097_152)
2022-12-14 18:18:02 +01:00
}
tt.RequestPost[tt.Void](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_042",
"content": longContent,
2022-12-14 18:18:02 +01:00
})
}
{
longContent := ""
for i := 0; i < 800; i++ {
2023-07-30 16:37:39 +02:00
longContent += "123456789\n" // 10 * 800 = 8_000 (max = 2_097_152)
2022-12-14 18:18:02 +01:00
}
tt.RequestPost[tt.Void](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_042",
"content": longContent,
2022-12-14 18:18:02 +01:00
})
}
{
longContent := ""
2023-07-30 16:37:39 +02:00
for i := 0; i < 16; i++ {
longContent += str1k // 16 * 1000 = 16_000 (max = 2_097_152)
2022-12-14 18:18:02 +01:00
}
tt.RequestPost[tt.Void](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_042",
"content": longContent,
2022-12-14 18:18:02 +01:00
})
}
{
longContent := ""
2023-07-30 16:37:39 +02:00
for i := 0; i < 21; i++ {
longContent += str100k // 21 * 200_000 = 2_100_000 (max = 2_097_152)
2022-12-14 18:18:02 +01:00
}
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "HelloWorld_042",
"content": longContent,
2022-12-14 18:18:02 +01:00
}, 400, apierr.CONTENT_TOO_LONG)
}
}
2022-12-09 00:13:10 +01:00
func TestSendTooLongTitle(t *testing.T) {
2022-12-14 17:02:18 +01:00
_, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
2022-12-09 00:13:10 +01:00
}, 400, apierr.TITLE_TOO_LONG)
}
func TestSendIdempotent(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
readtok := r0["admin_key"].(string)
sendtok := r0["send_key"].(string)
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "Hello SCN",
"content": "mamma mia",
"msg_id": "c0235a49-dabc-4cdc-a0ce-453966e0c2d5",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.suppress_send", msg1["suppress_send"], false)
tt.AssertStrRepEqual(t, "msg.msg_id", "c0235a49-dabc-4cdc-a0ce-453966e0c2d5", pusher.Last().Message.UserMessageID)
tt.AssertStrRepEqual(t, "msg.title", "Hello SCN", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", "mamma mia", pusher.Last().Message.Content)
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, readtok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
msg2 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "Hello again",
"content": "mother mia",
"msg_id": "c0235a49-dabc-4cdc-a0ce-453966e0c2d5",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], msg2["scn_msg_id"])
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg2["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.suppress_send", msg2["suppress_send"], true)
tt.AssertStrRepEqual(t, "msg.msg_id", "c0235a49-dabc-4cdc-a0ce-453966e0c2d5", pusher.Last().Message.UserMessageID)
tt.AssertStrRepEqual(t, "msg.title", "Hello SCN", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", "mamma mia", pusher.Last().Message.Content)
msgList2 := tt.RequestAuthGet[mglist](t, readtok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList2.Messages))
msg3 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "Hello third",
"content": "let me go",
"msg_id": "3238e68e-c1ea-44ce-b21b-2576614082b5",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 2, len(pusher.Data))
tt.AssertNotStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], msg3["scn_msg_id"])
tt.AssertNotStrRepEqual(t, "msg.scn_msg_id", msg2["scn_msg_id"], msg3["scn_msg_id"])
tt.AssertStrRepEqual(t, "msg.suppress_send", msg3["suppress_send"], false)
tt.AssertStrRepEqual(t, "msg.msg_id", "3238e68e-c1ea-44ce-b21b-2576614082b5", pusher.Last().Message.UserMessageID)
tt.AssertStrRepEqual(t, "msg.title", "Hello third", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.content", "let me go", pusher.Last().Message.Content)
msgList3 := tt.RequestAuthGet[mglist](t, readtok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 2, len(msgList3.Messages))
}
func TestSendWithPriority(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
admintok := r0["admin_key"].(string)
{
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M_001",
"content": "TestSendWithPriority#001",
2022-12-09 00:13:10 +01:00
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.prio", 1, pusher.Last().Message.Priority)
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "M_001", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.content", "TestSendWithPriority#001", msg1Get["content"])
tt.AssertStrRepEqual(t, "msg.content", 1, msg1Get["priority"])
}
{
msg2 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "M_002",
"content": "TestSendWithPriority#002",
"priority": 0,
})
tt.AssertEqual(t, "messageCount", 2, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.prio", 0, pusher.Last().Message.Priority)
msg2Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg2["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "M_002", msg2Get["title"])
tt.AssertStrRepEqual(t, "msg.content", "TestSendWithPriority#002", msg2Get["content"])
tt.AssertStrRepEqual(t, "msg.content", 0, msg2Get["priority"])
}
{
msg3 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "M_003",
"content": "TestSendWithPriority#003",
"priority": 1,
})
tt.AssertEqual(t, "messageCount", 3, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.prio", 1, pusher.Last().Message.Priority)
msg3Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg3["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "M_003", msg3Get["title"])
tt.AssertStrRepEqual(t, "msg.content", "TestSendWithPriority#003", msg3Get["content"])
tt.AssertStrRepEqual(t, "msg.content", 1, msg3Get["priority"])
}
{
msg4 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "M_004",
"content": "TestSendWithPriority#004",
"priority": 2,
})
tt.AssertEqual(t, "messageCount", 4, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.prio", 2, pusher.Last().Message.Priority)
msg4Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg4["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "M_004", msg4Get["title"])
tt.AssertStrRepEqual(t, "msg.content", "TestSendWithPriority#004", msg4Get["content"])
tt.AssertStrRepEqual(t, "msg.content", 2, msg4Get["priority"])
}
}
func TestSendInvalidPriority(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
admintok := r0["admin_key"].(string)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "(title)",
"content": "(content)",
"priority": -1,
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "(title)",
"content": "(content)",
"priority": 4,
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "(title)",
"content": "(content)",
"priority": 9999,
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": admintok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "(title)",
"content": "(content)",
"priority": -1,
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": admintok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "(title)",
"content": "(content)",
"priority": 4,
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": admintok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "(title)",
"content": "(content)",
"priority": 9999,
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
2022-12-09 00:13:10 +01:00
"title": "(title)",
"content": "(content)",
"priority": "-1",
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
2022-12-09 00:13:10 +01:00
"title": "(title)",
"content": "(content)",
"priority": "4",
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
2022-12-09 00:13:10 +01:00
"title": "(title)",
"content": "(content)",
"priority": "9999",
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": admintok,
"user_id": uid,
2022-12-09 00:13:10 +01:00
"title": "(title)",
"content": "(content)",
"priority": "-1",
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": admintok,
"user_id": uid,
2022-12-09 00:13:10 +01:00
"title": "(title)",
"content": "(content)",
"priority": "4",
}, 400, apierr.INVALID_PRIO)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": admintok,
"user_id": uid,
2022-12-09 00:13:10 +01:00
"title": "(title)",
"content": "(content)",
"priority": "9999",
}, 400, apierr.INVALID_PRIO)
tt.AssertEqual(t, "messageCount", 0, len(pusher.Data))
}
func TestSendWithTimestamp(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
admintok := r0["admin_key"].(string)
ts := time.Now().Unix() - int64(time.Hour.Seconds())
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": fmt.Sprintf("%s", uid),
2022-12-09 00:13:10 +01:00
"title": "TTT",
"timestamp": fmt.Sprintf("%d", ts),
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.title", "TTT", pusher.Last().Message.Title)
tt.AssertStrRepEqual(t, "msg.TimestampClient", ts, pusher.Last().Message.TimestampClient.Time().Unix())
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.Timestamp", ts, pusher.Last().Message.Timestamp().Unix())
tt.AssertNotStrRepEqual(t, "msg.ts", pusher.Last().Message.TimestampClient, pusher.Last().Message.TimestampReal)
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID)
2022-12-09 00:13:10 +01:00
type mglist struct {
Messages []gin.H `json:"messages"`
}
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/v2/messages")
2022-12-09 00:13:10 +01:00
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
tt.AssertStrRepEqual(t, "msg.title", "TTT", msgList1.Messages[0]["title"])
tt.AssertStrRepEqual(t, "msg.content", nil, msgList1.Messages[0]["sender_name"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msgList1.Messages[0]["channel_internal_name"])
2022-12-09 00:13:10 +01:00
tm1, err := time.Parse(time.RFC3339Nano, msgList1.Messages[0]["timestamp"].(string))
tt.TestFailIfErr(t, err)
tt.AssertStrRepEqual(t, "msg.timestamp", ts, tm1.Unix())
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
2022-12-09 00:13:10 +01:00
tt.AssertStrRepEqual(t, "msg.title", "TTT", msg1Get["title"])
tt.AssertStrRepEqual(t, "msg.content", nil, msg1Get["sender_name"])
tt.AssertStrRepEqual(t, "msg.channel_internal_name", "main", msg1Get["channel_internal_name"])
2022-12-09 00:13:10 +01:00
tmg1, err := time.Parse(time.RFC3339Nano, msg1Get["timestamp"].(string))
tt.TestFailIfErr(t, err)
tt.AssertStrRepEqual(t, "msg.timestamp", ts, tmg1.Unix())
}
func TestSendInvalidTimestamp(t *testing.T) {
2022-12-14 17:02:18 +01:00
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-09 00:13:10 +01:00
defer stop()
pusher := ws.Pusher.(*push.TestSink)
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-09 00:13:10 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-09 00:13:10 +01:00
sendtok := r0["send_key"].(string)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": fmt.Sprintf("%s", uid),
2022-12-09 00:13:10 +01:00
"title": "TTT",
"timestamp": "-10000",
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": fmt.Sprintf("%s", uid),
2022-12-09 00:13:10 +01:00
"title": "TTT",
"timestamp": "0",
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": fmt.Sprintf("%s", uid),
2022-12-09 00:13:10 +01:00
"title": "TTT",
"timestamp": fmt.Sprintf("%d", time.Now().Unix()-int64(25*time.Hour.Seconds())),
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": fmt.Sprintf("%s", uid),
2022-12-09 00:13:10 +01:00
"title": "TTT",
"timestamp": fmt.Sprintf("%d", time.Now().Unix()+int64(25*time.Hour.Seconds())),
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "TTT",
"timestamp": -10000,
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "TTT",
"timestamp": 0,
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "TTT",
"timestamp": time.Now().Unix() - int64(25*time.Hour.Seconds()),
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
2022-12-09 00:13:10 +01:00
"user_id": uid,
"title": "TTT",
"timestamp": time.Now().Unix() + int64(25*time.Hour.Seconds()),
}, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
2023-04-21 21:45:16 +02:00
tt.RequestPostShouldFail(t, baseUrl, fmt.Sprintf("/?key=%s&user_id=%s&title=%s&timestamp=%d",
2022-12-09 00:13:10 +01:00
sendtok,
uid,
"TTT",
-10000,
), nil, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
2023-04-21 21:45:16 +02:00
tt.RequestPostShouldFail(t, baseUrl, fmt.Sprintf("/?key=%s&user_id=%s&title=%s&timestamp=%d",
2022-12-09 00:13:10 +01:00
sendtok,
uid,
"TTT",
0,
), nil, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
2023-04-21 21:45:16 +02:00
tt.RequestPostShouldFail(t, baseUrl, fmt.Sprintf("/?key=%s&user_id=%s&title=%s&timestamp=%d",
2022-12-09 00:13:10 +01:00
sendtok,
uid,
"TTT",
time.Now().Unix()-int64(25*time.Hour.Seconds()),
), nil, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
2023-04-21 21:45:16 +02:00
tt.RequestPostShouldFail(t, baseUrl, fmt.Sprintf("/?key=%s&user_id=%s&title=%s&timestamp=%d",
2022-12-09 00:13:10 +01:00
sendtok,
uid,
"TTT",
time.Now().Unix()+int64(25*time.Hour.Seconds()),
), nil, 400, apierr.TIMESTAMP_OUT_OF_RANGE)
tt.AssertEqual(t, "messageCount", 0, len(pusher.Data))
}
2022-12-14 14:30:34 +01:00
func TestSendToNewChannel(t *testing.T) {
2022-12-14 17:02:18 +01:00
_, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-14 14:30:34 +01:00
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-14 14:30:34 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-14 14:30:34 +01:00
sendtok := r0["send_key"].(string)
admintok := r0["admin_key"].(string)
type chanlist struct {
Channels []gin.H `json:"channels"`
}
{
chan0 := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
2022-12-14 16:38:01 +01:00
tt.AssertEqual(t, "chan-count", 0, len(chan0.Channels))
}
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M0",
2022-12-14 16:38:01 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main"}, clist.Channels, "internal_name")
2022-12-14 16:38:01 +01:00
}
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M1",
"content": tt.ShortLipsum0(4),
"channel": "main",
2022-12-14 16:38:01 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main"}, clist.Channels, "internal_name")
2022-12-14 16:38:01 +01:00
}
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M2",
"content": tt.ShortLipsum0(4),
"channel": "test",
2022-12-14 16:38:01 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "internal_name")
2022-12-14 16:38:01 +01:00
}
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M3",
"channel": "test",
2022-12-14 16:38:01 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "internal_name")
2022-12-14 16:38:01 +01:00
}
}
func TestSendToManualChannel(t *testing.T) {
2022-12-14 17:02:18 +01:00
_, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-14 16:38:01 +01:00
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-14 16:38:01 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-14 16:38:01 +01:00
sendtok := r0["send_key"].(string)
admintok := r0["admin_key"].(string)
type chanlist struct {
Channels []gin.H `json:"channels"`
}
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{}, clist.Channels, "internal_name")
2022-12-14 16:38:01 +01:00
}
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M0",
2022-12-14 16:38:01 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main"}, clist.Channels, "internal_name")
2022-12-14 14:30:34 +01:00
}
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M1",
"content": tt.ShortLipsum0(4),
"channel": "main",
2022-12-14 14:30:34 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
2022-12-14 14:30:34 +01:00
tt.AssertEqual(t, "chan.len", 1, len(clist.Channels))
tt.AssertEqual(t, "chan.internal_name", "main", clist.Channels[0]["internal_name"])
tt.AssertEqual(t, "chan.display_name", "main", clist.Channels[0]["display_name"])
2022-12-14 14:30:34 +01:00
}
tt.RequestAuthPost[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid), gin.H{
2022-12-14 16:38:01 +01:00
"name": "test",
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "internal_name")
2022-12-14 16:38:01 +01:00
}
2022-12-14 14:30:34 +01:00
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M2",
"content": tt.ShortLipsum0(4),
"channel": "test",
2022-12-14 14:30:34 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "internal_name")
2022-12-14 14:30:34 +01:00
}
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M3",
"channel": "test",
2022-12-14 14:30:34 +01:00
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"main", "test"}, clist.Channels, "internal_name")
2022-12-14 14:30:34 +01:00
}
}
2022-12-14 16:57:08 +01:00
func TestSendToTooLongChannel(t *testing.T) {
2022-12-14 17:02:18 +01:00
_, baseUrl, stop := tt.StartSimpleWebserver(t)
2022-12-14 16:57:08 +01:00
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-14 16:57:08 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-14 16:57:08 +01:00
sendtok := r0["send_key"].(string)
2022-12-09 00:13:10 +01:00
2022-12-14 16:57:08 +01:00
tt.RequestPost[tt.Void](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M3",
"channel": "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
2022-12-14 16:57:08 +01:00
})
tt.RequestPost[tt.Void](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M3",
"channel": "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
2022-12-14 16:57:08 +01:00
})
2022-12-09 00:13:10 +01:00
2022-12-14 16:57:08 +01:00
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": "M3",
"channel": "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901",
2022-12-14 16:57:08 +01:00
}, 400, apierr.CHANNEL_TOO_LONG)
}
2022-12-14 14:30:34 +01:00
2022-12-14 17:56:03 +01:00
func TestQuotaExceededNoPro(t *testing.T) {
_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-14 17:56:03 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := r0["user_id"].(string)
2022-12-14 17:56:03 +01:00
admintok := r0["admin_key"].(string)
sendtok := r0["send_key"].(string)
tt.AssertStrRepEqual(t, "quota.0", 0, r0["quota_used"])
tt.AssertStrRepEqual(t, "quota.0", 50, r0["quota_max"])
tt.AssertStrRepEqual(t, "quota.0", 50, r0["quota_remaining"])
{
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:03 +01:00
})
tt.AssertStrRepEqual(t, "quota.msg.1", 1, msg1["quota"])
tt.AssertStrRepEqual(t, "quota.msg.1", 50, msg1["quota_max"])
}
{
usr := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s", uid))
2022-12-14 17:56:03 +01:00
tt.AssertStrRepEqual(t, "quota.1", 1, usr["quota_used"])
tt.AssertStrRepEqual(t, "quota.1", 50, usr["quota_max"])
tt.AssertStrRepEqual(t, "quota.1", 49, usr["quota_remaining"])
}
for i := 0; i < 48; i++ {
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:03 +01:00
})
}
{
usr := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s", uid))
2022-12-14 17:56:03 +01:00
tt.AssertStrRepEqual(t, "quota.49", 49, usr["quota_used"])
tt.AssertStrRepEqual(t, "quota.49", 50, usr["quota_max"])
tt.AssertStrRepEqual(t, "quota.49", 1, usr["quota_remaining"])
}
msg50 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:03 +01:00
})
tt.AssertStrRepEqual(t, "quota.msg.50", 50, msg50["quota"])
tt.AssertStrRepEqual(t, "quota.msg.50", 50, msg50["quota_max"])
{
usr := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s", uid))
2022-12-14 17:56:03 +01:00
tt.AssertStrRepEqual(t, "quota.50", 50, usr["quota_used"])
tt.AssertStrRepEqual(t, "quota.50", 50, usr["quota_max"])
tt.AssertStrRepEqual(t, "quota.50", 0, usr["quota_remaining"])
}
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:03 +01:00
}, 403, apierr.QUOTA_REACHED)
}
2022-12-14 17:56:14 +01:00
func TestQuotaExceededPro(t *testing.T) {
_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-14 17:56:14 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
"pro_token": "ANDROID|v2|PURCHASED:DUMMY_TOK_XX",
})
uid := r0["user_id"].(string)
2022-12-14 17:56:14 +01:00
admintok := r0["admin_key"].(string)
sendtok := r0["send_key"].(string)
tt.AssertStrRepEqual(t, "quota.0", 0, r0["quota_used"])
2023-07-30 16:37:39 +02:00
tt.AssertStrRepEqual(t, "quota.0", 5000, r0["quota_max"])
tt.AssertStrRepEqual(t, "quota.0", 5000, r0["quota_remaining"])
2022-12-14 17:56:14 +01:00
{
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:14 +01:00
})
tt.AssertStrRepEqual(t, "quota.msg.1", 1, msg1["quota"])
2023-07-30 16:37:39 +02:00
tt.AssertStrRepEqual(t, "quota.msg.1", 5000, msg1["quota_max"])
2022-12-14 17:56:14 +01:00
}
{
usr := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s", uid))
2022-12-14 17:56:14 +01:00
tt.AssertStrRepEqual(t, "quota.1", 1, usr["quota_used"])
2023-07-30 16:37:39 +02:00
tt.AssertStrRepEqual(t, "quota.1", 5000, usr["quota_max"])
tt.AssertStrRepEqual(t, "quota.1", 4999, usr["quota_remaining"])
2022-12-14 17:56:14 +01:00
}
2023-07-30 16:37:39 +02:00
for i := 0; i < 4998; i++ {
2022-12-14 17:56:14 +01:00
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:14 +01:00
})
}
{
usr := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s", uid))
2022-12-14 17:56:14 +01:00
2023-07-30 16:37:39 +02:00
tt.AssertStrRepEqual(t, "quota.999", 4999, usr["quota_used"])
tt.AssertStrRepEqual(t, "quota.999", 5000, usr["quota_max"])
2022-12-14 17:56:14 +01:00
tt.AssertStrRepEqual(t, "quota.999", 1, usr["quota_remaining"])
}
msg50 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:14 +01:00
})
2023-07-30 16:37:39 +02:00
tt.AssertStrRepEqual(t, "quota.msg.5000", 5000, msg50["quota"])
tt.AssertStrRepEqual(t, "quota.msg.5000", 5000, msg50["quota_max"])
2022-12-14 17:56:14 +01:00
{
usr := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s", uid))
2022-12-14 17:56:14 +01:00
2023-07-30 16:37:39 +02:00
tt.AssertStrRepEqual(t, "quota.5000", 5000, usr["quota_used"])
tt.AssertStrRepEqual(t, "quota.5000", 5000, usr["quota_max"])
tt.AssertStrRepEqual(t, "quota.5000", 0, usr["quota_remaining"])
2022-12-14 17:56:14 +01:00
}
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
2023-04-21 21:45:16 +02:00
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
2022-12-14 17:56:14 +01:00
}, 403, apierr.QUOTA_REACHED)
}
2022-12-14 18:08:03 +01:00
func TestSendParallel(t *testing.T) {
_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
2022-12-14 18:08:03 +01:00
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
"pro_token": "ANDROID|v2|PURCHASED:DUMMY_TOK_XX",
})
uid := r0["user_id"].(string)
2022-12-14 18:08:03 +01:00
sendtok := r0["send_key"].(string)
admintok := r0["admin_key"].(string)
count := 512
2022-12-14 18:08:03 +01:00
chanNames := make([]string, 0)
for i := 0; i < count/50; i++ {
chanNames = append(chanNames, tt.ShortLipsum0(1))
}
sem := make(chan tt.Void, count) // semaphore pattern
for i := 0; i < count; i++ {
2022-12-14 18:08:03 +01:00
go func() {
defer func() {
sem <- tt.Void{}
}()
if rand.Int()%2 == 0 {
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
})
} else {
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": sendtok,
"user_id": uid,
"title": tt.ShortLipsum0(2),
"channel": chanNames[rand.IntN(len(chanNames))],
})
}
tt.RequestGet[tt.Void](t, baseUrl, fmt.Sprintf("/api/ping"))
tt.RequestAuthGet[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/messages"))
tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/users/"+uid)
tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/users/"+uid+"/channels")
tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/v2/users/"+uid+"/clients")
2022-12-14 18:08:03 +01:00
}()
}
// wait for goroutines to finish
for i := 0; i < count; i++ {
2022-12-14 18:08:03 +01:00
<-sem
}
}
func TestSendWithAdminKey(t *testing.T) {
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
data1 := tt.InitSingleData(t, ws)
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 subobj 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"`
}
type sublist struct {
Subscriptions []subobj `json:"subscriptions"`
}
2023-05-29 01:51:51 +02:00
chan1 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan1",
})
2023-05-29 01:51:51 +02:00
chan2 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan2",
})
2023-05-29 01:51:51 +02:00
chan3 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan3",
})
tt.AssertNotDefault(t, "chan1", chan1)
tt.AssertNotDefault(t, "chan2", chan2)
tt.AssertNotDefault(t, "chan3", chan3)
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": data1.AdminKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan1",
"title": tt.LipsumWord(1001, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": data1.AdminKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan2",
"title": tt.LipsumWord(1001, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": data1.AdminKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan3",
"title": tt.LipsumWord(1001, 1),
})
}
func TestSendWithSendKey(t *testing.T) {
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
data1 := tt.InitSingleData(t, ws)
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 subobj 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"`
}
type sublist struct {
Subscriptions []subobj `json:"subscriptions"`
}
2023-05-29 01:51:51 +02:00
chan1 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan1",
})
2023-05-29 01:51:51 +02:00
chan2 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan2",
})
2023-05-29 01:51:51 +02:00
chan3 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan3",
})
tt.AssertNotDefault(t, "chan1", chan1)
tt.AssertNotDefault(t, "chan2", chan2)
tt.AssertNotDefault(t, "chan3", chan3)
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": data1.SendKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan1",
"title": tt.LipsumWord(1001, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": data1.SendKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan2",
"title": tt.LipsumWord(1001, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": data1.SendKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan3",
"title": tt.LipsumWord(1001, 1),
})
}
func TestSendWithReadKey(t *testing.T) {
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
data1 := tt.InitSingleData(t, ws)
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 subobj 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"`
}
type sublist struct {
Subscriptions []subobj `json:"subscriptions"`
}
2023-05-29 01:51:51 +02:00
chan1 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan1",
})
2023-05-29 01:51:51 +02:00
chan2 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan2",
})
2023-05-29 01:51:51 +02:00
chan3 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan3",
})
tt.AssertNotDefault(t, "chan1", chan1)
tt.AssertNotDefault(t, "chan2", chan2)
tt.AssertNotDefault(t, "chan3", chan3)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": data1.ReadKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan1",
"title": tt.LipsumWord(1001, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": data1.ReadKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan2",
"title": tt.LipsumWord(1002, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": data1.ReadKey,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan3",
"title": tt.LipsumWord(1003, 1),
}, 401, apierr.USER_AUTH_FAILED)
}
func TestSendWithPermissionSendKey(t *testing.T) {
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
data1 := tt.InitSingleData(t, ws)
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 subobj 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"`
}
type sublist struct {
Subscriptions []subobj `json:"subscriptions"`
}
type keyobj struct {
AllChannels bool `json:"all_channels"`
Channels []string `json:"channels"`
KeytokenId string `json:"keytoken_id"`
MessagesSent int `json:"messages_sent"`
Name string `json:"name"`
OwnerUserId string `json:"owner_user_id"`
Permissions string `json:"permissions"`
Token string `json:"token"` // only in create
}
2023-05-29 01:51:51 +02:00
chan1 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan1",
})
2023-05-29 01:51:51 +02:00
chan2 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan2",
})
2023-05-29 01:51:51 +02:00
chan3 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan3",
})
2023-05-29 01:51:51 +02:00
chan4 := tt.RequestAuthPost[chanobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data1.UID), gin.H{
"name": "Chan4",
})
tt.AssertNotDefault(t, "chan1", chan1)
tt.AssertNotDefault(t, "chan2", chan2)
tt.AssertNotDefault(t, "chan3", chan3)
tt.AssertNotDefault(t, "chan4", chan4)
{
2023-05-29 01:51:51 +02:00
keyOK := tt.RequestAuthPost[keyobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys", data1.UID), gin.H{
"all_channels": false,
"channels": []string{chan1.ChannelId, chan2.ChannelId, chan3.ChannelId},
"name": "K2",
"permissions": "CS",
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": keyOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan1",
"title": tt.LipsumWord(1001, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": keyOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan2",
"title": tt.LipsumWord(1002, 1),
})
tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"key": keyOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan3",
"title": tt.LipsumWord(1003, 1),
})
}
{
2023-05-29 01:51:51 +02:00
keyNOK := tt.RequestAuthPost[keyobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys", data1.UID), gin.H{
"all_channels": false,
"channels": []string{},
"name": "K3",
"permissions": "CS",
})
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan1",
"title": tt.LipsumWord(1001, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan2",
"title": tt.LipsumWord(1002, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan3",
"title": tt.LipsumWord(1003, 1),
}, 401, apierr.USER_AUTH_FAILED)
}
{
2023-05-29 01:51:51 +02:00
keyNOK := tt.RequestAuthPost[keyobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys", data1.UID), gin.H{
"all_channels": false,
"channels": []string{chan4.ChannelId},
"name": "K4",
"permissions": "CS",
})
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan1",
"title": tt.LipsumWord(1001, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan2",
"title": tt.LipsumWord(1002, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan3",
"title": tt.LipsumWord(1003, 1),
}, 401, apierr.USER_AUTH_FAILED)
}
{
2023-05-29 01:51:51 +02:00
keyNOK := tt.RequestAuthPost[keyobj](t, data1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys", data1.UID), gin.H{
"all_channels": false,
"channels": []string{chan1.ChannelId, chan2.ChannelId, chan3.ChannelId},
"name": "K4",
"permissions": "CR",
})
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan1",
"title": tt.LipsumWord(1001, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan2",
"title": tt.LipsumWord(1002, 1),
}, 401, apierr.USER_AUTH_FAILED)
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
"key": keyNOK.Token,
2023-05-29 01:51:51 +02:00
"user_id": data1.UID,
"channel": "Chan3",
"title": tt.LipsumWord(1003, 1),
}, 401, apierr.USER_AUTH_FAILED)
}
}
func TestSendDeliveryRetry(t *testing.T) {
t.SkipNow() //TODO
}