| 1 | package lfsapi |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "regexp" |
| 6 | "runtime" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/creds" |
| 10 | "github.com/git-lfs/git-lfs/v3/lfshttp" |
| 11 | "github.com/git-lfs/git-lfs/v3/ssh" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | ) |
| 14 | |
| 15 | func TestEndpointDefaultsToOrigin(t *testing.T) { |
| 16 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 17 | "remote.origin.lfsurl": "abc", |
| 18 | })) |
| 19 | |
| 20 | e := finder.Endpoint("download", "") |
| 21 | assert.Equal(t, "abc", e.Url) |
| 22 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 23 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 24 | } |
| 25 | |
| 26 | func TestEndpointOverridesOrigin(t *testing.T) { |
| 27 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 28 | "lfs.url": "abc", |
| 29 | "remote.origin.lfsurl": "def", |
| 30 | })) |
| 31 | |
| 32 | e := finder.Endpoint("download", "") |
| 33 | assert.Equal(t, "abc", e.Url) |
| 34 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 35 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 36 | } |
| 37 | |
| 38 | func TestEndpointNoOverrideDefaultRemote(t *testing.T) { |
| 39 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 40 | "remote.origin.lfsurl": "abc", |
| 41 | "remote.other.lfsurl": "def", |
| 42 | })) |
| 43 | |
| 44 | e := finder.Endpoint("download", "") |
| 45 | assert.Equal(t, "abc", e.Url) |
| 46 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 47 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 48 | } |
| 49 | |
| 50 | func TestEndpointUseAlternateRemote(t *testing.T) { |
| 51 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 52 | "remote.origin.lfsurl": "abc", |
| 53 | "remote.other.lfsurl": "def", |
| 54 | })) |
| 55 | |
| 56 | e := finder.Endpoint("download", "other") |
| 57 | assert.Equal(t, "def", e.Url) |
| 58 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 59 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 60 | } |
| 61 | |
| 62 | func TestEndpointAddsLfsSuffix(t *testing.T) { |
| 63 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 64 | "remote.origin.url": "https://example.com/foo/bar", |
| 65 | })) |
| 66 | |
| 67 | e := finder.Endpoint("download", "") |
| 68 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 69 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 70 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 71 | } |
| 72 | |
| 73 | func TestBareEndpointAddsLfsSuffix(t *testing.T) { |
| 74 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 75 | "remote.origin.url": "https://example.com/foo/bar.git", |
| 76 | })) |
| 77 | |
| 78 | e := finder.Endpoint("download", "") |
| 79 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 80 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 81 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 82 | } |
| 83 | |
| 84 | func TestEndpointSeparateClonePushUrl(t *testing.T) { |
| 85 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 86 | "remote.origin.url": "https://example.com/foo/bar.git", |
| 87 | "remote.origin.pushurl": "https://readwrite.com/foo/bar.git", |
| 88 | })) |
| 89 | |
| 90 | e := finder.Endpoint("download", "") |
| 91 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 92 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 93 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 94 | |
| 95 | e = finder.Endpoint("upload", "") |
| 96 | assert.Equal(t, "https://readwrite.com/foo/bar.git/info/lfs", e.Url) |
| 97 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 98 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 99 | } |
| 100 | |
| 101 | func TestEndpointOverriddenSeparateClonePushLfsUrl(t *testing.T) { |
| 102 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 103 | "remote.origin.url": "https://example.com/foo/bar.git", |
| 104 | "remote.origin.pushurl": "https://readwrite.com/foo/bar.git", |
| 105 | "remote.origin.lfsurl": "https://examplelfs.com/foo/bar", |
| 106 | "remote.origin.lfspushurl": "https://readwritelfs.com/foo/bar", |
| 107 | })) |
| 108 | |
| 109 | e := finder.Endpoint("download", "") |
| 110 | assert.Equal(t, "https://examplelfs.com/foo/bar", e.Url) |
| 111 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 112 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 113 | |
| 114 | e = finder.Endpoint("upload", "") |
| 115 | assert.Equal(t, "https://readwritelfs.com/foo/bar", e.Url) |
| 116 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 117 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 118 | } |
| 119 | |
| 120 | func TestEndpointGlobalSeparateLfsPush(t *testing.T) { |
| 121 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 122 | "lfs.url": "https://readonly.com/foo/bar", |
| 123 | "lfs.pushurl": "https://write.com/foo/bar", |
| 124 | })) |
| 125 | |
| 126 | e := finder.Endpoint("download", "") |
| 127 | assert.Equal(t, "https://readonly.com/foo/bar", e.Url) |
| 128 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 129 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 130 | |
| 131 | e = finder.Endpoint("upload", "") |
| 132 | assert.Equal(t, "https://write.com/foo/bar", e.Url) |
| 133 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 134 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 135 | } |
| 136 | |
| 137 | func TestSSHEndpointOverridden(t *testing.T) { |
| 138 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 139 | "remote.origin.url": "[email protected]:foo/bar", |
| 140 | "remote.origin.lfsurl": "lfs", |
| 141 | })) |
| 142 | |
| 143 | e := finder.Endpoint("download", "") |
| 144 | assert.Equal(t, "lfs", e.Url) |
| 145 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 146 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 147 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 148 | } |
| 149 | |
| 150 | func TestSSHEndpointAddsLfsSuffix(t *testing.T) { |
| 151 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 152 | "remote.origin.url": "ssh://[email protected]/foo/bar", |
| 153 | })) |
| 154 | |
| 155 | e := finder.Endpoint("download", "") |
| 156 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 157 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 158 | assert.Equal(t, "/foo/bar", e.SSHMetadata.Path) |
| 159 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 160 | } |
| 161 | |
| 162 | func TestSSHCustomPortEndpointAddsLfsSuffix(t *testing.T) { |
| 163 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 164 | "remote.origin.url": "ssh://[email protected]:9000/foo/bar", |
| 165 | })) |
| 166 | |
| 167 | e := finder.Endpoint("download", "") |
| 168 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 169 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 170 | assert.Equal(t, "/foo/bar", e.SSHMetadata.Path) |
| 171 | assert.Equal(t, "9000", e.SSHMetadata.Port) |
| 172 | } |
| 173 | |
| 174 | func TestGitSSHEndpointAddsLfsSuffix(t *testing.T) { |
| 175 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 176 | "remote.origin.url": "git+ssh://[email protected]/foo/bar", |
| 177 | })) |
| 178 | |
| 179 | e := finder.Endpoint("download", "") |
| 180 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 181 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 182 | assert.Equal(t, "/foo/bar", e.SSHMetadata.Path) |
| 183 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 184 | } |
| 185 | |
| 186 | func TestGitSSHCustomPortEndpointAddsLfsSuffix(t *testing.T) { |
| 187 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 188 | "remote.origin.url": "git+ssh://[email protected]:9000/foo/bar", |
| 189 | })) |
| 190 | |
| 191 | e := finder.Endpoint("download", "") |
| 192 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 193 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 194 | assert.Equal(t, "/foo/bar", e.SSHMetadata.Path) |
| 195 | assert.Equal(t, "9000", e.SSHMetadata.Port) |
| 196 | } |
| 197 | |
| 198 | func TestSSHGitEndpointAddsLfsSuffix(t *testing.T) { |
| 199 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 200 | "remote.origin.url": "ssh+git://[email protected]/foo/bar", |
| 201 | })) |
| 202 | |
| 203 | e := finder.Endpoint("download", "") |
| 204 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 205 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 206 | assert.Equal(t, "/foo/bar", e.SSHMetadata.Path) |
| 207 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 208 | } |
| 209 | |
| 210 | func TestSSHGitCustomPortEndpointAddsLfsSuffix(t *testing.T) { |
| 211 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 212 | "remote.origin.url": "ssh+git://[email protected]:9000/foo/bar", |
| 213 | })) |
| 214 | |
| 215 | e := finder.Endpoint("download", "") |
| 216 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 217 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 218 | assert.Equal(t, "/foo/bar", e.SSHMetadata.Path) |
| 219 | assert.Equal(t, "9000", e.SSHMetadata.Port) |
| 220 | } |
| 221 | |
| 222 | func TestBareSSHEndpointAddsLfsSuffix(t *testing.T) { |
| 223 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 224 | "remote.origin.url": "[email protected]:foo/bar.git", |
| 225 | })) |
| 226 | |
| 227 | e := finder.Endpoint("download", "") |
| 228 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 229 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 230 | assert.Equal(t, "foo/bar.git", e.SSHMetadata.Path) |
| 231 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 232 | } |
| 233 | |
| 234 | func TestBareSSSHEndpointWithCustomPortInBrackets(t *testing.T) { |
| 235 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 236 | "remote.origin.url": "[[email protected]:2222]:foo/bar.git", |
| 237 | })) |
| 238 | |
| 239 | e := finder.Endpoint("download", "") |
| 240 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 241 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 242 | assert.Equal(t, "foo/bar.git", e.SSHMetadata.Path) |
| 243 | assert.Equal(t, "2222", e.SSHMetadata.Port) |
| 244 | } |
| 245 | |
| 246 | func TestSSHEndpointFromGlobalLfsUrl(t *testing.T) { |
| 247 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 248 | "lfs.url": "[email protected]:foo/bar.git", |
| 249 | })) |
| 250 | |
| 251 | e := finder.Endpoint("download", "") |
| 252 | assert.Equal(t, "https://example.com/foo/bar.git", e.Url) |
| 253 | assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost) |
| 254 | assert.Equal(t, "foo/bar.git", e.SSHMetadata.Path) |
| 255 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 256 | } |
| 257 | |
| 258 | func TestHTTPEndpointAddsLfsSuffix(t *testing.T) { |
| 259 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 260 | "remote.origin.url": "http://example.com/foo/bar", |
| 261 | })) |
| 262 | |
| 263 | e := finder.Endpoint("download", "") |
| 264 | assert.Equal(t, "http://example.com/foo/bar.git/info/lfs", e.Url) |
| 265 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 266 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 267 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 268 | } |
| 269 | |
| 270 | func TestBareHTTPEndpointAddsLfsSuffix(t *testing.T) { |
| 271 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 272 | "remote.origin.url": "http://example.com/foo/bar.git", |
| 273 | })) |
| 274 | |
| 275 | e := finder.Endpoint("download", "") |
| 276 | assert.Equal(t, "http://example.com/foo/bar.git/info/lfs", e.Url) |
| 277 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 278 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 279 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 280 | } |
| 281 | |
| 282 | func TestGitEndpointAddsLfsSuffix(t *testing.T) { |
| 283 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 284 | "remote.origin.url": "git://example.com/foo/bar", |
| 285 | })) |
| 286 | |
| 287 | e := finder.Endpoint("download", "") |
| 288 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 289 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 290 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 291 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 292 | } |
| 293 | |
| 294 | func TestGitEndpointAddsLfsSuffixWithCustomProtocol(t *testing.T) { |
| 295 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 296 | "remote.origin.url": "git://example.com/foo/bar", |
| 297 | "lfs.gitprotocol": "http", |
| 298 | })) |
| 299 | |
| 300 | e := finder.Endpoint("download", "") |
| 301 | assert.Equal(t, "http://example.com/foo/bar.git/info/lfs", e.Url) |
| 302 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 303 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 304 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 305 | } |
| 306 | |
| 307 | func TestBareGitEndpointAddsLfsSuffix(t *testing.T) { |
| 308 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 309 | "remote.origin.url": "git://example.com/foo/bar.git", |
| 310 | })) |
| 311 | |
| 312 | e := finder.Endpoint("download", "") |
| 313 | assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url) |
| 314 | assert.Equal(t, "", e.SSHMetadata.UserAndHost) |
| 315 | assert.Equal(t, "", e.SSHMetadata.Path) |
| 316 | assert.Equal(t, "", e.SSHMetadata.Port) |
| 317 | } |
| 318 | |
| 319 | func TestLocalPathEndpointAddsDotGitForWorkingRepo(t *testing.T) { |
| 320 | // Windows will add a drive letter to the paths below since we |
| 321 | // canonicalize them. |
| 322 | if runtime.GOOS == "windows" { |
| 323 | return |
| 324 | } |
| 325 | |
| 326 | path := t.TempDir() + "/local/path" |
| 327 | err := os.MkdirAll(path+"/.git", 0755) |
| 328 | assert.Nil(t, err) |
| 329 | |
| 330 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 331 | "remote.origin.url": path, |
| 332 | })) |
| 333 | e := finder.Endpoint("download", "") |
| 334 | assert.Equal(t, "file://"+path+"/.git", e.Url) |
| 335 | |
| 336 | os.RemoveAll(path) |
| 337 | } |
| 338 | |
| 339 | func TestLocalPathEndpointPreservesDotGitForWorkingRepo(t *testing.T) { |
| 340 | // Windows will add a drive letter to the paths below since we |
| 341 | // canonicalize them. |
| 342 | if runtime.GOOS == "windows" { |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | path := t.TempDir() + "/local/path/.git" |
| 347 | err := os.MkdirAll(path, 0755) |
| 348 | assert.Nil(t, err) |
| 349 | |
| 350 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 351 | "remote.origin.url": path, |
| 352 | })) |
| 353 | e := finder.Endpoint("download", "") |
| 354 | assert.Equal(t, "file://"+path, e.Url) |
| 355 | |
| 356 | os.RemoveAll(path) |
| 357 | } |
| 358 | |
| 359 | func TestLocalPathEndpointPreservesNoDotGitForBareRepo(t *testing.T) { |
| 360 | // Windows will add a drive letter to the paths below since we |
| 361 | // canonicalize them. |
| 362 | if runtime.GOOS == "windows" { |
| 363 | return |
| 364 | } |
| 365 | |
| 366 | path := t.TempDir() + "/local/path" |
| 367 | err := os.MkdirAll(path, 0755) |
| 368 | assert.Nil(t, err) |
| 369 | |
| 370 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 371 | "remote.origin.url": path, |
| 372 | })) |
| 373 | e := finder.Endpoint("download", "") |
| 374 | assert.Equal(t, "file://"+path, e.Url) |
| 375 | |
| 376 | os.RemoveAll(path) |
| 377 | } |
| 378 | |
| 379 | func TestLocalPathEndpointRemovesDotGitForBareRepo(t *testing.T) { |
| 380 | // Windows will add a drive letter to the paths below since we |
| 381 | // canonicalize them. |
| 382 | if runtime.GOOS == "windows" { |
| 383 | return |
| 384 | } |
| 385 | |
| 386 | path := t.TempDir() + "/local/path" |
| 387 | err := os.MkdirAll(path, 0755) |
| 388 | assert.Nil(t, err) |
| 389 | |
| 390 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 391 | "remote.origin.url": path + "/.git", |
| 392 | })) |
| 393 | e := finder.Endpoint("download", "") |
| 394 | assert.Equal(t, "file://"+path, e.Url) |
| 395 | |
| 396 | os.RemoveAll(path) |
| 397 | } |
| 398 | |
| 399 | func TestAccessConfig(t *testing.T) { |
| 400 | type accessTest struct { |
| 401 | AccessMode string |
| 402 | PrivateAccess bool |
| 403 | } |
| 404 | |
| 405 | tests := map[string]accessTest{ |
| 406 | "": {"none", false}, |
| 407 | "basic": {"basic", true}, |
| 408 | "BASIC": {"basic", true}, |
| 409 | "private": {"basic", true}, |
| 410 | "PRIVATE": {"basic", true}, |
| 411 | "invalidauth": {"invalidauth", true}, |
| 412 | } |
| 413 | |
| 414 | for value, expected := range tests { |
| 415 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 416 | "lfs.url": "http://example.com", |
| 417 | "lfs.http://example.com.access": value, |
| 418 | "lfs.https://example.com.access": "bad", |
| 419 | })) |
| 420 | |
| 421 | dl := finder.Endpoint("upload", "") |
| 422 | ul := finder.Endpoint("download", "") |
| 423 | |
| 424 | if access := finder.AccessFor(dl.Url); access.Mode() != creds.AccessMode(expected.AccessMode) { |
| 425 | t.Errorf("Expected creds.AccessMode() with value %q to be %v, got %v", value, expected.AccessMode, access) |
| 426 | } |
| 427 | if access := finder.AccessFor(ul.Url); access.Mode() != creds.AccessMode(expected.AccessMode) { |
| 428 | t.Errorf("Expected creds.AccessMode() with value %q to be %v, got %v", value, expected.AccessMode, access) |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // Test again but with separate push url |
| 433 | for value, expected := range tests { |
| 434 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 435 | "lfs.url": "http://example.com", |
| 436 | "lfs.pushurl": "http://examplepush.com", |
| 437 | "lfs.http://example.com.access": value, |
| 438 | "lfs.http://examplepush.com.access": value, |
| 439 | "lfs.https://example.com.access": "bad", |
| 440 | })) |
| 441 | |
| 442 | dl := finder.Endpoint("upload", "") |
| 443 | ul := finder.Endpoint("download", "") |
| 444 | |
| 445 | if access := finder.AccessFor(dl.Url); access.Mode() != creds.AccessMode(expected.AccessMode) { |
| 446 | t.Errorf("Expected creds.AccessMode() with value %q to be %v, got %v", value, expected.AccessMode, access) |
| 447 | } |
| 448 | if access := finder.AccessFor(ul.Url); access.Mode() != creds.AccessMode(expected.AccessMode) { |
| 449 | t.Errorf("Expected creds.AccessMode() with value %q to be %v, got %v", value, expected.AccessMode, access) |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | func TestAccessAbsentConfig(t *testing.T) { |
| 455 | finder := NewEndpointFinder(nil) |
| 456 | |
| 457 | downloadAccess := finder.AccessFor(finder.Endpoint("download", "").Url) |
| 458 | assert.Equal(t, creds.NoneAccess, downloadAccess.Mode()) |
| 459 | |
| 460 | uploadAccess := finder.AccessFor(finder.Endpoint("upload", "").Url) |
| 461 | assert.Equal(t, creds.NoneAccess, uploadAccess.Mode()) |
| 462 | } |
| 463 | |
| 464 | func TestSetAccess(t *testing.T) { |
| 465 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{})) |
| 466 | url := "http://example.com" |
| 467 | access := finder.AccessFor(url) |
| 468 | |
| 469 | assert.Equal(t, creds.NoneAccess, access.Mode()) |
| 470 | assert.Equal(t, url, access.URL()) |
| 471 | |
| 472 | finder.SetAccess(access.Upgrade(creds.NegotiateAccess)) |
| 473 | |
| 474 | newAccess := finder.AccessFor(url) |
| 475 | assert.Equal(t, creds.NegotiateAccess, newAccess.Mode()) |
| 476 | assert.Equal(t, url, newAccess.URL()) |
| 477 | } |
| 478 | |
| 479 | func TestChangeAccess(t *testing.T) { |
| 480 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 481 | "lfs.http://example.com.access": "basic", |
| 482 | })) |
| 483 | |
| 484 | url := "http://example.com" |
| 485 | access := finder.AccessFor(url) |
| 486 | assert.Equal(t, creds.BasicAccess, access.Mode()) |
| 487 | assert.Equal(t, url, access.URL()) |
| 488 | |
| 489 | finder.SetAccess(access.Upgrade(creds.NegotiateAccess)) |
| 490 | |
| 491 | newAccess := finder.AccessFor(url) |
| 492 | assert.Equal(t, creds.NegotiateAccess, newAccess.Mode()) |
| 493 | assert.Equal(t, url, newAccess.URL()) |
| 494 | } |
| 495 | |
| 496 | func TestDeleteAccessWithNone(t *testing.T) { |
| 497 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 498 | "lfs.http://example.com.access": "basic", |
| 499 | })) |
| 500 | |
| 501 | url := "http://example.com" |
| 502 | |
| 503 | access := finder.AccessFor(url) |
| 504 | assert.Equal(t, creds.BasicAccess, access.Mode()) |
| 505 | assert.Equal(t, url, access.URL()) |
| 506 | |
| 507 | finder.SetAccess(access.Upgrade(creds.NoneAccess)) |
| 508 | |
| 509 | newAccess := finder.AccessFor(url) |
| 510 | assert.Equal(t, creds.NoneAccess, newAccess.Mode()) |
| 511 | assert.Equal(t, url, newAccess.URL()) |
| 512 | } |
| 513 | |
| 514 | func TestDeleteAccessWithEmptyString(t *testing.T) { |
| 515 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 516 | "lfs.http://example.com.access": "basic", |
| 517 | })) |
| 518 | |
| 519 | url := "http://example.com" |
| 520 | |
| 521 | access := finder.AccessFor(url) |
| 522 | assert.Equal(t, creds.BasicAccess, access.Mode()) |
| 523 | assert.Equal(t, url, access.URL()) |
| 524 | |
| 525 | finder.SetAccess(access.Upgrade(creds.AccessMode(""))) |
| 526 | |
| 527 | newAccess := finder.AccessFor(url) |
| 528 | assert.Equal(t, creds.NoneAccess, newAccess.Mode()) |
| 529 | assert.Equal(t, url, newAccess.URL()) |
| 530 | } |
| 531 | |
| 532 | type EndpointParsingTestCase struct { |
| 533 | Given string |
| 534 | Expected lfshttp.Endpoint |
| 535 | } |
| 536 | |
| 537 | func (c *EndpointParsingTestCase) Assert(t *testing.T) { |
| 538 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 539 | "url.https://github.com/.insteadof": "gh:", |
| 540 | })) |
| 541 | actual := finder.NewEndpoint("upload", c.Given) |
| 542 | assert.Equal(t, c.Expected, actual, "lfsapi: expected endpoint for %q to be %#v (was %#v)", c.Given, c.Expected, actual) |
| 543 | } |
| 544 | |
| 545 | func TestEndpointParsing(t *testing.T) { |
| 546 | // Note that many of these tests will produce silly or completely broken |
| 547 | // values for the Url, and that's okay: they work nevertheless. |
| 548 | for desc, c := range map[string]EndpointParsingTestCase{ |
| 549 | "simple bare ssh": { |
| 550 | "[email protected]:git-lfs/git-lfs.git", |
| 551 | lfshttp.Endpoint{ |
| 552 | Url: "https://github.com/git-lfs/git-lfs.git", |
| 553 | SSHMetadata: ssh.SSHMetadata{ |
| 554 | UserAndHost: "[email protected]", |
| 555 | Path: "git-lfs/git-lfs.git", |
| 556 | Port: "", |
| 557 | }, |
| 558 | Operation: "", |
| 559 | OriginalUrl: "ssh://[email protected]/git-lfs/git-lfs.git", |
| 560 | }, |
| 561 | }, |
| 562 | "port bare ssh": { |
| 563 | "[[email protected]:443]:git-lfs/git-lfs.git", |
| 564 | lfshttp.Endpoint{ |
| 565 | Url: "https://lfshttp.github.com/git-lfs/git-lfs.git", |
| 566 | SSHMetadata: ssh.SSHMetadata{ |
| 567 | UserAndHost: "[email protected]", |
| 568 | Path: "git-lfs/git-lfs.git", |
| 569 | Port: "443", |
| 570 | }, |
| 571 | Operation: "", |
| 572 | OriginalUrl: "ssh://[email protected]:443/git-lfs/git-lfs.git", |
| 573 | }, |
| 574 | }, |
| 575 | "no user bare ssh": { |
| 576 | "github.com:git-lfs/git-lfs.git", |
| 577 | lfshttp.Endpoint{ |
| 578 | Url: "https://github.com/git-lfs/git-lfs.git", |
| 579 | SSHMetadata: ssh.SSHMetadata{ |
| 580 | UserAndHost: "github.com", |
| 581 | Path: "git-lfs/git-lfs.git", |
| 582 | Port: "", |
| 583 | }, |
| 584 | Operation: "", |
| 585 | OriginalUrl: "ssh://github.com/git-lfs/git-lfs.git", |
| 586 | }, |
| 587 | }, |
| 588 | "bare word bare ssh": { |
| 589 | "github:git-lfs/git-lfs.git", |
| 590 | lfshttp.Endpoint{ |
| 591 | Url: "https://github/git-lfs/git-lfs.git", |
| 592 | SSHMetadata: ssh.SSHMetadata{ |
| 593 | UserAndHost: "github", |
| 594 | Path: "git-lfs/git-lfs.git", |
| 595 | Port: "", |
| 596 | }, |
| 597 | Operation: "", |
| 598 | OriginalUrl: "ssh://github/git-lfs/git-lfs.git", |
| 599 | }, |
| 600 | }, |
| 601 | "insteadof alias": { |
| 602 | "gh:git-lfs/git-lfs.git", |
| 603 | lfshttp.Endpoint{ |
| 604 | Url: "https://github.com/git-lfs/git-lfs.git", |
| 605 | SSHMetadata: ssh.SSHMetadata{ |
| 606 | UserAndHost: "", |
| 607 | Path: "", |
| 608 | Port: "", |
| 609 | }, |
| 610 | Operation: "", |
| 611 | OriginalUrl: "https://github.com/git-lfs/git-lfs.git", |
| 612 | }, |
| 613 | }, |
| 614 | "remote helper": { |
| 615 | "remote::git-lfs/git-lfs.git", |
| 616 | lfshttp.Endpoint{ |
| 617 | Url: "remote::git-lfs/git-lfs.git", |
| 618 | SSHMetadata: ssh.SSHMetadata{ |
| 619 | UserAndHost: "", |
| 620 | Path: "", |
| 621 | Port: "", |
| 622 | }, |
| 623 | Operation: "", |
| 624 | }, |
| 625 | }, |
| 626 | } { |
| 627 | t.Run(desc, c.Assert) |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | type InsteadOfTestCase struct { |
| 632 | Given string |
| 633 | Operation string |
| 634 | Expected lfshttp.Endpoint |
| 635 | } |
| 636 | |
| 637 | func (c *InsteadOfTestCase) Assert(t *testing.T) { |
| 638 | finder := NewEndpointFinder(lfshttp.NewContext(nil, nil, map[string]string{ |
| 639 | "remote.test.url": c.Given, |
| 640 | "url.https://example.com/.insteadof": "ex:", |
| 641 | "url.ssh://example.com/.pushinsteadof": "ex:", |
| 642 | "url.ssh://example.com/.insteadof": "exp:", |
| 643 | })) |
| 644 | actual := finder.Endpoint(c.Operation, "test") |
| 645 | assert.Equal(t, c.Expected, actual, "lfsapi: expected endpoint for %q to be %#v (was %#v)", c.Given, c.Expected, actual) |
| 646 | } |
| 647 | |
| 648 | func TestInsteadOf(t *testing.T) { |
| 649 | // Note that many of these tests will produce silly or completely broken |
| 650 | // values for the Url, and that's okay: they work nevertheless. |
| 651 | for desc, c := range map[string]InsteadOfTestCase{ |
| 652 | "insteadof alias (download)": { |
| 653 | "ex:git-lfs/git-lfs.git", |
| 654 | "download", |
| 655 | lfshttp.Endpoint{ |
| 656 | Url: "https://example.com/git-lfs/git-lfs.git/info/lfs", |
| 657 | SSHMetadata: ssh.SSHMetadata{ |
| 658 | UserAndHost: "", |
| 659 | Path: "", |
| 660 | Port: "", |
| 661 | }, |
| 662 | Operation: "download", |
| 663 | OriginalUrl: "https://example.com/git-lfs/git-lfs.git", |
| 664 | }, |
| 665 | }, |
| 666 | "pushinsteadof alias (upload)": { |
| 667 | "ex:git-lfs/git-lfs.git", |
| 668 | "upload", |
| 669 | lfshttp.Endpoint{ |
| 670 | Url: "https://example.com/git-lfs/git-lfs.git/info/lfs", |
| 671 | SSHMetadata: ssh.SSHMetadata{ |
| 672 | UserAndHost: "example.com", |
| 673 | Path: "/git-lfs/git-lfs.git", |
| 674 | Port: "", |
| 675 | }, |
| 676 | Operation: "upload", |
| 677 | OriginalUrl: "ssh://example.com/git-lfs/git-lfs.git", |
| 678 | }, |
| 679 | }, |
| 680 | "exp alias (download)": { |
| 681 | "exp:git-lfs/git-lfs.git", |
| 682 | "download", |
| 683 | lfshttp.Endpoint{ |
| 684 | Url: "https://example.com/git-lfs/git-lfs.git/info/lfs", |
| 685 | SSHMetadata: ssh.SSHMetadata{ |
| 686 | UserAndHost: "example.com", |
| 687 | Path: "/git-lfs/git-lfs.git", |
| 688 | Port: "", |
| 689 | }, |
| 690 | Operation: "download", |
| 691 | OriginalUrl: "ssh://example.com/git-lfs/git-lfs.git", |
| 692 | }, |
| 693 | }, |
| 694 | "exp alias (upload)": { |
| 695 | "exp:git-lfs/git-lfs.git", |
| 696 | "upload", |
| 697 | lfshttp.Endpoint{ |
| 698 | Url: "https://example.com/git-lfs/git-lfs.git/info/lfs", |
| 699 | SSHMetadata: ssh.SSHMetadata{ |
| 700 | UserAndHost: "example.com", |
| 701 | Path: "/git-lfs/git-lfs.git", |
| 702 | Port: "", |
| 703 | }, |
| 704 | Operation: "upload", |
| 705 | OriginalUrl: "ssh://example.com/git-lfs/git-lfs.git", |
| 706 | }, |
| 707 | }, |
| 708 | } { |
| 709 | t.Run(desc, c.Assert) |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | func TestNewEndpointFromCloneURLWithConfig(t *testing.T) { |
| 714 | expected := "https://foo/bar.git/info/lfs" |
| 715 | tests := []string{ |
| 716 | "https://foo/bar", |
| 717 | "https://foo/bar/", |
| 718 | "https://foo/bar.git", |
| 719 | "https://foo/bar.git/", |
| 720 | } |
| 721 | |
| 722 | finder := NewEndpointFinder(nil) |
| 723 | for _, actual := range tests { |
| 724 | e := finder.NewEndpointFromCloneURL("upload", actual) |
| 725 | if e.Url != expected { |
| 726 | t.Errorf("%s returned bad endpoint url %s", actual, e.Url) |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | func TestExtractRemoteUrlForHTTPS(t *testing.T) { |
| 732 | line := "14d0e09d4643d7547267c1cbf9972ac1c4db0b2d not-for-merge branch 'master' of https://example.com/git-lfs/git-lfs" |
| 733 | expected := "https://example.com/git-lfs/git-lfs" |
| 734 | |
| 735 | result, err := ExtractRemoteUrl(line) |
| 736 | assert.Nil(t, err) |
| 737 | assert.Equal(t, expected, result) |
| 738 | } |
| 739 | |
| 740 | func TestExtractRemoteUrlForSSH(t *testing.T) { |
| 741 | line := "cb2ad9f68531e6afe76326d46acf566acf8af4f9 branch 'master' of ssh://example.com/git-lfs/git-lfs" |
| 742 | expected := "ssh://example.com/git-lfs/git-lfs" |
| 743 | |
| 744 | result, err := ExtractRemoteUrl(line) |
| 745 | assert.Nil(t, err) |
| 746 | assert.Equal(t, expected, result) |
| 747 | } |
| 748 | |
| 749 | func TestExtractRemoteUrlForGit(t *testing.T) { |
| 750 | line := "90ed234fb0708235a733bcae0e5b90bd4fac5321 branch 'master' of example.com:git-lfs/git-lfs" |
| 751 | expected := "example.com:git-lfs/git-lfs" |
| 752 | |
| 753 | result, err := ExtractRemoteUrl(line) |
| 754 | assert.Nil(t, err) |
| 755 | assert.Equal(t, expected, result) |
| 756 | } |
| 757 | |
| 758 | func TestExtractRemoteUrlNoURL(t *testing.T) { |
| 759 | invalid := []string{ |
| 760 | "text without url", |
| 761 | // invalid characters in base64 git hash |
| 762 | "qwert34fb0708235a733bcae0e5b90bd4fac5321 branch 'master' of example.com:git-lfs/git-lfs", |
| 763 | // invalid git hash length |
| 764 | "90ed234fb0708235a733bcae0e5b90bd4fac532 branch 'master' of example.com:git-lfs/git-lfs", |
| 765 | // other label present where only `not-for-merge` label allowed |
| 766 | "90ed234fb0708235a733bcae0e5b90bd4fac532 disallowed-label branch 'master' of example.com:git-lfs/git-lfs", |
| 767 | // other type present where only `tag` or `branch` allowed |
| 768 | "90ed234fb0708235a733bcae0e5b90bd4fac532 othertype 'master' of example.com:git-lfs/git-lfs", |
| 769 | // missing `of` |
| 770 | "90ed234fb0708235a733bcae0e5b90bd4fac5321 branch 'master' example.com:git-lfs/git-lfs", |
| 771 | // missing `'` |
| 772 | "90ed234fb0708235a733bcae0e5b90bd4fac5321 branch of example.com:git-lfs/git-lfs", |
| 773 | } |
| 774 | for _, line := range invalid { |
| 775 | result, err := ExtractRemoteUrl(line) |
| 776 | assert.NotNil(t, err) |
| 777 | assert.Regexp(t, regexp.MustCompile("^failed to extract remote URL.*$"), err.Error()) |
| 778 | assert.Equal(t, "", result) |
| 779 | } |
| 780 | } |