Files
GaCy-GO/internal/sdkserver/api_command_truth_test.go
T

49 lines
1.3 KiB
Go

package sdkserver
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestUnsupportedAndReadOnlyCommandsDoNotRewriteSave(t *testing.T) {
chdirRepoRootSDK(t)
beforeData, err := os.ReadFile("freesr-data.json")
if err != nil {
t.Fatal(err)
}
beforePersistent, err := os.ReadFile("persistent")
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
command string
success bool
}{
{"/give 200001 100", false}, {"/heal", false}, {"/not_a_command", false}, {"/help", true},
} {
body, _ := json.Marshal(map[string]string{"command": tc.command})
response := httptest.NewRecorder()
apiCommand(response, httptest.NewRequest(http.MethodPost, "/api/command", strings.NewReader(string(body))))
var result struct {
Success bool `json:"success"`
Output string `json:"output"`
}
if err := json.Unmarshal(response.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.Success != tc.success || result.Output == "" {
t.Fatalf("%s: %+v", tc.command, result)
}
}
afterData, _ := os.ReadFile("freesr-data.json")
afterPersistent, _ := os.ReadFile("persistent")
if !bytes.Equal(beforeData, afterData) || !bytes.Equal(beforePersistent, afterPersistent) {
t.Fatal("read-only or unsupported command rewrote user data")
}
}