1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-09-30 19:22:08 +00:00
forgejo/models/repo/update_test.go
patka 668e9bb2ac fix: check target repo limit instead of user repo limit (#9360)
Fixes https://codeberg.org/forgejo/forgejo/issues/9344

## 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...
  - [x] 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/9360
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: patka <patka@patka.dev>
Co-committed-by: patka <patka@patka.dev>
2025-09-21 08:47:10 +02:00

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", true)
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", true)
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", true)
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/", true)
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", true)
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", false)
require.ErrorIs(t, err, repo_model.ErrRepoAlreadyExist{Name: "repo1", Uname: "user2"})
})
}