WIP tests

trunk
vilmibm 2022-06-17 17:02:27 -05:00
parent db5f52f1fb
commit 1e0eb4d13f
1 changed files with 24 additions and 6 deletions

View File

@ -43,6 +43,7 @@ func TestUserRegister(t *testing.T) {
ts := []struct { ts := []struct {
name string name string
req func() *http.Request
setup func(opts *config.Options) error setup func(opts *config.Options) error
assert func(t *testing.T) error assert func(t *testing.T) error
wantErr *HTTPError wantErr *HTTPError
@ -57,15 +58,22 @@ func TestUserRegister(t *testing.T) {
}) })
}, },
assert: func(t *testing.T) error { assert: func(t *testing.T) error {
// TODO // TODO ensure user count is still 1
return nil return nil
}, },
wantErr: &HTTPError{Code: 403, Msg: "user already exists"}, wantErr: &HTTPError{Code: 403, Msg: "user already exists"},
}, },
// TODO
} }
for _, tt := range ts { for _, tt := range ts {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var req *http.Request
if tt.req == nil {
req, _ = http.NewRequest("GET", "", strings.NewReader(""))
} else {
req = tt.req()
}
teardown, err := db.Setup(opts) teardown, err := db.Setup(opts)
if err != nil { if err != nil {
t.Fatalf("could not initialize DB: %s", err.Error()) t.Fatalf("could not initialize DB: %s", err.Error())
@ -85,14 +93,26 @@ func TestUserRegister(t *testing.T) {
return return
} }
// TODO api := &API{Opts: *opts}
ctx := &ReqCtx{Req: req}
_, err = api.InstanceInfo(ctx)
if tt.wantErr != nil && err != nil {
if !reflect.DeepEqual(tt.wantErr, err) {
t.Errorf("got unwanted error: %s", err.Error())
}
return
}
if tt.wantErr != nil && err == nil {
t.Errorf("expected error")
return
}
err = tt.assert(t) err = tt.assert(t)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
return return
} }
}) })
} }
} }
@ -138,9 +158,7 @@ func TestInstanceInfo(t *testing.T) {
} else { } else {
req = tt.req() req = tt.req()
} }
api := &API{ api := &API{Opts: *opts}
Opts: *opts,
}
ctx := &ReqCtx{Req: req} ctx := &ReqCtx{Req: req}
resp, err := api.InstanceInfo(ctx) resp, err := api.InstanceInfo(ctx)
if tt.wantErr != nil && err != nil { if tt.wantErr != nil && err != nil {