github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/services/scm_provider/gitea_test.go (about) 1 package scm_provider 2 3 import ( 4 "io" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/argoproj/argo-cd/v3/applicationset/services/scm_provider/testdata" 13 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 14 ) 15 16 func giteaMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) { 17 t.Helper() 18 return func(w http.ResponseWriter, r *http.Request) { 19 w.Header().Set("Content-Type", "application/json") 20 switch r.RequestURI { 21 case "/api/v1/version": 22 _, err := io.WriteString(w, `{"version":"1.17.0+dev-452-g1f0541780"}`) 23 if err != nil { 24 t.Fail() 25 } 26 case "/api/v1/orgs/test-argocd/repos?limit=0&page=1": 27 _, err := io.WriteString(w, `[{ 28 "id": 21618, 29 "owner": { 30 "id": 31480, 31 "login": "test-argocd", 32 "full_name": "", 33 "email": "", 34 "avatar_url": "https://gitea.com/avatars/22d1b1d3f61abf95951c4a958731d848", 35 "language": "", 36 "is_admin": false, 37 "last_login": "0001-01-01T00:00:00Z", 38 "created": "2022-04-06T02:28:06+08:00", 39 "restricted": false, 40 "active": false, 41 "prohibit_login": false, 42 "location": "", 43 "website": "", 44 "description": "", 45 "visibility": "public", 46 "followers_count": 0, 47 "following_count": 0, 48 "starred_repos_count": 0, 49 "username": "test-argocd" 50 }, 51 "name": "pr-test", 52 "full_name": "test-argocd/pr-test", 53 "description": "", 54 "empty": false, 55 "private": false, 56 "fork": false, 57 "template": false, 58 "parent": null, 59 "mirror": false, 60 "size": 28, 61 "language": "", 62 "languages_url": "https://gitea.com/api/v1/repos/test-argocd/pr-test/languages", 63 "html_url": "https://gitea.com/test-argocd/pr-test", 64 "ssh_url": "git@gitea.com:test-argocd/pr-test.git", 65 "clone_url": "https://gitea.com/test-argocd/pr-test.git", 66 "original_url": "", 67 "website": "", 68 "stars_count": 0, 69 "forks_count": 0, 70 "watchers_count": 1, 71 "open_issues_count": 0, 72 "open_pr_counter": 1, 73 "release_counter": 0, 74 "default_branch": "main", 75 "archived": false, 76 "created_at": "2022-04-06T02:32:09+08:00", 77 "updated_at": "2022-04-06T02:33:12+08:00", 78 "permissions": { 79 "admin": false, 80 "push": false, 81 "pull": true 82 }, 83 "has_issues": true, 84 "internal_tracker": { 85 "enable_time_tracker": true, 86 "allow_only_contributors_to_track_time": true, 87 "enable_issue_dependencies": true 88 }, 89 "has_wiki": true, 90 "has_pull_requests": true, 91 "has_projects": true, 92 "ignore_whitespace_conflicts": false, 93 "allow_merge_commits": true, 94 "allow_rebase": true, 95 "allow_rebase_explicit": true, 96 "allow_squash_merge": true, 97 "default_merge_style": "merge", 98 "avatar_url": "", 99 "internal": false, 100 "mirror_interval": "", 101 "mirror_updated": "0001-01-01T00:00:00Z", 102 "repo_transfer": null 103 }]`) 104 if err != nil { 105 t.Fail() 106 } 107 case "/api/v1/repos/test-argocd/pr-test/branches/main": 108 _, err := io.WriteString(w, `{ 109 "name": "main", 110 "commit": { 111 "id": "72687815ccba81ef014a96201cc2e846a68789d8", 112 "message": "initial commit\n", 113 "url": "https://gitea.com/test-argocd/pr-test/commit/72687815ccba81ef014a96201cc2e846a68789d8", 114 "author": { 115 "name": "Dan Molik", 116 "email": "dan@danmolik.com", 117 "username": "graytshirt" 118 }, 119 "committer": { 120 "name": "Dan Molik", 121 "email": "dan@danmolik.com", 122 "username": "graytshirt" 123 }, 124 "verification": { 125 "verified": false, 126 "reason": "gpg.error.no_gpg_keys_found", 127 "signature": "-----BEGIN PGP SIGNATURE-----\n\niQEzBAABCAAdFiEEXYAkwEBRpXzXgHFWlgCr7m50zBMFAmJMiqUACgkQlgCr7m50\nzBPSmQgAiVVEIxC42tuks4iGFNURrtYvypZAEIc+hJgt2kBpmdCrAphYPeAj+Wtr\n9KT7dDscCZIba2wx39HEXO2S7wNCXESvAzrA8rdfbXjR4L2miZ1urfBkEoqK5i/F\noblWGuAyjurX4KPa2ARROd0H4AXxt6gNAXaFPgZO+xXCyNKZfad/lkEP1AiPRknD\nvTTMbEkIzFHK9iVwZ9DORGpfF1wnLzxWmMfhYatZnBgFNnoeJNtFhCJo05rHBgqc\nqVZWXt1iF7nysBoXSzyx1ZAsmBr/Qerkuj0nonh0aPVa6NKJsdmeJyPX4zXXoi6E\ne/jpxX2UQJkpFezg3IjUpvE5FvIiYg==\n=3Af2\n-----END PGP SIGNATURE-----\n", 128 "signer": null, 129 "payload": "tree 64d47c7fc6e31dcf00654223ec4ab749dd0a464e\nauthor Dan Molik \u003cdan@danmolik.com\u003e 1649183391 -0400\ncommitter Dan Molik \u003cdan@danmolik.com\u003e 1649183391 -0400\n\ninitial commit\n" 130 }, 131 "timestamp": "2022-04-05T14:29:51-04:00", 132 "added": null, 133 "removed": null, 134 "modified": null 135 }, 136 "protected": false, 137 "required_approvals": 0, 138 "enable_status_check": false, 139 "status_check_contexts": [], 140 "user_can_push": false, 141 "user_can_merge": false, 142 "effective_branch_protection_name": "" 143 }`) 144 if err != nil { 145 t.Fail() 146 } 147 case "/api/v1/repos/test-argocd/pr-test/branches?limit=0&page=1": 148 _, err := io.WriteString(w, `[{ 149 "name": "main", 150 "commit": { 151 "id": "72687815ccba81ef014a96201cc2e846a68789d8", 152 "message": "initial commit\n", 153 "url": "https://gitea.com/test-argocd/pr-test/commit/72687815ccba81ef014a96201cc2e846a68789d8", 154 "author": { 155 "name": "Dan Molik", 156 "email": "dan@danmolik.com", 157 "username": "graytshirt" 158 }, 159 "committer": { 160 "name": "Dan Molik", 161 "email": "dan@danmolik.com", 162 "username": "graytshirt" 163 }, 164 "verification": { 165 "verified": false, 166 "reason": "gpg.error.no_gpg_keys_found", 167 "signature": "-----BEGIN PGP SIGNATURE-----\n\niQEzBAABCAAdFiEEXYAkwEBRpXzXgHFWlgCr7m50zBMFAmJMiqUACgkQlgCr7m50\nzBPSmQgAiVVEIxC42tuks4iGFNURrtYvypZAEIc+hJgt2kBpmdCrAphYPeAj+Wtr\n9KT7dDscCZIba2wx39HEXO2S7wNCXESvAzrA8rdfbXjR4L2miZ1urfBkEoqK5i/F\noblWGuAyjurX4KPa2ARROd0H4AXxt6gNAXaFPgZO+xXCyNKZfad/lkEP1AiPRknD\nvTTMbEkIzFHK9iVwZ9DORGpfF1wnLzxWmMfhYatZnBgFNnoeJNtFhCJo05rHBgqc\nqVZWXt1iF7nysBoXSzyx1ZAsmBr/Qerkuj0nonh0aPVa6NKJsdmeJyPX4zXXoi6E\ne/jpxX2UQJkpFezg3IjUpvE5FvIiYg==\n=3Af2\n-----END PGP SIGNATURE-----\n", 168 "signer": null, 169 "payload": "tree 64d47c7fc6e31dcf00654223ec4ab749dd0a464e\nauthor Dan Molik \u003cdan@danmolik.com\u003e 1649183391 -0400\ncommitter Dan Molik \u003cdan@danmolik.com\u003e 1649183391 -0400\n\ninitial commit\n" 170 }, 171 "timestamp": "2022-04-05T14:29:51-04:00", 172 "added": null, 173 "removed": null, 174 "modified": null 175 }, 176 "protected": false, 177 "required_approvals": 0, 178 "enable_status_check": false, 179 "status_check_contexts": [], 180 "user_can_push": false, 181 "user_can_merge": false, 182 "effective_branch_protection_name": "" 183 }, { 184 "name": "test", 185 "commit": { 186 "id": "7bbaf62d92ddfafd9cc8b340c619abaec32bc09f", 187 "message": "add an empty file\n", 188 "url": "https://gitea.com/test-argocd/pr-test/commit/7bbaf62d92ddfafd9cc8b340c619abaec32bc09f", 189 "author": { 190 "name": "Dan Molik", 191 "email": "dan@danmolik.com", 192 "username": "graytshirt" 193 }, 194 "committer": { 195 "name": "Dan Molik", 196 "email": "dan@danmolik.com", 197 "username": "graytshirt" 198 }, 199 "verification": { 200 "verified": false, 201 "reason": "gpg.error.no_gpg_keys_found", 202 "signature": "-----BEGIN PGP SIGNATURE-----\n\niQEzBAABCAAdFiEEXYAkwEBRpXzXgHFWlgCr7m50zBMFAmJMiugACgkQlgCr7m50\nzBN+7wgAkCHD3KfX3Ffkqv2qPwqgHNYM1bA6Hmffzhv0YeD9jWCI3tp0JulP4iFZ\ncQ7jqx9xP9tCQMSFCaijLRHaE6Js1xrVtf0OKRkbpdlvkyrIM3sQhqyQgAsISrDG\nLzSqeoQQjglzeWESYh2Tjn1CgqQNKjI6LLepSwvF1pIeV4pJpJobaEbIfTgStdzM\nWEk8o0I+EZaYqK0C0vU9N0LK/LR/jnlaHsb4OUjvk+S7lRjZwBkrsg7P/QsqtCVd\nw5nkxDiCx1J58zKMnQ7ZinJEK9A5WYdnMYc6aBn7ARgZrblXPPBkkKUhEv3ZSPeW\nKv9i4GQy838xkVSTFkHNj1+a5o6zEA==\n=JiFw\n-----END PGP SIGNATURE-----\n", 203 "signer": null, 204 "payload": "tree cdddf3e1d6a8a7e6899a044d0e1bc73bf798e2f5\nparent 72687815ccba81ef014a96201cc2e846a68789d8\nauthor Dan Molik \u003cdan@danmolik.com\u003e 1649183458 -0400\ncommitter Dan Molik \u003cdan@danmolik.com\u003e 1649183458 -0400\n\nadd an empty file\n" 205 }, 206 "timestamp": "2022-04-05T14:30:58-04:00", 207 "added": null, 208 "removed": null, 209 "modified": null 210 }, 211 "protected": false, 212 "required_approvals": 0, 213 "enable_status_check": false, 214 "status_check_contexts": [], 215 "user_can_push": false, 216 "user_can_merge": false, 217 "effective_branch_protection_name": "" 218 }]`) 219 if err != nil { 220 t.Fail() 221 } 222 case "/api/v1/repos/gitea/go-sdk/contents/README.md?ref=master": 223 _, err := io.WriteString(w, `{ 224 "name": "README.md", 225 "path": "README.md", 226 "sha": "3605625ef3f80dc092167b54e3f55eb0663d729f", 227 "last_commit_sha": "6b6fdd91ce769bb4641084e15f76554fb841bf27", 228 "type": "file", 229 "size": 1673, 230 "encoding": "base64", 231 "content": "IyBHaXRlYSBTREsgZm9yIEdvCgpbIVtMaWNlbnNlOiBNSVRdKGh0dHBzOi8vaW1nLnNoaWVsZHMuaW8vYmFkZ2UvTGljZW5zZS1NSVQtYmx1ZS5zdmcpXShodHRwczovL29wZW5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/go-sdk/contents/README.md?ref=master", 234 "html_url": "https://gitea.com/gitea/go-sdk/src/branch/master/README.md", 235 "git_url": "https://gitea.com/api/v1/repos/gitea/go-sdk/git/blobs/3605625ef3f80dc092167b54e3f55eb0663d729f", 236 "download_url": "https://gitea.com/gitea/go-sdk/raw/branch/master/README.md", 237 "submodule_git_url": null, 238 "_links": { 239 "self": "https://gitea.com/api/v1/repos/gitea/go-sdk/contents/README.md?ref=master", 240 "git": "https://gitea.com/api/v1/repos/gitea/go-sdk/git/blobs/3605625ef3f80dc092167b54e3f55eb0663d729f", 241 "html": "https://gitea.com/gitea/go-sdk/src/branch/master/README.md" 242 } 243 } 244 `) 245 require.NoError(t, err) 246 case "/api/v1/repos/gitea/go-sdk/contents/gitea?ref=master": 247 _, err := io.WriteString(w, testdata.ReposGiteaGoSdkContentsGiteaResponse) 248 require.NoError(t, err) 249 case "/api/v1/repos/gitea/go-sdk/contents/notathing?ref=master": 250 w.WriteHeader(http.StatusNotFound) 251 _, err := io.WriteString(w, `{"errors":["object does not exist [id: , rel_path: notathing]"],"message":"GetContentsOrList","url":"https://gitea.com/api/swagger"}`) 252 require.NoError(t, err) 253 default: 254 _, err := io.WriteString(w, `[]`) 255 if err != nil { 256 t.Fail() 257 } 258 } 259 } 260 } 261 262 func TestGiteaListRepos(t *testing.T) { 263 cases := []struct { 264 name, proto, url string 265 hasError, allBranches, includeSubgroups bool 266 branches []string 267 filters []v1alpha1.SCMProviderGeneratorFilter 268 }{ 269 { 270 name: "blank protocol", 271 allBranches: false, 272 url: "git@gitea.com:test-argocd/pr-test.git", 273 branches: []string{"main"}, 274 }, 275 { 276 name: "ssh protocol", 277 allBranches: false, 278 proto: "ssh", 279 url: "git@gitea.com:test-argocd/pr-test.git", 280 }, 281 { 282 name: "https protocol", 283 allBranches: false, 284 proto: "https", 285 url: "https://gitea.com/test-argocd/pr-test", 286 }, 287 { 288 name: "other protocol", 289 allBranches: false, 290 proto: "other", 291 hasError: true, 292 }, 293 { 294 name: "all branches", 295 allBranches: true, 296 url: "git@gitea.com:test-argocd/pr-test.git", 297 branches: []string{"main"}, 298 }, 299 } 300 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 301 giteaMockHandler(t)(w, r) 302 })) 303 defer ts.Close() 304 for _, c := range cases { 305 t.Run(c.name, func(t *testing.T) { 306 provider, _ := NewGiteaProvider("test-argocd", "", ts.URL, c.allBranches, false) 307 rawRepos, err := ListRepos(t.Context(), provider, c.filters, c.proto) 308 if c.hasError { 309 require.Error(t, err) 310 } else { 311 require.NoError(t, err) 312 // Just check that this one project shows up. Not a great test but better thing nothing? 313 repos := []*Repository{} 314 branches := []string{} 315 for _, r := range rawRepos { 316 if r.Repository == "pr-test" { 317 repos = append(repos, r) 318 branches = append(branches, r.Branch) 319 } 320 } 321 assert.NotEmpty(t, repos) 322 assert.Equal(t, c.url, repos[0].URL) 323 for _, b := range c.branches { 324 assert.Contains(t, branches, b) 325 } 326 } 327 }) 328 } 329 } 330 331 func TestGiteaHasPath(t *testing.T) { 332 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 333 giteaMockHandler(t)(w, r) 334 })) 335 defer ts.Close() 336 host, _ := NewGiteaProvider("gitea", "", ts.URL, false, false) 337 repo := &Repository{ 338 Organization: "gitea", 339 Repository: "go-sdk", 340 Branch: "master", 341 } 342 343 t.Run("file exists", func(t *testing.T) { 344 ok, err := host.RepoHasPath(t.Context(), repo, "README.md") 345 require.NoError(t, err) 346 assert.True(t, ok) 347 }) 348 349 t.Run("directory exists", func(t *testing.T) { 350 ok, err := host.RepoHasPath(t.Context(), repo, "gitea") 351 require.NoError(t, err) 352 assert.True(t, ok) 353 }) 354 355 t.Run("does not exists", func(t *testing.T) { 356 ok, err := host.RepoHasPath(t.Context(), repo, "notathing") 357 require.NoError(t, err) 358 assert.False(t, ok) 359 }) 360 }