MCPcopy
hub / github.com/git-lfs/git-lfs / TestDoWithAuthApprove

Function TestDoWithAuthApprove

lfsapi/auth_test.go:59–116  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

57}
58
59func TestDoWithAuthApprove(t *testing.T) {
60 var called uint32
61
62 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
63 atomic.AddUint32(&called, 1)
64 assert.Equal(t, "POST", req.Method)
65
66 body := &authRequest{}
67 err := json.NewDecoder(req.Body).Decode(body)
68 assert.Nil(t, err)
69 assert.Equal(t, "Approve", body.Test)
70
71 w.Header().Set("Lfs-Authenticate", "Basic")
72 actual := req.Header.Get("Authorization")
73 if len(actual) == 0 {
74 w.WriteHeader(http.StatusUnauthorized)
75 return
76 }
77
78 expected := "Basic " + strings.TrimSpace(
79 base64.StdEncoding.EncodeToString([]byte("user:pass")),
80 )
81 assert.Equal(t, expected, actual)
82 }))
83 defer srv.Close()
84
85 cred := newMockCredentialHelper()
86 c, err := NewClient(lfshttp.NewContext(git.NewReadOnlyConfig("", ""),
87 nil, map[string]string{
88 "lfs.url": srv.URL + "/repo/lfs",
89 },
90 ))
91 require.Nil(t, err)
92 c.Credentials = cred
93
94 access := c.Endpoints.AccessFor(srv.URL + "/repo/lfs")
95 assert.Equal(t, creds.NoneAccess, (&access).Mode())
96
97 req, err := http.NewRequest("POST", srv.URL+"/repo/lfs/foo", nil)
98 require.Nil(t, err)
99
100 err = MarshalToRequest(req, &authRequest{Test: "Approve"})
101 require.Nil(t, err)
102
103 res, err := c.DoWithAuth("", c.Endpoints.AccessFor(srv.URL+"/repo/lfs"), req)
104 require.Nil(t, err)
105
106 assert.Equal(t, http.StatusOK, res.StatusCode)
107 assert.True(t, cred.IsApproved(creds.Creds(map[string][]string{
108 "username": []string{"user"},
109 "password": []string{"pass"},
110 "protocol": []string{"http"},
111 "host": []string{srv.Listener.Addr().String()},
112 })))
113 access = c.Endpoints.AccessFor(srv.URL + "/repo/lfs")
114 assert.Equal(t, creds.BasicAccess, (&access).Mode())
115 assert.EqualValues(t, 2, called)
116}

Callers

nothing calls this directly

Calls 15

DoWithAuthMethod · 0.95
NewContextFunction · 0.92
NewReadOnlyConfigFunction · 0.92
CredsTypeAlias · 0.92
newMockCredentialHelperFunction · 0.85
ModeMethod · 0.80
IsApprovedMethod · 0.80
NewClientFunction · 0.70
MarshalToRequestFunction · 0.70
SetMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected