2022-01-15 08:07:00 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-01-17 22:25:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-15 08:07:00 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 07:00:08 +00:00
|
|
|
func TestAddAccount(t *testing.T) {
|
2022-01-15 08:07:00 +00:00
|
|
|
|
2022-01-22 18:45:39 +00:00
|
|
|
PrintMsg("ADD")
|
|
|
|
|
2022-01-15 22:54:49 +00:00
|
|
|
// acquire new token for creating accounts
|
2022-01-17 21:52:19 +00:00
|
|
|
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
|
|
|
|
SetBasicAuth(r, "admin:pass");
|
2022-01-17 05:11:24 +00:00
|
|
|
AddNodeAccount(w, r)
|
|
|
|
var token string
|
2022-01-17 22:25:39 +00:00
|
|
|
assert.NoError(t, ReadResponse(w, &token))
|
2022-01-15 08:07:00 +00:00
|
|
|
|
2022-01-15 22:54:49 +00:00
|
|
|
// validate account token
|
2022-01-17 21:52:19 +00:00
|
|
|
r, w, _ = NewRequest("GET", "/account/token", nil)
|
|
|
|
SetBearerAuth(r, token)
|
2022-01-17 05:11:24 +00:00
|
|
|
GetAccountToken(w, r)
|
|
|
|
var tokenType string
|
2022-01-17 22:25:39 +00:00
|
|
|
assert.NoError(t, ReadResponse(w, &tokenType))
|
2022-01-16 07:25:43 +00:00
|
|
|
|
|
|
|
// check if username is available
|
2022-01-19 19:00:20 +00:00
|
|
|
r, w, _ = NewRequest("GET", "/account/claimable?username=addaccount", nil)
|
2022-01-17 21:52:19 +00:00
|
|
|
SetBearerAuth(r, token)
|
2022-01-17 05:11:24 +00:00
|
|
|
GetAccountUsername(w, r)
|
2022-01-17 21:27:48 +00:00
|
|
|
var available bool
|
2022-01-17 22:25:39 +00:00
|
|
|
assert.NoError(t, ReadResponse(w, &available))
|
|
|
|
assert.True(t, available)
|
2022-01-15 22:54:49 +00:00
|
|
|
|
2022-01-17 05:11:24 +00:00
|
|
|
// create account
|
2022-01-17 21:52:19 +00:00
|
|
|
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
2022-01-19 19:00:20 +00:00
|
|
|
SetCredentials(r, "addaccount:pass")
|
2022-01-17 21:52:19 +00:00
|
|
|
SetBearerAuth(r, token)
|
2022-01-17 05:11:24 +00:00
|
|
|
AddAccount(w, r)
|
|
|
|
var profile Profile
|
2022-01-17 22:25:39 +00:00
|
|
|
assert.NoError(t, ReadResponse(w, &profile))
|
2022-01-17 06:46:55 +00:00
|
|
|
|
|
|
|
// acquire new token for creating accounts
|
2022-01-17 21:52:19 +00:00
|
|
|
r, w, _ = NewRequest("POST", "/admin/accounts", nil)
|
|
|
|
SetBasicAuth(r, "admin:pass")
|
2022-01-17 06:46:55 +00:00
|
|
|
AddNodeAccount(w, r)
|
2022-01-17 22:25:39 +00:00
|
|
|
assert.NoError(t, ReadResponse(w, &token))
|
2022-01-17 06:46:55 +00:00
|
|
|
|
2022-01-17 21:27:48 +00:00
|
|
|
// check if dup is available
|
2022-01-19 19:00:20 +00:00
|
|
|
r, w, _ = NewRequest("GET", "/account/claimable?username=addaccount", nil)
|
2022-01-17 21:52:19 +00:00
|
|
|
SetBearerAuth(r, token)
|
2022-01-17 06:46:55 +00:00
|
|
|
GetAccountUsername(w, r)
|
2022-01-17 22:25:39 +00:00
|
|
|
assert.NoError(t, ReadResponse(w, &available))
|
|
|
|
assert.False(t, available);
|
2022-01-17 06:46:55 +00:00
|
|
|
|
2022-01-17 21:27:48 +00:00
|
|
|
// create dup account
|
2022-01-17 21:52:19 +00:00
|
|
|
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
2022-01-19 19:00:20 +00:00
|
|
|
SetCredentials(r, "addaccount:pass")
|
2022-01-17 21:52:19 +00:00
|
|
|
SetBearerAuth(r, token);
|
2022-01-17 06:46:55 +00:00
|
|
|
AddAccount(w, r)
|
2022-01-17 22:25:39 +00:00
|
|
|
assert.Error(t, ReadResponse(w, &profile))
|
2022-01-15 08:07:00 +00:00
|
|
|
}
|