2021-03-18 16:58:47 +03:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2021-03-18 16:58:47 +03:00
2022-09-02 22:18:23 +03:00
package integration
2021-03-18 16:58:47 +03:00
import (
2022-07-04 15:36:24 +03:00
"fmt"
"net/http"
"net/url"
2021-03-18 16:58:47 +03:00
"os"
2022-09-04 18:14:53 +03:00
"path/filepath"
2021-03-18 16:58:47 +03:00
"testing"
2023-01-18 00:46:03 +03:00
auth_model "code.gitea.io/gitea/models/auth"
2022-07-04 15:36:24 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-16 18:25:33 +03:00
"code.gitea.io/gitea/models/unittest"
2021-11-24 12:49:20 +03:00
user_model "code.gitea.io/gitea/models/user"
2021-03-18 16:58:47 +03:00
"code.gitea.io/gitea/modules/setting"
2022-07-04 15:36:24 +03:00
"code.gitea.io/gitea/modules/structs"
2021-11-16 18:25:33 +03:00
"code.gitea.io/gitea/services/migrations"
2021-03-18 16:58:47 +03:00
"github.com/stretchr/testify/assert"
)
func TestMigrateLocalPath ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-03-18 16:58:47 +03:00
2022-08-16 05:22:25 +03:00
adminUser := unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : "user1" } )
2021-03-18 16:58:47 +03:00
old := setting . ImportLocalPaths
setting . ImportLocalPaths = true
2022-09-04 18:14:53 +03:00
basePath := t . TempDir ( )
lowercasePath := filepath . Join ( basePath , "lowercase" )
err := os . Mkdir ( lowercasePath , 0 o700 )
2021-03-18 16:58:47 +03:00
assert . NoError ( t , err )
err = migrations . IsMigrateURLAllowed ( lowercasePath , adminUser )
assert . NoError ( t , err , "case lowercase path" )
2022-09-04 18:14:53 +03:00
mixedcasePath := filepath . Join ( basePath , "mIxeDCaSe" )
err = os . Mkdir ( mixedcasePath , 0 o700 )
2021-03-18 16:58:47 +03:00
assert . NoError ( t , err )
err = migrations . IsMigrateURLAllowed ( mixedcasePath , adminUser )
assert . NoError ( t , err , "case mixedcase path" )
setting . ImportLocalPaths = old
}
2022-07-04 15:36:24 +03:00
func TestMigrateGiteaForm ( t * testing . T ) {
onGiteaRun ( t , func ( t * testing . T , u * url . URL ) {
AllowLocalNetworks := setting . Migrations . AllowLocalNetworks
setting . Migrations . AllowLocalNetworks = true
AppVer := setting . AppVer
// Gitea SDK (go-sdk) need to parse the AppVer from server response, so we must set it to a valid version string.
setting . AppVer = "1.16.0"
defer func ( ) {
setting . Migrations . AllowLocalNetworks = AllowLocalNetworks
setting . AppVer = AppVer
migrations . Init ( )
} ( )
assert . NoError ( t , migrations . Init ( ) )
ownerName := "user2"
repoName := "repo1"
2022-08-16 05:22:25 +03:00
repoOwner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : ownerName } )
2022-07-04 15:36:24 +03:00
session := loginUser ( t , ownerName )
2023-01-18 00:46:03 +03:00
token := getTokenForLoggedInUser ( t , session , auth_model . AccessTokenScopeRepo )
2022-07-04 15:36:24 +03:00
// Step 0: verify the repo is available
req := NewRequestf ( t , "GET" , fmt . Sprintf ( "/%s/%s" , ownerName , repoName ) )
_ = session . MakeRequest ( t , req , http . StatusOK )
// Step 1: get the Gitea migration form
req = NewRequestf ( t , "GET" , "/repo/migrate/?service_type=%d" , structs . GiteaService )
resp := session . MakeRequest ( t , req , http . StatusOK )
// Step 2: load the form
htmlDoc := NewHTMLParser ( t , resp . Body )
link , exists := htmlDoc . doc . Find ( ` form.ui.form[action^="/repo/migrate"] ` ) . Attr ( "action" )
assert . True ( t , exists , "The template has changed" )
// Step 4: submit the migration to only migrate issues
migratedRepoName := "otherrepo"
req = NewRequestWithValues ( t , "POST" , link , map [ string ] string {
"_csrf" : htmlDoc . GetCSRF ( ) ,
"service" : fmt . Sprintf ( "%d" , structs . GiteaService ) ,
"clone_addr" : fmt . Sprintf ( "%s%s/%s" , u , ownerName , repoName ) ,
"auth_token" : token ,
"issues" : "on" ,
"repo_name" : migratedRepoName ,
"description" : "" ,
"uid" : fmt . Sprintf ( "%d" , repoOwner . ID ) ,
} )
resp = session . MakeRequest ( t , req , http . StatusSeeOther )
// Step 5: a redirection displays the migrated repository
loc := resp . Header ( ) . Get ( "Location" )
assert . EqualValues ( t , fmt . Sprintf ( "/%s/%s" , ownerName , migratedRepoName ) , loc )
// Step 6: check the repo was created
unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { Name : migratedRepoName } )
} )
}