mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-02 16:26:56 +00:00
171 lines
4.7 KiB
Go
171 lines
4.7 KiB
Go
package labeler
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
// TestParseTakedownURL exercises the URL-shaped parsing path offline by feeding only
|
|
// inputs whose identifier is a DID — DIDs short-circuit ResolveIdentity entirely, so
|
|
// this whole table runs without network. Every case here came up while debugging
|
|
// browser-paste bugs (URL fragments, query strings, trailing /digest/... segments).
|
|
func TestParseTakedownURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantDID string
|
|
wantRepo string
|
|
wantErr bool
|
|
}{
|
|
// /r/<handle>/<repo>
|
|
{"r path with scheme", "https://atcr.io/r/did:plc:abc/myimage", "did:plc:abc", "myimage", false},
|
|
{"r path no scheme", "atcr.io/r/did:plc:abc/myimage", "did:plc:abc", "myimage", false},
|
|
{"r path trailing slash", "atcr.io/r/did:plc:abc/myimage/", "did:plc:abc", "myimage", false},
|
|
{"r path custom domain", "https://seamark.dev/r/did:plc:abc/myimage", "did:plc:abc", "myimage", false},
|
|
{"r path subroute (digest)", "https://atcr.io/r/did:plc:abc/myimage/digest/sha256-deadbeef", "did:plc:abc", "myimage", false},
|
|
{"r path with hash fragment", "https://atcr.io/r/did:plc:abc/myimage#overview", "did:plc:abc", "myimage", false},
|
|
{"r path with query string", "https://atcr.io/r/did:plc:abc/myimage?tag=latest", "did:plc:abc", "myimage", false},
|
|
{"r path missing repo", "https://atcr.io/r/did:plc:abc", "", "", true},
|
|
|
|
// /u/<handle>
|
|
{"u path with scheme", "https://atcr.io/u/did:plc:abc", "did:plc:abc", "", false},
|
|
{"u path no scheme", "atcr.io/u/did:plc:abc", "did:plc:abc", "", false},
|
|
{"u path with hash", "https://atcr.io/u/did:plc:abc#tab", "did:plc:abc", "", false},
|
|
{"u path missing handle", "https://atcr.io/u", "", "", true},
|
|
|
|
// Unknown route
|
|
{"unsupported path", "https://atcr.io/foo/did:plc:abc", "", "", true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := parseTakedownURL(context.Background(), tt.input)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("expected error, got %+v", got)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got.DID != tt.wantDID {
|
|
t.Errorf("DID = %q, want %q", got.DID, tt.wantDID)
|
|
}
|
|
if got.Repository != tt.wantRepo {
|
|
t.Errorf("Repository = %q, want %q", got.Repository, tt.wantRepo)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseTakedownInput_BareDID(t *testing.T) {
|
|
// Bare DIDs short-circuit network resolution.
|
|
got, err := ParseTakedownInput(context.Background(), "did:plc:abc123")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got.DID != "did:plc:abc123" {
|
|
t.Errorf("DID = %q, want did:plc:abc123", got.DID)
|
|
}
|
|
if got.Repository != "" {
|
|
t.Errorf("Repository = %q, want empty (user-level)", got.Repository)
|
|
}
|
|
}
|
|
|
|
func TestParseTakedownInput_ATURI(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantDID string
|
|
wantRepo string
|
|
}{
|
|
{
|
|
"full AT URI with collection",
|
|
"at://did:plc:abc123/io.atcr.repo.page/myimage",
|
|
"did:plc:abc123",
|
|
"myimage",
|
|
},
|
|
{
|
|
"DID only (user-level)",
|
|
"at://did:plc:abc123",
|
|
"did:plc:abc123",
|
|
"",
|
|
},
|
|
{
|
|
"DID with collection and rkey",
|
|
"at://did:plc:xyz/io.atcr.manifest/sha256-deadbeef",
|
|
"did:plc:xyz",
|
|
"sha256-deadbeef",
|
|
},
|
|
{
|
|
"AT URI with hash fragment",
|
|
"at://did:plc:abc#frag",
|
|
"did:plc:abc",
|
|
"",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ParseTakedownInput(context.Background(), tt.input)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got.DID != tt.wantDID {
|
|
t.Errorf("DID = %q, want %q", got.DID, tt.wantDID)
|
|
}
|
|
if got.Repository != tt.wantRepo {
|
|
t.Errorf("Repository = %q, want %q", got.Repository, tt.wantRepo)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseTakedownInput_Empty(t *testing.T) {
|
|
if _, err := ParseTakedownInput(context.Background(), ""); err == nil {
|
|
t.Error("expected error for empty input")
|
|
}
|
|
}
|
|
|
|
func TestExtractRepoField(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
value string
|
|
collection string
|
|
want string
|
|
}{
|
|
{
|
|
"manifest with repository",
|
|
`{"$type":"io.atcr.manifest","repository":"myimage","digest":"sha256:abc"}`,
|
|
"io.atcr.manifest",
|
|
"myimage",
|
|
},
|
|
{
|
|
"tag with repository",
|
|
`{"$type":"io.atcr.tag","repository":"myimage","tag":"latest"}`,
|
|
"io.atcr.tag",
|
|
"myimage",
|
|
},
|
|
{
|
|
"no repository field",
|
|
`{"$type":"io.atcr.manifest","digest":"sha256:abc"}`,
|
|
"io.atcr.manifest",
|
|
"",
|
|
},
|
|
{
|
|
"invalid JSON",
|
|
`{invalid}`,
|
|
"io.atcr.manifest",
|
|
"",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := extractRepoField([]byte(tt.value), tt.collection)
|
|
if got != tt.want {
|
|
t.Errorf("extractRepoField() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|