databag/net/server/main_test.go

89 lines
2.2 KiB
Go
Raw Normal View History

2022-01-12 07:04:27 +00:00
package main
2022-01-11 17:36:16 +00:00
2022-01-12 07:04:27 +00:00
import (
2022-01-12 21:12:40 +00:00
"strings"
2022-01-12 07:04:27 +00:00
"testing"
"net/http/httptest"
2022-01-12 07:16:08 +00:00
"encoding/base64"
2022-01-12 21:12:40 +00:00
"encoding/json"
2022-01-12 07:04:27 +00:00
app "databag/internal"
2022-01-12 21:12:40 +00:00
"databag/internal/store"
2022-01-12 07:04:27 +00:00
)
2022-01-11 17:36:16 +00:00
2022-01-12 07:16:08 +00:00
func TestSetup(t *testing.T) {
2022-01-12 07:04:27 +00:00
store.SetPath("file::memory:?cache=shared");
2022-01-12 21:12:40 +00:00
//store.SetPath("databag.db");
2022-01-12 07:16:08 +00:00
Claimable(t);
Claim(t);
2022-01-15 05:27:43 +00:00
SetConfig(t);
GetConfig(t);
2022-01-12 07:16:08 +00:00
}
2022-01-12 07:04:27 +00:00
2022-01-12 07:16:08 +00:00
func Claimable(t *testing.T) {
2022-01-12 21:12:40 +00:00
r := httptest.NewRequest("GET", "/admin/claimable", nil)
2022-01-12 07:04:27 +00:00
w := httptest.NewRecorder()
2022-01-12 21:12:40 +00:00
app.GetNodeClaimable(w, r)
2022-01-13 18:06:19 +00:00
//body, _ := ioutil.ReadAll(resp.Body)
resp := w.Result()
dec := json.NewDecoder(resp.Body);
var res bool
err := dec.Decode(&res)
if err != nil {
t.Errorf("failed to get claimable response")
}
if resp.StatusCode != 200 {
2022-01-12 21:12:40 +00:00
t.Errorf("server not initially claimable")
2022-01-12 07:04:27 +00:00
}
2022-01-11 17:36:16 +00:00
}
2022-01-12 07:16:08 +00:00
func Claim(t *testing.T) {
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
2022-01-12 21:12:40 +00:00
r := httptest.NewRequest("PUT", "/admin/claim", nil)
2022-01-12 07:16:08 +00:00
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
2022-01-12 21:12:40 +00:00
app.SetNodeClaim(w, r)
2022-01-12 07:16:08 +00:00
if w.Code != 200 {
2022-01-12 21:12:40 +00:00
t.Errorf("server not initially claimable")
2022-01-12 07:16:08 +00:00
}
2022-01-12 21:12:40 +00:00
}
2022-01-12 07:16:08 +00:00
2022-01-15 05:27:43 +00:00
func SetConfig(t *testing.T) {
2022-01-12 21:12:40 +00:00
config := app.NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096}
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
body,_ := json.Marshal(config)
r := httptest.NewRequest("PUT", "/admin/config", strings.NewReader(string(body)))
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
app.SetNodeConfig(w, r);
if w.Code != 200 {
t.Errorf("failed to set node config")
}
2022-01-12 07:16:08 +00:00
}
2022-01-15 05:27:43 +00:00
func GetConfig(t *testing.T) {
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
r := httptest.NewRequest("GET", "/admin/config", nil)
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
app.GetNodeConfig(w, r);
resp := w.Result();
dec := json.NewDecoder(resp.Body);
var config app.NodeConfig;
dec.Decode(&config);
if resp.StatusCode != 200 {
t.Errorf("failed to get node config")
}
if config.Domain != "example.com" {
t.Error("failed to set config domain");
}
if config.PublicLimit != 1024 {
t.Error("failed to set public limit");
}
if config.AccountStorage != 4096 {
t.Error("failed to set account storage");
}
}