From b152861222f5e90390d700163d4bef64357dcfb0 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Tue, 2 Jun 2020 17:59:33 +0100 Subject: [PATCH] Add test for /spawn Required small refactor --- go.mod | 1 + go.sum | 10 ++++++++++ pkg/accounts/accounts.go | 4 ++-- pkg/accounts/accounts_test.go | 14 +++++--------- pkg/server/router.go | 4 +--- pkg/server/router_test.go | 24 ++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 25e663c..80da8b7 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.14 require ( github.com/google/uuid v1.1.1 github.com/gorilla/mux v1.7.4 + github.com/stretchr/testify v1.6.0 ) diff --git a/go.sum b/go.sum index ee6c4bc..c150150 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,14 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho= +github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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= diff --git a/pkg/accounts/accounts.go b/pkg/accounts/accounts.go index 578271f..1ce4ec7 100644 --- a/pkg/accounts/accounts.go +++ b/pkg/accounts/accounts.go @@ -43,7 +43,7 @@ func NewAccountant(dataPath string) *Accountant { } // RegisterAccount adds an account to the set of internal accounts -func (a *Accountant) RegisterAccount(acc Account) (Account, error) { +func (a *Accountant) RegisterAccount(name string) (acc Account, err error) { // Set the account ID to a new UUID acc.Id = uuid.New() @@ -60,7 +60,7 @@ func (a *Accountant) RegisterAccount(acc Account) (Account, error) { // Simply add the account to the map a.Accounts[acc.Id] = acc - return acc, nil + return } // path returns the full path to the data file diff --git a/pkg/accounts/accounts_test.go b/pkg/accounts/accounts_test.go index 0b5da87..275cde3 100644 --- a/pkg/accounts/accounts_test.go +++ b/pkg/accounts/accounts_test.go @@ -22,8 +22,7 @@ func TestAccountant_RegisterAccount(t *testing.T) { // Start by making two accounts namea := "one" - a := Account{Name: namea} - acca, err := accountant.RegisterAccount(a) + acca, err := accountant.RegisterAccount(namea) if err != nil { t.Error(err) } else if acca.Name != namea { @@ -31,8 +30,7 @@ func TestAccountant_RegisterAccount(t *testing.T) { } nameb := "two" - b := Account{Name: nameb} - accb, err := accountant.RegisterAccount(b) + accb, err := accountant.RegisterAccount(nameb) if err != nil { t.Error(err) } else if accb.Name != nameb { @@ -45,7 +43,7 @@ func TestAccountant_RegisterAccount(t *testing.T) { } // Verify another request gets rejected - _, err = accountant.RegisterAccount(a) + _, err = accountant.RegisterAccount(namea) if err == nil { t.Error("Duplicate account name did not produce error") } @@ -58,8 +56,7 @@ func TestAccountant_LoadSave(t *testing.T) { } name := "one" - a := Account{Name: name} - a, err := accountant.RegisterAccount(a) + a, err := accountant.RegisterAccount(name) if err != nil { t.Error(err) } @@ -101,8 +98,7 @@ func TestAccountant_AssignPrimary(t *testing.T) { } name := "one" - a := Account{Name: name} - a, err := accountant.RegisterAccount(a) + a, err := accountant.RegisterAccount(name) if err != nil { t.Error(err) } diff --git a/pkg/server/router.go b/pkg/server/router.go index 370f684..ff297e1 100644 --- a/pkg/server/router.go +++ b/pkg/server/router.go @@ -6,7 +6,6 @@ import ( "net/http" "github.com/google/uuid" - "github.com/mdiluz/rove/pkg/accounts" "github.com/mdiluz/rove/pkg/game" "github.com/mdiluz/rove/pkg/version" ) @@ -125,8 +124,7 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) { fmt.Printf("\tdata: %+v\n", data) // Register the account with the server - acc := accounts.Account{Name: data.Name} - acc, err := s.accountant.RegisterAccount(acc) + acc, err := s.accountant.RegisterAccount(data.Name) // If we didn't fail, respond with the account ID string if err == nil { diff --git a/pkg/server/router_test.go b/pkg/server/router_test.go index 09a5c1d..21440c5 100644 --- a/pkg/server/router_test.go +++ b/pkg/server/router_test.go @@ -6,6 +6,8 @@ import ( "net/http" "net/http/httptest" "testing" + + "github.com/stretchr/testify/assert" ) func TestHandleStatus(t *testing.T) { @@ -47,3 +49,25 @@ func TestHandleRegister(t *testing.T) { t.Errorf("got false for /register") } } + +func TestHandleSpawn(t *testing.T) { + s := NewServer() + a, err := s.accountant.RegisterAccount("test") + assert.NoError(t, err, "Error registering account") + data := SpawnData{BasicAccountData: BasicAccountData{Id: a.Id.String()}} + + b, err := json.Marshal(data) + assert.NoError(t, err, "Error marshalling data") + + request, _ := http.NewRequest(http.MethodPost, "/spawn", bytes.NewReader(b)) + response := httptest.NewRecorder() + + s.HandleSpawn(response, request) + + var status SpawnResponse + json.NewDecoder(response.Body).Decode(&status) + + if status.Success != true { + t.Errorf("got false for /spawn") + } +}