Tests[SendIdempotent]

This commit is contained in:
Mike Schwörer 2022-11-30 21:17:29 +01:00
parent 4e5eac6178
commit a7df476e79
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 102 additions and 16 deletions

View File

@ -348,7 +348,72 @@ func TestSendTooLongTitle(t *testing.T) {
}, 400, 1202)
}
//TODO trim too-long content
func TestSendIdempotent(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))
sendtok := r0["send_key"].(string)
msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"user_key": sendtok,
"user_id": uid,
"title": "Hello SCN",
"content": "mamma mia",
"msg_id": "c0235a49-dabc-4cdc-a0ce-453966e0c2d5",
})
tt.AssertEqual(t, "messageCount", 1, len(pusher.Data))
tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID)
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)
msg2 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"user_key": sendtok,
"user_id": uid,
"title": "Hello again",
"content": "mother mia",
"msg_id": "c0235a49-dabc-4cdc-a0ce-453966e0c2d5",
})
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.SCNMessageID)
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)
msg3 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{
"user_key": sendtok,
"user_id": uid,
"title": "Hello third",
"content": "let me go",
"msg_id": "3238e68e-c1ea-44ce-b21b-2576614082b5",
})
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)
}
//TODO compat route
@ -356,8 +421,6 @@ func TestSendTooLongTitle(t *testing.T) {
//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
@ -365,8 +428,6 @@ func TestSendTooLongTitle(t *testing.T) {
//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

View File

@ -56,6 +56,31 @@ func AssertEqual(t *testing.T, key string, expected any, actual any) {
}
}
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)
str1 := fmt.Sprintf("%v", expected)
str2 := fmt.Sprintf("%v", 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("Not Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
} else {
t.Errorf("Not Expected := \"%v\"\n", actual)
}
t.Error(string(debug.Stack()))
t.FailNow()
}
}
func AssertStrRepEqual(t *testing.T, key string, expected any, actual any) {
strExp := fmt.Sprintf("%v", unpointer(expected))
strAct := fmt.Sprintf("%v", unpointer(actual))
@ -81,23 +106,23 @@ func AssertStrRepEqual(t *testing.T, key string, expected any, actual any) {
}
}
func AssertNotEqual(t *testing.T, key string, expected any, actual any) {
if expected == actual {
func AssertNotStrRepEqual(t *testing.T, key string, expected any, actual any) {
strExp := fmt.Sprintf("%v", unpointer(expected))
strAct := fmt.Sprintf("%v", unpointer(actual))
if strAct == strExp {
t.Errorf("Value [%s] does not differ (%T <-> %T):\n", key, expected, actual)
str1 := fmt.Sprintf("%v", expected)
str2 := fmt.Sprintf("%v", actual)
if strings.Contains(str1, "\n") {
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected)
if strings.Contains(strAct, "\n") {
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strAct)
} else {
t.Errorf("Actual := \"%v\"\n", expected)
t.Errorf("Actual := \"%v\"\n", strAct)
}
if strings.Contains(str2, "\n") {
t.Errorf("Not Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
if strings.Contains(strExp, "\n") {
t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strExp)
} else {
t.Errorf("Not Expected := \"%v\"\n", actual)
t.Errorf("Expected := \"%v\"\n", strExp)
}
t.Error(string(debug.Stack()))