|
|
|
@ -9,7 +9,9 @@ import ( |
|
|
|
|
"net/http" |
|
|
|
|
"net/url" |
|
|
|
|
"strconv" |
|
|
|
|
"strings" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db" |
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo" |
|
|
|
@ -32,11 +34,10 @@ func TestMirrorPush(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testMirrorPush(t *testing.T, u *url.URL) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
|
|
|
|
|
setting.Migrations.AllowLocalNetworks = true |
|
|
|
|
assert.NoError(t, migrations.Init()) |
|
|
|
|
|
|
|
|
|
_ = db.TruncateBeans(db.DefaultContext, &repo_model.PushMirror{}) |
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
|
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
|
|
|
|
|
|
|
|
@ -45,9 +46,10 @@ func testMirrorPush(t *testing.T, u *url.URL) { |
|
|
|
|
}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
|
|
ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name) |
|
|
|
|
session := loginUser(t, user.Name) |
|
|
|
|
|
|
|
|
|
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t) |
|
|
|
|
pushMirrorURL := fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(user.Name), url.PathEscape(mirrorRepo.Name)) |
|
|
|
|
testCreatePushMirror(t, session, user.Name, srcRepo.Name, pushMirrorURL, user.LowerName, userPassword, "0") |
|
|
|
|
|
|
|
|
|
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
@ -73,49 +75,81 @@ func testMirrorPush(t *testing.T, u *url.URL) { |
|
|
|
|
assert.Equal(t, srcCommit.ID, mirrorCommit.ID) |
|
|
|
|
|
|
|
|
|
// 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) |
|
|
|
|
assert.True(t, doRemovePushMirror(t, session, user.Name, srcRepo.Name, mirrors[0].ID)) |
|
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, mirrors, 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func doCreatePushMirror(ctx APITestContext, address, username, password string) func(t *testing.T) { |
|
|
|
|
return func(t *testing.T) { |
|
|
|
|
csrf := GetUserCSRFToken(t, ctx.Session) |
|
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{ |
|
|
|
|
"_csrf": csrf, |
|
|
|
|
func testCreatePushMirror(t *testing.T, session *TestSession, owner, repo, address, username, password, interval string) { |
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(owner), url.PathEscape(repo)), map[string]string{ |
|
|
|
|
"_csrf": GetUserCSRFToken(t, session), |
|
|
|
|
"action": "push-mirror-add", |
|
|
|
|
"push_mirror_address": address, |
|
|
|
|
"push_mirror_username": username, |
|
|
|
|
"push_mirror_password": password, |
|
|
|
|
"push_mirror_interval": "0", |
|
|
|
|
"push_mirror_interval": interval, |
|
|
|
|
}) |
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther) |
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther) |
|
|
|
|
|
|
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash) |
|
|
|
|
flashCookie := session.GetCookie(gitea_context.CookieNameFlash) |
|
|
|
|
assert.NotNil(t, flashCookie) |
|
|
|
|
assert.Contains(t, flashCookie.Value, "success") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func doRemovePushMirror(ctx APITestContext, address, username, password string, pushMirrorID int) func(t *testing.T) { |
|
|
|
|
return func(t *testing.T) { |
|
|
|
|
csrf := GetUserCSRFToken(t, ctx.Session) |
|
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{ |
|
|
|
|
"_csrf": csrf, |
|
|
|
|
func doRemovePushMirror(t *testing.T, session *TestSession, owner, repo string, pushMirrorID int64) bool { |
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(owner), url.PathEscape(repo)), map[string]string{ |
|
|
|
|
"_csrf": GetUserCSRFToken(t, session), |
|
|
|
|
"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", |
|
|
|
|
"push_mirror_id": strconv.FormatInt(pushMirrorID, 10), |
|
|
|
|
}) |
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther) |
|
|
|
|
resp := session.MakeRequest(t, req, NoExpectedStatus) |
|
|
|
|
flashCookie := session.GetCookie(gitea_context.CookieNameFlash) |
|
|
|
|
return resp.Code == http.StatusSeeOther && flashCookie != nil && strings.Contains(flashCookie.Value, "success") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash) |
|
|
|
|
assert.NotNil(t, flashCookie) |
|
|
|
|
assert.Contains(t, flashCookie.Value, "success") |
|
|
|
|
func doUpdatePushMirror(t *testing.T, session *TestSession, owner, repo string, pushMirrorID int64, interval string) bool { |
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", owner, repo), map[string]string{ |
|
|
|
|
"_csrf": GetUserCSRFToken(t, session), |
|
|
|
|
"action": "push-mirror-update", |
|
|
|
|
"push_mirror_id": strconv.FormatInt(pushMirrorID, 10), |
|
|
|
|
"push_mirror_interval": interval, |
|
|
|
|
"push_mirror_defer_sync": "true", |
|
|
|
|
}) |
|
|
|
|
resp := session.MakeRequest(t, req, NoExpectedStatus) |
|
|
|
|
return resp.Code == http.StatusSeeOther |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRepoSettingPushMirrorUpdate(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
setting.Migrations.AllowLocalNetworks = true |
|
|
|
|
assert.NoError(t, migrations.Init()) |
|
|
|
|
|
|
|
|
|
session := loginUser(t, "user2") |
|
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) |
|
|
|
|
testCreatePushMirror(t, session, "user2", "repo2", "https://127.0.0.1/user1/repo1.git", "", "", "24h") |
|
|
|
|
|
|
|
|
|
pushMirrors, cnt, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, repo2.ID, db.ListOptions{}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.EqualValues(t, 1, cnt) |
|
|
|
|
assert.EqualValues(t, 24*time.Hour, pushMirrors[0].Interval) |
|
|
|
|
repo2PushMirrorID := pushMirrors[0].ID |
|
|
|
|
|
|
|
|
|
// update repo2 push mirror
|
|
|
|
|
assert.True(t, doUpdatePushMirror(t, session, "user2", "repo2", repo2PushMirrorID, "10m0s")) |
|
|
|
|
pushMirror := unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID}) |
|
|
|
|
assert.EqualValues(t, 10*time.Minute, pushMirror.Interval) |
|
|
|
|
|
|
|
|
|
// avoid updating repo2 push mirror from repo1
|
|
|
|
|
assert.False(t, doUpdatePushMirror(t, session, "user2", "repo1", repo2PushMirrorID, "20m0s")) |
|
|
|
|
pushMirror = unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID}) |
|
|
|
|
assert.EqualValues(t, 10*time.Minute, pushMirror.Interval) // not changed
|
|
|
|
|
|
|
|
|
|
// avoid deleting repo2 push mirror from repo1
|
|
|
|
|
assert.False(t, doRemovePushMirror(t, session, "user2", "repo1", repo2PushMirrorID)) |
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID}) |
|
|
|
|
|
|
|
|
|
// delete repo2 push mirror
|
|
|
|
|
assert.True(t, doRemovePushMirror(t, session, "user2", "repo2", repo2PushMirrorID)) |
|
|
|
|
unittest.AssertNotExistsBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID}) |
|
|
|
|
} |
|
|
|
|