databag/net/server/internal/addAccount_test.go

45 lines
1.0 KiB
Go
Raw Normal View History

2022-01-15 08:07:00 +00:00
package databag
import (
2022-01-15 22:54:49 +00:00
"log"
2022-01-15 08:07:00 +00:00
"testing"
"net/http/httptest"
"encoding/base64"
"encoding/json"
)
func TestAccount(t *testing.T) {
2022-01-15 22:54:49 +00:00
// acquire new token for creating accounts
2022-01-15 08:07:00 +00:00
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
r := httptest.NewRequest("POST", "/admin/accounts", nil)
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
AddNodeAccount(w, r);
resp := w.Result();
dec := json.NewDecoder(resp.Body);
var token string;
dec.Decode(&token);
if resp.StatusCode != 200 {
t.Errorf("failed to create account")
}
2022-01-15 22:54:49 +00:00
// validate account token
r = httptest.NewRequest("GET", "/account/token", nil)
r.Header.Add("Authorization","Bearer " + token)
w = httptest.NewRecorder()
GetAccountToken(w, r);
resp = w.Result();
if resp.StatusCode != 200 {
t.Errorf("invalid token value")
}
dec = json.NewDecoder(resp.Body);
var tokenType string;
dec.Decode(&tokenType);
if tokenType != "create" {
t.Errorf("invalid token type")
}
log.Println("TestAccount: done");
2022-01-15 08:07:00 +00:00
}