You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.4 KiB
99 lines
3.4 KiB
4 years ago
|
// Copyright 2021 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.
|
||
|
|
||
2 years ago
|
package integration
|
||
4 years ago
|
|
||
|
import (
|
||
2 years ago
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
4 years ago
|
"os"
|
||
|
"testing"
|
||
|
|
||
2 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
2 years ago
|
"code.gitea.io/gitea/modules/structs"
|
||
3 years ago
|
"code.gitea.io/gitea/services/migrations"
|
||
4 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestMigrateLocalPath(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
4 years ago
|
|
||
2 years ago
|
adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
|
||
4 years ago
|
|
||
|
old := setting.ImportLocalPaths
|
||
|
setting.ImportLocalPaths = true
|
||
|
|
||
3 years ago
|
lowercasePath, err := os.MkdirTemp("", "lowercase") // may not be lowercase because MkdirTemp creates a random directory name which may be mixedcase
|
||
4 years ago
|
assert.NoError(t, err)
|
||
|
defer os.RemoveAll(lowercasePath)
|
||
|
|
||
|
err = migrations.IsMigrateURLAllowed(lowercasePath, adminUser)
|
||
|
assert.NoError(t, err, "case lowercase path")
|
||
|
|
||
3 years ago
|
mixedcasePath, err := os.MkdirTemp("", "mIxeDCaSe")
|
||
4 years ago
|
assert.NoError(t, err)
|
||
|
defer os.RemoveAll(mixedcasePath)
|
||
|
|
||
|
err = migrations.IsMigrateURLAllowed(mixedcasePath, adminUser)
|
||
|
assert.NoError(t, err, "case mixedcase path")
|
||
|
|
||
|
setting.ImportLocalPaths = old
|
||
|
}
|
||
2 years ago
|
|
||
|
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"
|
||
2 years ago
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: ownerName})
|
||
2 years ago
|
session := loginUser(t, ownerName)
|
||
|
token := getTokenForLoggedInUser(t, session)
|
||
|
|
||
|
// 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})
|
||
|
})
|
||
|
}
|