package pullauth import ( "strings" "testing" ) func TestResolve(t *testing.T) { const ref = "artifact.example.test:8082/sample-svc1:11f8b56d0fdd" cases := []struct { name string imageRef string username string hasPassword bool want Auth wantErr bool }{ {"no credentials: unchanged", ref, "", false, Auth{}, false}, {"username without password: no login", ref, "ci", false, Auth{}, false}, {"password without username: no login", ref, "", true, Auth{}, false}, {"source scan: no login", "", "ci", true, Auth{}, false}, {"host with port", ref, "ci", true, Auth{Authority: "artifact.example.test:8082", Username: "ci"}, false}, {"host with namespace", "harbor.internal/library/orders:abc", "ci", true, Auth{Authority: "harbor.internal", Username: "ci"}, false}, {"digest-pinned", "harbor.internal/orders@sha256:" + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "ci", true, Auth{Authority: "harbor.internal", Username: "ci"}, false}, {"localhost", "localhost/app:1", "ci", true, Auth{Authority: "localhost", Username: "ci"}, false}, {"docker.io shorthand refuses", "library/app:1", "ci", true, Auth{}, true}, {"bare name refuses", "app:1", "ci", true, Auth{}, true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { got, err := Resolve(tc.imageRef, tc.username, tc.hasPassword) if (err != nil) != tc.wantErr { t.Fatalf("err = %v, wantErr %v", err, tc.wantErr) } if got != tc.want { t.Fatalf("got %+v, want %+v", got, tc.want) } if got.Enabled() != (tc.want.Authority != "") { t.Fatalf("Enabled() = %v for %+v", got.Enabled(), got) } }) } } // The refusal must not echo the credential it refused to place. func TestResolveErrorNamesNoCredential(t *testing.T) { _, err := Resolve("app:1", "the-user-name", true) if err == nil { t.Fatal("expected an error") } if got := err.Error(); strings.Contains(got, "the-user-name") { t.Fatalf("error leaks the username: %s", got) } }