databag/net/server/internal/main_test.go

90 lines
1.9 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 (
"testing"
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-15 08:07:00 +00:00
func TestMain(m *testing.M) {
2022-01-12 07:04:27 +00:00
2022-01-17 21:27:48 +00:00
hideLog = true
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();
2022-01-17 07:00:08 +00:00
token := SetToken()
SetAccount(token)
2022-01-15 08:07:00 +00:00
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-17 21:27:48 +00:00
r, w, _ := NewRequest("GET", "/admin/claimable", nil)
2022-01-15 08:07:00 +00:00
GetNodeClaimable(w, r)
2022-01-17 21:27:48 +00:00
var available bool
if ReadResponse(w, &available) != nil {
panic("server not 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-17 21:27:48 +00:00
r, w, _ := NewRequest("PUT", "/admin/claim", nil)
SetCredentials(r, "admin:pass");
2022-01-15 08:07:00 +00:00
SetNodeClaim(w, r)
2022-01-17 21:27:48 +00:00
if ReadResponse(w, nil) != nil {
panic("failed to claim server")
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-17 21:27:48 +00:00
r, w, _ := NewRequest("PUT", "/admin/config", &config)
SetBasicAuth(r, "admin:pass")
SetNodeConfig(w, r)
if ReadResponse(w, nil) != nil {
panic("failed to set 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-17 21:27:48 +00:00
r, w, _ := NewRequest("GET", "/admin/config", nil)
SetBasicAuth(r, "admin:pass")
GetNodeConfig(w, r)
var config NodeConfig
if ReadResponse(w, &config) != nil {
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
}
}
2022-01-17 07:00:08 +00:00
func SetToken() string {
2022-01-17 21:27:48 +00:00
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
SetBasicAuth(r, "admin:pass")
2022-01-17 07:00:08 +00:00
AddNodeAccount(w, r)
var token string
2022-01-17 21:27:48 +00:00
if ReadResponse(w, &token) != nil {
2022-01-17 07:00:08 +00:00
panic("failed to create token")
}
return token
}
func SetAccount(token string) {
2022-01-17 21:27:48 +00:00
r, w, _ := NewRequest("GET", "/account/profile", nil)
SetBearerAuth(r, token);
SetCredentials(r, "test:pass")
2022-01-17 07:00:08 +00:00
AddAccount(w, r)
2022-01-17 21:27:48 +00:00
if ReadResponse(w, nil) != nil {
2022-01-17 07:00:08 +00:00
panic("failed to create account")
}
}