From d2bb9c65635c42e054186fbdd0fd7c49c70848f2 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 28 Jun 2022 23:32:35 -0500 Subject: [PATCH] get user from req tests --- server/cmd/api/api_test.go | 138 ++++++++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/server/cmd/api/api_test.go b/server/cmd/api/api_test.go index 990eeb9..f4e52a9 100644 --- a/server/cmd/api/api_test.go +++ b/server/cmd/api/api_test.go @@ -2,6 +2,7 @@ package api import ( "database/sql" + "errors" "log" "net/http" "os" @@ -230,5 +231,140 @@ func Test_InstanceInfo(t *testing.T) { } func Test_getUserFromReq(t *testing.T) { - // TODO + ts := []struct { + name string + req func() *http.Request + setup func(*config.Options) error + assert func(*db.User, *testing.T) + wantErr error + }{ + { + name: "no auth attempt", + req: func() *http.Request { + r, _ := http.NewRequest("GET", "", strings.NewReader("")) + return r + }, + assert: func(u *db.User, t *testing.T) { + if u != nil { + t.Errorf("expected nil, got %v", u) + } + }, + }, + { + name: "anon", + req: func() *http.Request { + r, _ := http.NewRequest("GET", "", strings.NewReader("")) + r.Header.Add("User", "anon") + return r + }, + assert: func(u *db.User, t *testing.T) { + if u != nil { + t.Errorf("expected nil, got %v", u) + } + }, + }, + { + name: "no such user", + req: func() *http.Request { + r, _ := http.NewRequest("GET", "", strings.NewReader("")) + r.Header.Set("User", "williambirkin") + r.Header.Set("Auth", "abc123") + return r + }, + wantErr: errors.New("no such user"), + }, + { + name: "bad creds", + setup: func(opts *config.Options) error { + return db.CreateUser(opts.DB, db.User{ + Username: "jillvalentine", + Hash: "abc123", + Created: time.Now(), + }) + }, + req: func() *http.Request { + r, _ := http.NewRequest("GET", "", strings.NewReader("")) + r.Header.Set("User", "jillvalentine") + r.Header.Set("Auth", "xyz789") + return r + }, + wantErr: errors.New("bad credentials"), + }, + { + name: "good creds", + setup: func(opts *config.Options) error { + return db.CreateUser(opts.DB, db.User{ + Username: "jillvalentine", + Hash: "abc123", + Created: time.Now(), + }) + }, + req: func() *http.Request { + r, _ := http.NewRequest("GET", "", strings.NewReader("")) + r.Header.Set("User", "jillvalentine") + r.Header.Set("Auth", "abc123") + return r + }, + assert: func(u *db.User, t *testing.T) { + if u.Username != "jillvalentine" { + t.Errorf("expected 'jillvalentine' got %s", u.Username) + } + }, + }, + } + + for _, tt := range ts { + t.Run(tt.name, func(t *testing.T) { + // TODO BOILERPLATE + opts, err := createTestState() + if err != nil { + t.Fatalf("failed to create test state: %s", err.Error()) + return + } + var req *http.Request + if tt.req == nil { + req, _ = http.NewRequest("POST", "", strings.NewReader(`{"user_name":"albertwesker","auth_hash":"1234abc"}`)) + } else { + req = tt.req() + } + teardown, err := db.Setup(opts) + if err != nil { + t.Fatalf("could not initialize DB: %s", err.Error()) + return + } + defer teardown() + + err = db.EnsureSchema(*opts) + if err != nil { + t.Fatalf("could not initialize DB: %s", err.Error()) + return + } + + if tt.setup != nil { + err = tt.setup(opts) + if err != nil { + t.Fatalf("setup failed: %s", err.Error()) + return + } + } + + // END BOILERPLATE + + u, err := getUserFromReq(*opts, req) + + if err != nil { + if tt.wantErr == nil || tt.wantErr.Error() != err.Error() { + t.Errorf("got unexpected error: %s", err) + } + return + } else { + if tt.wantErr != nil { + t.Error("expected error, got none") + return + } + } + + tt.assert(u, t) + }) + } }