|
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package integrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "code.gitea.io/gitea/models" |
| 15 | + "code.gitea.io/gitea/modules/git" |
| 16 | + "code.gitea.io/gitea/modules/repository" |
| 17 | + "code.gitea.io/gitea/modules/setting" |
| 18 | + mirror_service "code.gitea.io/gitea/services/mirror" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | +) |
| 22 | + |
| 23 | +func TestMirrorPush(t *testing.T) { |
| 24 | + onGiteaRun(t, testMirrorPush) |
| 25 | +} |
| 26 | + |
| 27 | +func testMirrorPush(t *testing.T, u *url.URL) { |
| 28 | + defer prepareTestEnv(t)() |
| 29 | + |
| 30 | + setting.Migrations.AllowLocalNetworks = true |
| 31 | + |
| 32 | + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) |
| 33 | + srcRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) |
| 34 | + |
| 35 | + mirrorRepo, err := repository.CreateRepository(user, user, models.CreateRepoOptions{ |
| 36 | + Name: "test-push-mirror", |
| 37 | + }) |
| 38 | + assert.NoError(t, err) |
| 39 | + |
| 40 | + ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name) |
| 41 | + |
| 42 | + doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t) |
| 43 | + |
| 44 | + mirrors, err := models.GetPushMirrorsByRepoID(srcRepo.ID) |
| 45 | + assert.NoError(t, err) |
| 46 | + assert.Len(t, mirrors, 1) |
| 47 | + |
| 48 | + ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID) |
| 49 | + assert.True(t, ok) |
| 50 | + |
| 51 | + srcGitRepo, err := git.OpenRepository(srcRepo.RepoPath()) |
| 52 | + assert.NoError(t, err) |
| 53 | + defer srcGitRepo.Close() |
| 54 | + |
| 55 | + srcCommit, err := srcGitRepo.GetBranchCommit("master") |
| 56 | + assert.NoError(t, err) |
| 57 | + |
| 58 | + mirrorGitRepo, err := git.OpenRepository(mirrorRepo.RepoPath()) |
| 59 | + assert.NoError(t, err) |
| 60 | + defer mirrorGitRepo.Close() |
| 61 | + |
| 62 | + mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master") |
| 63 | + assert.NoError(t, err) |
| 64 | + |
| 65 | + assert.Equal(t, srcCommit.ID, mirrorCommit.ID) |
| 66 | +} |
| 67 | + |
| 68 | +func doCreatePushMirror(ctx APITestContext, address, username, password string) func(t *testing.T) { |
| 69 | + return func(t *testing.T) { |
| 70 | + csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame))) |
| 71 | + |
| 72 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{ |
| 73 | + "_csrf": csrf, |
| 74 | + "action": "push-mirror-add", |
| 75 | + "push_mirror_address": address, |
| 76 | + "push_mirror_username": username, |
| 77 | + "push_mirror_password": password, |
| 78 | + "push_mirror_interval": "0", |
| 79 | + }) |
| 80 | + ctx.Session.MakeRequest(t, req, http.StatusFound) |
| 81 | + |
| 82 | + flashCookie := ctx.Session.GetCookie("macaron_flash") |
| 83 | + assert.NotNil(t, flashCookie) |
| 84 | + assert.Contains(t, flashCookie.Value, "success") |
| 85 | + } |
| 86 | +} |
0 commit comments