|
| 1 | +// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + repo_model "code.gitea.io/gitea/models/repo" |
| 13 | + unit_model "code.gitea.io/gitea/models/unit" |
| 14 | + "code.gitea.io/gitea/models/unittest" |
| 15 | + user_model "code.gitea.io/gitea/models/user" |
| 16 | + "code.gitea.io/gitea/modules/setting" |
| 17 | + repo_service "code.gitea.io/gitea/services/repository" |
| 18 | + "code.gitea.io/gitea/tests" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | +) |
| 22 | + |
| 23 | +func TestRepoSettingsUnits(t *testing.T) { |
| 24 | + defer tests.PrepareTestEnv(t)() |
| 25 | + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"}) |
| 26 | + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, Name: "repo1"}) |
| 27 | + session := loginUser(t, user.Name) |
| 28 | + |
| 29 | + req := NewRequest(t, "GET", fmt.Sprintf("%s/settings/units", repo.Link())) |
| 30 | + session.MakeRequest(t, req, http.StatusOK) |
| 31 | +} |
| 32 | + |
| 33 | +func TestRepoAddMoreUnits(t *testing.T) { |
| 34 | + defer tests.PrepareTestEnv(t)() |
| 35 | + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"}) |
| 36 | + session := loginUser(t, user.Name) |
| 37 | + |
| 38 | + // Make sure there are no disabled repos in the settings! |
| 39 | + setting.Repository.DisabledRepoUnits = []string{} |
| 40 | + unit_model.LoadUnitConfig() |
| 41 | + |
| 42 | + // Create a known-good repo, with all units enabled. |
| 43 | + repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{ |
| 44 | + unit_model.TypeCode, |
| 45 | + unit_model.TypePullRequests, |
| 46 | + unit_model.TypeProjects, |
| 47 | + unit_model.TypePackages, |
| 48 | + unit_model.TypeActions, |
| 49 | + unit_model.TypeIssues, |
| 50 | + unit_model.TypeWiki, |
| 51 | + }, nil, nil) |
| 52 | + defer f() |
| 53 | + |
| 54 | + assertAddMore := func(t *testing.T, present bool) { |
| 55 | + t.Helper() |
| 56 | + |
| 57 | + req := NewRequest(t, "GET", repo.Link()) |
| 58 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 59 | + htmlDoc := NewHTMLParser(t, resp.Body) |
| 60 | + htmlDoc.AssertElement(t, fmt.Sprintf("a[href='%s/settings/units']", repo.Link()), present) |
| 61 | + } |
| 62 | + |
| 63 | + t.Run("no add more with all units enabled", func(t *testing.T) { |
| 64 | + defer tests.PrintCurrentTest(t)() |
| 65 | + |
| 66 | + assertAddMore(t, false) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("add more if units can be enabled", func(t *testing.T) { |
| 70 | + defer tests.PrintCurrentTest(t)() |
| 71 | + defer func() { |
| 72 | + repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, []repo_model.RepoUnit{{ |
| 73 | + RepoID: repo.ID, |
| 74 | + Type: unit_model.TypePackages, |
| 75 | + }}, nil) |
| 76 | + }() |
| 77 | + |
| 78 | + // Disable the Packages unit |
| 79 | + err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, nil, []unit_model.Type{unit_model.TypePackages}) |
| 80 | + assert.NoError(t, err) |
| 81 | + |
| 82 | + assertAddMore(t, true) |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("no add more if unit is globally disabled", func(t *testing.T) { |
| 86 | + defer tests.PrintCurrentTest(t)() |
| 87 | + defer func() { |
| 88 | + repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, []repo_model.RepoUnit{{ |
| 89 | + RepoID: repo.ID, |
| 90 | + Type: unit_model.TypePackages, |
| 91 | + }}, nil) |
| 92 | + setting.Repository.DisabledRepoUnits = []string{} |
| 93 | + unit_model.LoadUnitConfig() |
| 94 | + }() |
| 95 | + |
| 96 | + // Disable the Packages unit globally |
| 97 | + setting.Repository.DisabledRepoUnits = []string{"repo.packages"} |
| 98 | + unit_model.LoadUnitConfig() |
| 99 | + |
| 100 | + // Disable the Packages unit |
| 101 | + err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, nil, []unit_model.Type{unit_model.TypePackages}) |
| 102 | + assert.NoError(t, err) |
| 103 | + |
| 104 | + // The "Add more" link appears no more |
| 105 | + assertAddMore(t, false) |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("issues & ext tracker globally disabled", func(t *testing.T) { |
| 109 | + defer tests.PrintCurrentTest(t)() |
| 110 | + defer func() { |
| 111 | + repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, []repo_model.RepoUnit{{ |
| 112 | + RepoID: repo.ID, |
| 113 | + Type: unit_model.TypeIssues, |
| 114 | + }}, nil) |
| 115 | + setting.Repository.DisabledRepoUnits = []string{} |
| 116 | + unit_model.LoadUnitConfig() |
| 117 | + }() |
| 118 | + |
| 119 | + // Disable both Issues and ExternalTracker units globally |
| 120 | + setting.Repository.DisabledRepoUnits = []string{"repo.issues", "repo.ext_issues"} |
| 121 | + unit_model.LoadUnitConfig() |
| 122 | + |
| 123 | + // Disable the Issues unit |
| 124 | + err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, nil, []unit_model.Type{unit_model.TypeIssues}) |
| 125 | + assert.NoError(t, err) |
| 126 | + |
| 127 | + // The "Add more" link appears no more |
| 128 | + assertAddMore(t, false) |
| 129 | + }) |
| 130 | +} |
0 commit comments