From 1e0eb4d13fb3b76adfc6c396a395c07674bd3b56 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 17 Jun 2022 17:02:27 -0500 Subject: [PATCH] WIP tests --- server/cmd/api/api_test.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/server/cmd/api/api_test.go b/server/cmd/api/api_test.go index ab2bc57..75cbe9a 100644 --- a/server/cmd/api/api_test.go +++ b/server/cmd/api/api_test.go @@ -43,6 +43,7 @@ func TestUserRegister(t *testing.T) { ts := []struct { name string + req func() *http.Request setup func(opts *config.Options) error assert func(t *testing.T) error wantErr *HTTPError @@ -57,15 +58,22 @@ func TestUserRegister(t *testing.T) { }) }, assert: func(t *testing.T) error { - // TODO + // TODO ensure user count is still 1 return nil }, wantErr: &HTTPError{Code: 403, Msg: "user already exists"}, }, + // TODO } for _, tt := range ts { 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) if err != nil { t.Fatalf("could not initialize DB: %s", err.Error()) @@ -85,14 +93,26 @@ func TestUserRegister(t *testing.T) { 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) if err != nil { t.Fatal(err.Error()) return } - }) } } @@ -138,9 +158,7 @@ func TestInstanceInfo(t *testing.T) { } else { req = tt.req() } - api := &API{ - Opts: *opts, - } + api := &API{Opts: *opts} ctx := &ReqCtx{Req: req} resp, err := api.InstanceInfo(ctx) if tt.wantErr != nil && err != nil {