code.gitea.io/gitea@v1.21.7/tests/integration/mirror_push_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "context" 8 "fmt" 9 "net/http" 10 "net/url" 11 "strconv" 12 "testing" 13 14 "code.gitea.io/gitea/models/db" 15 repo_model "code.gitea.io/gitea/models/repo" 16 "code.gitea.io/gitea/models/unittest" 17 user_model "code.gitea.io/gitea/models/user" 18 gitea_context "code.gitea.io/gitea/modules/context" 19 "code.gitea.io/gitea/modules/git" 20 "code.gitea.io/gitea/modules/setting" 21 "code.gitea.io/gitea/services/migrations" 22 mirror_service "code.gitea.io/gitea/services/mirror" 23 repo_service "code.gitea.io/gitea/services/repository" 24 "code.gitea.io/gitea/tests" 25 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestMirrorPush(t *testing.T) { 30 onGiteaRun(t, testMirrorPush) 31 } 32 33 func testMirrorPush(t *testing.T, u *url.URL) { 34 defer tests.PrepareTestEnv(t)() 35 36 setting.Migrations.AllowLocalNetworks = true 37 assert.NoError(t, migrations.Init()) 38 39 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 40 srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 41 42 mirrorRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user, user, repo_service.CreateRepoOptions{ 43 Name: "test-push-mirror", 44 }) 45 assert.NoError(t, err) 46 47 ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name) 48 49 doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t) 50 51 mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{}) 52 assert.NoError(t, err) 53 assert.Len(t, mirrors, 1) 54 55 ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID) 56 assert.True(t, ok) 57 58 srcGitRepo, err := git.OpenRepository(git.DefaultContext, srcRepo.RepoPath()) 59 assert.NoError(t, err) 60 defer srcGitRepo.Close() 61 62 srcCommit, err := srcGitRepo.GetBranchCommit("master") 63 assert.NoError(t, err) 64 65 mirrorGitRepo, err := git.OpenRepository(git.DefaultContext, mirrorRepo.RepoPath()) 66 assert.NoError(t, err) 67 defer mirrorGitRepo.Close() 68 69 mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master") 70 assert.NoError(t, err) 71 72 assert.Equal(t, srcCommit.ID, mirrorCommit.ID) 73 74 // Cleanup 75 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) 76 mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{}) 77 assert.NoError(t, err) 78 assert.Len(t, mirrors, 0) 79 } 80 81 func doCreatePushMirror(ctx APITestContext, address, username, password string) func(t *testing.T) { 82 return func(t *testing.T) { 83 csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame))) 84 85 req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{ 86 "_csrf": csrf, 87 "action": "push-mirror-add", 88 "push_mirror_address": address, 89 "push_mirror_username": username, 90 "push_mirror_password": password, 91 "push_mirror_interval": "0", 92 }) 93 ctx.Session.MakeRequest(t, req, http.StatusSeeOther) 94 95 flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash) 96 assert.NotNil(t, flashCookie) 97 assert.Contains(t, flashCookie.Value, "success") 98 } 99 } 100 101 func doRemovePushMirror(ctx APITestContext, address, username, password string, pushMirrorID int) func(t *testing.T) { 102 return func(t *testing.T) { 103 csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame))) 104 105 req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{ 106 "_csrf": csrf, 107 "action": "push-mirror-remove", 108 "push_mirror_id": strconv.Itoa(pushMirrorID), 109 "push_mirror_address": address, 110 "push_mirror_username": username, 111 "push_mirror_password": password, 112 "push_mirror_interval": "0", 113 }) 114 ctx.Session.MakeRequest(t, req, http.StatusSeeOther) 115 116 flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash) 117 assert.NotNil(t, flashCookie) 118 assert.Contains(t, flashCookie.Value, "success") 119 } 120 }