bbj2/server/cmd/api/api_test.go

89 lines
1.9 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"
2022-06-14 21:05:39 +00:00
"git.tilde.town/tildetown/bbj2/server/cmd/db"
2022-06-01 03:27:55 +00:00
)
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 21:05:39 +00:00
opts := 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-14 21:05:39 +00:00
teardown, err := db.Setup(opts)
if err != nil {
t.Fatalf("could not initialize DB: %s", err.Error())
return
}
defer teardown()
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",
2022-06-14 21:05:39 +00:00
opts: opts,
2022-06-14 16:33:27 +00:00
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
})
}
}