Files
fn_registry/functions/infra/oauth2_auth_url_test.go
T
egutierrez 1e5dfa5193 feat: oauth2_auth_url (pure), oauth2_exchange, oauth2_refresh
Fase 4 del issue 0010 — cliente OAuth2 sin golang.org/x/oauth2.
- Oauth2AuthURL es pura: solo construye la URL con net/url
- Oauth2Exchange/Refresh hacen POST application/x-www-form-urlencoded
- ExpiresAt calculado como now + expires_in del proveedor
- Refresh conserva el token original si el proveedor no devuelve uno nuevo
- Tests con httptest.NewServer como mock del proveedor
2026-04-18 17:41:42 +02:00

69 lines
1.7 KiB
Go

package infra
import (
"net/url"
"strings"
"testing"
)
func TestOauth2AuthURL_BuildsBasicParams(t *testing.T) {
cfg := OAuthConfig{
ClientID: "abc-client",
AuthURL: "https://example.com/authorize",
RedirectURL: "http://localhost/callback",
Scopes: []string{"openid", "email"},
}
got := Oauth2AuthURL(cfg, "state-xyz")
u, err := url.Parse(got)
if err != nil {
t.Fatalf("parse: %v", err)
}
if u.Scheme != "https" || u.Host != "example.com" || u.Path != "/authorize" {
t.Fatalf("base URL incorrecta: %s", got)
}
q := u.Query()
if q.Get("client_id") != "abc-client" {
t.Errorf("client_id = %q", q.Get("client_id"))
}
if q.Get("redirect_uri") != "http://localhost/callback" {
t.Errorf("redirect_uri = %q", q.Get("redirect_uri"))
}
if q.Get("response_type") != "code" {
t.Errorf("response_type = %q", q.Get("response_type"))
}
if q.Get("scope") != "openid email" {
t.Errorf("scope = %q", q.Get("scope"))
}
if q.Get("state") != "state-xyz" {
t.Errorf("state = %q", q.Get("state"))
}
}
func TestOauth2AuthURL_OmitsEmptyState(t *testing.T) {
cfg := OAuthConfig{ClientID: "c", AuthURL: "https://x.test/a", RedirectURL: "http://r"}
got := Oauth2AuthURL(cfg, "")
if strings.Contains(got, "state=") {
t.Errorf("state deberia estar ausente: %s", got)
}
}
func TestOauth2AuthURL_HandlesExistingQueryString(t *testing.T) {
cfg := OAuthConfig{
ClientID: "c",
AuthURL: "https://example.com/authorize?hd=domain.com",
RedirectURL: "http://r",
}
got := Oauth2AuthURL(cfg, "s")
u, err := url.Parse(got)
if err != nil {
t.Fatalf("parse: %v", err)
}
q := u.Query()
if q.Get("hd") != "domain.com" {
t.Errorf("param pre-existente se perdio")
}
if q.Get("client_id") != "c" {
t.Errorf("client_id no agregado")
}
}