diff --git a/server/go.mod b/server/go.mod index 4d94432..056e7ee 100644 --- a/server/go.mod +++ b/server/go.mod @@ -8,7 +8,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.16 github.com/rs/zerolog v1.28.0 github.com/swaggo/swag v1.8.7 - gogs.mikescher.com/BlackForestBytes/goext v0.0.34 + gogs.mikescher.com/BlackForestBytes/goext v0.0.35 ) require ( diff --git a/server/go.sum b/server/go.sum index cd660db..e3adf8e 100644 --- a/server/go.sum +++ b/server/go.sum @@ -106,6 +106,8 @@ gogs.mikescher.com/BlackForestBytes/goext v0.0.33 h1:NQRgsEs2j8eY9V45Ynq84+F0FgB gogs.mikescher.com/BlackForestBytes/goext v0.0.33/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g= gogs.mikescher.com/BlackForestBytes/goext v0.0.34 h1:fi6nA+7vDiAbIjs+meIo/jGXw4rig/nrjF/QNWSKN08= gogs.mikescher.com/BlackForestBytes/goext v0.0.34/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g= +gogs.mikescher.com/BlackForestBytes/goext v0.0.35 h1:K5IMnAns68D6DmkryCN8CrLcmlo9zmdeCcCN0ljP/3E= +gogs.mikescher.com/BlackForestBytes/goext v0.0.35/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= diff --git a/server/test/channel_test.go b/server/test/channel_test.go index 6912ff5..0aa30e7 100644 --- a/server/test/channel_test.go +++ b/server/test/channel_test.go @@ -1,3 +1,140 @@ package test +import ( + "blackforestbytes.com/simplecloudnotifier/api/apierr" + tt "blackforestbytes.com/simplecloudnotifier/test/util" + "fmt" + "github.com/gin-gonic/gin" + "gogs.mikescher.com/BlackForestBytes/goext/langext" + "testing" +) + +func TestCreateChannel(t *testing.T) { + _, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + 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) + + type chanlist struct { + Channels []gin.H `json:"channels"` + } + + { + chan0 := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid)) + tt.AssertEqual(t, "chan-count", 0, len(chan0.Channels)) + } + + tt.RequestAuthPost[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": "test", + }) + + { + clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid)) + tt.AssertEqual(t, "chan.len", 1, len(clist.Channels)) + tt.AssertEqual(t, "chan.name", "test", clist.Channels[0]["name"]) + } + + tt.RequestAuthPost[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": "asdf", + }) + + { + clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid)) + tt.AssertEqual(t, "chan-count", 2, len(clist.Channels)) + tt.AssertArrAny(t, "chan.has('asdf')", clist.Channels, func(msg gin.H) bool { return msg["name"].(string) == "asdf" }) + tt.AssertArrAny(t, "chan.has('test')", clist.Channels, func(msg gin.H) bool { return msg["name"].(string) == "test" }) + } +} + +func TestCreateChannelNameTooLong(t *testing.T) { + _, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + 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) + + tt.RequestAuthPostShouldFail(t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": langext.StrRepeat("X", 121), + }, 400, apierr.CHANNEL_TOO_LONG) +} + +func TestChannelNameNormalization(t *testing.T) { + _, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + 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) + + type chanlist struct { + Channels []gin.H `json:"channels"` + } + + { + chan0 := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid)) + tt.AssertEqual(t, "chan-count", 0, len(chan0.Channels)) + } + + tt.RequestAuthPost[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": "tESt", + }) + + { + clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid)) + tt.AssertEqual(t, "chan.len", 1, len(clist.Channels)) + tt.AssertEqual(t, "chan.name", "test", clist.Channels[0]["name"]) + } + + tt.RequestAuthPostShouldFail(t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": "test", + }, 409, apierr.CHANNEL_ALREADY_EXISTS) + + tt.RequestAuthPostShouldFail(t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": "TEST", + }, 409, apierr.CHANNEL_ALREADY_EXISTS) + + tt.RequestAuthPostShouldFail(t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": "Test", + }, 409, apierr.CHANNEL_ALREADY_EXISTS) + + tt.RequestAuthPostShouldFail(t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": "Test ", + }, 409, apierr.CHANNEL_ALREADY_EXISTS) + + tt.RequestAuthPostShouldFail(t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": " Test", + }, 409, apierr.CHANNEL_ALREADY_EXISTS) + + tt.RequestAuthPostShouldFail(t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid), gin.H{ + "name": " T e s t ", + }, 409, apierr.CHANNEL_ALREADY_EXISTS) + + { + clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/users/%d/channels", uid)) + tt.AssertEqual(t, "chan.len", 1, len(clist.Channels)) + tt.AssertEqual(t, "chan.name", "test", clist.Channels[0]["name"]) + } +} + //TODO test missing channel-xx methods diff --git a/server/test/message_test.go b/server/test/message_test.go index 951d20d..29490cc 100644 --- a/server/test/message_test.go +++ b/server/test/message_test.go @@ -172,3 +172,5 @@ func TestGetMessageFull(t *testing.T) { tt.AssertEqual(t, "msg.sender_name", "unit-test-[TestGetMessageFull]", msgIn["sender_name"]) tt.AssertEqual(t, "msg.timestamp", time.Unix(ts, 0).In(timeext.TimezoneBerlin).Format(time.RFC3339Nano), msgIn["timestamp"]) } + +//TODO test pagination diff --git a/server/test/send_test.go b/server/test/send_test.go index 003b3b2..8b7889f 100644 --- a/server/test/send_test.go +++ b/server/test/send_test.go @@ -1466,12 +1466,8 @@ func TestSendParallel(t *testing.T) { //TODO post to foreign channel via send-key -//TODO chan_name normalization - //TODO check message_counter + last_sent in channel //TODO check message_counter + last_sent in user -//TODO test pagination - //TODO test delivery-retry