xtablo-source/backend/internal/auth/oauth_test.go
2026-05-15 21:03:30 +02:00

56 lines
1.7 KiB
Go

package auth
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestGoogleProviderConfigConfigured(t *testing.T) {
empty := GoogleProviderConfig{}
if empty.Configured() {
t.Fatal("empty Google config must not be configured")
}
cfg := GoogleProviderConfig{
ClientID: "google-client",
ClientSecret: "google-secret",
RedirectURL: "https://xtablo.test/auth/google/callback",
}
if !cfg.Configured() {
t.Fatal("complete Google config must be configured")
}
}
func TestOAuthStateAndNonceCookiesValidateExactValue(t *testing.T) {
rec := httptest.NewRecorder()
SetOAuthCookie(rec, "google", OAuthCookieState, "state-value", false)
SetOAuthCookie(rec, "google", OAuthCookieNonce, "nonce-value", false)
req := httptest.NewRequest(http.MethodGet, "/auth/google/callback", nil)
for _, c := range rec.Result().Cookies() {
req.AddCookie(c)
}
if !ValidateOAuthCookie(req, "google", OAuthCookieState, "state-value") {
t.Fatal("state cookie should validate matching value")
}
if ValidateOAuthCookie(req, "google", OAuthCookieState, "wrong-state") {
t.Fatal("state cookie should reject mismatched value")
}
if !ValidateOAuthCookie(req, "google", OAuthCookieNonce, "nonce-value") {
t.Fatal("nonce cookie should validate matching value")
}
if ValidateOAuthCookie(req, "google", OAuthCookieNonce, "wrong-nonce") {
t.Fatal("nonce cookie should reject mismatched value")
}
}
func TestOAuthCookieNameIncludesProviderAndKind(t *testing.T) {
if got := OAuthCookieName("google", OAuthCookieState); got != "xtablo_oauth_google_state" {
t.Fatalf("state cookie name = %q", got)
}
if got := OAuthCookieName("google", OAuthCookieNonce); got != "xtablo_oauth_google_nonce" {
t.Fatalf("nonce cookie name = %q", got)
}
}