bbj2/server/cmd/api/api_test.go

81 lines
1.7 KiB
Go

package api
import (
"bufio"
"bytes"
"log"
"net/http"
"os"
"reflect"
"strings"
"testing"
"git.tilde.town/tildetown/bbj2/server/cmd/config"
)
func TestInstanceInfo(t *testing.T) {
// TODO a lot of this needs to be cleaned up and generalized etc
stderr := []byte{}
stdout := []byte{}
testIO := config.IOStreams{
Err: bufio.NewWriter(bytes.NewBuffer(stderr)),
Out: bufio.NewWriter(bytes.NewBuffer(stdout)),
}
dbFile, err := os.CreateTemp("", "bbj2-test")
if err != nil {
t.Fatalf("failed to make test db: %s", err.Error())
}
logger := log.New(os.Stdout, "bbj test", log.Lshortfile)
defaultOptions := config.Options{
IO: testIO,
Logger: logger,
Config: config.Config{
Admins: []string{"jillValentine", "rebeccaChambers"},
Port: 666,
Host: "hell.cool",
InstanceName: "cool test zone",
AllowAnon: true,
DBPath: dbFile.Name(),
},
}
ts := []struct {
name string
opts config.Options
wantData instanceInfo
wantErr *HTTPError
}{
{
name: "basic",
opts: defaultOptions,
wantData: instanceInfo{
InstanceName: "cool test zone",
AllowAnon: true,
Admins: []string{"jillValentine", "rebeccaChambers"},
},
},
}
for _, tt := range ts {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "", strings.NewReader(""))
api := &API{
Opts: tt.opts,
Req: req,
}
resp, err := api.InstanceInfo()
if tt.wantErr != nil && err != nil {
t.Errorf("got unwanted error: %s", err.Error())
return
}
ii, ok := resp.Data.(instanceInfo)
if !ok {
t.Errorf("could not cast data in %s", tt.name)
}
if !reflect.DeepEqual(ii, tt.wantData) {
t.Errorf("did not get expected data in %s", tt.name)
}
})
}
}