|
4 | 4 | package repo_test |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "path/filepath" |
| 8 | + "slices" |
7 | 9 | "strings" |
8 | 10 | "testing" |
9 | 11 |
|
10 | 12 | "code.gitea.io/gitea/models/db" |
11 | 13 | repo_model "code.gitea.io/gitea/models/repo" |
12 | 14 | "code.gitea.io/gitea/models/unittest" |
| 15 | + "code.gitea.io/gitea/models/user" |
13 | 16 | "code.gitea.io/gitea/modules/optional" |
| 17 | + "code.gitea.io/gitea/modules/setting" |
| 18 | + "code.gitea.io/gitea/modules/structs" |
14 | 19 |
|
15 | 20 | "github.com/stretchr/testify/assert" |
16 | 21 | "github.com/stretchr/testify/require" |
@@ -403,3 +408,43 @@ func TestSearchRepositoryByTopicName(t *testing.T) { |
403 | 408 | }) |
404 | 409 | } |
405 | 410 | } |
| 411 | + |
| 412 | +func TestSearchRepositoryIDsByCondition(t *testing.T) { |
| 413 | + defer unittest.OverrideFixtures( |
| 414 | + unittest.FixturesOptions{ |
| 415 | + Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"), |
| 416 | + Base: setting.AppWorkPath, |
| 417 | + Dirs: []string{"models/repo/TestSearchRepositoryIDsByCondition/"}, |
| 418 | + }, |
| 419 | + )() |
| 420 | + require.NoError(t, unittest.PrepareTestDatabase()) |
| 421 | + // Sanity check of the database |
| 422 | + limitedUser := unittest.AssertExistsAndLoadBean(t, &user.User{ID: 33, Visibility: structs.VisibleTypeLimited}) |
| 423 | + unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1001, OwnerID: limitedUser.ID}) |
| 424 | + |
| 425 | + testCases := []struct { |
| 426 | + user *user.User |
| 427 | + repoIDs []int64 |
| 428 | + }{ |
| 429 | + { |
| 430 | + user: nil, |
| 431 | + repoIDs: []int64{1, 4, 8, 9, 10, 11, 12, 14, 17, 18, 21, 23, 25, 27, 29, 32, 33, 34, 35, 36, 37, 42, 44, 45, 46, 47, 48, 49, 50, 51, 53, 57, 58, 60, 61, 62, 1059}, |
| 432 | + }, |
| 433 | + { |
| 434 | + user: unittest.AssertExistsAndLoadBean(t, &user.User{ID: 4}), |
| 435 | + repoIDs: []int64{1, 3, 4, 8, 9, 10, 11, 12, 14, 17, 18, 21, 23, 25, 27, 29, 32, 33, 34, 35, 36, 37, 38, 40, 42, 44, 45, 46, 47, 48, 49, 50, 51, 53, 57, 58, 60, 61, 62, 1001, 1059}, |
| 436 | + }, |
| 437 | + { |
| 438 | + user: unittest.AssertExistsAndLoadBean(t, &user.User{ID: 5}), |
| 439 | + repoIDs: []int64{1, 4, 8, 9, 10, 11, 12, 14, 17, 18, 21, 23, 25, 27, 29, 32, 33, 34, 35, 36, 37, 38, 40, 42, 44, 45, 46, 47, 48, 49, 50, 51, 53, 57, 58, 60, 61, 62, 1001, 1059}, |
| 440 | + }, |
| 441 | + } |
| 442 | + |
| 443 | + for _, testCase := range testCases { |
| 444 | + repoIDs, err := repo_model.FindUserCodeAccessibleRepoIDs(db.DefaultContext, testCase.user) |
| 445 | + require.NoError(t, err) |
| 446 | + |
| 447 | + slices.Sort(repoIDs) |
| 448 | + assert.EqualValues(t, testCase.repoIDs, repoIDs) |
| 449 | + } |
| 450 | +} |
0 commit comments