feat: init api, db, app

This commit is contained in:
2024-11-07 19:07:41 +00:00
parent ab9b3368d1
commit 11b063c2fa
43 changed files with 1487 additions and 137 deletions
+17
View File
@@ -0,0 +1,17 @@
package tests
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHealthCheck(t *testing.T) {
test := NewTest(t)
res, status, err := test.Fetch("GET", "/health-check", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, "OK", res["data"])
}
+37
View File
@@ -0,0 +1,37 @@
package tests
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAuthLogin(t *testing.T) {
test := NewTest(t)
sessionId := test.WithAuth()
assert.NotEmpty(t, sessionId)
}
func TestAuthGetUser(t *testing.T) {
test := NewTestWithAuth(t)
res, status, err := test.Fetch("GET", "/auth/user", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
assert.NotNil(t, res["user"])
user := res["user"].(map[string]interface{})
assert.NotEmpty(t, user["id"])
}
func TestAuthLogout(t *testing.T) {
test := NewTestWithAuth(t)
_, status, err := test.Fetch("POST", "/auth/logout", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
test.SessionID = ""
}
+74
View File
@@ -0,0 +1,74 @@
package tests
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHostsGetAll(t *testing.T) {
test := NewTestWithAuth(t)
res, status, err := test.Fetch("GET", "/hosts", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
assert.NotNil(t, res["rows"])
}
func TestHostsCreate(t *testing.T) {
test := NewTestWithAuth(t)
data := map[string]interface{}{
"type": "pve",
"label": "test ssh",
"host": "10.0.0.102",
"port": 22,
"keyId": "01jc3wkctzqrcz8qhwynr4p9pe",
}
// data := map[string]interface{}{
// "type": "pve",
// "label": "test pve qemu",
// "host": "10.0.0.1",
// "port": 8006,
// "keyId": "01jc3wkctzqrcz8qhwynr4p9pe",
// "metadata": map[string]interface{}{
// "node": "pve",
// "type": "qemu",
// "vmid": "105",
// },
// }
// data := map[string]interface{}{
// "type": "pve",
// "label": "test pve lxc",
// "host": "10.0.0.1",
// "port": 8006,
// "keyId": "01jc3xcn5qgybbpfppy9pe14ae",
// "metadata": map[string]interface{}{
// "node": "pve",
// "type": "lxc",
// "vmid": "102",
// },
// }
// data := map[string]interface{}{
// "type": "incus",
// "label": "test incus",
// "host": "100.64.0.3",
// "port": 8443,
// "keyId": "01jc3xjcm6ddt4zc0x7g69nv9q",
// "metadata": map[string]interface{}{
// "instance": "test",
// "shell": "/bin/sh",
// },
// }
res, status, err := test.Fetch("POST", "/hosts", &FetchOptions{Body: data})
assert.NoError(t, err)
assert.Equal(t, http.StatusCreated, status)
assert.NotNil(t, res["id"])
}
+55
View File
@@ -0,0 +1,55 @@
package tests
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeychainsGetAll(t *testing.T) {
test := NewTestWithAuth(t)
res, status, err := test.Fetch("GET", "/keychains", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
assert.NotNil(t, res["rows"])
}
func TestKeychainsCreate(t *testing.T) {
test := NewTestWithAuth(t)
data := map[string]interface{}{
"type": "user",
"label": "SSH Key",
"data": map[string]interface{}{
"username": "",
"password": "",
},
}
// data := map[string]interface{}{
// "type": "user",
// "label": "PVE Key",
// "data": map[string]interface{}{
// "username": "root@pam",
// "password": "",
// },
// }
// data := map[string]interface{}{
// "type": "cert",
// "label": "Certificate Key",
// "data": map[string]interface{}{
// "cert": "",
// "key": "",
// },
// }
res, status, err := test.Fetch("POST", "/keychains", &FetchOptions{Body: data})
assert.NoError(t, err)
assert.Equal(t, http.StatusCreated, status)
assert.NotNil(t, res["id"])
}
+24
View File
@@ -0,0 +1,24 @@
package tests
import (
"log"
"os"
"testing"
"rul.sh/vaulterm/db"
)
func TestMain(m *testing.M) {
log.Println("Starting tests...")
test := NewTest(nil)
// Run all tests
code := m.Run()
log.Println("Cleaning up...")
// Clean up
test.Close()
db.Close()
os.Exit(code)
}
+156
View File
@@ -0,0 +1,156 @@
package tests
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"runtime"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"rul.sh/vaulterm/app"
)
type HTTPTest struct {
t *testing.T
app *fiber.App
SessionID string
}
var instance *HTTPTest
func NewTest(t *testing.T) *HTTPTest {
if instance != nil {
return instance
}
instance = &HTTPTest{
t: t,
app: app.NewApp(),
}
return instance
}
func NewTestWithAuth(t *testing.T) *HTTPTest {
test := NewTest(t)
test.WithAuth()
return test
}
func init() {
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "..")
err := os.Chdir(dir)
if err != nil {
panic(err)
}
}
type FetchOptions struct {
Headers map[string]string
Body interface{}
SessionID string
}
type AuthOptions struct {
Username string `json:"username"`
Password string `json:"password"`
}
func (h *HTTPTest) Login(options *AuthOptions) string {
body := options
if options == nil {
body = &AuthOptions{
Username: "admin",
Password: "123456",
}
}
res, status, err := h.Fetch("POST", "/auth/login", &FetchOptions{
Body: body,
})
if h.t != nil {
assert.NoError(h.t, err)
assert.Equal(h.t, http.StatusOK, status)
assert.NotNil(h.t, res["user"])
assert.NotEmpty(h.t, res["sessionId"])
}
return res["sessionId"].(string)
}
func (h *HTTPTest) WithAuth() string {
if h.SessionID != "" {
return h.SessionID
}
sessionId := h.Login(nil)
h.SessionID = sessionId
return sessionId
}
func (h *HTTPTest) Close() {
if h.SessionID != "" {
h.Fetch("POST", "/auth/logout?force=true", nil)
}
h.app.Shutdown()
}
func (h *HTTPTest) Fetch(method string, path string, options *FetchOptions) (map[string]interface{}, int, error) {
var payload io.Reader
headers := map[string]string{}
if options != nil && options.Headers != nil {
headers = options.Headers
}
if options != nil && options.Body != nil {
json, _ := json.Marshal(options.Body)
payload = bytes.NewBuffer(json)
headers["Content-Type"] = "application/json"
}
sessionId := h.SessionID
if options != nil && options.SessionID != "" {
sessionId = options.SessionID
}
if sessionId != "" {
headers["Authorization"] = "Bearer " + sessionId
}
req := httptest.NewRequest(method, path, payload)
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := h.app.Test(req, -1)
if err != nil {
return nil, resp.StatusCode, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, err
}
contentType := resp.Header.Get("Content-Type")
if contentType == "application/json" {
var data map[string]interface{}
if err := json.Unmarshal(body, &data); err != nil {
return nil, resp.StatusCode, err
}
return data, resp.StatusCode, nil
}
data := map[string]interface{}{
"data": string(body),
}
return data, resp.StatusCode, err
}