diff options
Diffstat (limited to 'handlers_test.go')
| -rw-r--r-- | handlers_test.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/handlers_test.go b/handlers_test.go index 3e226d0..c539b60 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -84,3 +84,33 @@ func TestHTTPRegister(t *testing.T) { testHandler(t, register, cases, setup) } + +func TestHTTPAuthenticate(t *testing.T) { + cases := []httpCase{ + {"basic", "POST", []byte(`{"username":"testuser","password":"mypass"}`), []byte(""), http.StatusOK}, + {"missing-password", "POST", []byte(`{"username":"testuser"}`), []byte(""), http.StatusBadRequest}, + {"missing-username", "POST", []byte(`{"password":"mypass"}`), []byte(""), http.StatusBadRequest}, + {"unknown-field", "POST", []byte(`{"username":"testuser","password":"mypass","foo":"bar"}`), []byte(""), http.StatusBadRequest}, + {"invalid-json", "POST", []byte(`{username":"testuser","password":"mypass"}`), []byte(""), http.StatusBadRequest}, + {"wrong-username", "POST", []byte(`{"username":"wronguser","password":"mypass"}`), []byte(""), http.StatusUnauthorized}, + {"wrong-password", "POST", []byte(`{"username":"testuser","password":"wrongpass"}`), []byte(""), http.StatusUnauthorized}, + } + + setup := func(t *testing.T) *sql.DB { + db, _ := emptyDB(t) + + err := MigrateToLatest(db) + if err != nil { + t.Fatalf("Could not migrate test database: %v", err) + } + + err = Register(db, "testuser", "mypass") + if err != nil { + t.Fatalf("Failed to register test user: %v", err) + } + + return db + } + + testHandler(t, authenticate, cases, setup) +} |
