added GetNodeConfig endpoint

This commit is contained in:
Roland Osborne 2022-01-14 21:27:43 -08:00
parent 102646a755
commit 986a161787
2 changed files with 51 additions and 3 deletions

View File

@ -63,13 +63,35 @@ func GetNodeAccounts(w http.ResponseWriter, r *http.Request) {
func GetNodeClaimable(w http.ResponseWriter, r *http.Request) {
c := getBoolConfigValue(CONFIG_CONFIGURED, false);
body, _ := json.Marshal(!c);
body, err := json.Marshal(!c);
if err != nil {
log.Println("GetNodeClaimable - failed to marshal response");
}
w.Write(body);
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func GetNodeConfig(w http.ResponseWriter, r *http.Request) {
// validate login
if !adminLogin(r) {
log.Printf("SetNodeConfig - invalid admin credentials");
w.WriteHeader(http.StatusUnauthorized);
return
}
// get node config fields
var config NodeConfig;
config.Domain = getStrConfigValue(CONFIG_DOMAIN, "");
config.PublicLimit = getNumConfigValue(CONFIG_PUBLICLIMIT, 0);
config.AccountStorage = getNumConfigValue(CONFIG_STORAGE, 0);
body, err := json.Marshal(config);
if err != nil {
log.Println("GetNodeConfig - failed to marshal response");
}
w.Write(body);
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -16,7 +16,8 @@ func TestSetup(t *testing.T) {
//store.SetPath("databag.db");
Claimable(t);
Claim(t);
Config(t);
SetConfig(t);
GetConfig(t);
}
func Claimable(t *testing.T) {
@ -48,7 +49,7 @@ func Claim(t *testing.T) {
}
}
func Config(t *testing.T) {
func SetConfig(t *testing.T) {
config := app.NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096}
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
body,_ := json.Marshal(config)
@ -60,3 +61,28 @@ func Config(t *testing.T) {
t.Errorf("failed to set node config")
}
}
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");
}
}