| 1 | package tq |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/lfsapi" |
| 11 | "github.com/git-lfs/git-lfs/v3/lfshttp" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | ) |
| 14 | |
| 15 | func TestVerifyWithoutAction(t *testing.T) { |
| 16 | c := lfsapi.NewClient(nil) |
| 17 | tr := &Transfer{ |
| 18 | Oid: "abc", |
| 19 | Size: 123, |
| 20 | } |
| 21 | |
| 22 | assert.Nil(t, verifyUpload(c, "origin", tr)) |
| 23 | } |
| 24 | |
| 25 | func TestVerifySuccess(t *testing.T) { |
| 26 | var called uint32 |
| 27 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | if r.URL.String() != "/verify" { |
| 29 | w.WriteHeader(http.StatusNotFound) |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | atomic.AddUint32(&called, 1) |
| 34 | |
| 35 | assert.Equal(t, "POST", r.Method) |
| 36 | assert.Equal(t, "bar", r.Header.Get("Foo")) |
| 37 | assert.Equal(t, "29", r.Header.Get("Content-Length")) |
| 38 | assert.Equal(t, "application/vnd.git-lfs+json", r.Header.Get("Content-Type")) |
| 39 | |
| 40 | var tr Transfer |
| 41 | assert.Nil(t, json.NewDecoder(r.Body).Decode(&tr)) |
| 42 | assert.Equal(t, "abcd1234", tr.Oid) |
| 43 | assert.EqualValues(t, 123, tr.Size) |
| 44 | })) |
| 45 | defer srv.Close() |
| 46 | |
| 47 | // Set auth on the server URL but not on the /verify endpoint. Since auth |
| 48 | // will cause the request to fail, this will test that the correct access |
| 49 | // mode is being passed to `DoWithAuth()` |
| 50 | c := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{ |
| 51 | "lfs.transfer.maxverifies": "1", |
| 52 | "lfs." + srv.URL + ".access": "Basic", |
| 53 | "lfs." + srv.URL + "/verify.access": "None", |
| 54 | })) |
| 55 | |
| 56 | tr := &Transfer{ |
| 57 | Oid: "abcd1234", |
| 58 | Size: 123, |
| 59 | Actions: map[string]*Action{ |
| 60 | "verify": &Action{ |
| 61 | Href: srv.URL + "/verify", |
| 62 | Header: map[string]string{ |
| 63 | "foo": "bar", |
| 64 | }, |
| 65 | }, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | assert.Nil(t, verifyUpload(c, "origin", tr)) |
| 70 | assert.EqualValues(t, 1, called) |
| 71 | } |