2021-06-15 01:20:43 +08:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-12-21 11:12:27 +08:00
|
|
|
"strconv"
|
2021-06-15 01:20:43 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 16:53:21 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-15 01:20:43 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/repository"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-11-20 17:34:05 +08:00
|
|
|
"code.gitea.io/gitea/services/migrations"
|
2021-06-15 01:20:43 +08:00
|
|
|
mirror_service "code.gitea.io/gitea/services/mirror"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMirrorPush(t *testing.T) {
|
|
|
|
onGiteaRun(t, testMirrorPush)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMirrorPush(t *testing.T, u *url.URL) {
|
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
|
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
2021-11-20 17:34:05 +08:00
|
|
|
assert.NoError(t, migrations.Init())
|
2021-06-15 01:20:43 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2021-12-10 09:27:50 +08:00
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
2021-06-15 01:20:43 +08:00
|
|
|
|
|
|
|
mirrorRepo, err := repository.CreateRepository(user, user, models.CreateRepoOptions{
|
|
|
|
Name: "test-push-mirror",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name)
|
|
|
|
|
|
|
|
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t)
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
mirrors, err := repo_model.GetPushMirrorsByRepoID(srcRepo.ID)
|
2021-06-15 01:20:43 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, mirrors, 1)
|
|
|
|
|
|
|
|
ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2022-03-30 03:13:41 +08:00
|
|
|
srcGitRepo, err := git.OpenRepository(git.DefaultContext, srcRepo.RepoPath())
|
2021-06-15 01:20:43 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer srcGitRepo.Close()
|
|
|
|
|
|
|
|
srcCommit, err := srcGitRepo.GetBranchCommit("master")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-03-30 03:13:41 +08:00
|
|
|
mirrorGitRepo, err := git.OpenRepository(git.DefaultContext, mirrorRepo.RepoPath())
|
2021-06-15 01:20:43 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer mirrorGitRepo.Close()
|
|
|
|
|
|
|
|
mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, srcCommit.ID, mirrorCommit.ID)
|
2021-12-21 11:12:27 +08:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
doRemovePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword, int(mirrors[0].ID))(t)
|
|
|
|
mirrors, err = repo_model.GetPushMirrorsByRepoID(srcRepo.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, mirrors, 0)
|
2021-06-15 01:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func doCreatePushMirror(ctx APITestContext, address, username, password string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": address,
|
|
|
|
"push_mirror_username": username,
|
|
|
|
"push_mirror_password": password,
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
2022-03-23 12:54:07 +08:00
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-06-15 01:20:43 +08:00
|
|
|
|
|
|
|
flashCookie := ctx.Session.GetCookie("macaron_flash")
|
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 11:12:27 +08:00
|
|
|
|
|
|
|
func doRemovePushMirror(ctx APITestContext, address, username, password string, pushMirrorID int) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"action": "push-mirror-remove",
|
|
|
|
"push_mirror_id": strconv.Itoa(pushMirrorID),
|
|
|
|
"push_mirror_address": address,
|
|
|
|
"push_mirror_username": username,
|
|
|
|
"push_mirror_password": password,
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
2022-03-23 12:54:07 +08:00
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-12-21 11:12:27 +08:00
|
|
|
|
|
|
|
flashCookie := ctx.Session.GetCookie("macaron_flash")
|
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|