Tests[UpdateUsername, RecreateKeys, DeleteUser]
This commit is contained in:
96
server/test/util/common.go
Normal file
96
server/test/util/common.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func AssertJsonMapEqual(t *testing.T, key string, expected map[string]any, actual map[string]any) {
|
||||
mkeys := make(map[string]string)
|
||||
for k := range expected {
|
||||
mkeys[k] = k
|
||||
}
|
||||
for k := range actual {
|
||||
mkeys[k] = k
|
||||
}
|
||||
|
||||
for mapkey := range mkeys {
|
||||
|
||||
if _, ok := expected[mapkey]; !ok {
|
||||
TestFailFmt(t, "Missing Key expected['%s'] ( assertJsonMapEqual[%s] )", mapkey, key)
|
||||
}
|
||||
if _, ok := actual[mapkey]; !ok {
|
||||
TestFailFmt(t, "Missing Key actual['%s'] ( assertJsonMapEqual[%s] )", mapkey, key)
|
||||
}
|
||||
|
||||
AssertEqual(t, key+"."+mapkey, expected[mapkey], actual[mapkey])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func AssertEqual(t *testing.T, key string, expected any, actual any) {
|
||||
if expected != actual {
|
||||
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
str1 := fmt.Sprintf("%v", expected)
|
||||
str2 := fmt.Sprintf("%v", actual)
|
||||
|
||||
if strings.Contains(str1, "\n") {
|
||||
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected)
|
||||
} else {
|
||||
t.Errorf("Actual := \"%v\"\n", expected)
|
||||
}
|
||||
|
||||
if strings.Contains(str2, "\n") {
|
||||
t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
|
||||
} else {
|
||||
t.Errorf("Expected := \"%v\"\n", actual)
|
||||
}
|
||||
|
||||
t.Error(string(debug.Stack()))
|
||||
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func AssertNotEqual(t *testing.T, key string, expected any, actual any) {
|
||||
if expected == actual {
|
||||
t.Errorf("Value [%s] does not differ (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
str1 := fmt.Sprintf("%v", expected)
|
||||
str2 := fmt.Sprintf("%v", actual)
|
||||
|
||||
if strings.Contains(str1, "\n") {
|
||||
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected)
|
||||
} else {
|
||||
t.Errorf("Actual := \"%v\"\n", expected)
|
||||
}
|
||||
|
||||
if strings.Contains(str2, "\n") {
|
||||
t.Errorf("Not Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
|
||||
} else {
|
||||
t.Errorf("Not Expected := \"%v\"\n", actual)
|
||||
}
|
||||
|
||||
t.Error(string(debug.Stack()))
|
||||
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestFail(t *testing.T, msg string) {
|
||||
t.Error(msg)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func TestFailFmt(t *testing.T, format string, args ...any) {
|
||||
t.Errorf(format, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func TestFailErr(t *testing.T, e error) {
|
||||
t.Error(fmt.Sprintf("Failed with error:\n%s\n\nError:\n%+v\n\nTrace:\n%s", e.Error(), e, string(debug.Stack())))
|
||||
t.FailNow()
|
||||
}
|
||||
197
server/test/util/requests.go
Normal file
197
server/test/util/requests.go
Normal file
@@ -0,0 +1,197 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func RequestGet[TResult any](t *testing.T, baseURL string, urlSuffix string) TResult {
|
||||
return RequestAny[TResult](t, "", "GET", baseURL, urlSuffix, nil)
|
||||
}
|
||||
|
||||
func RequestAuthGet[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string) TResult {
|
||||
return RequestAny[TResult](t, akey, "GET", baseURL, urlSuffix, nil)
|
||||
}
|
||||
|
||||
func RequestPost[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestAuthPost[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "POST", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestPut[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "PUT", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestAuthPUT[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "PUT", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestPatch[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "PATCH", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestAuthPatch[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "PATCH", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestDelete[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "DELETE", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestAuthDelete[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "DELETE", baseURL, urlSuffix, body)
|
||||
}
|
||||
|
||||
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any) TResult {
|
||||
client := http.Client{}
|
||||
|
||||
fmt.Printf("[-> REQUEST] (%s) %s%s [%s] [%s]\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), langext.Conditional(body == nil, "NO BODY", "BODY"))
|
||||
|
||||
bytesbody := make([]byte, 0)
|
||||
if body != nil {
|
||||
bjson, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
bytesbody = bjson
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, baseURL+urlSuffix, bytes.NewReader(bytesbody))
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
if akey != "" {
|
||||
req.Header.Set("Authorization", "SCN "+akey)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBodyBin, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
fmt.Printf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode)
|
||||
fmt.Println(langext.TryPrettyPrintJson(string(respBodyBin)))
|
||||
fmt.Println("---------------- -------- ----------------")
|
||||
fmt.Println("")
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
TestFail(t, "Statuscode != 200")
|
||||
}
|
||||
|
||||
var data TResult
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func RequestAuthGetShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "GET", baseURL, urlSuffix, nil, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthPostShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "POST", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthPatchShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "PATCH", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthDeleteShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
RequestAuthAnyShouldFail(t, akey, "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
||||
client := http.Client{}
|
||||
|
||||
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)
|
||||
if body != nil {
|
||||
bjson, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
bytesbody = bjson
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, baseURL+urlSuffix, bytes.NewReader(bytesbody))
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
if akey != "" {
|
||||
req.Header.Set("Authorization", "SCN "+akey)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBodyBin, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
fmt.Printf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode)
|
||||
fmt.Println(langext.TryPrettyPrintJson(string(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)
|
||||
}
|
||||
|
||||
var data gin.H
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
if v, ok := data["success"]; ok {
|
||||
if v.(bool) {
|
||||
TestFail(t, "Success == true (expected failure)")
|
||||
}
|
||||
} else {
|
||||
TestFail(t, "missing response['success']")
|
||||
}
|
||||
|
||||
if v, ok := data["error"]; ok {
|
||||
if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", errcode) {
|
||||
TestFailFmt(t, "wrong errorcode (expected: %d), (actual: %v)", errcode, v)
|
||||
}
|
||||
} else {
|
||||
TestFail(t, "missing response['error']")
|
||||
}
|
||||
}
|
||||
107
server/test/util/webserver.go
Normal file
107
server/test/util/webserver.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
scn "blackforestbytes.com/simplecloudnotifier"
|
||||
"blackforestbytes.com/simplecloudnotifier/api"
|
||||
"blackforestbytes.com/simplecloudnotifier/common/ginext"
|
||||
"blackforestbytes.com/simplecloudnotifier/db"
|
||||
"blackforestbytes.com/simplecloudnotifier/google"
|
||||
"blackforestbytes.com/simplecloudnotifier/jobs"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/push"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Void = struct{}
|
||||
|
||||
func StartSimpleWebserver(t *testing.T) (*logic.Application, func()) {
|
||||
cw := zerolog.ConsoleWriter{
|
||||
Out: os.Stdout,
|
||||
TimeFormat: "2006-01-02 15:04:05 Z07:00",
|
||||
}
|
||||
|
||||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||
multi := zerolog.MultiLevelWriter(cw)
|
||||
logger := zerolog.New(multi).With().
|
||||
Timestamp().
|
||||
Caller().
|
||||
Logger()
|
||||
|
||||
log.Logger = logger
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
|
||||
uuid2, _ := langext.NewHexUUID()
|
||||
dbdir := t.TempDir()
|
||||
dbfile := filepath.Join(dbdir, uuid2+".sqlite3")
|
||||
|
||||
err := os.MkdirAll(dbdir, os.ModePerm)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
f, err := os.Create(dbfile)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
err = os.Chmod(dbfile, 0777)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
fmt.Println("DatabaseFile: " + dbfile)
|
||||
|
||||
conf := scn.Config{
|
||||
Namespace: "test",
|
||||
GinDebug: true,
|
||||
ServerIP: "0.0.0.0",
|
||||
ServerPort: "0", // simply choose a free port
|
||||
DBFile: dbfile,
|
||||
RequestTimeout: 30 * time.Second,
|
||||
ReturnRawErrors: true,
|
||||
DummyFirebase: true,
|
||||
}
|
||||
|
||||
sqlite, err := db.NewDatabase(dbfile)
|
||||
if err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
app := logic.NewApp(sqlite)
|
||||
|
||||
if err := app.Migrate(); err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
|
||||
ginengine := ginext.NewEngine(conf)
|
||||
|
||||
router := api.NewRouter(app)
|
||||
|
||||
nc := push.NewTestSink()
|
||||
|
||||
apc := google.NewDummy()
|
||||
|
||||
jobRetry := jobs.NewDeliveryRetryJob(app)
|
||||
app.Init(conf, ginengine, nc, apc, []logic.Job{jobRetry})
|
||||
|
||||
router.Init(ginengine)
|
||||
|
||||
stop := func() { app.Stop(); _ = os.Remove(dbfile) }
|
||||
go func() { app.Run() }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return app, stop
|
||||
}
|
||||
Reference in New Issue
Block a user