Add test for /spawn
Required small refactor
This commit is contained in:
parent
68d117e0d8
commit
b152861222
6 changed files with 43 additions and 14 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue