mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Fixtures asserted against try.gitea.io, gitea.io and repositories under upstream's accounts. They now use reserved example hosts and an example owner, the recorded downloader responses were renamed to match, and the RPM and Alpine package fixtures were rebuilt with recomputed digests. The schema dumps the migration test restores are named for the version they capture. Two fixtures were wrong, not merely branded. readme-test carried a special-subdir-.gitea branch while the README lookup and its test both read .hanzo, so that case asserted against a branch that did not exist; the branch now holds .hanzo/README.md. The repository fixtures' delegate hooks still re-exec the pre-rename binary name, and the licenses end-to-end test looked for a module this tree no longer depends on. Co-authored-by: Hanzo Dev <[email protected]>
238 lines
8.2 KiB
Go
238 lines
8.2 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hanzoai/git/models/unit"
|
|
"github.com/hanzoai/git/models/unittest"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/markup"
|
|
"github.com/hanzoai/git/modules/optional"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var (
|
|
countRepospts = CountRepositoryOptions{OwnerID: 10}
|
|
countReposptsPublic = CountRepositoryOptions{OwnerID: 10, Private: optional.Some(false)}
|
|
countReposptsPrivate = CountRepositoryOptions{OwnerID: 10, Private: optional.Some(true)}
|
|
)
|
|
|
|
func TestGetRepositoryCount(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
ctx := t.Context()
|
|
count, err1 := CountRepositories(ctx, countRepospts)
|
|
privateCount, err2 := CountRepositories(ctx, countReposptsPrivate)
|
|
publicCount, err3 := CountRepositories(ctx, countReposptsPublic)
|
|
assert.NoError(t, err1)
|
|
assert.NoError(t, err2)
|
|
assert.NoError(t, err3)
|
|
assert.Equal(t, int64(3), count)
|
|
assert.Equal(t, privateCount+publicCount, count)
|
|
}
|
|
|
|
func TestGetPublicRepositoryCount(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
count, err := CountRepositories(t.Context(), countReposptsPublic)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(1), count)
|
|
}
|
|
|
|
func TestGetPrivateRepositoryCount(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
count, err := CountRepositories(t.Context(), countReposptsPrivate)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(2), count)
|
|
}
|
|
|
|
func TestRepoAPIURL(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10})
|
|
|
|
assert.Equal(t, "https://git.example.com/v1/repos/user12/repo10", repo.APIURL())
|
|
}
|
|
|
|
func TestWatchRepo(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3})
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
assert.NoError(t, WatchRepo(t.Context(), user, repo, true))
|
|
unittest.AssertExistsAndLoadBean(t, &Watch{RepoID: repo.ID, UserID: user.ID})
|
|
unittest.CheckConsistencyFor(t, &Repository{ID: repo.ID})
|
|
|
|
assert.NoError(t, WatchRepo(t.Context(), user, repo, false))
|
|
unittest.AssertNotExistsBean(t, &Watch{RepoID: repo.ID, UserID: user.ID})
|
|
unittest.CheckConsistencyFor(t, &Repository{ID: repo.ID})
|
|
}
|
|
|
|
func TestMetas(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := &Repository{Name: "testRepo"}
|
|
repo.Owner = &user_model.User{Name: "testOwner"}
|
|
repo.OwnerName = repo.Owner.Name
|
|
|
|
repo.Units = nil
|
|
|
|
metas := repo.ComposeCommentMetas(t.Context())
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
assert.Equal(t, "testOwner", metas["user"])
|
|
|
|
externalTracker := RepoUnit{
|
|
Type: unit.TypeExternalTracker,
|
|
Config: &ExternalTrackerConfig{
|
|
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
|
|
},
|
|
}
|
|
|
|
testSuccess := func(expectedStyle string) {
|
|
repo.Units = []*RepoUnit{&externalTracker}
|
|
repo.commonRenderingMetas = nil
|
|
metas := repo.ComposeCommentMetas(t.Context())
|
|
assert.Equal(t, expectedStyle, metas["style"])
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
assert.Equal(t, "testOwner", metas["user"])
|
|
assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
|
|
}
|
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
|
|
testSuccess(markup.IssueNameStyleAlphanumeric)
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleRegexp
|
|
testSuccess(markup.IssueNameStyleRegexp)
|
|
|
|
repo, err := GetRepositoryByID(t.Context(), 3)
|
|
assert.NoError(t, err)
|
|
|
|
metas = repo.ComposeCommentMetas(t.Context())
|
|
assert.Contains(t, metas, "org")
|
|
assert.Contains(t, metas, "teams")
|
|
assert.Equal(t, "org3", metas["org"])
|
|
assert.Equal(t, ",owners,team1,", metas["teams"])
|
|
}
|
|
|
|
func TestGetRepositoryByURL(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
t.Run("InvalidPath", func(t *testing.T) {
|
|
repo, err := GetRepositoryByURL(t.Context(), "something")
|
|
assert.Nil(t, repo)
|
|
assert.Error(t, err)
|
|
})
|
|
|
|
testRepo2 := func(t *testing.T, url string) {
|
|
repo, err := GetRepositoryByURL(t.Context(), url)
|
|
require.NoError(t, err)
|
|
assert.EqualValues(t, 2, repo.ID)
|
|
assert.EqualValues(t, 2, repo.OwnerID)
|
|
}
|
|
|
|
t.Run("ValidHttpURL", func(t *testing.T) {
|
|
testRepo2(t, "https://git.example.com/user2/repo2")
|
|
testRepo2(t, "https://git.example.com/user2/repo2.git")
|
|
})
|
|
|
|
t.Run("ValidGitSshURL", func(t *testing.T) {
|
|
testRepo2(t, "git+ssh://[email protected]/user2/repo2")
|
|
testRepo2(t, "git+ssh://[email protected]/user2/repo2.git")
|
|
|
|
testRepo2(t, "git+ssh://git.example.com/user2/repo2")
|
|
testRepo2(t, "git+ssh://git.example.com/user2/repo2.git")
|
|
})
|
|
|
|
t.Run("ValidImplicitSshURL", func(t *testing.T) {
|
|
testRepo2(t, "[email protected]:user2/repo2")
|
|
testRepo2(t, "[email protected]:user2/repo2.git")
|
|
|
|
testRelax := func(t *testing.T, url string) {
|
|
repo, err := GetRepositoryByURLRelax(t.Context(), url)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), repo.ID)
|
|
assert.Equal(t, int64(2), repo.OwnerID)
|
|
}
|
|
// TODO: it doesn't seem to be common git ssh URL, should we really support this?
|
|
testRelax(t, "git.example.com:user2/repo2")
|
|
testRelax(t, "git.example.com:user2/repo2.git")
|
|
})
|
|
}
|
|
|
|
func TestComposeSSHCloneURL(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.SSH, setting.SSH)()
|
|
defer test.MockVariableValue(&setting.Repository, setting.Repository)()
|
|
|
|
setting.SSH.User = "git"
|
|
|
|
// test SSH_DOMAIN
|
|
setting.SSH.Domain = "domain"
|
|
setting.SSH.Port = 22
|
|
setting.Repository.UseCompatSSHURI = false
|
|
assert.Equal(t, "git@domain:user/repo.git", ComposeSSHCloneURL(&user_model.User{Name: "doer"}, "user", "repo"))
|
|
setting.Repository.UseCompatSSHURI = true
|
|
assert.Equal(t, "ssh://git@domain/user/repo.git", ComposeSSHCloneURL(&user_model.User{Name: "doer"}, "user", "repo"))
|
|
// test SSH_DOMAIN while use non-standard SSH port
|
|
setting.SSH.Port = 123
|
|
setting.Repository.UseCompatSSHURI = false
|
|
assert.Equal(t, "ssh://git@domain:123/user/repo.git", ComposeSSHCloneURL(nil, "user", "repo"))
|
|
setting.Repository.UseCompatSSHURI = true
|
|
assert.Equal(t, "ssh://git@domain:123/user/repo.git", ComposeSSHCloneURL(nil, "user", "repo"))
|
|
|
|
// test IPv6 SSH_DOMAIN
|
|
setting.Repository.UseCompatSSHURI = false
|
|
setting.SSH.Domain = "::1"
|
|
setting.SSH.Port = 22
|
|
assert.Equal(t, "git@[::1]:user/repo.git", ComposeSSHCloneURL(nil, "user", "repo"))
|
|
setting.SSH.Port = 123
|
|
assert.Equal(t, "ssh://git@[::1]:123/user/repo.git", ComposeSSHCloneURL(nil, "user", "repo"))
|
|
|
|
setting.SSH.User = "(DOER_USERNAME)"
|
|
setting.SSH.Domain = "domain"
|
|
setting.SSH.Port = 22
|
|
assert.Equal(t, "doer@domain:user/repo.git", ComposeSSHCloneURL(&user_model.User{Name: "doer"}, "user", "repo"))
|
|
setting.SSH.Port = 123
|
|
assert.Equal(t, "ssh://doer@domain:123/user/repo.git", ComposeSSHCloneURL(&user_model.User{Name: "doer"}, "user", "repo"))
|
|
}
|
|
|
|
func TestIsUsableRepoName(t *testing.T) {
|
|
assert.NoError(t, IsUsableRepoName("a"))
|
|
assert.NoError(t, IsUsableRepoName("-1_."))
|
|
assert.NoError(t, IsUsableRepoName(".profile"))
|
|
|
|
assert.Error(t, IsUsableRepoName("-"))
|
|
assert.Error(t, IsUsableRepoName("🌞"))
|
|
assert.Error(t, IsUsableRepoName("the/repo"))
|
|
assert.Error(t, IsUsableRepoName("the..repo"))
|
|
assert.Error(t, IsUsableRepoName("foo.wiki"))
|
|
assert.Error(t, IsUsableRepoName("foo.git"))
|
|
assert.Error(t, IsUsableRepoName("foo.RSS"))
|
|
}
|
|
|
|
func TestIsValidSSHAccessRepoName(t *testing.T) {
|
|
assert.True(t, IsValidSSHAccessRepoName("a"))
|
|
assert.True(t, IsValidSSHAccessRepoName("-1_."))
|
|
assert.True(t, IsValidSSHAccessRepoName(".profile"))
|
|
assert.True(t, IsValidSSHAccessRepoName("foo.wiki"))
|
|
|
|
assert.False(t, IsValidSSHAccessRepoName("-"))
|
|
assert.False(t, IsValidSSHAccessRepoName("🌞"))
|
|
assert.False(t, IsValidSSHAccessRepoName("the/repo"))
|
|
assert.False(t, IsValidSSHAccessRepoName("the..repo"))
|
|
assert.False(t, IsValidSSHAccessRepoName("foo.git"))
|
|
assert.False(t, IsValidSSHAccessRepoName("foo.RSS"))
|
|
}
|