2019-05-30 18:09:05 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
2022-09-02 22:18:23 +03:00
package integration
2019-05-30 18:09:05 +03:00
import (
"fmt"
"net/http"
"net/url"
"testing"
2021-12-10 04:27:50 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-09 22:57:58 +03:00
unit_model "code.gitea.io/gitea/models/unit"
2021-11-16 11:53:21 +03:00
"code.gitea.io/gitea/models/unittest"
2021-11-24 12:49:20 +03:00
user_model "code.gitea.io/gitea/models/user"
2019-05-30 18:09:05 +03:00
api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
)
// getRepoEditOptionFromRepo gets the options for an existing repo exactly as is
2021-12-10 04:27:50 +03:00
func getRepoEditOptionFromRepo ( repo * repo_model . Repository ) * api . EditRepoOption {
2019-05-30 18:09:05 +03:00
name := repo . Name
description := repo . Description
website := repo . Website
private := repo . IsPrivate
hasIssues := false
2019-10-02 12:30:41 +03:00
var internalTracker * api . InternalTracker
var externalTracker * api . ExternalTracker
2021-11-09 22:57:58 +03:00
if unit , err := repo . GetUnit ( unit_model . TypeIssues ) ; err == nil {
2019-10-02 12:30:41 +03:00
config := unit . IssuesConfig ( )
2019-05-30 18:09:05 +03:00
hasIssues = true
2019-10-02 12:30:41 +03:00
internalTracker = & api . InternalTracker {
EnableTimeTracker : config . EnableTimetracker ,
AllowOnlyContributorsToTrackTime : config . AllowOnlyContributorsToTrackTime ,
EnableIssueDependencies : config . EnableDependencies ,
}
2021-11-09 22:57:58 +03:00
} else if unit , err := repo . GetUnit ( unit_model . TypeExternalTracker ) ; err == nil {
2019-10-02 12:30:41 +03:00
config := unit . ExternalTrackerConfig ( )
hasIssues = true
externalTracker = & api . ExternalTracker {
ExternalTrackerURL : config . ExternalTrackerURL ,
ExternalTrackerFormat : config . ExternalTrackerFormat ,
ExternalTrackerStyle : config . ExternalTrackerStyle ,
}
2019-05-30 18:09:05 +03:00
}
hasWiki := false
2019-10-02 12:30:41 +03:00
var externalWiki * api . ExternalWiki
2021-11-09 22:57:58 +03:00
if _ , err := repo . GetUnit ( unit_model . TypeWiki ) ; err == nil {
2019-05-30 18:09:05 +03:00
hasWiki = true
2021-11-09 22:57:58 +03:00
} else if unit , err := repo . GetUnit ( unit_model . TypeExternalWiki ) ; err == nil {
2019-10-02 12:30:41 +03:00
hasWiki = true
config := unit . ExternalWikiConfig ( )
externalWiki = & api . ExternalWiki {
ExternalWikiURL : config . ExternalWikiURL ,
}
2019-05-30 18:09:05 +03:00
}
defaultBranch := repo . DefaultBranch
hasPullRequests := false
ignoreWhitespaceConflicts := false
allowMerge := false
allowRebase := false
allowRebaseMerge := false
allowSquash := false
2021-11-09 22:57:58 +03:00
if unit , err := repo . GetUnit ( unit_model . TypePullRequests ) ; err == nil {
2019-05-30 18:09:05 +03:00
config := unit . PullRequestsConfig ( )
hasPullRequests = true
ignoreWhitespaceConflicts = config . IgnoreWhitespaceConflicts
allowMerge = config . AllowMerge
allowRebase = config . AllowRebase
allowRebaseMerge = config . AllowRebaseMerge
allowSquash = config . AllowSquash
}
archived := repo . IsArchived
return & api . EditRepoOption {
Name : & name ,
Description : & description ,
Website : & website ,
Private : & private ,
HasIssues : & hasIssues ,
2019-10-02 12:30:41 +03:00
ExternalTracker : externalTracker ,
InternalTracker : internalTracker ,
2019-05-30 18:09:05 +03:00
HasWiki : & hasWiki ,
2019-10-02 12:30:41 +03:00
ExternalWiki : externalWiki ,
2019-05-30 18:09:05 +03:00
DefaultBranch : & defaultBranch ,
HasPullRequests : & hasPullRequests ,
IgnoreWhitespaceConflicts : & ignoreWhitespaceConflicts ,
AllowMerge : & allowMerge ,
AllowRebase : & allowRebase ,
AllowRebaseMerge : & allowRebaseMerge ,
AllowSquash : & allowSquash ,
Archived : & archived ,
}
}
// getNewRepoEditOption Gets the options to change everything about an existing repo by adding to strings or changing
// the boolean
func getNewRepoEditOption ( opts * api . EditRepoOption ) * api . EditRepoOption {
// Gives a new property to everything
name := * opts . Name + "renamed"
description := "new description"
website := "http://wwww.newwebsite.com"
private := ! * opts . Private
hasIssues := ! * opts . HasIssues
hasWiki := ! * opts . HasWiki
defaultBranch := "master"
hasPullRequests := ! * opts . HasPullRequests
ignoreWhitespaceConflicts := ! * opts . IgnoreWhitespaceConflicts
allowMerge := ! * opts . AllowMerge
allowRebase := ! * opts . AllowRebase
allowRebaseMerge := ! * opts . AllowRebaseMerge
allowSquash := ! * opts . AllowSquash
archived := ! * opts . Archived
return & api . EditRepoOption {
Name : & name ,
Description : & description ,
Website : & website ,
Private : & private ,
DefaultBranch : & defaultBranch ,
HasIssues : & hasIssues ,
HasWiki : & hasWiki ,
HasPullRequests : & hasPullRequests ,
IgnoreWhitespaceConflicts : & ignoreWhitespaceConflicts ,
AllowMerge : & allowMerge ,
AllowRebase : & allowRebase ,
AllowRebaseMerge : & allowRebaseMerge ,
AllowSquash : & allowSquash ,
Archived : & archived ,
}
}
func TestAPIRepoEdit ( t * testing . T ) {
onGiteaRun ( t , func ( t * testing . T , u * url . URL ) {
2021-05-08 15:11:36 +03:00
bFalse , bTrue := false , true
2022-08-16 05:22:25 +03:00
user2 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } ) // owner of the repo1 & repo16
user3 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 3 } ) // owner of the repo3, is an org
user4 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 4 } ) // owner of neither repos
repo1 := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) // public repo
repo3 := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 3 } ) // public repo
repo15 := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 15 } ) // empty repo
repo16 := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 16 } ) // private repo
2019-05-30 18:09:05 +03:00
// Get user2's token
session := loginUser ( t , user2 . Name )
token2 := getTokenForLoggedInUser ( t , session )
// Get user4's token
session = loginUser ( t , user4 . Name )
token4 := getTokenForLoggedInUser ( t , session )
session = emptyTestSession ( t )
// Test editing a repo1 which user2 owns, changing name and many properties
origRepoEditOption := getRepoEditOptionFromRepo ( repo1 )
repoEditOption := getNewRepoEditOption ( origRepoEditOption )
url := fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , repo1 . Name , token2 )
req := NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
resp := session . MakeRequest ( t , req , http . StatusOK )
var repo api . Repository
DecodeJSON ( t , resp , & repo )
assert . NotNil ( t , repo )
// check response
assert . Equal ( t , * repoEditOption . Name , repo . Name )
assert . Equal ( t , * repoEditOption . Description , repo . Description )
assert . Equal ( t , * repoEditOption . Website , repo . Website )
assert . Equal ( t , * repoEditOption . Archived , repo . Archived )
// check repo1 from database
2022-08-16 05:22:25 +03:00
repo1edited := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2019-05-30 18:09:05 +03:00
repo1editedOption := getRepoEditOptionFromRepo ( repo1edited )
assert . Equal ( t , * repoEditOption . Name , * repo1editedOption . Name )
assert . Equal ( t , * repoEditOption . Description , * repo1editedOption . Description )
assert . Equal ( t , * repoEditOption . Website , * repo1editedOption . Website )
assert . Equal ( t , * repoEditOption . Archived , * repo1editedOption . Archived )
assert . Equal ( t , * repoEditOption . Private , * repo1editedOption . Private )
assert . Equal ( t , * repoEditOption . HasWiki , * repo1editedOption . HasWiki )
2019-10-02 12:30:41 +03:00
2022-01-20 20:46:10 +03:00
// Test editing repo1 to use internal issue and wiki (default)
2019-10-02 12:30:41 +03:00
* repoEditOption . HasIssues = true
repoEditOption . ExternalTracker = nil
repoEditOption . InternalTracker = & api . InternalTracker {
EnableTimeTracker : false ,
AllowOnlyContributorsToTrackTime : false ,
EnableIssueDependencies : false ,
}
* repoEditOption . HasWiki = true
repoEditOption . ExternalWiki = nil
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , * repoEditOption . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
resp = session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & repo )
assert . NotNil ( t , repo )
// check repo1 was written to database
2022-08-16 05:22:25 +03:00
repo1edited = unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2019-10-02 12:30:41 +03:00
repo1editedOption = getRepoEditOptionFromRepo ( repo1edited )
assert . Equal ( t , * repo1editedOption . HasIssues , true )
assert . Nil ( t , repo1editedOption . ExternalTracker )
assert . Equal ( t , * repo1editedOption . InternalTracker , * repoEditOption . InternalTracker )
assert . Equal ( t , * repo1editedOption . HasWiki , true )
assert . Nil ( t , repo1editedOption . ExternalWiki )
2022-01-20 20:46:10 +03:00
// Test editing repo1 to use external issue and wiki
2019-10-02 12:30:41 +03:00
repoEditOption . ExternalTracker = & api . ExternalTracker {
ExternalTrackerURL : "http://www.somewebsite.com" ,
ExternalTrackerFormat : "http://www.somewebsite.com/{user}/{repo}?issue={index}" ,
ExternalTrackerStyle : "alphanumeric" ,
}
repoEditOption . ExternalWiki = & api . ExternalWiki {
ExternalWikiURL : "http://www.somewebsite.com" ,
}
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
resp = session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & repo )
assert . NotNil ( t , repo )
// check repo1 was written to database
2022-08-16 05:22:25 +03:00
repo1edited = unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2019-10-02 12:30:41 +03:00
repo1editedOption = getRepoEditOptionFromRepo ( repo1edited )
assert . Equal ( t , * repo1editedOption . HasIssues , true )
assert . Equal ( t , * repo1editedOption . ExternalTracker , * repoEditOption . ExternalTracker )
assert . Equal ( t , * repo1editedOption . HasWiki , true )
assert . Equal ( t , * repo1editedOption . ExternalWiki , * repoEditOption . ExternalWiki )
// Do some tests with invalid URL for external tracker and wiki
repoEditOption . ExternalTracker . ExternalTrackerURL = "htp://www.somewebsite.com"
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
2021-11-18 04:33:06 +03:00
session . MakeRequest ( t , req , http . StatusUnprocessableEntity )
2019-10-02 12:30:41 +03:00
repoEditOption . ExternalTracker . ExternalTrackerURL = "http://www.somewebsite.com"
repoEditOption . ExternalTracker . ExternalTrackerFormat = "http://www.somewebsite.com/{user/{repo}?issue={index}"
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
2021-11-18 04:33:06 +03:00
session . MakeRequest ( t , req , http . StatusUnprocessableEntity )
2019-10-02 12:30:41 +03:00
repoEditOption . ExternalTracker . ExternalTrackerFormat = "http://www.somewebsite.com/{user}/{repo}?issue={index}"
repoEditOption . ExternalWiki . ExternalWikiURL = "htp://www.somewebsite.com"
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
2021-11-18 04:33:06 +03:00
session . MakeRequest ( t , req , http . StatusUnprocessableEntity )
2019-10-02 12:30:41 +03:00
2022-01-20 20:46:10 +03:00
// Test small repo change through API with issue and wiki option not set; They shall not be touched.
2019-10-02 12:30:41 +03:00
* repoEditOption . Description = "small change"
repoEditOption . HasIssues = nil
repoEditOption . ExternalTracker = nil
repoEditOption . HasWiki = nil
repoEditOption . ExternalWiki = nil
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
resp = session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & repo )
assert . NotNil ( t , repo )
// check repo1 was written to database
2022-08-16 05:22:25 +03:00
repo1edited = unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2019-10-02 12:30:41 +03:00
repo1editedOption = getRepoEditOptionFromRepo ( repo1edited )
assert . Equal ( t , * repo1editedOption . Description , * repoEditOption . Description )
assert . Equal ( t , * repo1editedOption . HasIssues , true )
assert . NotNil ( t , * repo1editedOption . ExternalTracker )
assert . Equal ( t , * repo1editedOption . HasWiki , true )
assert . NotNil ( t , * repo1editedOption . ExternalWiki )
2019-05-30 18:09:05 +03:00
// reset repo in db
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , * repoEditOption . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & origRepoEditOption )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusOK )
2019-05-30 18:09:05 +03:00
// Test editing a non-existing repo
name := "repodoesnotexist"
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & api . EditRepoOption { Name : & name } )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusNotFound )
2019-05-30 18:09:05 +03:00
// Test editing repo16 by user4 who does not have write access
origRepoEditOption = getRepoEditOptionFromRepo ( repo16 )
repoEditOption = getNewRepoEditOption ( origRepoEditOption )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , repo16 . Name , token4 )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
session . MakeRequest ( t , req , http . StatusNotFound )
// Tests a repo with no token given so will fail
origRepoEditOption = getRepoEditOptionFromRepo ( repo16 )
repoEditOption = getNewRepoEditOption ( origRepoEditOption )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s" , user2 . Name , repo16 . Name )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusNotFound )
2019-05-30 18:09:05 +03:00
// Test using access token for a private repo that the user of the token owns
origRepoEditOption = getRepoEditOptionFromRepo ( repo16 )
repoEditOption = getNewRepoEditOption ( origRepoEditOption )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , repo16 . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusOK )
2019-05-30 18:09:05 +03:00
// reset repo in db
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , * repoEditOption . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & origRepoEditOption )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusOK )
2019-05-30 18:09:05 +03:00
// Test making a repo public that is private
2022-08-16 05:22:25 +03:00
repo16 = unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 16 } )
2019-05-30 18:09:05 +03:00
assert . True ( t , repo16 . IsPrivate )
repoEditOption = & api . EditRepoOption {
2021-05-08 15:11:36 +03:00
Private : & bFalse ,
2019-05-30 18:09:05 +03:00
}
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , repo16 . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusOK )
2022-08-16 05:22:25 +03:00
repo16 = unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 16 } )
2019-05-30 18:09:05 +03:00
assert . False ( t , repo16 . IsPrivate )
// Make it private again
2021-05-08 15:11:36 +03:00
repoEditOption . Private = & bTrue
2019-05-30 18:09:05 +03:00
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusOK )
2019-05-30 18:09:05 +03:00
2021-05-08 15:11:36 +03:00
// Test to change empty repo
assert . False ( t , repo15 . IsArchived )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , repo15 . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & api . EditRepoOption {
Archived : & bTrue ,
} )
_ = session . MakeRequest ( t , req , http . StatusOK )
2022-08-16 05:22:25 +03:00
repo15 = unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 15 } )
2021-05-08 15:11:36 +03:00
assert . True ( t , repo15 . IsArchived )
req = NewRequestWithJSON ( t , "PATCH" , url , & api . EditRepoOption {
Archived : & bFalse ,
} )
_ = session . MakeRequest ( t , req , http . StatusOK )
2019-05-30 18:09:05 +03:00
// Test using org repo "user3/repo3" where user2 is a collaborator
origRepoEditOption = getRepoEditOptionFromRepo ( repo3 )
repoEditOption = getNewRepoEditOption ( origRepoEditOption )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user3 . Name , repo3 . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
session . MakeRequest ( t , req , http . StatusOK )
// reset repo in db
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user3 . Name , * repoEditOption . Name , token2 )
req = NewRequestWithJSON ( t , "PATCH" , url , & origRepoEditOption )
2019-07-22 10:11:16 +03:00
_ = session . MakeRequest ( t , req , http . StatusOK )
2019-05-30 18:09:05 +03:00
// Test using org repo "user3/repo3" with no user token
origRepoEditOption = getRepoEditOptionFromRepo ( repo3 )
repoEditOption = getNewRepoEditOption ( origRepoEditOption )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s" , user3 . Name , repo3 . Name )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
session . MakeRequest ( t , req , http . StatusNotFound )
// Test using repo "user2/repo1" where user4 is a NOT collaborator
origRepoEditOption = getRepoEditOptionFromRepo ( repo1 )
repoEditOption = getNewRepoEditOption ( origRepoEditOption )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s?token=%s" , user2 . Name , repo1 . Name , token4 )
req = NewRequestWithJSON ( t , "PATCH" , url , & repoEditOption )
session . MakeRequest ( t , req , http . StatusForbidden )
} )
}