Fix TestRequestLogAPI
This commit is contained in:
@@ -435,7 +435,7 @@ func InitDefaultData(t *testing.T, ws *logic.Application) DefData {
|
||||
ID string `json:"keytoken_id"`
|
||||
}
|
||||
type keylist struct {
|
||||
Keys []skey `json:"channels"`
|
||||
Keys []skey `json:"keys"`
|
||||
}
|
||||
r0 := RequestAuthGet[keylist](t, usr.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys", usr.UID))
|
||||
users[i].Keys = langext.ArrMap(r0.Keys, func(v skey) string { return v.ID })
|
||||
@@ -448,10 +448,10 @@ func InitDefaultData(t *testing.T, ws *logic.Application) DefData {
|
||||
ID string `json:"subscription_id"`
|
||||
}
|
||||
type sublist struct {
|
||||
Subs []ssub `json:"channels"`
|
||||
Subs []ssub `json:"subscriptions"`
|
||||
}
|
||||
r0 := RequestAuthGet[sublist](t, usr.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?direction=%s&confirmation=%s", usr.UID, "outgoing", "confirmed"))
|
||||
users[i].Keys = langext.ArrMap(r0.Subs, func(v ssub) string { return v.ID })
|
||||
users[i].Subscriptions = langext.ArrMap(r0.Subs, func(v ssub) string { return v.ID })
|
||||
}
|
||||
|
||||
// Sub/Unsub for Users 12+13
|
||||
@@ -510,13 +510,15 @@ func InitSingleData(t *testing.T, ws *logic.Application) SingleData {
|
||||
|
||||
success = true
|
||||
|
||||
return SingleData{
|
||||
sd := SingleData{
|
||||
UID: r0.UserId,
|
||||
AdminKey: r0.AdminKey,
|
||||
SendKey: r0.SendKey,
|
||||
ReadKey: r0.ReadKey,
|
||||
ClientID: r0.Clients[0].ClientId,
|
||||
}
|
||||
|
||||
return sd
|
||||
}
|
||||
|
||||
func doSubscribe(t *testing.T, baseUrl string, user Userdat, chanOwner Userdat, chanInternalName string) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -18,101 +19,158 @@ func AssertJsonStructureMatch(t *testing.T, key string, jsonData string, expecte
|
||||
return
|
||||
}
|
||||
|
||||
AssertJsonStructureMatchOfMap(t, key, realData, expected)
|
||||
assertjsonStructureMatchMapObject(t, expected, realData, key)
|
||||
}
|
||||
|
||||
func AssertJsonStructureMatchOfMap(t *testing.T, key string, realData map[string]any, expected map[string]any) {
|
||||
func assertJsonStructureMatch(t *testing.T, schema any, realValue any, keyPath string) {
|
||||
|
||||
for k := range expected {
|
||||
if _, ok := realData[k]; !ok {
|
||||
t.Errorf("Missing Key in data '%s': [[%s]]", key, k)
|
||||
if strschema, ok := schema.(string); ok {
|
||||
|
||||
assertjsonStructureMatchSingleValue(t, strschema, realValue, keyPath)
|
||||
|
||||
} else if mapschema, ok := schema.(map[string]any); ok {
|
||||
|
||||
if reflect.ValueOf(realValue).Kind() != reflect.Map {
|
||||
t.Errorf("Key < %s > is not a object (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
if _, ok := realValue.(map[string]any); !ok {
|
||||
t.Errorf("Key < %s > is not a object[recursive] (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
|
||||
assertjsonStructureMatchMapObject(t, mapschema, realValue.(map[string]any), keyPath)
|
||||
|
||||
} else if arrschema, ok := schema.([]any); ok && len(arrschema) == 1 {
|
||||
|
||||
if _, ok := realValue.([]any); !ok {
|
||||
t.Errorf("Key < %s > is not a array[recursive] (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
|
||||
assertjsonStructureMatchArray(t, arrschema, realValue.([]any), keyPath)
|
||||
|
||||
} else {
|
||||
t.Errorf("Unknown schema type '%s' for key < %s >", schema, keyPath)
|
||||
}
|
||||
}
|
||||
|
||||
func assertjsonStructureMatchSingleValue(t *testing.T, strschema string, realValue any, keyPath string) {
|
||||
switch strschema {
|
||||
case "id":
|
||||
if _, ok := realValue.(string); !ok {
|
||||
t.Errorf("Key < %s > is not a string<id> (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
if len(realValue.(string)) != 24 { //TODO validate checksum?
|
||||
t.Errorf("Key < %s > is not a valid entity-id date (its '%v')", keyPath, realValue)
|
||||
return
|
||||
}
|
||||
case "string":
|
||||
if _, ok := realValue.(string); !ok {
|
||||
t.Errorf("Key < %s > is not a string (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
case "null":
|
||||
if !langext.IsNil(realValue) {
|
||||
t.Errorf("Key < %s > is not a NULL (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
case "string|null":
|
||||
if langext.IsNil(realValue) {
|
||||
return // OK
|
||||
} else if _, ok := realValue.(string); !ok {
|
||||
return // OK
|
||||
} else {
|
||||
t.Errorf("Key < %s > is not a string|null (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
case "rfc3339":
|
||||
if _, ok := realValue.(string); !ok {
|
||||
t.Errorf("Key < %s > is not a string<rfc3339> (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339, realValue.(string)); err != nil {
|
||||
t.Errorf("Key < %s > is not a valid rfc3339 date (its '%v')", keyPath, realValue)
|
||||
return
|
||||
}
|
||||
case "rfc3339|null":
|
||||
if langext.IsNil(realValue) {
|
||||
return // OK
|
||||
}
|
||||
if _, ok := realValue.(string); !ok {
|
||||
t.Errorf("Key < %s > is not a string<rfc3339> (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339, realValue.(string)); err != nil {
|
||||
t.Errorf("Key < %s > is not a valid rfc3339 date (its '%v')", keyPath, realValue)
|
||||
return
|
||||
}
|
||||
case "int":
|
||||
if _, ok := realValue.(float64); !ok {
|
||||
t.Errorf("Key < %s > is not a int (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
if realValue.(float64) != float64(int(realValue.(float64))) {
|
||||
t.Errorf("Key < %s > is not a int (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
case "float":
|
||||
if _, ok := realValue.(float64); !ok {
|
||||
t.Errorf("Key < %s > is not a int (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
case "bool":
|
||||
if _, ok := realValue.(bool); !ok {
|
||||
t.Errorf("Key < %s > is not a int (its actually %T: '%v')", keyPath, realValue, realValue)
|
||||
return
|
||||
}
|
||||
default:
|
||||
t.Errorf("Unknown schema type '%s' for key < %s >", strschema, keyPath)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func assertjsonStructureMatchMapObject(t *testing.T, mapschema map[string]any, realValue map[string]any, keyPath string) {
|
||||
|
||||
for k := range mapschema {
|
||||
if _, ok := realValue[k]; !ok {
|
||||
t.Errorf("Missing Key: < %s >", keyPath)
|
||||
}
|
||||
}
|
||||
|
||||
for k := range realData {
|
||||
if _, ok := expected[k]; !ok {
|
||||
t.Errorf("Additional key in data '%s': [[%s]]", key, k)
|
||||
for k := range realValue {
|
||||
if _, ok := mapschema[k]; !ok {
|
||||
t.Errorf("Additional key: < %s >", keyPath)
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range realData {
|
||||
for k, v := range realValue {
|
||||
|
||||
schema, ok := expected[k]
|
||||
kpath := keyPath + "." + k
|
||||
|
||||
schema, ok := mapschema[k]
|
||||
|
||||
if !ok {
|
||||
t.Errorf("Key < %s > is missing in response", kpath)
|
||||
continue
|
||||
}
|
||||
|
||||
if strschema, ok := schema.(string); ok {
|
||||
switch strschema {
|
||||
case "id":
|
||||
if _, ok := v.(string); !ok {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a string<id> (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
if len(v.(string)) != 24 { //TODO validate checksum?
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a valid entity-id date (its '%v')", k, key, v)
|
||||
continue
|
||||
}
|
||||
case "string":
|
||||
if _, ok := v.(string); !ok {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a string (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
case "null":
|
||||
if !langext.IsNil(v) {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a NULL (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
case "rfc3339":
|
||||
if _, ok := v.(string); !ok {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a string<rfc3339> (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339, v.(string)); err != nil {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a valid rfc3339 date (its '%v')", k, key, v)
|
||||
continue
|
||||
}
|
||||
case "int":
|
||||
if _, ok := v.(float64); !ok {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a int (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
if v.(float64) != float64(int(v.(float64))) {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a int (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
case "float":
|
||||
if _, ok := v.(float64); !ok {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a int (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
case "bool":
|
||||
if _, ok := v.(bool); !ok {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a int (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
case "object":
|
||||
if reflect.ValueOf(v).Kind() != reflect.Map {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a object (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
case "array":
|
||||
if reflect.ValueOf(v).Kind() != reflect.Array {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a array (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else if mapschema, ok := schema.(map[string]any); ok {
|
||||
if reflect.ValueOf(v).Kind() != reflect.Map {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a object (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
if _, ok := v.(map[string]any); !ok {
|
||||
t.Errorf("Key [[%s]] in data '%s' is not a object[recursive] (its actually %T: '%v')", k, key, v, v)
|
||||
continue
|
||||
}
|
||||
AssertJsonStructureMatchOfMap(t, key+".["+k+"]", v.(map[string]any), mapschema)
|
||||
}
|
||||
assertJsonStructureMatch(t, schema, v, kpath)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func assertjsonStructureMatchArray(t *testing.T, arrschema []any, realValue []any, keyPath string) {
|
||||
|
||||
if len(arrschema) != 1 {
|
||||
t.Errorf("Array schema must have exactly one element, but got %d", len(arrschema))
|
||||
return
|
||||
}
|
||||
|
||||
for i, realArrVal := range realValue {
|
||||
assertJsonStructureMatch(t, arrschema[0], realArrVal, fmt.Sprintf("%s[%d]", keyPath, i))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user