2021-06-14 22:50:43 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-14 22:50:43 +05:30
|
|
|
|
2022-09-03 00:48:23 +05:30
|
|
|
package integration
|
2021-06-14 22:50:43 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-12-21 08:42:27 +05:30
|
|
|
"strconv"
|
2021-06-14 22:50:43 +05:30
|
|
|
"testing"
|
|
|
|
|
2022-07-30 22:15:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 14:23:21 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-04-14 01:15:33 +05:30
|
|
|
gitea_context "code.gitea.io/gitea/modules/context"
|
2021-06-14 22:50:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-11-20 15:04:05 +05:30
|
|
|
"code.gitea.io/gitea/services/migrations"
|
2021-06-14 22:50:43 +05:30
|
|
|
mirror_service "code.gitea.io/gitea/services/mirror"
|
2023-09-06 17:38:51 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2022-09-03 00:48:23 +05:30
|
|
|
"code.gitea.io/gitea/tests"
|
2021-06-14 22:50:43 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMirrorPush(t *testing.T) {
|
|
|
|
onGiteaRun(t, testMirrorPush)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMirrorPush(t *testing.T, u *url.URL) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-14 22:50:43 +05:30
|
|
|
|
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
2021-11-20 15:04:05 +05:30
|
|
|
assert.NoError(t, migrations.Init())
|
2021-06-14 22:50:43 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-06-14 22:50:43 +05:30
|
|
|
|
2023-09-06 17:38:51 +05:30
|
|
|
mirrorRepo, err := repo_service.CreateRepositoryDirectly(user, user, repo_service.CreateRepoOptions{
|
2021-06-14 22:50:43 +05:30
|
|
|
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)
|
|
|
|
|
2022-07-30 22:15:59 +05:30
|
|
|
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2021-06-14 22:50:43 +05:30
|
|
|
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 00:43:41 +05:30
|
|
|
srcGitRepo, err := git.OpenRepository(git.DefaultContext, srcRepo.RepoPath())
|
2021-06-14 22:50:43 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
defer srcGitRepo.Close()
|
|
|
|
|
|
|
|
srcCommit, err := srcGitRepo.GetBranchCommit("master")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
mirrorGitRepo, err := git.OpenRepository(git.DefaultContext, mirrorRepo.RepoPath())
|
2021-06-14 22:50:43 +05:30
|
|
|
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 08:42:27 +05:30
|
|
|
|
|
|
|
// 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)
|
2022-07-30 22:15:59 +05:30
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2021-12-21 08:42:27 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, mirrors, 0)
|
2021-06-14 22:50:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 10:24:07 +05:30
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-06-14 22:50:43 +05:30
|
|
|
|
2023-04-14 01:15:33 +05:30
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash)
|
2021-06-14 22:50:43 +05:30
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 08:42:27 +05:30
|
|
|
|
|
|
|
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 10:24:07 +05:30
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-12-21 08:42:27 +05:30
|
|
|
|
2023-04-14 01:15:33 +05:30
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash)
|
2021-12-21 08:42:27 +05:30
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|