get user from req tests

trunk
vilmibm 2022-06-28 23:32:35 -05:00
parent 1ed26d4cbc
commit d2bb9c6563
1 changed files with 137 additions and 1 deletions

View File

@ -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)
})
}
}