Tests[SendInvalidPriority]

This commit is contained in:
Mike Schwörer 2022-11-30 21:51:48 +01:00
parent 1ca09c16d3
commit e0ecd4d9ff
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 159 additions and 21 deletions

View File

@ -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 {

View File

@ -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()),
},
}

View File

@ -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

View File

@ -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)
}