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

Function TestDoWithAuthReject

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

Source from the content-addressed store, hash-verified

116}
117
118func TestDoWithAuthReject(t *testing.T) {
119 var called uint32
120
121 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
122 atomic.AddUint32(&called, 1)
123 assert.Equal(t, "POST", req.Method)
124
125 body := &authRequest{}
126 err := json.NewDecoder(req.Body).Decode(body)
127 assert.Nil(t, err)
128 assert.Equal(t, "Reject", body.Test)
129
130 actual := req.Header.Get("Authorization")
131 expected := "Basic " + strings.TrimSpace(
132 base64.StdEncoding.EncodeToString([]byte("user:pass")),
133 )
134
135 w.Header().Set("Lfs-Authenticate", "Basic")
136 if actual != expected {
137 // Write http.StatusUnauthorized to force the credential
138 // helper to reject the credentials
139 w.WriteHeader(http.StatusUnauthorized)
140 } else {
141 w.WriteHeader(http.StatusOK)
142 }
143 }))
144 defer srv.Close()
145
146 invalidCreds := creds.Creds(map[string][]string{
147 "username": []string{"user"},
148 "password": []string{"wrong_pass"},
149 "path": []string{""},
150 "protocol": []string{"http"},
151 "host": []string{srv.Listener.Addr().String()},
152 })
153
154 cred := newMockCredentialHelper()
155 cred.Approve(invalidCreds)
156 assert.True(t, cred.IsApproved(invalidCreds))
157
158 c, _ := NewClient(nil)
159 c.Credentials = cred
160 c.Endpoints = NewEndpointFinder(lfshttp.NewContext(git.NewReadOnlyConfig("", ""),
161 nil, map[string]string{
162 "lfs.url": srv.URL,
163 },
164 ))
165
166 req, err := http.NewRequest("POST", srv.URL, nil)
167 require.Nil(t, err)
168
169 err = MarshalToRequest(req, &authRequest{Test: "Reject"})
170 require.Nil(t, err)
171
172 res, err := c.DoWithAuth("", c.Endpoints.AccessFor(srv.URL), req)
173 require.Nil(t, err)
174
175 assert.Equal(t, http.StatusOK, res.StatusCode)

Callers

nothing calls this directly

Calls 15

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

Tested by

no test coverage detected