@@ -0,0 +1,325 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package search_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "io" |
| 9 | + "log/slog" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/jackc/pgx/v5/pgtype" |
| 14 | + |
| 15 | + "github.com/tenseleyFlow/shithub/internal/auth/policy" |
| 16 | + policydb "github.com/tenseleyFlow/shithub/internal/auth/policy/sqlc" |
| 17 | + "github.com/tenseleyFlow/shithub/internal/issues" |
| 18 | + issuesdb "github.com/tenseleyFlow/shithub/internal/issues/sqlc" |
| 19 | + reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc" |
| 20 | + "github.com/tenseleyFlow/shithub/internal/search" |
| 21 | + "github.com/tenseleyFlow/shithub/internal/testing/dbtest" |
| 22 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| 23 | +) |
| 24 | + |
| 25 | +const fixtureHash = "$argon2id$v=19$m=16384,t=1,p=1$" + |
| 26 | + "AAAAAAAAAAAAAAAA$" + |
| 27 | + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 28 | + |
| 29 | +// TestParseQuery covers the operator parser end-to-end. |
| 30 | +func TestParseQuery(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + cases := []struct { |
| 33 | + in string |
| 34 | + want search.ParsedQuery |
| 35 | + }{ |
| 36 | + {"", search.ParsedQuery{}}, |
| 37 | + {"hello world", search.ParsedQuery{Text: "hello world"}}, |
| 38 | + {`"quoted phrase"`, search.ParsedQuery{Phrase: "quoted phrase"}}, |
| 39 | + {"repo:alice/demo bug", search.ParsedQuery{Text: "bug", |
| 40 | + RepoFilter: &search.RepoFilter{Owner: "alice", Name: "demo"}}}, |
| 41 | + {"repo:noslash bug", search.ParsedQuery{Text: "repo:noslash bug"}}, |
| 42 | + {"is:open broken", search.ParsedQuery{Text: "broken", StateFilter: "open"}}, |
| 43 | + {"state:closed bug", search.ParsedQuery{Text: "bug", StateFilter: "closed"}}, |
| 44 | + {"author:bob fix", search.ParsedQuery{Text: "fix", AuthorFilter: "bob"}}, |
| 45 | + {"language:Go x", search.ParsedQuery{Text: "language:Go x"}}, |
| 46 | + } |
| 47 | + for _, c := range cases { |
| 48 | + got := search.ParseQuery(c.in) |
| 49 | + if got.Text != c.want.Text || got.Phrase != c.want.Phrase || |
| 50 | + got.StateFilter != c.want.StateFilter || got.AuthorFilter != c.want.AuthorFilter { |
| 51 | + t.Errorf("ParseQuery(%q):\n got %+v\n want %+v", c.in, got, c.want) |
| 52 | + continue |
| 53 | + } |
| 54 | + if (got.RepoFilter == nil) != (c.want.RepoFilter == nil) { |
| 55 | + t.Errorf("ParseQuery(%q): repo-filter presence mismatch", c.in) |
| 56 | + continue |
| 57 | + } |
| 58 | + if got.RepoFilter != nil && (*got.RepoFilter != *c.want.RepoFilter) { |
| 59 | + t.Errorf("ParseQuery(%q): repo-filter %+v, want %+v", |
| 60 | + c.in, *got.RepoFilter, *c.want.RepoFilter) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// TestParseQuery_TruncatesOverlong ensures the input cap fires. |
| 66 | +func TestParseQuery_TruncatesOverlong(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + long := strings.Repeat("x", search.MaxQueryBytes+50) |
| 69 | + got := search.ParseQuery(long) |
| 70 | + if len(got.Text) > search.MaxQueryBytes { |
| 71 | + t.Errorf("Text len = %d, want ≤ %d", len(got.Text), search.MaxQueryBytes) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// fxs is a fixture for visibility tests: alice owns one public + one |
| 76 | +// private repo, each with one issue. bob is a separate user, no |
| 77 | +// access to the private side. |
| 78 | +type fxs struct { |
| 79 | + deps search.Deps |
| 80 | + alice usersdb.User |
| 81 | + bob usersdb.User |
| 82 | + pubRepo reposdb.Repo |
| 83 | + prvRepo reposdb.Repo |
| 84 | +} |
| 85 | + |
| 86 | +func setup(t *testing.T) fxs { |
| 87 | + t.Helper() |
| 88 | + pool := dbtest.NewTestDB(t) |
| 89 | + ctx := context.Background() |
| 90 | + |
| 91 | + uq := usersdb.New() |
| 92 | + alice, err := uq.CreateUser(ctx, pool, usersdb.CreateUserParams{ |
| 93 | + Username: "alice", DisplayName: "Alice", PasswordHash: fixtureHash, |
| 94 | + }) |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("CreateUser alice: %v", err) |
| 97 | + } |
| 98 | + bob, err := uq.CreateUser(ctx, pool, usersdb.CreateUserParams{ |
| 99 | + Username: "bob", DisplayName: "Bob", PasswordHash: fixtureHash, |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("CreateUser bob: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + rq := reposdb.New() |
| 106 | + pubRepo, err := rq.CreateRepo(ctx, pool, reposdb.CreateRepoParams{ |
| 107 | + OwnerUserID: pgtype.Int8{Int64: alice.ID, Valid: true}, |
| 108 | + Name: "publicrepo", |
| 109 | + Description: "a public sample", |
| 110 | + DefaultBranch: "trunk", |
| 111 | + Visibility: reposdb.RepoVisibilityPublic, |
| 112 | + }) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("CreateRepo public: %v", err) |
| 115 | + } |
| 116 | + prvRepo, err := rq.CreateRepo(ctx, pool, reposdb.CreateRepoParams{ |
| 117 | + OwnerUserID: pgtype.Int8{Int64: alice.ID, Valid: true}, |
| 118 | + Name: "privaterepo", |
| 119 | + Description: "secrets here", |
| 120 | + DefaultBranch: "trunk", |
| 121 | + Visibility: reposdb.RepoVisibilityPrivate, |
| 122 | + }) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("CreateRepo private: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + iq := issuesdb.New() |
| 128 | + for _, r := range []reposdb.Repo{pubRepo, prvRepo} { |
| 129 | + if err := iq.EnsureRepoIssueCounter(ctx, pool, r.ID); err != nil { |
| 130 | + t.Fatalf("EnsureRepoIssueCounter: %v", err) |
| 131 | + } |
| 132 | + } |
| 133 | + idep := issues.Deps{Pool: pool, Logger: slog.New(slog.NewTextHandler(io.Discard, nil))} |
| 134 | + if _, err := issues.Create(ctx, idep, issues.CreateParams{ |
| 135 | + RepoID: pubRepo.ID, AuthorUserID: alice.ID, |
| 136 | + Title: "public bug report", Body: "nothing secret", |
| 137 | + }); err != nil { |
| 138 | + t.Fatalf("Create issue pub: %v", err) |
| 139 | + } |
| 140 | + if _, err := issues.Create(ctx, idep, issues.CreateParams{ |
| 141 | + RepoID: prvRepo.ID, AuthorUserID: alice.ID, |
| 142 | + Title: "private secret design", Body: "internal only", |
| 143 | + }); err != nil { |
| 144 | + t.Fatalf("Create issue prv: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + return fxs{ |
| 148 | + deps: search.Deps{ |
| 149 | + Pool: pool, |
| 150 | + Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 151 | + }, |
| 152 | + alice: alice, bob: bob, pubRepo: pubRepo, prvRepo: prvRepo, |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// TestSearchRepos_AnonymousSeesOnlyPublic guards the visibility |
| 157 | +// boundary — the highest-stakes assertion in the search surface. |
| 158 | +func TestSearchRepos_AnonymousSeesOnlyPublic(t *testing.T) { |
| 159 | + f := setup(t) |
| 160 | + got, _, err := search.SearchRepos(context.Background(), f.deps, |
| 161 | + policy.AnonymousActor(), |
| 162 | + search.ParseQuery("repo"), |
| 163 | + 20, 0) |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("SearchRepos: %v", err) |
| 166 | + } |
| 167 | + for _, r := range got { |
| 168 | + if r.Visibility == "private" { |
| 169 | + t.Errorf("anonymous saw private repo %q — visibility leak!", r.Name) |
| 170 | + } |
| 171 | + } |
| 172 | + // Sanity: public repo is in the results. |
| 173 | + found := false |
| 174 | + for _, r := range got { |
| 175 | + if r.Name == "publicrepo" { |
| 176 | + found = true |
| 177 | + } |
| 178 | + } |
| 179 | + if !found { |
| 180 | + t.Errorf("expected publicrepo in anon results, got %d rows", len(got)) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// TestSearchRepos_NonCollabOnPrivate matches the spec's private- |
| 185 | +// content-stays-private contract. |
| 186 | +func TestSearchRepos_NonCollabOnPrivate(t *testing.T) { |
| 187 | + f := setup(t) |
| 188 | + bobActor := policy.UserActor(f.bob.ID, f.bob.Username, false, false) |
| 189 | + got, _, err := search.SearchRepos(context.Background(), f.deps, bobActor, |
| 190 | + search.ParseQuery("secrets"), 20, 0) |
| 191 | + if err != nil { |
| 192 | + t.Fatalf("SearchRepos: %v", err) |
| 193 | + } |
| 194 | + if len(got) != 0 { |
| 195 | + t.Errorf("non-collab bob saw %d results for 'secrets', want 0", len(got)) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +// TestSearchRepos_OwnerSeesPrivate confirms the predicate's owner |
| 200 | +// branch. |
| 201 | +func TestSearchRepos_OwnerSeesPrivate(t *testing.T) { |
| 202 | + f := setup(t) |
| 203 | + alice := policy.UserActor(f.alice.ID, f.alice.Username, false, false) |
| 204 | + got, _, err := search.SearchRepos(context.Background(), f.deps, alice, |
| 205 | + search.ParseQuery("secrets"), 20, 0) |
| 206 | + if err != nil { |
| 207 | + t.Fatalf("SearchRepos: %v", err) |
| 208 | + } |
| 209 | + if len(got) == 0 { |
| 210 | + t.Fatalf("owner alice should see her private repo for 'secrets'") |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +// TestSearchRepos_CollabSeesPrivate exercises the collaborator |
| 215 | +// branch of the visibility predicate. |
| 216 | +func TestSearchRepos_CollabSeesPrivate(t *testing.T) { |
| 217 | + f := setup(t) |
| 218 | + ctx := context.Background() |
| 219 | + pq := policydb.New() |
| 220 | + if err := pq.UpsertCollabRole(ctx, f.deps.Pool, policydb.UpsertCollabRoleParams{ |
| 221 | + RepoID: f.prvRepo.ID, UserID: f.bob.ID, Role: policydb.CollabRoleRead, |
| 222 | + }); err != nil { |
| 223 | + t.Fatalf("UpsertCollabRole: %v", err) |
| 224 | + } |
| 225 | + bobActor := policy.UserActor(f.bob.ID, f.bob.Username, false, false) |
| 226 | + got, _, err := search.SearchRepos(ctx, f.deps, bobActor, |
| 227 | + search.ParseQuery("secrets"), 20, 0) |
| 228 | + if err != nil { |
| 229 | + t.Fatalf("SearchRepos: %v", err) |
| 230 | + } |
| 231 | + if len(got) == 0 { |
| 232 | + t.Errorf("collab bob should see private repo via 'secrets'") |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +// TestSearchIssues_AnonymousSeesOnlyPublic mirrors the repo test |
| 237 | +// for the issue surface — issues inherit visibility from their repo. |
| 238 | +func TestSearchIssues_AnonymousSeesOnlyPublic(t *testing.T) { |
| 239 | + f := setup(t) |
| 240 | + got, _, err := search.SearchIssues(context.Background(), f.deps, |
| 241 | + policy.AnonymousActor(), |
| 242 | + search.ParseQuery("secret"), |
| 243 | + "issue", 20, 0) |
| 244 | + if err != nil { |
| 245 | + t.Fatalf("SearchIssues: %v", err) |
| 246 | + } |
| 247 | + if len(got) != 0 { |
| 248 | + t.Errorf("anonymous saw %d issues for 'secret', want 0 (private leak)", len(got)) |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +func TestSearchIssues_StateFilter(t *testing.T) { |
| 253 | + f := setup(t) |
| 254 | + ctx := context.Background() |
| 255 | + alice := policy.UserActor(f.alice.ID, f.alice.Username, false, false) |
| 256 | + |
| 257 | + // Open a second issue and close it. |
| 258 | + idep := issues.Deps{Pool: f.deps.Pool, Logger: slog.New(slog.NewTextHandler(io.Discard, nil))} |
| 259 | + closed, _ := issues.Create(ctx, idep, issues.CreateParams{ |
| 260 | + RepoID: f.pubRepo.ID, AuthorUserID: f.alice.ID, |
| 261 | + Title: "closed bug", Body: "fixed", |
| 262 | + }) |
| 263 | + if err := issues.SetState(ctx, idep, f.alice.ID, closed.ID, "closed", "completed"); err != nil { |
| 264 | + t.Fatalf("SetState: %v", err) |
| 265 | + } |
| 266 | + |
| 267 | + openHits, _, _ := search.SearchIssues(ctx, f.deps, alice, |
| 268 | + search.ParseQuery("is:open bug"), "", 20, 0) |
| 269 | + for _, h := range openHits { |
| 270 | + if h.State != "open" { |
| 271 | + t.Errorf("is:open: got state=%s", h.State) |
| 272 | + } |
| 273 | + } |
| 274 | + closedHits, _, _ := search.SearchIssues(ctx, f.deps, alice, |
| 275 | + search.ParseQuery("is:closed bug"), "", 20, 0) |
| 276 | + for _, h := range closedHits { |
| 277 | + if h.State != "closed" { |
| 278 | + t.Errorf("is:closed: got state=%s", h.State) |
| 279 | + } |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +func TestSearchIssues_RepoFilter(t *testing.T) { |
| 284 | + f := setup(t) |
| 285 | + alice := policy.UserActor(f.alice.ID, f.alice.Username, false, false) |
| 286 | + got, _, err := search.SearchIssues(context.Background(), f.deps, alice, |
| 287 | + search.ParseQuery("repo:alice/publicrepo bug"), "", 20, 0) |
| 288 | + if err != nil { |
| 289 | + t.Fatalf("SearchIssues: %v", err) |
| 290 | + } |
| 291 | + for _, h := range got { |
| 292 | + if h.OwnerUsername != "alice" || h.RepoName != "publicrepo" { |
| 293 | + t.Errorf("repo: filter let through %s/%s", h.OwnerUsername, h.RepoName) |
| 294 | + } |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +func TestSearchUsers_ExcludesSuspended(t *testing.T) { |
| 299 | + f := setup(t) |
| 300 | + ctx := context.Background() |
| 301 | + if _, err := f.deps.Pool.Exec(ctx, |
| 302 | + "UPDATE users SET suspended_at = now() WHERE id = $1", f.bob.ID); err != nil { |
| 303 | + t.Fatalf("suspend: %v", err) |
| 304 | + } |
| 305 | + got, _, err := search.SearchUsers(ctx, f.deps, search.ParseQuery("bob"), 20, 0) |
| 306 | + if err != nil { |
| 307 | + t.Fatalf("SearchUsers: %v", err) |
| 308 | + } |
| 309 | + for _, u := range got { |
| 310 | + if u.Username == "bob" { |
| 311 | + t.Errorf("suspended bob in user search results") |
| 312 | + } |
| 313 | + } |
| 314 | +} |
| 315 | + |
| 316 | +// TestSearchRepos_EmptyQuery surfaces the typed error so handlers |
| 317 | +// can render a friendly empty state rather than a SQL error. |
| 318 | +func TestSearchRepos_EmptyQuery(t *testing.T) { |
| 319 | + f := setup(t) |
| 320 | + _, _, err := search.SearchRepos(context.Background(), f.deps, |
| 321 | + policy.AnonymousActor(), search.ParsedQuery{}, 20, 0) |
| 322 | + if !errors.Is(err, search.ErrEmptyQuery) { |
| 323 | + t.Errorf("expected ErrEmptyQuery, got %v", err) |
| 324 | + } |
| 325 | +} |