mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-09-30 19:22:08 +00:00
Found this while working on !9360. Making sure that the dir doesn't exist is already done a few lines above. The removed parameter was always set to false, since the function is only called in a single place. (it smells like this code was refactored at some point, but some stuff was forgotten) ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9378 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: patka <patka@patka.dev> Co-committed-by: patka <patka@patka.dev>
74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"forgejo.org/models/db"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/models/unittest"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCheckCreateRepository(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
user := &user_model.User{MaxRepoCreation: 1}
|
|
|
|
err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName")
|
|
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("AdminIgnoresRepoLimit", func(t *testing.T) {
|
|
user := &user_model.User{MaxRepoCreation: 0, IsAdmin: true}
|
|
|
|
err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName")
|
|
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("RepoLimitReached", func(t *testing.T) {
|
|
user := &user_model.User{MaxRepoCreation: 0}
|
|
|
|
err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName")
|
|
|
|
require.ErrorIs(t, err, repo_model.ErrReachLimitOfRepo{})
|
|
})
|
|
|
|
t.Run("UnusableRepoName", func(t *testing.T) {
|
|
user := &user_model.User{MaxRepoCreation: 1}
|
|
|
|
err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName/")
|
|
|
|
require.ErrorIs(t, err, db.ErrNameCharsNotAllowed{Name: "testName/"})
|
|
})
|
|
|
|
t.Run("RepoAlreadyExists", func(t *testing.T) {
|
|
unittest.AssertExistsIf(t, true, &repo_model.Repository{Name: "repo1"})
|
|
user := &user_model.User{MaxRepoCreation: 2}
|
|
|
|
err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "repo1")
|
|
|
|
require.ErrorIs(t, err, repo_model.ErrRepoAlreadyExist{Name: "repo1"})
|
|
})
|
|
|
|
t.Run("RepoDirAlreadyExists", func(t *testing.T) {
|
|
db.DeleteByBean(db.DefaultContext, &repo_model.Repository{Name: "repo1"})
|
|
user := &user_model.User{MaxRepoCreation: 2, Name: "user2"}
|
|
|
|
exists, _ := util.IsExist(repo_model.RepoPath("user2", "repo1"))
|
|
assert.True(t, exists)
|
|
|
|
err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "repo1")
|
|
|
|
require.ErrorIs(t, err, repo_model.ErrRepoAlreadyExist{Name: "repo1", Uname: "user2"})
|
|
})
|
|
}
|