summaryrefslogtreecommitdiffstats
path: root/tests/integration/migrate_test.go
blob: 43cfc4f37d23d1a1a4162664f0918f5efce6b515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2021 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"testing"

	auth_model "code.gitea.io/gitea/models/auth"
	"code.gitea.io/gitea/models/db"
	issues_model "code.gitea.io/gitea/models/issues"
	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/modules/translation"
	"code.gitea.io/gitea/services/migrations"
	"code.gitea.io/gitea/services/repository"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestMigrateLocalPath(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())

	adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})

	old := setting.ImportLocalPaths
	setting.ImportLocalPaths = true

	basePath := t.TempDir()

	lowercasePath := filepath.Join(basePath, "lowercase")
	err := os.Mkdir(lowercasePath, 0o700)
	require.NoError(t, err)

	err = migrations.IsMigrateURLAllowed(lowercasePath, adminUser)
	require.NoError(t, err, "case lowercase path")

	mixedcasePath := filepath.Join(basePath, "mIxeDCaSe")
	err = os.Mkdir(mixedcasePath, 0o700)
	require.NoError(t, err)

	err = migrations.IsMigrateURLAllowed(mixedcasePath, adminUser)
	require.NoError(t, err, "case mixedcase path")

	setting.ImportLocalPaths = old
}

func TestMigrate(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()
		}()
		require.NoError(t, migrations.Init())

		ownerName := "user2"
		repoName := "repo1"
		repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: ownerName})
		session := loginUser(t, ownerName)
		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeReadMisc)

		for _, s := range []struct {
			svc structs.GitServiceType
		}{
			{svc: structs.GiteaService},
			{svc: structs.ForgejoService},
		} {
			// Step 0: verify the repo is available
			req := NewRequestf(t, "GET", "/%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", s.svc)
			resp := session.MakeRequest(t, req, http.StatusOK)
			// Step 2: load the form
			htmlDoc := NewHTMLParser(t, resp.Body)
			// Check form title
			title := htmlDoc.doc.Find("title").Text()
			assert.Contains(t, title, translation.NewLocale("en-US").TrString("new_migrate.title"))
			// Get the link of migration button
			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", s.svc),
				"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
			repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: migratedRepoName})

			// Step 7: delete the repository, so we can test with other services
			err := repository.DeleteRepository(context.Background(), repoOwner, repo, false)
			require.NoError(t, err)
		}
	})
}

func Test_UpdateCommentsMigrationsByType(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())

	err := issues_model.UpdateCommentsMigrationsByType(db.DefaultContext, structs.GithubService, "1", 1)
	require.NoError(t, err)
}