diff --git a/net/server/go.mod b/net/server/go.mod index 6f6be907..d84187a3 100644 --- a/net/server/go.mod +++ b/net/server/go.mod @@ -3,6 +3,7 @@ module databag go 1.17 require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect @@ -10,9 +11,12 @@ require ( github.com/kr/pretty v0.3.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-sqlite3 v1.14.9 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.6.1 // indirect + github.com/stretchr/testify v1.7.0 // indirect github.com/theckman/go-securerandom v0.1.1 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect gorm.io/driver/sqlite v1.2.6 // indirect gorm.io/gorm v1.22.4 // indirect ) diff --git a/net/server/go.sum b/net/server/go.sum index 1f395c5b..1d0abe57 100644 --- a/net/server/go.sum +++ b/net/server/go.sum @@ -1,5 +1,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= @@ -19,10 +20,12 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/theckman/go-securerandom v0.1.1 h1:5KctSyM0D5KKFK+bsypIyLq7yik0CEaI5i2fGcUGcsQ= github.com/theckman/go-securerandom v0.1.1/go.mod h1:bmkysLfBH6i891sBpcP4xRM3XIB7jMeiKJB31jlResI= @@ -31,6 +34,7 @@ golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/sqlite v1.2.6 h1:SStaH/b+280M7C8vXeZLz/zo9cLQmIGwwj3cSj7p6l4= gorm.io/driver/sqlite v1.2.6/go.mod h1:gyoX0vHiiwi0g49tv+x2E7l8ksauLK0U/gShcdUsjWY= diff --git a/net/server/internal/addAccount_test.go b/net/server/internal/addAccount_test.go index cf5f6809..151a87a9 100644 --- a/net/server/internal/addAccount_test.go +++ b/net/server/internal/addAccount_test.go @@ -2,6 +2,7 @@ package databag import ( "testing" + "github.com/stretchr/testify/assert" ) func TestAddAccount(t *testing.T) { @@ -11,34 +12,22 @@ func TestAddAccount(t *testing.T) { SetBasicAuth(r, "admin:pass"); AddNodeAccount(w, r) var token string - if ReadResponse(w, &token) != nil { - t.Errorf("failed to create token"); - return - } + assert.NoError(t, ReadResponse(w, &token)) // validate account token r, w, _ = NewRequest("GET", "/account/token", nil) SetBearerAuth(r, token) GetAccountToken(w, r) var tokenType string - if ReadResponse(w, &tokenType) != nil { - t.Errorf("failed to validate token") - return - } + assert.NoError(t, ReadResponse(w, &tokenType)) // check if username is available r, w, _ = NewRequest("GET", "/account/claimable?username=user", nil) SetBearerAuth(r, token) GetAccountUsername(w, r) var available bool - if ReadResponse(w, &available) != nil { - t.Errorf("failed to check username") - return - } - if !available { - t.Errorf("username not available") - return - } + assert.NoError(t, ReadResponse(w, &available)) + assert.True(t, available) // create account r, w, _ = NewRequest("GET", "/account/profile", nil) @@ -46,41 +35,25 @@ func TestAddAccount(t *testing.T) { SetBearerAuth(r, token) AddAccount(w, r) var profile Profile - if ReadResponse(w, &profile) != nil { - t.Errorf("failed to create account") - return - } + assert.NoError(t, ReadResponse(w, &profile)) // acquire new token for creating accounts r, w, _ = NewRequest("POST", "/admin/accounts", nil) SetBasicAuth(r, "admin:pass") AddNodeAccount(w, r) - if ReadResponse(w, &token) != nil { - t.Errorf("failed to create token") - return - } + assert.NoError(t, ReadResponse(w, &token)) // check if dup is available r, w, _ = NewRequest("GET", "/account/claimable?username=user", nil) SetBearerAuth(r, token) GetAccountUsername(w, r) - if ReadResponse(w, &available) != nil { - t.Errorf("failed to check username") - return - } - if available { - t.Errorf("username duplicate available") - return - } + assert.NoError(t, ReadResponse(w, &available)) + assert.False(t, available); // create dup account r, w, _ = NewRequest("GET", "/account/profile", nil) SetCredentials(r, "user:pass") SetBearerAuth(r, token); AddAccount(w, r) - if ReadResponse(w, &profile) == nil { - t.Errorf("duplicate handle set") - return - } - + assert.Error(t, ReadResponse(w, &profile)) } diff --git a/net/server/internal/main_test.go b/net/server/internal/main_test.go index b185689f..049c90b5 100644 --- a/net/server/internal/main_test.go +++ b/net/server/internal/main_test.go @@ -11,79 +11,66 @@ func TestMain(m *testing.M) { store.SetPath("file::memory:?cache=shared"); //store.SetPath("databag.db"); - Claimable(); - Claim(); - SetConfig(); - GetConfig(); - token := SetToken() - SetAccount(token) - m.Run() -} - -func Claimable() { r, w, _ := NewRequest("GET", "/admin/claimable", nil) GetNodeClaimable(w, r) var available bool if ReadResponse(w, &available) != nil { panic("server not claimable") } -} -func Claim() { - r, w, _ := NewRequest("PUT", "/admin/claim", nil) + // claim server + r, w, _ = NewRequest("PUT", "/admin/claim", nil) SetCredentials(r, "admin:pass"); SetNodeClaim(w, r) if ReadResponse(w, nil) != nil { panic("failed to claim server") } -} -func SetConfig() { + // config server config := NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096} - r, w, _ := NewRequest("PUT", "/admin/config", &config) + r, w, _ = NewRequest("PUT", "/admin/config", &config) SetBasicAuth(r, "admin:pass") SetNodeConfig(w, r) if ReadResponse(w, nil) != nil { panic("failed to set config") } -} -func GetConfig() { - r, w, _ := NewRequest("GET", "/admin/config", nil) + // check config + r, w, _ = NewRequest("GET", "/admin/config", nil) SetBasicAuth(r, "admin:pass") GetNodeConfig(w, r) - var config NodeConfig - if ReadResponse(w, &config) != nil { + var check NodeConfig + if ReadResponse(w, &check) != nil { panic("failed to get node config") } - if config.Domain != "example.com" { + if check.Domain != "example.com" { panic("failed to set config domain"); } - if config.PublicLimit != 1024 { + if check.PublicLimit != 1024 { panic("failed to set public limit"); } - if config.AccountStorage != 4096 { + if check.AccountStorage != 4096 { panic("failed to set account storage"); } -} -func SetToken() string { - r, w, _ := NewRequest("POST", "/admin/accounts", nil) + // get account token + r, w, _ = NewRequest("POST", "/admin/accounts", nil) SetBasicAuth(r, "admin:pass") AddNodeAccount(w, r) var token string if ReadResponse(w, &token) != nil { panic("failed to create token") } - return token -} -func SetAccount(token string) { - r, w, _ := NewRequest("GET", "/account/profile", nil) + // set account profile + r, w, _ = NewRequest("GET", "/account/profile", nil) SetBearerAuth(r, token); SetCredentials(r, "test:pass") AddAccount(w, r) if ReadResponse(w, nil) != nil { panic("failed to create account") } + + m.Run() } +