Tests[TestSendSimpleMessageJSON]
This commit is contained in:
88
server/test/message_test.go
Normal file
88
server/test/message_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/push"
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSendSimpleMessageJSON(t *testing.T) {
|
||||
ws, stop := tt.StartSimpleWebserver(t)
|
||||
defer stop()
|
||||
|
||||
pusher := ws.Pusher.(*push.TestSink)
|
||||
|
||||
baseUrl := "http://127.0.0.1:" + ws.Port
|
||||
|
||||
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))
|
||||
admintok := r0["admin_key"].(string)
|
||||
readtok := r0["read_key"].(string)
|
||||
sendtok := r0["send_key"].(string)
|
||||
|
||||
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
|
||||
"user_key": sendtok,
|
||||
"user_id": uid,
|
||||
"title": "HelloWorld_001",
|
||||
})
|
||||
|
||||
tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{
|
||||
"user_key": readtok,
|
||||
"user_id": uid,
|
||||
"title": "HelloWorld_001",
|
||||
}, 401, 1311)
|
||||
|
||||
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
|
||||
tt.AssertEqual(t, "msg.title", "HelloWorld_001", pusher.Last().Message.Title)
|
||||
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID)
|
||||
|
||||
type mglist struct {
|
||||
Messages []gin.H `json:"messages"`
|
||||
}
|
||||
|
||||
msgList1 := tt.RequestAuthGet[mglist](t, admintok, baseUrl, "/api/messages")
|
||||
tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages))
|
||||
|
||||
msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"]))
|
||||
tt.AssertEqual(t, "msg.title", "HelloWorld_001", msg1Get["title"])
|
||||
tt.AssertEqual(t, "msg.channel_name", "main", msg1Get["channel_name"])
|
||||
|
||||
}
|
||||
|
||||
//TODO message -> query
|
||||
//TODO message -> form
|
||||
//TODO overwrite order when all 3 are specified
|
||||
|
||||
//TODO send content
|
||||
//TODO sendername
|
||||
|
||||
//TODO trim too-long content
|
||||
|
||||
//TODO compat route
|
||||
|
||||
//TODO post to channel
|
||||
//TODO post to newly-created-channel
|
||||
//TODO post to foreign channel via send-key
|
||||
|
||||
//TODO usr_msg_id
|
||||
|
||||
//TODO quota exceed (+ quota counter)
|
||||
|
||||
//TODO invalid priority
|
||||
//TODO chan_naem too long
|
||||
//TODO chan_name normalization
|
||||
//TODO custom_timestamp
|
||||
//TODO invalid time
|
||||
//TODO title too long
|
||||
//TODO content too long
|
||||
|
||||
//TODO check message_counter + last_sent in channel
|
||||
//TODO check message_counter + last_sent in user
|
||||
@@ -22,8 +22,13 @@ func TestCreateUserNoClient(t *testing.T) {
|
||||
|
||||
uid := fmt.Sprintf("%v", r0["user_id"])
|
||||
admintok := r0["admin_key"].(string)
|
||||
readtok := r0["read_key"].(string)
|
||||
sendtok := r0["send_key"].(string)
|
||||
|
||||
r1 := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/users/"+uid)
|
||||
tt.RequestAuthGetShouldFail(t, sendtok, baseUrl, "/api/users/"+uid, 401, apierr.USER_AUTH_FAILED)
|
||||
tt.RequestAuthGetShouldFail(t, "", baseUrl, "/api/users/"+uid, 401, apierr.USER_AUTH_FAILED)
|
||||
|
||||
r1 := tt.RequestAuthGet[gin.H](t, readtok, baseUrl, "/api/users/"+uid)
|
||||
|
||||
tt.AssertEqual(t, "uid", uid, fmt.Sprintf("%v", r1["user_id"]))
|
||||
tt.AssertEqual(t, "admin_key", admintok, r1["admin_key"])
|
||||
@@ -214,6 +219,8 @@ func TestDeleteUser(t *testing.T) {
|
||||
|
||||
tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/users/"+uid)
|
||||
|
||||
tt.RequestAuthDeleteShouldFail(t, admintok, baseUrl, "/api/users/"+uid, nil, 401, apierr.USER_AUTH_FAILED)
|
||||
|
||||
tt.RequestAuthDelete[tt.Void](t, admintok, baseUrl, "/api/users/"+uid, nil)
|
||||
|
||||
tt.RequestAuthGetShouldFail(t, admintok, baseUrl, "/api/users/"+uid, 404, apierr.USER_NOT_FOUND)
|
||||
|
||||
@@ -55,6 +55,31 @@ func AssertEqual(t *testing.T, key string, expected any, actual any) {
|
||||
}
|
||||
}
|
||||
|
||||
func AssertStrRepEqual(t *testing.T, key string, expected any, actual any) {
|
||||
str1 := fmt.Sprintf("%v", expected)
|
||||
str2 := fmt.Sprintf("%v", actual)
|
||||
|
||||
if str1 != str2 {
|
||||
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
if strings.Contains(str1, "\n") {
|
||||
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected)
|
||||
} else {
|
||||
t.Errorf("Actual := \"%v\"\n", expected)
|
||||
}
|
||||
|
||||
if strings.Contains(str2, "\n") {
|
||||
t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
|
||||
} else {
|
||||
t.Errorf("Expected := \"%v\"\n", actual)
|
||||
}
|
||||
|
||||
t.Error(string(debug.Stack()))
|
||||
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func AssertNotEqual(t *testing.T, key string, expected any, actual any) {
|
||||
if expected == actual {
|
||||
t.Errorf("Value [%s] does not differ (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
@@ -52,6 +52,38 @@ func RequestAuthDelete[TResult any](t *testing.T, akey string, baseURL string, u
|
||||
return RequestAny[TResult](t, akey, "DELETE", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestGetShouldFail(t *testing.T, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, "", "GET", baseURL, urlSuffix, nil, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestPostShouldFail(t *testing.T, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, "", "POST", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestPatchShouldFail(t *testing.T, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, "", "PATCH", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestDeleteShouldFail(t *testing.T, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, "", "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthGetShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "GET", baseURL, urlSuffix, nil, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthPostShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "POST", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthPatchShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "PATCH", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthDeleteShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any) TResult {
|
||||
client := http.Client{}
|
||||
|
||||
@@ -108,22 +140,6 @@ func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL s
|
||||
return data
|
||||
}
|
||||
|
||||
func RequestAuthGetShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "GET", baseURL, urlSuffix, nil, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthPostShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "POST", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthPatchShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "PATCH", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthDeleteShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
client := http.Client{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user