From a7df476e79deccd71375fcea9240d6d37ec343fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 30 Nov 2022 21:17:29 +0100 Subject: [PATCH] Tests[SendIdempotent] --- server/test/message_test.go | 71 ++++++++++++++++++++++++++++++++++--- server/test/util/common.go | 47 ++++++++++++++++++------ 2 files changed, 102 insertions(+), 16 deletions(-) diff --git a/server/test/message_test.go b/server/test/message_test.go index 16c2560..ef3048c 100644 --- a/server/test/message_test.go +++ b/server/test/message_test.go @@ -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 diff --git a/server/test/util/common.go b/server/test/util/common.go index ae45d89..bbe3684 100644 --- a/server/test/util/common.go +++ b/server/test/util/common.go @@ -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()))