From e05d88682afa223e6d7416ddeb64c90eddda1517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 18 Jan 2023 21:56:37 +0100 Subject: [PATCH] Tests[ListClients] --- scnserver/README.md | 4 +++- scnserver/test/client_test.go | 45 ++++++++++++++++++++++++++++++++++- scnserver/test/util/common.go | 23 ++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/scnserver/README.md b/scnserver/README.md index 0d78a8b..4fa4e99 100644 --- a/scnserver/README.md +++ b/scnserver/README.md @@ -47,7 +47,9 @@ - endpoint to list all servernames of user (distinct select) - - ios purchasw verification + - ios purchase verification + + - api versioning (api/v2 ?) #### PERSONAL diff --git a/scnserver/test/client_test.go b/scnserver/test/client_test.go index 97e1fb6..fda0420 100644 --- a/scnserver/test/client_test.go +++ b/scnserver/test/client_test.go @@ -1,9 +1,11 @@ package test import ( + "blackforestbytes.com/simplecloudnotifier/api/apierr" tt "blackforestbytes.com/simplecloudnotifier/test/util" "fmt" "github.com/gin-gonic/gin" + "net/url" "testing" ) @@ -147,5 +149,46 @@ func TestReuseFCM(t *testing.T) { } func TestListClients(t *testing.T) { - t.SkipNow() //TODO + ws, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + data := tt.InitDefaultData(t, ws) + + type clientlist struct { + Clients []gin.H `json:"clients"` + } + + type T2 struct { + CT []string + AM []string + AV []string + } + + vals := map[int]T2{ + 1: {[]string{"ANDROID"}, []string{"Galaxy Quest"}, []string{"2022"}}, + 2: {[]string{"IOS", "IOS"}, []string{"GalaxySurfer", "Ocean Explorer"}, []string{"Triple-XXX", "737edc01"}}, + 3: {[]string{"ANDROID"}, []string{"Snow Leopard"}, []string{"1.0.1.99~3"}}, + 8: {[]string{"ANDROID"}, []string{"Galaxy Quest"}, []string{"2023.1"}}, + 9: {[]string{"ANDROID", "IOS", "IOS", "ANDROID"}, []string{"Galaxy Quest", "DreamWeaver", "GalaxySurfer", "Galaxy Quest"}, []string{"2023.2", "Triple-XXX", "Triple-XXX", "2023.1"}}, + 5: {[]string{"IOS"}, []string{"Ocean Explorer"}, []string{"737edc01"}}, + 7: {[]string{"ANDROID"}, []string{"Galaxy Quest"}, []string{"2023.1"}}, + 10: {[]string{}, []string{}, []string{}}, + 14: {[]string{"IOS"}, []string{"StarfireXX"}, []string{"1.x"}}, + 11: {[]string{}, []string{}, []string{}}, + 12: {[]string{"IOS"}, []string{"Ocean Explorer"}, []string{"737edc01"}}, + 13: {[]string{}, []string{}, []string{}}, + 0: {[]string{"IOS"}, []string{"Starfire"}, []string{"2.0"}}, + 4: {[]string{"ANDROID"}, []string{"Thunder-Bolt-4$"}, []string{"#12"}}, + 6: {[]string{"IOS", "IOS"}, []string{"GalaxySurfer", "Cyber Nova"}, []string{"Triple-XXX", "Cyber 4"}}, + 15: {[]string{"IOS"}, []string{"StarfireXX"}, []string{"1.x"}}, + } + + for k, v := range vals { + clist1 := tt.RequestAuthGet[clientlist](t, data.User[k].AdminKey, baseUrl, fmt.Sprintf("/api/users/%s/clients", url.QueryEscape(data.User[k].UID))) + tt.AssertMappedSet(t, fmt.Sprintf("clients[%d]->type", k), v.CT, clist1.Clients, "type") + tt.AssertMappedSet(t, fmt.Sprintf("clients[%d]->agent_model", k), v.AM, clist1.Clients, "agent_model") + tt.AssertMappedSet(t, fmt.Sprintf("clients[%d]->agent_version", k), v.AV, clist1.Clients, "agent_version") + } + + tt.RequestAuthGetShouldFail(t, data.User[0].AdminKey, baseUrl, fmt.Sprintf("/api/users/%s/clients", url.QueryEscape(data.User[1].UID)), 401, apierr.USER_AUTH_FAILED) } diff --git a/scnserver/test/util/common.go b/scnserver/test/util/common.go index cbb0a46..5623968 100644 --- a/scnserver/test/util/common.go +++ b/scnserver/test/util/common.go @@ -333,6 +333,29 @@ func AssertMappedSet[T langext.OrderedConstraint](t *testing.T, key string, expe } } +func AssertMappedArr[T langext.OrderedConstraint](t *testing.T, key string, expected []T, values []gin.H, objkey string) { + + actual := make([]T, 0) + for idx, vv := range values { + if tv, ok := vv[objkey].(T); ok { + actual = append(actual, tv) + } else { + TestFailFmt(t, "[%s]->[%d] is wrong type (expected: %T, actual: %T)", key, idx, *new(T), vv) + } + } + + if !langext.ArrEqualsExact(actual, expected) { + t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual) + + t.Errorf("Actual := [%v]\n", actual) + t.Errorf("Expected := [%v]\n", expected) + + t.Error(string(debug.Stack())) + + t.FailNow() + } +} + func IsWholeFloat[T langext.FloatConstraint](v T) bool { _, frac := math.Modf(math.Abs(float64(v))) return frac == 0.0