2017-07-11 21:23:41 -04:00
// Copyright 2017 The Gogs Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2017-07-11 21:23:41 -04:00
2022-09-02 15:18:23 -04:00
package integration
2017-07-11 21:23:41 -04:00
import (
"net/http"
"testing"
2024-11-17 19:06:25 -08:00
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
org_model "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
2019-05-11 18:21:34 +08:00
api "code.gitea.io/gitea/modules/structs"
2024-11-27 13:12:26 -08:00
org_service "code.gitea.io/gitea/services/org"
2022-09-02 15:18:23 -04:00
"code.gitea.io/gitea/tests"
2024-11-17 19:06:25 -08:00
"github.com/stretchr/testify/assert"
2017-07-11 21:23:41 -04:00
)
func TestCreateForkNoLogin ( t * testing . T ) {
2022-09-02 15:18:23 -04:00
defer tests . PrepareTestEnv ( t ) ( )
2017-07-11 21:23:41 -04:00
req := NewRequestWithJSON ( t , "POST" , "/api/v1/repos/user2/repo1/forks" , & api . CreateForkOption { } )
MakeRequest ( t , req , http . StatusUnauthorized )
}
2024-11-17 19:06:25 -08:00
func TestAPIForkListLimitedAndPrivateRepos ( t * testing . T ) {
defer tests . PrepareTestEnv ( t ) ( )
user1Sess := loginUser ( t , "user1" )
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : "user1" } )
// fork into a limited org
limitedOrg := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 22 } )
assert . EqualValues ( t , api . VisibleTypeLimited , limitedOrg . Visibility )
ownerTeam1 , err := org_model . OrgFromUser ( limitedOrg ) . GetOwnerTeam ( db . DefaultContext )
assert . NoError ( t , err )
2024-11-27 13:12:26 -08:00
assert . NoError ( t , org_service . AddTeamMember ( db . DefaultContext , ownerTeam1 , user1 ) )
2024-11-17 19:06:25 -08:00
user1Token := getTokenForLoggedInUser ( t , user1Sess , auth_model . AccessTokenScopeWriteRepository , auth_model . AccessTokenScopeWriteOrganization )
req := NewRequestWithJSON ( t , "POST" , "/api/v1/repos/user2/repo1/forks" , & api . CreateForkOption {
Organization : & limitedOrg . Name ,
} ) . AddTokenAuth ( user1Token )
MakeRequest ( t , req , http . StatusAccepted )
// fork into a private org
user4Sess := loginUser ( t , "user4" )
user4 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : "user4" } )
privateOrg := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 23 } )
assert . EqualValues ( t , api . VisibleTypePrivate , privateOrg . Visibility )
ownerTeam2 , err := org_model . OrgFromUser ( privateOrg ) . GetOwnerTeam ( db . DefaultContext )
assert . NoError ( t , err )
2024-11-27 13:12:26 -08:00
assert . NoError ( t , org_service . AddTeamMember ( db . DefaultContext , ownerTeam2 , user4 ) )
2024-11-17 19:06:25 -08:00
user4Token := getTokenForLoggedInUser ( t , user4Sess , auth_model . AccessTokenScopeWriteRepository , auth_model . AccessTokenScopeWriteOrganization )
req = NewRequestWithJSON ( t , "POST" , "/api/v1/repos/user2/repo1/forks" , & api . CreateForkOption {
Organization : & privateOrg . Name ,
} ) . AddTokenAuth ( user4Token )
MakeRequest ( t , req , http . StatusAccepted )
t . Run ( "Anonymous" , func ( t * testing . T ) {
defer tests . PrintCurrentTest ( t ) ( )
req := NewRequest ( t , "GET" , "/api/v1/repos/user2/repo1/forks" )
resp := MakeRequest ( t , req , http . StatusOK )
var forks [ ] * api . Repository
DecodeJSON ( t , resp , & forks )
assert . Empty ( t , forks )
assert . EqualValues ( t , "0" , resp . Header ( ) . Get ( "X-Total-Count" ) )
} )
t . Run ( "Logged in" , func ( t * testing . T ) {
defer tests . PrintCurrentTest ( t ) ( )
req := NewRequest ( t , "GET" , "/api/v1/repos/user2/repo1/forks" ) . AddTokenAuth ( user1Token )
resp := MakeRequest ( t , req , http . StatusOK )
var forks [ ] * api . Repository
DecodeJSON ( t , resp , & forks )
assert . Len ( t , forks , 1 )
assert . EqualValues ( t , "1" , resp . Header ( ) . Get ( "X-Total-Count" ) )
2024-11-27 13:12:26 -08:00
assert . NoError ( t , org_service . AddTeamMember ( db . DefaultContext , ownerTeam2 , user1 ) )
2024-11-17 19:06:25 -08:00
req = NewRequest ( t , "GET" , "/api/v1/repos/user2/repo1/forks" ) . AddTokenAuth ( user1Token )
resp = MakeRequest ( t , req , http . StatusOK )
forks = [ ] * api . Repository { }
DecodeJSON ( t , resp , & forks )
assert . Len ( t , forks , 2 )
assert . EqualValues ( t , "2" , resp . Header ( ) . Get ( "X-Total-Count" ) )
} )
}