Add test for /spawn

Required small refactor
This commit is contained in:
Marc Di Luzio 2020-06-02 17:59:33 +01:00
parent 68d117e0d8
commit b152861222
6 changed files with 43 additions and 14 deletions

View file

@ -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 {

View file

@ -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")
}
}