@@ -10,9 +10,14 @@ import (
1010 "code.gitea.io/gitea/models/db"
1111 repo_model "code.gitea.io/gitea/models/repo"
1212 "code.gitea.io/gitea/models/unittest"
13+ user_model "code.gitea.io/gitea/models/user"
1314 "code.gitea.io/gitea/modules/optional"
15+ "code.gitea.io/gitea/modules/setting"
16+ "code.gitea.io/gitea/modules/structs"
17+ "code.gitea.io/gitea/modules/test"
1418
1519 "github.com/stretchr/testify/assert"
20+ "github.com/stretchr/testify/require"
1621)
1722
1823func getTestCases () []struct {
@@ -182,7 +187,16 @@ func getTestCases() []struct {
182187
183188func TestSearchRepository (t * testing.T ) {
184189 assert .NoError (t , unittest .PrepareTestDatabase ())
190+ t .Run ("SearchRepositoryPublic" , testSearchRepositoryPublic )
191+ t .Run ("SearchRepositoryPublicRestricted" , testSearchRepositoryRestricted )
192+ t .Run ("SearchRepositoryPrivate" , testSearchRepositoryPrivate )
193+ t .Run ("SearchRepositoryNonExistingOwner" , testSearchRepositoryNonExistingOwner )
194+ t .Run ("SearchRepositoryWithInDescription" , testSearchRepositoryWithInDescription )
195+ t .Run ("SearchRepositoryNotInDescription" , testSearchRepositoryNotInDescription )
196+ t .Run ("SearchRepositoryCases" , testSearchRepositoryCases )
197+ }
185198
199+ func testSearchRepositoryPublic (t * testing.T ) {
186200 // test search public repository on explore page
187201 repos , count , err := repo_model .SearchRepositoryByName (t .Context (), repo_model.SearchRepoOptions {
188202 ListOptions : db.ListOptions {
@@ -211,9 +225,54 @@ func TestSearchRepository(t *testing.T) {
211225 assert .NoError (t , err )
212226 assert .Equal (t , int64 (2 ), count )
213227 assert .Len (t , repos , 2 )
228+ }
229+
230+ func testSearchRepositoryRestricted (t * testing.T ) {
231+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
232+ restrictedUser := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 29 , IsRestricted : true })
233+
234+ performSearch := func (t * testing.T , user * user_model.User ) (publicRepoIDs []int64 ) {
235+ repos , count , err := repo_model .SearchRepositoryByName (t .Context (), repo_model.SearchRepoOptions {
236+ ListOptions : db.ListOptions {Page : 1 , PageSize : 10000 },
237+ Actor : user ,
238+ })
239+ require .NoError (t , err )
240+ assert .Len (t , repos , int (count ))
241+ for _ , repo := range repos {
242+ require .NoError (t , repo .LoadOwner (t .Context ()))
243+ if repo .Owner .Visibility == structs .VisibleTypePublic && ! repo .IsPrivate {
244+ publicRepoIDs = append (publicRepoIDs , repo .ID )
245+ }
246+ }
247+ return publicRepoIDs
248+ }
249+
250+ normalPublicRepoIDs := performSearch (t , user2 )
251+ require .Greater (t , len (normalPublicRepoIDs ), 10 ) // quite a lot
252+
253+ t .Run ("RestrictedUser-NoSignInRequirement" , func (t * testing.T ) {
254+ // restricted user can also see public repositories if no "required sign-in"
255+ repoIDs := performSearch (t , restrictedUser )
256+ assert .ElementsMatch (t , normalPublicRepoIDs , repoIDs )
257+ })
258+
259+ defer test .MockVariableValue (& setting .Service .RequireSignInViewStrict , true )()
214260
261+ t .Run ("NormalUser-RequiredSignIn" , func (t * testing.T ) {
262+ // normal user can still see all public repos, not affected by "required sign-in"
263+ repoIDs := performSearch (t , user2 )
264+ assert .ElementsMatch (t , normalPublicRepoIDs , repoIDs )
265+ })
266+ t .Run ("RestrictedUser-RequiredSignIn" , func (t * testing.T ) {
267+ // restricted user can see only their own repo
268+ repoIDs := performSearch (t , restrictedUser )
269+ assert .Equal (t , []int64 {4 }, repoIDs )
270+ })
271+ }
272+
273+ func testSearchRepositoryPrivate (t * testing.T ) {
215274 // test search private repository on explore page
216- repos , count , err = repo_model .SearchRepositoryByName (t .Context (), repo_model.SearchRepoOptions {
275+ repos , count , err : = repo_model .SearchRepositoryByName (t .Context (), repo_model.SearchRepoOptions {
217276 ListOptions : db.ListOptions {
218277 Page : 1 ,
219278 PageSize : 10 ,
@@ -242,16 +301,18 @@ func TestSearchRepository(t *testing.T) {
242301 assert .NoError (t , err )
243302 assert .Equal (t , int64 (3 ), count )
244303 assert .Len (t , repos , 3 )
304+ }
245305
246- // Test non existing owner
247- repos , count , err = repo_model .SearchRepositoryByName (t .Context (), repo_model.SearchRepoOptions {OwnerID : unittest .NonexistentID })
306+ func testSearchRepositoryNonExistingOwner ( t * testing. T ) {
307+ repos , count , err : = repo_model .SearchRepositoryByName (t .Context (), repo_model.SearchRepoOptions {OwnerID : unittest .NonexistentID })
248308
249309 assert .NoError (t , err )
250310 assert .Empty (t , repos )
251311 assert .Equal (t , int64 (0 ), count )
312+ }
252313
253- // Test search within description
254- repos , count , err = repo_model .SearchRepository (t .Context (), repo_model.SearchRepoOptions {
314+ func testSearchRepositoryWithInDescription ( t * testing. T ) {
315+ repos , count , err : = repo_model .SearchRepository (t .Context (), repo_model.SearchRepoOptions {
255316 ListOptions : db.ListOptions {
256317 Page : 1 ,
257318 PageSize : 10 ,
@@ -266,9 +327,10 @@ func TestSearchRepository(t *testing.T) {
266327 assert .Equal (t , "test_repo_14" , repos [0 ].Name )
267328 }
268329 assert .Equal (t , int64 (1 ), count )
330+ }
269331
270- // Test NOT search within description
271- repos , count , err = repo_model .SearchRepository (t .Context (), repo_model.SearchRepoOptions {
332+ func testSearchRepositoryNotInDescription ( t * testing. T ) {
333+ repos , count , err : = repo_model .SearchRepository (t .Context (), repo_model.SearchRepoOptions {
272334 ListOptions : db.ListOptions {
273335 Page : 1 ,
274336 PageSize : 10 ,
@@ -281,7 +343,9 @@ func TestSearchRepository(t *testing.T) {
281343 assert .NoError (t , err )
282344 assert .Empty (t , repos )
283345 assert .Equal (t , int64 (0 ), count )
346+ }
284347
348+ func testSearchRepositoryCases (t * testing.T ) {
285349 testCases := getTestCases ()
286350
287351 for _ , testCase := range testCases {
0 commit comments