File: verify_test.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (72 lines) | stat: -rw-r--r-- 1,817 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tq

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"sync/atomic"
	"testing"

	"github.com/git-lfs/git-lfs/v3/lfsapi"
	"github.com/git-lfs/git-lfs/v3/lfshttp"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestVerifyWithoutAction(t *testing.T) {
	c, _ := lfsapi.NewClient(nil)
	tr := &Transfer{
		Oid:  "abc",
		Size: 123,
	}

	assert.Nil(t, verifyUpload(c, "origin", tr))
}

func TestVerifySuccess(t *testing.T) {
	var called uint32
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.String() != "/verify" {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		atomic.AddUint32(&called, 1)

		assert.Equal(t, "POST", r.Method)
		assert.Equal(t, "bar", r.Header.Get("Foo"))
		assert.Equal(t, "29", r.Header.Get("Content-Length"))
		assert.Equal(t, "application/vnd.git-lfs+json", r.Header.Get("Content-Type"))

		var tr Transfer
		assert.Nil(t, json.NewDecoder(r.Body).Decode(&tr))
		assert.Equal(t, "abcd1234", tr.Oid)
		assert.EqualValues(t, 123, tr.Size)
	}))
	defer srv.Close()

	// Set auth on the server URL but not on the /verify endpoint. Since auth
	// will cause the request to fail, this will test that the correct access
	// mode is being passed to `DoWithAuth()`
	c, err := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
		"lfs.transfer.maxverifies":          "1",
		"lfs." + srv.URL + ".access":        "Basic",
		"lfs." + srv.URL + "/verify.access": "None",
	}))
	require.Nil(t, err)
	tr := &Transfer{
		Oid:  "abcd1234",
		Size: 123,
		Actions: map[string]*Action{
			"verify": &Action{
				Href: srv.URL + "/verify",
				Header: map[string]string{
					"foo": "bar",
				},
			},
		},
	}

	assert.Nil(t, verifyUpload(c, "origin", tr))
	assert.EqualValues(t, 1, called)
}