code.gitea.io/gitea@v1.21.7/models/repo/repo_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package repo_test 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/db" 10 repo_model "code.gitea.io/gitea/models/repo" 11 "code.gitea.io/gitea/models/unit" 12 "code.gitea.io/gitea/models/unittest" 13 user_model "code.gitea.io/gitea/models/user" 14 "code.gitea.io/gitea/modules/markup" 15 "code.gitea.io/gitea/modules/setting" 16 "code.gitea.io/gitea/modules/test" 17 "code.gitea.io/gitea/modules/util" 18 19 "github.com/stretchr/testify/assert" 20 ) 21 22 var ( 23 countRepospts = repo_model.CountRepositoryOptions{OwnerID: 10} 24 countReposptsPublic = repo_model.CountRepositoryOptions{OwnerID: 10, Private: util.OptionalBoolFalse} 25 countReposptsPrivate = repo_model.CountRepositoryOptions{OwnerID: 10, Private: util.OptionalBoolTrue} 26 ) 27 28 func TestGetRepositoryCount(t *testing.T) { 29 assert.NoError(t, unittest.PrepareTestDatabase()) 30 31 ctx := db.DefaultContext 32 count, err1 := repo_model.CountRepositories(ctx, countRepospts) 33 privateCount, err2 := repo_model.CountRepositories(ctx, countReposptsPrivate) 34 publicCount, err3 := repo_model.CountRepositories(ctx, countReposptsPublic) 35 assert.NoError(t, err1) 36 assert.NoError(t, err2) 37 assert.NoError(t, err3) 38 assert.Equal(t, int64(3), count) 39 assert.Equal(t, privateCount+publicCount, count) 40 } 41 42 func TestGetPublicRepositoryCount(t *testing.T) { 43 assert.NoError(t, unittest.PrepareTestDatabase()) 44 45 count, err := repo_model.CountRepositories(db.DefaultContext, countReposptsPublic) 46 assert.NoError(t, err) 47 assert.Equal(t, int64(1), count) 48 } 49 50 func TestGetPrivateRepositoryCount(t *testing.T) { 51 assert.NoError(t, unittest.PrepareTestDatabase()) 52 53 count, err := repo_model.CountRepositories(db.DefaultContext, countReposptsPrivate) 54 assert.NoError(t, err) 55 assert.Equal(t, int64(2), count) 56 } 57 58 func TestRepoAPIURL(t *testing.T) { 59 assert.NoError(t, unittest.PrepareTestDatabase()) 60 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) 61 62 assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL()) 63 } 64 65 func TestWatchRepo(t *testing.T) { 66 assert.NoError(t, unittest.PrepareTestDatabase()) 67 const repoID = 3 68 const userID = 2 69 70 assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, userID, repoID, true)) 71 unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{RepoID: repoID, UserID: userID}) 72 unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID}) 73 74 assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, userID, repoID, false)) 75 unittest.AssertNotExistsBean(t, &repo_model.Watch{RepoID: repoID, UserID: userID}) 76 unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID}) 77 } 78 79 func TestMetas(t *testing.T) { 80 assert.NoError(t, unittest.PrepareTestDatabase()) 81 82 repo := &repo_model.Repository{Name: "testRepo"} 83 repo.Owner = &user_model.User{Name: "testOwner"} 84 repo.OwnerName = repo.Owner.Name 85 86 repo.Units = nil 87 88 metas := repo.ComposeMetas() 89 assert.Equal(t, "testRepo", metas["repo"]) 90 assert.Equal(t, "testOwner", metas["user"]) 91 92 externalTracker := repo_model.RepoUnit{ 93 Type: unit.TypeExternalTracker, 94 Config: &repo_model.ExternalTrackerConfig{ 95 ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}", 96 }, 97 } 98 99 testSuccess := func(expectedStyle string) { 100 repo.Units = []*repo_model.RepoUnit{&externalTracker} 101 repo.RenderingMetas = nil 102 metas := repo.ComposeMetas() 103 assert.Equal(t, expectedStyle, metas["style"]) 104 assert.Equal(t, "testRepo", metas["repo"]) 105 assert.Equal(t, "testOwner", metas["user"]) 106 assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"]) 107 } 108 109 testSuccess(markup.IssueNameStyleNumeric) 110 111 externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric 112 testSuccess(markup.IssueNameStyleAlphanumeric) 113 114 externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric 115 testSuccess(markup.IssueNameStyleNumeric) 116 117 externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleRegexp 118 testSuccess(markup.IssueNameStyleRegexp) 119 120 repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 3) 121 assert.NoError(t, err) 122 123 metas = repo.ComposeMetas() 124 assert.Contains(t, metas, "org") 125 assert.Contains(t, metas, "teams") 126 assert.Equal(t, "org3", metas["org"]) 127 assert.Equal(t, ",owners,team1,", metas["teams"]) 128 } 129 130 func TestGetRepositoryByURL(t *testing.T) { 131 assert.NoError(t, unittest.PrepareTestDatabase()) 132 133 t.Run("InvalidPath", func(t *testing.T) { 134 repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, "something") 135 136 assert.Nil(t, repo) 137 assert.Error(t, err) 138 }) 139 140 t.Run("ValidHttpURL", func(t *testing.T) { 141 test := func(t *testing.T, url string) { 142 repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, url) 143 144 assert.NotNil(t, repo) 145 assert.NoError(t, err) 146 147 assert.Equal(t, repo.ID, int64(2)) 148 assert.Equal(t, repo.OwnerID, int64(2)) 149 } 150 151 test(t, "https://try.gitea.io/user2/repo2") 152 test(t, "https://try.gitea.io/user2/repo2.git") 153 }) 154 155 t.Run("ValidGitSshURL", func(t *testing.T) { 156 test := func(t *testing.T, url string) { 157 repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, url) 158 159 assert.NotNil(t, repo) 160 assert.NoError(t, err) 161 162 assert.Equal(t, repo.ID, int64(2)) 163 assert.Equal(t, repo.OwnerID, int64(2)) 164 } 165 166 test(t, "git+ssh://sshuser@try.gitea.io/user2/repo2") 167 test(t, "git+ssh://sshuser@try.gitea.io/user2/repo2.git") 168 169 test(t, "git+ssh://try.gitea.io/user2/repo2") 170 test(t, "git+ssh://try.gitea.io/user2/repo2.git") 171 }) 172 173 t.Run("ValidImplicitSshURL", func(t *testing.T) { 174 test := func(t *testing.T, url string) { 175 repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, url) 176 177 assert.NotNil(t, repo) 178 assert.NoError(t, err) 179 180 assert.Equal(t, repo.ID, int64(2)) 181 assert.Equal(t, repo.OwnerID, int64(2)) 182 } 183 184 test(t, "sshuser@try.gitea.io:user2/repo2") 185 test(t, "sshuser@try.gitea.io:user2/repo2.git") 186 187 test(t, "try.gitea.io:user2/repo2") 188 test(t, "try.gitea.io:user2/repo2.git") 189 }) 190 } 191 192 func TestComposeSSHCloneURL(t *testing.T) { 193 defer test.MockVariableValue(&setting.SSH, setting.SSH)() 194 defer test.MockVariableValue(&setting.Repository, setting.Repository)() 195 196 setting.SSH.User = "git" 197 198 // test SSH_DOMAIN 199 setting.SSH.Domain = "domain" 200 setting.SSH.Port = 22 201 setting.Repository.UseCompatSSHURI = false 202 assert.Equal(t, "git@domain:user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo")) 203 setting.Repository.UseCompatSSHURI = true 204 assert.Equal(t, "ssh://git@domain/user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo")) 205 // test SSH_DOMAIN while use non-standard SSH port 206 setting.SSH.Port = 123 207 setting.Repository.UseCompatSSHURI = false 208 assert.Equal(t, "ssh://git@domain:123/user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo")) 209 setting.Repository.UseCompatSSHURI = true 210 assert.Equal(t, "ssh://git@domain:123/user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo")) 211 212 // test IPv6 SSH_DOMAIN 213 setting.Repository.UseCompatSSHURI = false 214 setting.SSH.Domain = "::1" 215 setting.SSH.Port = 22 216 assert.Equal(t, "git@[::1]:user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo")) 217 setting.SSH.Port = 123 218 assert.Equal(t, "ssh://git@[::1]:123/user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo")) 219 }