Refactor server to go-sqlite and ginext [WIP]

This commit is contained in:
2024-07-16 18:06:26 +02:00
parent c204dc5a8b
commit ca05d6e3cc
4 changed files with 331 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"gopkg.in/loremipsum.v1"
"testing"
@@ -59,10 +60,15 @@ type clientex struct {
}
type Userdat struct {
UID string
SendKey string
AdminKey string
ReadKey string
UID string
SendKey string
AdminKey string
ReadKey string
Clients []string
Channels []string
Messages []string
Keys []string
Subscriptions []string
}
const PX = -1
@@ -367,7 +373,8 @@ func InitDefaultData(t *testing.T, ws *logic.Application) DefData {
body["agent_version"] = cex.AgentVersion
body["client_type"] = cex.ClientType
body["fcm_token"] = cex.FCMTok
RequestAuthPost[gin.H](t, users[cex.User].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/clients", users[cex.User].UID), body)
r0 := RequestAuthPost[gin.H](t, users[cex.User].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/clients", users[cex.User].UID), body)
users[cex.User].Clients = append(users[cex.User].Clients, r0["client_id"].(string))
}
// Create Messages
@@ -398,7 +405,8 @@ func InitDefaultData(t *testing.T, ws *logic.Application) DefData {
body["timestamp"] = (time.Now().Add(mex.TSOffset)).Unix()
}
RequestPost[gin.H](t, baseUrl, "/", body)
r0 := RequestPost[gin.H](t, baseUrl, "/", body)
users[mex.User].Messages = append(users[mex.User].Messages, r0["scn_msg_id"].(string))
}
// create manual channels
@@ -407,6 +415,45 @@ func InitDefaultData(t *testing.T, ws *logic.Application) DefData {
RequestAuthPost[Void](t, users[9].AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", users[9].UID), gin.H{"name": "manual@chan"})
}
// list channels
for i, usr := range users {
type schan struct {
ID string `json:"channel_id"`
}
type chanlist struct {
Channels []schan `json:"channels"`
}
r0 := RequestAuthGet[chanlist](t, usr.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels?selector=%s", usr.UID, "owned"))
users[i].Channels = langext.ArrMap(r0.Channels, func(v schan) string { return v.ID })
}
// list keys
for i, usr := range users {
type skey struct {
ID string `json:"keytoken_id"`
}
type keylist struct {
Keys []skey `json:"channels"`
}
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 })
}
// list subscriptions
for i, usr := range users {
type ssub struct {
ID string `json:"subscription_id"`
}
type sublist struct {
Subs []ssub `json:"channels"`
}
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 })
}
// Sub/Unsub for Users 12+13
{
@@ -474,7 +521,7 @@ func InitSingleData(t *testing.T, ws *logic.Application) SingleData {
func doSubscribe(t *testing.T, baseUrl string, user Userdat, chanOwner Userdat, chanInternalName string) {
if user == chanOwner {
if user.UID == chanOwner.UID {
RequestAuthPost[Void](t, user.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", user.UID), gin.H{
"channel_owner_user_id": chanOwner.UID,

View File

@@ -26,6 +26,10 @@ func RequestAuthGet[TResult any](t *testing.T, akey string, baseURL string, urlS
return RequestAny[TResult](t, akey, "GET", baseURL, urlSuffix, nil, true)
}
func RequestAuthGetRaw(t *testing.T, akey string, baseURL string, urlSuffix string) string {
return RequestAny[string](t, akey, "GET", baseURL, urlSuffix, nil, false)
}
func RequestPost[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body, true)
}
@@ -166,14 +170,22 @@ func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL s
TestFailFmt(t, "Statuscode != 200 (actual = %d)", resp.StatusCode)
}
var data TResult
if deserialize {
var data TResult
if err := json.Unmarshal(respBodyBin, &data); err != nil {
TestFailErr(t, err)
return data
}
return data
} else {
if _, ok := (any(*new(TResult))).([]byte); ok {
return any(respBodyBin).(TResult)
} else if _, ok := (any(*new(TResult))).(string); ok {
return any(string(respBodyBin)).(TResult)
} else {
return *new(TResult)
}
}
return data
}
func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, expectedStatusCode int, errcode apierr.APIError) {

View File

@@ -0,0 +1,118 @@
package util
import (
"encoding/json"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"reflect"
"testing"
"time"
)
func AssertJsonStructureMatch(t *testing.T, key string, jsonData string, expected map[string]any) {
realData := make(map[string]any)
err := json.Unmarshal([]byte(jsonData), &realData)
if err != nil {
t.Errorf("Failed to decode json of [%s]: %s", key, err.Error())
return
}
AssertJsonStructureMatchOfMap(t, key, realData, expected)
}
func AssertJsonStructureMatchOfMap(t *testing.T, key string, realData map[string]any, expected map[string]any) {
for k := range expected {
if _, ok := realData[k]; !ok {
t.Errorf("Missing Key in data '%s': [[%s]]", key, k)
}
}
for k := range realData {
if _, ok := expected[k]; !ok {
t.Errorf("Additional key in data '%s': [[%s]]", key, k)
}
}
for k, v := range realData {
schema, ok := expected[k]
if !ok {
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)
}
}
}