2022-01-15 08:07:00 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"net/http/httptest"
|
|
|
|
"encoding/base64"
|
|
|
|
)
|
|
|
|
|
2022-01-17 07:00:08 +00:00
|
|
|
func TestAddAccount(t *testing.T) {
|
2022-01-15 08:07:00 +00:00
|
|
|
|
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()
|
2022-01-17 05:11:24 +00:00
|
|
|
AddNodeAccount(w, r)
|
|
|
|
var token string
|
2022-01-17 21:27:48 +00:00
|
|
|
if ReadResponse(w, &token) != nil {
|
|
|
|
t.Errorf("failed to create token");
|
2022-01-16 07:25:43 +00:00
|
|
|
return
|
2022-01-15 08:07:00 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2022-01-17 05:11:24 +00:00
|
|
|
GetAccountToken(w, r)
|
|
|
|
var tokenType string
|
2022-01-17 21:27:48 +00:00
|
|
|
if ReadResponse(w, &tokenType) != nil {
|
|
|
|
t.Errorf("failed to validate token")
|
2022-01-16 07:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if username is available
|
2022-01-17 05:11:24 +00:00
|
|
|
r = httptest.NewRequest("GET", "/account/claimable?username=user", nil)
|
2022-01-16 07:25:43 +00:00
|
|
|
r.Header.Add("Authorization","Bearer " + token)
|
|
|
|
w = httptest.NewRecorder()
|
2022-01-17 05:11:24 +00:00
|
|
|
GetAccountUsername(w, r)
|
2022-01-17 21:27:48 +00:00
|
|
|
var available bool
|
|
|
|
if ReadResponse(w, &available) != nil {
|
|
|
|
t.Errorf("failed to check username")
|
2022-01-16 07:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !available {
|
|
|
|
t.Errorf("username not available")
|
|
|
|
return
|
2022-01-15 22:54:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 05:11:24 +00:00
|
|
|
// create account
|
|
|
|
auth = base64.StdEncoding.EncodeToString([]byte("user:pass"))
|
|
|
|
r = httptest.NewRequest("GET", "/account/profile", nil)
|
|
|
|
r.Header.Add("Credentials","Basic " + auth)
|
|
|
|
r.Header.Add("Authorization","Bearer " + token)
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
AddAccount(w, r)
|
|
|
|
var profile Profile
|
2022-01-17 21:27:48 +00:00
|
|
|
if ReadResponse(w, &profile) != nil {
|
|
|
|
t.Errorf("failed to create account")
|
2022-01-17 05:11:24 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-17 06:46:55 +00:00
|
|
|
|
|
|
|
// acquire new token for creating accounts
|
|
|
|
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)
|
2022-01-17 21:27:48 +00:00
|
|
|
if ReadResponse(w, &token) != nil {
|
|
|
|
t.Errorf("failed to create token")
|
2022-01-17 06:46:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:27:48 +00:00
|
|
|
// check if dup is available
|
2022-01-17 06:46:55 +00:00
|
|
|
r = httptest.NewRequest("GET", "/account/claimable?username=user", nil)
|
|
|
|
r.Header.Add("Authorization","Bearer " + token)
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
GetAccountUsername(w, r)
|
2022-01-17 21:27:48 +00:00
|
|
|
if ReadResponse(w, &available) != nil {
|
|
|
|
t.Errorf("failed to check username")
|
2022-01-17 06:46:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if available {
|
|
|
|
t.Errorf("username duplicate available")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:27:48 +00:00
|
|
|
// create dup account
|
2022-01-17 06:46:55 +00:00
|
|
|
auth = base64.StdEncoding.EncodeToString([]byte("user:pass"))
|
|
|
|
r = httptest.NewRequest("GET", "/account/profile", nil)
|
|
|
|
r.Header.Add("Credentials","Basic " + auth)
|
|
|
|
r.Header.Add("Authorization","Bearer " + token)
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
AddAccount(w, r)
|
2022-01-17 21:27:48 +00:00
|
|
|
if ReadResponse(w, &profile) == nil {
|
2022-01-17 06:46:55 +00:00
|
|
|
t.Errorf("duplicate handle set")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-15 08:07:00 +00:00
|
|
|
}
|