bbj2/server/cmd/api/api_test.go

149 lines
2.9 KiB
Go
Raw Normal View History

2022-06-01 03:27:55 +00:00
package api
import (
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"
2022-06-17 21:01:25 +00:00
"time"
2022-06-01 03:27:55 +00:00
"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 createTestState() (opts *config.Options, err error) {
var dbFile *os.File
if dbFile, err = os.CreateTemp("", "bbj2-test"); err != nil {
return
2022-06-14 16:33:27 +00:00
}
opts = &config.Options{
Logger: log.New(os.Stdout, "bbj2 test", log.Lshortfile),
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
return
}
func TestUserRegister(t *testing.T) {
opts, err := createTestState()
if err != nil {
t.Fatalf("failed to create test state: %s", err.Error())
return
}
ts := []struct {
name string
opts config.Options
setup func(opts *config.Options) error
assert func(t *testing.T) error
wantErr *HTTPError
}{
{
name: "user already exists",
opts: *opts,
setup: func(opts *config.Options) error {
2022-06-17 21:01:25 +00:00
return db.CreateUser(opts.DB, db.User{
Username: "albertwesker",
Hash: "1234abc",
Created: time.Now(),
})
},
assert: func(t *testing.T) error {
// TODO
return nil
},
wantErr: &HTTPError{Code: 403, Msg: "user already exists"},
},
}
for _, tt := range ts {
t.Run(tt.name, func(t *testing.T) {
2022-06-17 21:01:25 +00:00
teardown, err := db.Setup(opts)
if err != nil {
t.Fatalf("could not initialize DB: %s", err.Error())
return
}
defer teardown()
2022-06-17 21:01:25 +00:00
err = tt.setup(&tt.opts)
if err != nil {
t.Fatalf("setup failed: %s", err.Error())
return
}
// TODO
2022-06-17 21:01:25 +00:00
err = tt.assert(t)
if err != nil {
t.Fatal(err.Error())
return
}
})
}
}
func TestInstanceInfo(t *testing.T) {
opts, err := createTestState()
if err != nil {
t.Fatalf("failed to create test state: %s", err.Error())
return
}
2022-06-17 21:01:25 +00:00
teardown, err := db.Setup(opts)
2022-06-14 21:05:39 +00:00
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",
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
})
}
}