trunk
vilmibm 2022-06-17 16:12:05 -05:00
parent 2d22877f11
commit 89de893bf9
3 changed files with 12 additions and 21 deletions

View File

@ -71,12 +71,7 @@ func (a *API) Invoke(w http.ResponseWriter, req *http.Request, apiFn APIHandler)
})
}
ctx := ReqCtx{
User: *user,
Req: req,
}
resp, err := apiFn(&ctx)
resp, err := apiFn(&ReqCtx{*user, req})
if err != nil {
he := &HTTPError{}
_ = errors.As(err, &he)

View File

@ -43,14 +43,12 @@ func TestUserRegister(t *testing.T) {
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 {
return db.CreateUser(opts.DB, db.User{
Username: "albertwesker",
@ -75,7 +73,13 @@ func TestUserRegister(t *testing.T) {
}
defer teardown()
err = tt.setup(&tt.opts)
err = db.EnsureSchema(*opts)
if err != nil {
t.Fatalf("could not initialize DB: %s", err.Error())
return
}
err = tt.setup(opts)
if err != nil {
t.Fatalf("setup failed: %s", err.Error())
return
@ -100,22 +104,13 @@ func TestInstanceInfo(t *testing.T) {
return
}
teardown, err := db.Setup(opts)
if err != nil {
t.Fatalf("could not initialize DB: %s", err.Error())
return
}
defer teardown()
ts := []struct {
name string
opts config.Options
wantData instanceInfo
wantErr *HTTPError
}{
{
name: "basic",
opts: *opts,
wantData: instanceInfo{
InstanceName: "cool test zone",
AllowAnon: true,
@ -127,10 +122,10 @@ func TestInstanceInfo(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "", strings.NewReader(""))
api := &API{
Opts: tt.opts,
Req: req,
Opts: *opts,
}
resp, err := api.InstanceInfo()
ctx := &ReqCtx{Req: req}
resp, err := api.InstanceInfo(ctx)
if tt.wantErr != nil && err != nil {
t.Errorf("got unwanted error: %s", err.Error())
return

View File

@ -54,6 +54,7 @@ type Message struct {
func Setup(opts *config.Options) (func(), error) {
db, err := sql.Open("sqlite3", opts.Config.DBPath)
opts.DB = db
// TODO consider inlining EnsureSchema here
return func() { db.Close() }, err
}