aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo/repo_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/repo/repo_test.go')
-rw-r--r--models/repo/repo_test.go91
1 files changed, 47 insertions, 44 deletions
diff --git a/models/repo/repo_test.go b/models/repo/repo_test.go
index 6d88d170da..a9e2cdfb75 100644
--- a/models/repo/repo_test.go
+++ b/models/repo/repo_test.go
@@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
var (
@@ -132,60 +133,43 @@ func TestGetRepositoryByURL(t *testing.T) {
t.Run("InvalidPath", func(t *testing.T) {
repo, err := GetRepositoryByURL(db.DefaultContext, "something")
-
assert.Nil(t, repo)
assert.Error(t, err)
})
- t.Run("ValidHttpURL", func(t *testing.T) {
- test := func(t *testing.T, url string) {
- repo, err := GetRepositoryByURL(db.DefaultContext, url)
-
- assert.NotNil(t, repo)
- assert.NoError(t, err)
-
- assert.Equal(t, int64(2), repo.ID)
- assert.Equal(t, int64(2), repo.OwnerID)
- }
+ testRepo2 := func(t *testing.T, url string) {
+ repo, err := GetRepositoryByURL(db.DefaultContext, url)
+ require.NoError(t, err)
+ assert.EqualValues(t, 2, repo.ID)
+ assert.EqualValues(t, 2, repo.OwnerID)
+ }
- test(t, "https://try.gitea.io/user2/repo2")
- test(t, "https://try.gitea.io/user2/repo2.git")
+ t.Run("ValidHttpURL", func(t *testing.T) {
+ testRepo2(t, "https://try.gitea.io/user2/repo2")
+ testRepo2(t, "https://try.gitea.io/user2/repo2.git")
})
t.Run("ValidGitSshURL", func(t *testing.T) {
- test := func(t *testing.T, url string) {
- repo, err := GetRepositoryByURL(db.DefaultContext, url)
-
- assert.NotNil(t, repo)
- assert.NoError(t, err)
+ testRepo2(t, "git+ssh://sshuser@try.gitea.io/user2/repo2")
+ testRepo2(t, "git+ssh://sshuser@try.gitea.io/user2/repo2.git")
- assert.Equal(t, int64(2), repo.ID)
- assert.Equal(t, int64(2), repo.OwnerID)
- }
-
- test(t, "git+ssh://sshuser@try.gitea.io/user2/repo2")
- test(t, "git+ssh://sshuser@try.gitea.io/user2/repo2.git")
-
- test(t, "git+ssh://try.gitea.io/user2/repo2")
- test(t, "git+ssh://try.gitea.io/user2/repo2.git")
+ testRepo2(t, "git+ssh://try.gitea.io/user2/repo2")
+ testRepo2(t, "git+ssh://try.gitea.io/user2/repo2.git")
})
t.Run("ValidImplicitSshURL", func(t *testing.T) {
- test := func(t *testing.T, url string) {
- repo, err := GetRepositoryByURL(db.DefaultContext, url)
-
- assert.NotNil(t, repo)
- assert.NoError(t, err)
+ testRepo2(t, "sshuser@try.gitea.io:user2/repo2")
+ testRepo2(t, "sshuser@try.gitea.io:user2/repo2.git")
+ testRelax := func(t *testing.T, url string) {
+ repo, err := GetRepositoryByURLRelax(db.DefaultContext, url)
+ require.NoError(t, err)
assert.Equal(t, int64(2), repo.ID)
assert.Equal(t, int64(2), repo.OwnerID)
}
-
- test(t, "sshuser@try.gitea.io:user2/repo2")
- test(t, "sshuser@try.gitea.io:user2/repo2.git")
-
- test(t, "try.gitea.io:user2/repo2")
- test(t, "try.gitea.io:user2/repo2.git")
+ // TODO: it doesn't seem to be common git ssh URL, should we really support this?
+ testRelax(t, "try.gitea.io:user2/repo2")
+ testRelax(t, "try.gitea.io:user2/repo2.git")
})
}
@@ -199,21 +183,40 @@ func TestComposeSSHCloneURL(t *testing.T) {
setting.SSH.Domain = "domain"
setting.SSH.Port = 22
setting.Repository.UseCompatSSHURI = false
- assert.Equal(t, "git@domain:user/repo.git", ComposeSSHCloneURL("user", "repo"))
+ 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", "repo"))
+ 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("user", "repo"))
+ 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("user", "repo"))
+ 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("user", "repo"))
+ 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://git@[::1]:123/user/repo.git", ComposeSSHCloneURL("user", "repo"))
+ 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("foo.wiki"))
+ assert.Error(t, IsUsableRepoName("foo.git"))
}