bbj2/server/cmd/api/api_test.go

81 lines
1.7 KiB
Go
Raw Normal View History

2022-06-01 03:27:55 +00:00
package api
import (
2022-06-14 16:33:27 +00:00
"bufio"
"bytes"
2022-06-14 17:44:03 +00:00
"log"
"net/http"
2022-06-14 16:33:27 +00:00
"os"
"reflect"
2022-06-14 17:44:03 +00:00
"strings"
2022-06-01 03:27:55 +00:00
"testing"
"git.tilde.town/tildetown/bbj2/server/cmd/config"
)
func TestInstanceInfo(t *testing.T) {
2022-06-14 17:44:03 +00:00
// TODO a lot of this needs to be cleaned up and generalized etc
2022-06-14 16:33:27 +00:00
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())
}
2022-06-14 17:44:03 +00:00
logger := log.New(os.Stdout, "bbj test", log.Lshortfile)
2022-06-14 16:33:27 +00:00
defaultOptions := config.Options{
2022-06-14 17:44:03 +00:00
IO: testIO,
Logger: logger,
2022-06-14 16:33:27 +00:00
Config: config.Config{
Admins: []string{"jillValentine", "rebeccaChambers"},
Port: 666,
Host: "hell.cool",
InstanceName: "cool test zone",
AllowAnon: true,
DBPath: dbFile.Name(),
},
}
2022-06-01 03:27:55 +00:00
ts := []struct {
name string
opts config.Options
2022-06-14 16:33:27 +00:00
wantData instanceInfo
2022-06-02 04:40:43 +00:00
wantErr *HTTPError
2022-06-14 16:33:27 +00:00
}{
{
name: "basic",
opts: defaultOptions,
wantData: instanceInfo{
InstanceName: "cool test zone",
AllowAnon: true,
Admins: []string{"jillValentine", "rebeccaChambers"},
},
},
}
2022-06-01 03:27:55 +00:00
for _, tt := range ts {
t.Run(tt.name, func(t *testing.T) {
2022-06-14 17:44:03 +00:00
req, _ := http.NewRequest("GET", "", strings.NewReader(""))
2022-06-02 04:40:43 +00:00
api := &API{
2022-06-14 16:33:27 +00:00
Opts: tt.opts,
2022-06-14 17:44:03 +00:00
Req: req,
2022-06-02 04:40:43 +00:00
}
resp, err := api.InstanceInfo()
if tt.wantErr != nil && err != nil {
t.Errorf("got unwanted error: %s", err.Error())
return
}
2022-06-14 16:33:27 +00:00
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)
2022-06-02 04:40:43 +00:00
}
2022-06-01 03:27:55 +00:00
})
}
}