ci: expand shared package validation and test coverage
- Add lint, formatting, vet, and publish workflow checks - Run Go database tests with PostgreSQL and add Java tests - Make consumer sync opt-in; update docs and Go dependency
This commit is contained in:
1 parent
937f51b4fb
commit
0ff94f9b10
107 files changed
+10124
-410
No files matched your search
@@ -4,7 +4,10 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeProxyStore is a minimal in-memory ProxyStore for constructing a
|
||||
@@ -95,7 +98,10 @@ func TestLoginDisabledUsesAdminIdentityWithoutCredentials(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInternalLoginOverrideWorksWithNoOpStore(t *testing.T) {
|
||||
// TestNoOpStoreDoesNotKeepAProcessLocalLoginOverride checks that the login-required setting is read
|
||||
// from the store on each call. A store that discards writes keeps login required everywhere, rather
|
||||
// than letting one process diverge from the rest.
|
||||
func TestNoOpStoreDoesNotKeepAProcessLocalLoginOverride(t *testing.T) {
|
||||
store := &noOpLoginProxyStore{fakeProxyStore: fakeProxyStore{loginRequired: true}}
|
||||
service := NewWithStore(store, "session", false, "", "testproject")
|
||||
if err := service.SetLoginRequired(context.Background(), false); err != nil {
|
||||
@@ -103,8 +109,142 @@ func TestInternalLoginOverrideWorksWithNoOpStore(t *testing.T) {
|
||||
}
|
||||
|
||||
user, err := service.CurrentUser(httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
if err != nil || user == nil || user.Username != "Admin" {
|
||||
t.Fatalf("no-op store override resolved user=%+v err=%v, want Admin", user, err)
|
||||
if err != nil || user != nil {
|
||||
t.Fatalf("no-op store resolved user=%+v err=%v, want no user while login is still required", user, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBodylessStateChangesRefuseCrossSite checks the routes that take no JSON body, which
|
||||
// decodeProxyJSON cannot guard, refuse a request the browser marks as cross-site.
|
||||
func TestBodylessStateChangesRefuseCrossSite(t *testing.T) {
|
||||
service := NewWithStore(&fakeProxyStore{loginRequired: true}, "session", false, "", "testproject")
|
||||
handlers := map[string]http.HandlerFunc{
|
||||
"logout": service.Logout,
|
||||
"totp initiate": service.TOTPInitiate,
|
||||
"delete user": service.ManagedDeleteUser,
|
||||
}
|
||||
for name, handler := range handlers {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
req.Header.Set("Sec-Fetch-Site", "cross-site")
|
||||
rec := httptest.NewRecorder()
|
||||
handler(rec, req)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Errorf("%s with Sec-Fetch-Site: cross-site returned %d, want 403", name, rec.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetLoginRequiredNeedsAStore(t *testing.T) {
|
||||
service := NewWithStore(nil, "session", false, "", "testproject")
|
||||
if err := service.SetLoginRequired(context.Background(), false); err == nil {
|
||||
t.Fatal("SetLoginRequired with no store succeeded, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeProxyJSONRequiresAJSONBodyAndRefusesCrossSite(t *testing.T) {
|
||||
decode := func(contentType, fetchSite string) error {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"loginRequired":true}`))
|
||||
if contentType != "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
if fetchSite != "" {
|
||||
req.Header.Set("Sec-Fetch-Site", fetchSite)
|
||||
}
|
||||
var input struct {
|
||||
LoginRequired bool `json:"loginRequired"`
|
||||
}
|
||||
return decodeProxyJSON(req, &input)
|
||||
}
|
||||
if err := decode("application/json", ""); err != nil {
|
||||
t.Fatalf("application/json body refused: %v", err)
|
||||
}
|
||||
if err := decode("application/json; charset=utf-8", "same-site"); err != nil {
|
||||
t.Fatalf("same-site application/json body refused: %v", err)
|
||||
}
|
||||
for _, contentType := range []string{"", "text/plain", "application/x-www-form-urlencoded", "multipart/form-data; boundary=x"} {
|
||||
if err := decode(contentType, ""); err == nil {
|
||||
t.Errorf("Content-Type %q accepted, want refused", contentType)
|
||||
}
|
||||
}
|
||||
if err := decode("application/json", "cross-site"); err == nil {
|
||||
t.Error("cross-site application/json body accepted, want refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignupIsRefusedWhileLoginIsNotRequired(t *testing.T) {
|
||||
store := &fakeProxyStore{loginRequired: false}
|
||||
service := NewWithStore(store, "session", false, "", "testproject")
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"email":"a@b.com","username":"a","password":"password123"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
service.Signup(rec, req)
|
||||
if rec.Code != http.StatusConflict {
|
||||
t.Fatalf("signup with login off status = %d, want 409", rec.Code)
|
||||
}
|
||||
if store.setupClaimed {
|
||||
t.Fatal("refused signup still claimed the setup flag")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginIsRateLimitedPerAccount(t *testing.T) {
|
||||
service := NewWithStore(&fakeProxyStore{loginRequired: true}, "session", false, "", "testproject")
|
||||
post := func() int {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"identifier":"Victim","password":"guess"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
service.Login(rec, req)
|
||||
return rec.Code
|
||||
}
|
||||
for i := 0; i < loginAccountLimit; i++ {
|
||||
if code := post(); code == http.StatusTooManyRequests {
|
||||
t.Fatalf("attempt %d refused before the limit", i+1)
|
||||
}
|
||||
}
|
||||
if code := post(); code != http.StatusTooManyRequests {
|
||||
t.Fatalf("attempt past the limit status = %d, want 429", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttemptLimiterWindowAndTableBound(t *testing.T) {
|
||||
now := time.Now()
|
||||
limiter := newAttemptLimiter(time.Minute, 2)
|
||||
if !limiter.allow("k", now) || !limiter.allow("k", now) {
|
||||
t.Fatal("attempts within the limit were refused")
|
||||
}
|
||||
if limiter.allow("k", now) {
|
||||
t.Fatal("attempt past the limit was allowed")
|
||||
}
|
||||
if !limiter.allow("k", now.Add(time.Minute)) {
|
||||
t.Fatal("limit did not reset after the window")
|
||||
}
|
||||
|
||||
full := newAttemptLimiter(time.Hour, 1)
|
||||
for i := 0; i < maxTrackedAttemptKeys; i++ {
|
||||
full.allow(strconv.Itoa(i), now)
|
||||
}
|
||||
if full.allow("new-key", now) {
|
||||
t.Fatal("a new key was tracked past the table bound")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateProxyPasswordRefusesMoreThan72Bytes(t *testing.T) {
|
||||
if err := validateProxyPassword(strings.Repeat("a", 72)); err != nil {
|
||||
t.Fatalf("72-byte password refused: %v", err)
|
||||
}
|
||||
if err := validateProxyPassword(strings.Repeat("a", 73)); err == nil {
|
||||
t.Fatal("73-byte password accepted, want refused")
|
||||
}
|
||||
if err := validateProxyPassword(strings.Repeat("é", 40)); err == nil {
|
||||
t.Fatal("80-byte multi-byte password accepted, want refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeProxyUsernameRefusesAt(t *testing.T) {
|
||||
if _, err := normalizeProxyUsername("name@example.com"); err == nil {
|
||||
t.Fatal("username containing @ accepted")
|
||||
}
|
||||
if got, err := normalizeProxyUsername(" plain "); err != nil || got != "plain" {
|
||||
t.Fatalf("plain username = %q, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user