85 lines
2.7 KiB
Go
85 lines
2.7 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.yuntuo.hk/k2safe/orbit/internal/consoleauth"
|
|
core "git.yuntuo.hk/k2safe/orbit/model"
|
|
"git.yuntuo.hk/k2safe/orbit/platform/auth"
|
|
)
|
|
|
|
type fakeConsoleAuth struct {
|
|
valid bool
|
|
login consoleauth.LoginResult
|
|
}
|
|
|
|
func (f *fakeConsoleAuth) Login(context.Context, string, string, string, string) (consoleauth.LoginResult, error) {
|
|
return f.login, nil
|
|
}
|
|
func (f *fakeConsoleAuth) ValidateSession(context.Context, auth.Token) error {
|
|
if !f.valid {
|
|
return context.Canceled
|
|
}
|
|
return nil
|
|
}
|
|
func (f *fakeConsoleAuth) RevokeSession(context.Context, auth.Token) error { return nil }
|
|
|
|
func TestConsoleBusinessPageRedirectsToLogin(t *testing.T) {
|
|
server := NewWithConsoleAuth("127.0.0.1:0", nil, &fakeConsoleAuth{})
|
|
req := httptest.NewRequest(http.MethodGet, "/console/", nil)
|
|
res := httptest.NewRecorder()
|
|
server.Handler.ServeHTTP(res, req)
|
|
if res.Code != http.StatusFound || res.Header().Get("Location") != "/console/login?return=%2Fconsole%2F" {
|
|
t.Fatalf("redirect = %d %q", res.Code, res.Header().Get("Location"))
|
|
}
|
|
}
|
|
|
|
func TestConsoleSessionMiddlewareCopiesValidatedCookieToBearer(t *testing.T) {
|
|
ns, _ := core.NewID()
|
|
cred, _ := core.NewID()
|
|
raw, _, err := auth.Issue(ns, cred)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store := &fakeConsoleAuth{valid: true}
|
|
handler := ConsoleSessionMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.Header.Get("Authorization"); got != "Bearer "+raw {
|
|
t.Fatalf("authorization = %q", got)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}), store)
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/tasks", nil)
|
|
req.AddCookie(&http.Cookie{Name: consoleSessionCookie, Value: raw})
|
|
res := httptest.NewRecorder()
|
|
handler.ServeHTTP(res, req)
|
|
if res.Code != http.StatusNoContent {
|
|
t.Fatalf("status = %d", res.Code)
|
|
}
|
|
}
|
|
|
|
func TestConsoleLoginSetsRootScopedHttpOnlyCookie(t *testing.T) {
|
|
ns, _ := core.NewID()
|
|
cred, _ := core.NewID()
|
|
raw, _, err := auth.Issue(ns, cred)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := NewWithConsoleAuth("127.0.0.1:0", nil, &fakeConsoleAuth{login: consoleauth.LoginResult{Token: raw, ExpiresAt: time.Now().Add(time.Hour)}})
|
|
req := httptest.NewRequest(http.MethodPost, "/console/auth/login", strings.NewReader(`{"username":"operator","password":"password-password"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
res := httptest.NewRecorder()
|
|
server.Handler.ServeHTTP(res, req)
|
|
if res.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", res.Code)
|
|
}
|
|
cookie := res.Result().Cookies()[0]
|
|
if cookie.Path != "/" || !cookie.HttpOnly {
|
|
t.Fatalf("cookie path/httpOnly = %q/%v", cookie.Path, cookie.HttpOnly)
|
|
}
|
|
}
|