From e1211c8d6fdff8f871417f420f4ec11f1ed732a6 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 14 Jun 2022 11:33:27 -0500 Subject: [PATCH] simple test for InstanceInfo --- server/cmd/api/api.go | 11 +++---- server/cmd/api/api_test.go | 60 ++++++++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/server/cmd/api/api.go b/server/cmd/api/api.go index ed976bd..bc8ed5c 100644 --- a/server/cmd/api/api.go +++ b/server/cmd/api/api.go @@ -27,12 +27,13 @@ type API struct { Opts config.Options } +type instanceInfo struct { + InstanceName string `json:"instance_name"` + AllowAnon bool `json:"allow_anon"` + Admins []string +} + func (a *API) InstanceInfo() (*BBJResponse, error) { - type instanceInfo struct { - InstanceName string `json:"instance_name"` - AllowAnon bool `json:"allow_anon"` - Admins []string - } return &BBJResponse{ Data: instanceInfo{ InstanceName: a.Opts.Config.InstanceName, diff --git a/server/cmd/api/api_test.go b/server/cmd/api/api_test.go index c9b78f4..77bed04 100644 --- a/server/cmd/api/api_test.go +++ b/server/cmd/api/api_test.go @@ -1,35 +1,77 @@ package api import ( + "bufio" + "bytes" + "fmt" + "os" + "reflect" "testing" "git.tilde.town/tildetown/bbj2/server/cmd/config" ) func TestInstanceInfo(t *testing.T) { + 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()) + } + defaultOptions := config.Options{ + IO: testIO, + Log: func(s string) { fmt.Fprintln(testIO.Out, s) }, + Logf: func(s string, args ...interface{}) { + fmt.Fprintf(testIO.Out, s, args...) + fmt.Fprintln(testIO.Out) + }, + 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 - wantResp *BBJResponse + 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) { api := &API{ - Opts: config.Options{ - // TODO - }, - User: nil, + Opts: tt.opts, } resp, err := api.InstanceInfo() if tt.wantErr != nil && err != nil { t.Errorf("got unwanted error: %s", err.Error()) return } - if tt.wantResp != resp { - t.Errorf("wanted %#v got %#v", tt.wantResp, resp) - 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) } }) }