|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/containerd/errdefs" |
| 11 | + "github.com/moby/moby/api/types/registry" |
| 12 | + "gotest.tools/v3/assert" |
| 13 | + is "gotest.tools/v3/assert/cmp" |
| 14 | +) |
| 15 | + |
| 16 | +func TestLoginV2BasicAuthUnauthorized(t *testing.T) { |
| 17 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + user, pass, ok := r.BasicAuth() |
| 19 | + if !ok || user != "alice" || pass != "secret" { |
| 20 | + w.Header().Set("WWW-Authenticate", `Basic realm="test"`) |
| 21 | + http.Error(w, "401 Unauthorized", http.StatusUnauthorized) |
| 22 | + return |
| 23 | + } |
| 24 | + w.WriteHeader(http.StatusOK) |
| 25 | + })) |
| 26 | + t.Cleanup(srv.Close) |
| 27 | + |
| 28 | + u, err := url.Parse(srv.URL) |
| 29 | + assert.NilError(t, err) |
| 30 | + endpoint := APIEndpoint{URL: u} |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + _, err = loginV2(ctx, ®istry.AuthConfig{Username: "alice", Password: "wrong"}, endpoint, "docker-test") |
| 34 | + assert.ErrorContains(t, err, "401") |
| 35 | + assert.Check(t, errdefs.IsUnauthorized(err)) |
| 36 | + assert.Check(t, is.ErrorType(err, errdefs.IsUnauthorized)) |
| 37 | + |
| 38 | + token, err := loginV2(ctx, ®istry.AuthConfig{Username: "alice", Password: "secret"}, endpoint, "docker-test") |
| 39 | + assert.NilError(t, err) |
| 40 | + assert.Check(t, is.Equal("", token)) |
| 41 | +} |
0 commit comments