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") } }