You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mirror_push_test.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. package integrations
  5. import (
  6. "context"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/models/unittest"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/repository"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/services/migrations"
  18. mirror_service "code.gitea.io/gitea/services/mirror"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. func TestMirrorPush(t *testing.T) {
  22. onGiteaRun(t, testMirrorPush)
  23. }
  24. func testMirrorPush(t *testing.T, u *url.URL) {
  25. defer prepareTestEnv(t)()
  26. setting.Migrations.AllowLocalNetworks = true
  27. assert.NoError(t, migrations.Init())
  28. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  29. srcRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  30. mirrorRepo, err := repository.CreateRepository(user, user, models.CreateRepoOptions{
  31. Name: "test-push-mirror",
  32. })
  33. assert.NoError(t, err)
  34. ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name)
  35. doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t)
  36. mirrors, err := models.GetPushMirrorsByRepoID(srcRepo.ID)
  37. assert.NoError(t, err)
  38. assert.Len(t, mirrors, 1)
  39. ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID)
  40. assert.True(t, ok)
  41. srcGitRepo, err := git.OpenRepository(srcRepo.RepoPath())
  42. assert.NoError(t, err)
  43. defer srcGitRepo.Close()
  44. srcCommit, err := srcGitRepo.GetBranchCommit("master")
  45. assert.NoError(t, err)
  46. mirrorGitRepo, err := git.OpenRepository(mirrorRepo.RepoPath())
  47. assert.NoError(t, err)
  48. defer mirrorGitRepo.Close()
  49. mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master")
  50. assert.NoError(t, err)
  51. assert.Equal(t, srcCommit.ID, mirrorCommit.ID)
  52. }
  53. func doCreatePushMirror(ctx APITestContext, address, username, password string) func(t *testing.T) {
  54. return func(t *testing.T) {
  55. csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
  56. req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{
  57. "_csrf": csrf,
  58. "action": "push-mirror-add",
  59. "push_mirror_address": address,
  60. "push_mirror_username": username,
  61. "push_mirror_password": password,
  62. "push_mirror_interval": "0",
  63. })
  64. ctx.Session.MakeRequest(t, req, http.StatusFound)
  65. flashCookie := ctx.Session.GetCookie("macaron_flash")
  66. assert.NotNil(t, flashCookie)
  67. assert.Contains(t, flashCookie.Value, "success")
  68. }
  69. }