Skip to content

📚 The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Setup new tests

Smaug#67390ced3c7bdd13
diff --git a/backend/tests/client.go b/backend/tests/client.go
new file mode 100644
index 00000000..1c899933
--- /dev/null
+++ b/backend/tests/client.go
@@ -0,0 +1,21 @@
+package tests
+
+import (
+ "net/http"
+ "net/http/cookiejar"
+ "testing"
+)
+
+var client *http.Client
+
+func InitClient(t *testing.T) *http.Client {
+ if client != nil {
+ return client
+ }
+ jar, err := cookiejar.New(nil)
+ if err != nil {
+ t.Fatalf("Error with client init: %v", err)
+ }
+ client = &http.Client{Jar: jar}
+ return client
+}
diff --git a/backend/tests/helpers.go b/backend/tests/helpers.go
new file mode 100644
index 00000000..cdc4f0bd
--- /dev/null
+++ b/backend/tests/helpers.go
@@ -0,0 +1,74 @@
+package tests
+
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+ "net/http"
+ "strings"
+ "testing"
+)
+
+const BaseURL = "http://localhost:8080/api"
+
+type APIResponse struct {
+ Status string `json:"status"`
+ Timestamp int64 `json:"timestamp"`
+ Message string `json:"message,omitempty"`
+ Result any `json:"result,omitempty"`
+ StatusCode int
+}
+
+func DoPost(t *testing.T, client *http.Client, path string, body any) APIResponse {
+ var contentType string
+ var reader io.Reader
+
+ switch b := body.(type) {
+ case map[string]any:
+ jsonBody, err := json.Marshal(b)
+ if err != nil {
+ t.Fatalf("Erreur de marshal JSON: %v", err)
+ }
+ reader = bytes.NewReader(jsonBody)
+ if contentType == "" {
+ contentType = "application/json"
+ }
+ case string:
+ reader = strings.NewReader(b)
+ if contentType == "" {
+ contentType = "application/x-www-form-urlencoded"
+ }
+ case nil:
+ reader = nil
+ default:
+ t.Fatalf("Type de body non supporté: %T", body)
+ }
+ resp, err := client.Post(BaseURL+path, contentType, reader)
+ if err != nil {
+ t.Fatalf("POST %s failed: %v", path, err)
+ }
+ val := ParseJSONResponse[APIResponse](t, resp)
+ val.StatusCode = resp.StatusCode
+ return val
+}
+
+func DoGet(t *testing.T, client *http.Client, path string) *http.Response {
+ resp, err := client.Get(BaseURL + path)
+ if err != nil {
+ t.Fatalf("GET %s failed: %v", path, err)
+ }
+ return resp
+}
+
+func ParseJSONResponse[T any](t *testing.T, resp *http.Response) T {
+ defer resp.Body.Close()
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ t.Fatalf("Error during response reading: %v", err)
+ }
+ var data T
+ if err := json.Unmarshal(body, &data); err != nil {
+ t.Fatalf("Error unmarshal JSON: %v\nBody:\n%s", err, body)
+ }
+ return data
+}