databag/net/server/internal/main_test.go

91 lines
2.1 KiB
Go
Raw Normal View History

2022-01-15 08:07:00 +00:00
package databag
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"
"databag/internal/store"
2022-01-12 07:04:27 +00:00
)
2022-01-11 17:36:16 +00:00
2022-01-15 08:07:00 +00:00
func TestMain(m *testing.M) {
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-15 08:07:00 +00:00
Claimable();
Claim();
SetConfig();
GetConfig();
m.Run()
2022-01-12 07:16:08 +00:00
}
2022-01-12 07:04:27 +00:00
2022-01-15 08:07:00 +00:00
func Claimable() {
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-15 08:07:00 +00:00
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 {
2022-01-15 08:07:00 +00:00
panic("failed to get claimable response")
2022-01-13 18:06:19 +00:00
}
if resp.StatusCode != 200 {
2022-01-15 08:07:00 +00:00
panic("server not initially claimable")
2022-01-12 07:04:27 +00:00
}
2022-01-11 17:36:16 +00:00
}
2022-01-15 08:07:00 +00:00
func Claim() {
2022-01-12 07:16:08 +00:00
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
2022-01-12 21:12:40 +00:00
r := httptest.NewRequest("PUT", "/admin/claim", nil)
2022-01-17 05:11:24 +00:00
r.Header.Add("Credentials","Basic " + auth)
2022-01-12 07:16:08 +00:00
w := httptest.NewRecorder()
2022-01-15 08:07:00 +00:00
SetNodeClaim(w, r)
2022-01-12 07:16:08 +00:00
if w.Code != 200 {
2022-01-15 08:07:00 +00:00
panic("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 08:07:00 +00:00
func SetConfig() {
config := NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096}
2022-01-12 21:12:40 +00:00
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()
2022-01-15 08:07:00 +00:00
SetNodeConfig(w, r);
2022-01-12 21:12:40 +00:00
if w.Code != 200 {
2022-01-15 08:07:00 +00:00
panic("failed to set node config")
2022-01-12 21:12:40 +00:00
}
2022-01-12 07:16:08 +00:00
}
2022-01-15 05:27:43 +00:00
2022-01-15 08:07:00 +00:00
func GetConfig() {
2022-01-15 05:27:43 +00:00
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
r := httptest.NewRequest("GET", "/admin/config", nil)
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
2022-01-15 08:07:00 +00:00
GetNodeConfig(w, r);
2022-01-15 05:27:43 +00:00
resp := w.Result();
dec := json.NewDecoder(resp.Body);
2022-01-15 08:07:00 +00:00
var config NodeConfig;
2022-01-15 05:27:43 +00:00
dec.Decode(&config);
if resp.StatusCode != 200 {
2022-01-15 08:07:00 +00:00
panic("failed to get node config")
2022-01-15 05:27:43 +00:00
}
if config.Domain != "example.com" {
2022-01-15 08:07:00 +00:00
panic("failed to set config domain");
2022-01-15 05:27:43 +00:00
}
if config.PublicLimit != 1024 {
2022-01-15 08:07:00 +00:00
panic("failed to set public limit");
2022-01-15 05:27:43 +00:00
}
if config.AccountStorage != 4096 {
2022-01-15 08:07:00 +00:00
panic("failed to set account storage");
2022-01-15 05:27:43 +00:00
}
}