From e0ecd4d9ffa4ceba9a1e6a1a847b6e21cd89d319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 30 Nov 2022 21:51:48 +0100 Subject: [PATCH] Tests[SendInvalidPriority] --- server/common/ginresp/apiError.go | 12 +-- server/common/ginresp/resp.go | 3 +- server/test/message_test.go | 129 ++++++++++++++++++++++++++++-- server/test/util/requests.go | 36 ++++++--- 4 files changed, 159 insertions(+), 21 deletions(-) diff --git a/server/common/ginresp/apiError.go b/server/common/ginresp/apiError.go index 8856269..aa22f5c 100644 --- a/server/common/ginresp/apiError.go +++ b/server/common/ginresp/apiError.go @@ -1,12 +1,12 @@ package ginresp type apiError struct { - Success bool `json:"success"` - Error int `json:"error"` - ErrorHighlight int `json:"errhighlight"` - Message string `json:"message"` - RawError string `json:"errorObj,omitempty"` - Trace string `json:"traceObj,omitempty"` + Success bool `json:"success"` + Error int `json:"error"` + ErrorHighlight int `json:"errhighlight"` + Message string `json:"message"` + RawError *string `json:"errorObj,omitempty"` + Trace string `json:"traceObj,omitempty"` } type compatAPIError struct { diff --git a/server/common/ginresp/resp.go b/server/common/ginresp/resp.go index 4f7e121..3ad8d86 100644 --- a/server/common/ginresp/resp.go +++ b/server/common/ginresp/resp.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" + "gogs.mikescher.com/BlackForestBytes/goext/langext" "runtime/debug" ) @@ -104,7 +105,7 @@ func createApiError(g *gin.Context, ident string, status int, errorid apierr.API Error: int(errorid), ErrorHighlight: int(highlight), Message: msg, - RawError: fmt.Sprintf("%+v", e), + RawError: langext.Ptr(langext.Conditional(e == nil, "", fmt.Sprintf("%+v", e))), Trace: string(debug.Stack()), }, } diff --git a/server/test/message_test.go b/server/test/message_test.go index 1fa6491..5d375b6 100644 --- a/server/test/message_test.go +++ b/server/test/message_test.go @@ -1,6 +1,7 @@ package test import ( + "blackforestbytes.com/simplecloudnotifier/api/apierr" "blackforestbytes.com/simplecloudnotifier/push" tt "blackforestbytes.com/simplecloudnotifier/test/util" "fmt" @@ -39,7 +40,7 @@ func TestSendSimpleMessageJSON(t *testing.T) { "user_key": readtok, "user_id": uid, "title": "HelloWorld_001", - }, 401, 1311) + }, 401, apierr.USER_AUTH_FAILED) tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_001", pusher.Last().Message.Title) @@ -360,7 +361,7 @@ func TestSendTooLongContent(t *testing.T) { "user_id": uid, "title": "HelloWorld_042", "content": longContent, - }, 400, 1203) + }, 400, apierr.CONTENT_TOO_LONG) } func TestSendTooLongTitle(t *testing.T) { @@ -383,7 +384,7 @@ func TestSendTooLongTitle(t *testing.T) { "user_key": sendtok, "user_id": uid, "title": "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", - }, 400, 1202) + }, 400, apierr.TITLE_TOO_LONG) } func TestSendIdempotent(t *testing.T) { @@ -561,6 +562,126 @@ func TestSendWithPriority(t *testing.T) { } } +func TestSendInvalidPriority(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) + admintok := r0["admin_key"].(string) + + tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ + "user_key": sendtok, + "user_id": uid, + "title": "(title)", + "content": "(content)", + "priority": -1, + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ + "user_key": sendtok, + "user_id": uid, + "title": "(title)", + "content": "(content)", + "priority": 4, + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ + "user_key": sendtok, + "user_id": uid, + "title": "(title)", + "content": "(content)", + "priority": 9999, + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ + "user_key": admintok, + "user_id": uid, + "title": "(title)", + "content": "(content)", + "priority": -1, + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ + "user_key": admintok, + "user_id": uid, + "title": "(title)", + "content": "(content)", + "priority": 4, + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ + "user_key": admintok, + "user_id": uid, + "title": "(title)", + "content": "(content)", + "priority": 9999, + }, 400, apierr.INVALID_PRIO) + + struid := fmt.Sprintf("%d", uid) + + tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{ + "user_key": sendtok, + "user_id": struid, + "title": "(title)", + "content": "(content)", + "priority": "-1", + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{ + "user_key": sendtok, + "user_id": struid, + "title": "(title)", + "content": "(content)", + "priority": "4", + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{ + "user_key": sendtok, + "user_id": struid, + "title": "(title)", + "content": "(content)", + "priority": "9999", + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{ + "user_key": admintok, + "user_id": struid, + "title": "(title)", + "content": "(content)", + "priority": "-1", + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{ + "user_key": admintok, + "user_id": struid, + "title": "(title)", + "content": "(content)", + "priority": "4", + }, 400, apierr.INVALID_PRIO) + + tt.RequestPostShouldFail(t, baseUrl, "/", tt.FormData{ + "user_key": admintok, + "user_id": struid, + "title": "(title)", + "content": "(content)", + "priority": "9999", + }, 400, apierr.INVALID_PRIO) + + tt.AssertEqual(t, "messageCount", 0, len(pusher.Data)) +} + //TODO compat route //TODO post to channel @@ -571,8 +692,6 @@ func TestSendWithPriority(t *testing.T) { //TODO quota exceed (+ quota counter) -//TODO invalid priority - //TODO chan_naem too long //TODO chan_name normalization diff --git a/server/test/util/requests.go b/server/test/util/requests.go index 07e551c..be0a601 100644 --- a/server/test/util/requests.go +++ b/server/test/util/requests.go @@ -169,12 +169,32 @@ func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL fmt.Printf("[-> REQUEST] (%s) %s%s [%s] (should-fail with %d/%d)\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), statusCode, errcode) bytesbody := make([]byte, 0) + contentType := "" if body != nil { - bjson, err := json.Marshal(body) - if err != nil { - TestFailErr(t, err) + switch bd := body.(type) { + case FormData: + bodybuffer := &bytes.Buffer{} + writer := multipart.NewWriter(bodybuffer) + for bdk, bdv := range bd { + err := writer.WriteField(bdk, bdv) + if err != nil { + TestFailErr(t, err) + } + } + err := writer.Close() + if err != nil { + TestFailErr(t, err) + } + bytesbody = bodybuffer.Bytes() + contentType = writer.FormDataContentType() + default: + bjson, err := json.Marshal(body) + if err != nil { + TestFailErr(t, err) + } + bytesbody = bjson + contentType = "application/json" } - bytesbody = bjson } req, err := http.NewRequest(method, baseURL+urlSuffix, bytes.NewReader(bytesbody)) @@ -182,8 +202,8 @@ func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL TestFailErr(t, err) } - if body != nil { - req.Header.Set("Content-Type", "application/json") + if contentType != "" { + req.Header.Set("Content-Type", contentType) } if akey != "" { @@ -204,13 +224,11 @@ func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL fmt.Println("") fmt.Printf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode) fmt.Println(langext.TryPrettyPrintJson(string(respBodyBin))) - TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "") + //TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "") fmt.Println("---------------- -------- ----------------") fmt.Println("") if resp.StatusCode != statusCode { - fmt.Println("Request: " + method + " :: " + baseURL + urlSuffix) - fmt.Println(string(respBodyBin)) TestFailFmt(t, "Statuscode != %d (expected failure)", statusCode) }