package web import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "net/url" "strconv" "strings" "testing" "time" "supermarket/internal/backlinks" "supermarket/internal/inventory" "supermarket/internal/oauth" "supermarket/internal/profile" "supermarket/internal/secrets" "supermarket/internal/signals" ) func TestSearchConsole(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { case r.URL.Path == "/token": r.ParseForm() if r.PostForm.Get("client_secret") != "sec" || (r.PostForm.Get("code") != "c0de" && r.PostForm.Get("grant_type") != "refresh_token") { w.WriteHeader(http.StatusBadRequest) return } json.NewEncoder(w).Encode(map[string]any{"access_token": "tok", "refresh_token": "ref", "expires_in": 3600}) case r.URL.Path == "/userinfo": json.NewEncoder(w).Encode(map[string]any{"email": "me@example.com"}) case r.URL.Path == "/sites": json.NewEncoder(w).Encode(map[string]any{"siteEntry": []any{map[string]any{"siteUrl": "sc-domain:us.example", "permissionLevel": "siteOwner"}}}) case strings.HasSuffix(r.URL.Path, "/searchAnalytics/query"): var q struct { StartDate string `json:"startDate"` Dimensions []string `json:"dimensions"` } json.NewDecoder(r.Body).Decode(&q) var rows []any if q.StartDate <= "2026-11-10" && q.StartDate >= "2026-10-20" { day := "2026-11-10" switch strings.Join(q.Dimensions, ",") { case "date": rows = append(rows, map[string]any{"keys": []string{day}, "clicks": 63, "impressions": 4200, "position": 9}) case "date,page": rows = append(rows, map[string]any{"keys": []string{day, "https://us.example/pricing/"}, "clicks": 0, "impressions": 4000, "position": 6.5}, map[string]any{"keys": []string{day, "https://us.example/"}, "clicks": 63, "impressions": 200, "position": 1.2}) case "date,page,query": rows = append(rows, map[string]any{"keys": []string{day, "https://us.example/pricing/", "nextcloud hosting price"}, "clicks": 0, "impressions": 3000, "position": 6}) } } json.NewEncoder(w).Encode(map[string]any{"rows": rows}) } })) defer srv.Close() oauth.SetGoogleURLs(srv.URL+"/auth", srv.URL+"/token", srv.URL+"/userinfo") gscBase = srv.URL defer func() { oauth.SetGoogleURLs("https://accounts.google.com/o/oauth2/v2/auth", "https://oauth2.googleapis.com/token", "https://openidconnect.googleapis.com/v1/userinfo") gscBase = "" gauth.token = "" }() s, h := newTestServer(t, nil) profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://us.example"}}}) if body := do(h, http.MethodGet, "/seo", nil, nil).Body.String(); !strings.Contains(body, "Connect Google Search Console") { t.Error("the SEO page doesn't say to connect") } postForm(h, "/settings/search", url.Values{secrets.GoogleClientID: {"cid"}, secrets.GoogleClientSecret: {"sec"}}) body := do(h, http.MethodGet, "/settings/search", nil, nil).Body.String() if !strings.Contains(body, "/auth/google/callback") || !strings.Contains(body, "Connect Search Console") { t.Fatal("no redirect URI or Connect button") } rec := postForm(h, "/auth/google/start", nil) loc, _ := url.Parse(rec.Header().Get("Location")) q := loc.Query() if !strings.HasPrefix(loc.String(), srv.URL+"/auth") || q.Get("access_type") != "offline" || !strings.Contains(q.Get("scope"), "webmasters.readonly") || q.Get("code_challenge") == "" { t.Fatalf("start: %s", loc) } rec = do(h, http.MethodGet, "/auth/google/callback?state="+url.QueryEscape(q.Get("state"))+"&code=c0de", nil, nil) if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/settings/search?msg=google-connected") { t.Fatalf("callback: %d %s %s", rec.Code, rec.Header().Get("Location"), rec.Body) } if s.Secrets.Get(secrets.GoogleRefreshToken) != "ref" { t.Error("the refresh token wasn't saved") } body = do(h, http.MethodGet, "/settings/search", nil, nil).Body.String() if !strings.Contains(body, "Connected as me@example.com") || !strings.Contains(body, `