CreateUser test
This commit is contained in:
@@ -10,26 +10,65 @@ import (
|
||||
"blackforestbytes.com/simplecloudnotifier/push"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewSimpleWebserver(t *testing.T) *logic.Application {
|
||||
type Void = struct{}
|
||||
|
||||
uuid, err := langext.NewHexUUID()
|
||||
func NewSimpleWebserver() (*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)
|
||||
|
||||
uuid1, _ := langext.NewHexUUID()
|
||||
uuid2, _ := langext.NewHexUUID()
|
||||
dbdir := filepath.Join(os.TempDir(), uuid1)
|
||||
dbfile := filepath.Join(dbdir, uuid2+".sqlite3")
|
||||
|
||||
err := os.MkdirAll(dbdir, os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dbfile := filepath.Join(os.TempDir(), uuid+"sqlite3")
|
||||
defer func() {
|
||||
_ = os.Remove(dbfile)
|
||||
}()
|
||||
f, err := os.Create(dbfile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.Chmod(dbfile, 0777)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//dbfile := "/home/mike/Code/private/SimpleCloudNotifier/server/.run-data/db_test.sqlite3"
|
||||
|
||||
fmt.Println("DatabaseFile: " + dbfile)
|
||||
|
||||
conf := scn.Config{
|
||||
Namespace: "test",
|
||||
@@ -64,39 +103,94 @@ func NewSimpleWebserver(t *testing.T) *logic.Application {
|
||||
|
||||
router.Init(ginengine)
|
||||
|
||||
return app
|
||||
return app, func() { app.Stop(); _ = os.Remove(dbfile) }
|
||||
}
|
||||
|
||||
func requestGet[T any](t *testing.T, baseURL string, prefix string) T {
|
||||
func requestGet[TResult any](baseURL string, prefix string) TResult {
|
||||
return requestAny[TResult]("", "GET", baseURL, prefix, nil)
|
||||
}
|
||||
|
||||
func requestAuthGet[TResult any](akey string, baseURL string, prefix string) TResult {
|
||||
return requestAny[TResult](akey, "GET", baseURL, prefix, nil)
|
||||
}
|
||||
|
||||
func requestPost[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "POST", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthPost[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "POST", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestPut[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "PUT", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthPUT[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "PUT", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestPatch[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "PATCH", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthPatch[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "PATCH", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestDelete[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "DELETE", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthDelete[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "DELETE", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAny[TResult any](akey string, method string, baseURL string, prefix string, body any) TResult {
|
||||
client := http.Client{}
|
||||
|
||||
req, err := http.NewRequest("GET", baseURL+prefix, bytes.NewReader([]byte{}))
|
||||
bytesbody := make([]byte, 0)
|
||||
if body != nil {
|
||||
bjson, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bytesbody = bjson
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, baseURL+prefix, bytes.NewReader(bytesbody))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return *new(T)
|
||||
panic(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 {
|
||||
t.Error(err)
|
||||
return *new(T)
|
||||
panic(err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
t.Error("Statuscode != 200")
|
||||
}
|
||||
|
||||
respBodyBin, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return *new(T)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var data T
|
||||
if resp.StatusCode != 200 {
|
||||
fmt.Println("Request: " + method + " :: " + baseURL + prefix)
|
||||
fmt.Println(string(respBodyBin))
|
||||
panic("Statuscode != 200")
|
||||
}
|
||||
|
||||
var data TResult
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
t.Error(err)
|
||||
return *new(T)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
29
server/test/user_test.go
Normal file
29
server/test/user_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCreateUserNoClient(t *testing.T) {
|
||||
ws, stop := NewSimpleWebserver()
|
||||
defer stop()
|
||||
go func() { ws.Run() }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
baseUrl := "http://127.0.0.1:" + ws.Port
|
||||
|
||||
res := requestPost[gin.H](baseUrl, "/api/users", gin.H{
|
||||
"no_client": true,
|
||||
})
|
||||
|
||||
uid := fmt.Sprintf("%v", res["user_id"])
|
||||
admintok := res["admin_key"].(string)
|
||||
|
||||
fmt.Printf("uid := %s\n", uid)
|
||||
fmt.Printf("admin_key := %s\n", admintok)
|
||||
|
||||
requestAuthGet[Void](admintok, baseUrl, "/api/users/"+uid)
|
||||
}
|
||||
@@ -6,20 +6,45 @@ import (
|
||||
)
|
||||
|
||||
func TestWebserver(t *testing.T) {
|
||||
ws := NewSimpleWebserver(t)
|
||||
defer ws.Stop()
|
||||
ws, stop := NewSimpleWebserver()
|
||||
defer stop()
|
||||
go func() { ws.Run() }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestPing(t *testing.T) {
|
||||
ws := NewSimpleWebserver(t)
|
||||
defer ws.Stop()
|
||||
ws, stop := NewSimpleWebserver()
|
||||
defer stop()
|
||||
go func() { ws.Run() }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
baseUrl := "http://127.0.0.1:" + ws.Port
|
||||
|
||||
_ = requestGet[struct{}](t, baseUrl, "/api/ping")
|
||||
|
||||
_ = requestGet[Void](baseUrl, "/api/ping")
|
||||
_ = requestPut[Void](baseUrl, "/api/ping", nil)
|
||||
_ = requestPost[Void](baseUrl, "/api/ping", nil)
|
||||
_ = requestPatch[Void](baseUrl, "/api/ping", nil)
|
||||
_ = requestDelete[Void](baseUrl, "/api/ping", nil)
|
||||
}
|
||||
|
||||
func TestMongo(t *testing.T) {
|
||||
ws, stop := NewSimpleWebserver()
|
||||
defer stop()
|
||||
go func() { ws.Run() }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
baseUrl := "http://127.0.0.1:" + ws.Port
|
||||
|
||||
_ = requestPost[Void](baseUrl, "/api/db-test", nil)
|
||||
}
|
||||
|
||||
func TestHealth(t *testing.T) {
|
||||
ws, stop := NewSimpleWebserver()
|
||||
defer stop()
|
||||
go func() { ws.Run() }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
baseUrl := "http://127.0.0.1:" + ws.Port
|
||||
|
||||
_ = requestGet[Void](baseUrl, "/api/health")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user