This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / appview / middleware / resolve_repo_test.go
2.8 kB 83 lines
1package middleware 2 3import ( 4 "context" 5 "io" 6 "log/slog" 7 "net/http" 8 "net/http/httptest" 9 "path/filepath" 10 "testing" 11 12 "github.com/bluesky-social/indigo/atproto/identity" 13 "github.com/bluesky-social/indigo/atproto/syntax" 14 "github.com/go-chi/chi/v5" 15 "tangled.org/core/appview/db" 16 "tangled.org/core/appview/models" 17) 18 19func TestResolveRepo_RenameAlias(t *testing.T) { 20 const ownerDid, handle, knot = "did:plc:boltless", "boltless.dev", "knot1.tangled.sh" 21 cases := []struct { 22 name string 23 repoName, repoRkey, did string 24 alias, reqRepo string 25 wantLocation string // empty => expect the repo to be served without a redirect 26 }{ 27 {"alias equal to live slug is served, not looped", "anemone", "3mpxmsvicr2zn", "did:plc:anemone", "anemone", "anemone", ""}, 28 {"genuine rename still redirects", "whelk", "whelk", "did:plc:whelk", "conch", "conch", "/boltless.dev/whelk"}, 29 } 30 31 for _, tc := range cases { 32 t.Run(tc.name, func(t *testing.T) { 33 d, err := db.Make(context.Background(), filepath.Join(t.TempDir(), "test.db")) 34 if err != nil { 35 t.Fatalf("Make: %v", err) 36 } 37 t.Cleanup(func() { d.Close() }) 38 39 tx, err := d.Begin() 40 if err != nil { 41 t.Fatalf("Begin: %v", err) 42 } 43 if err := db.AddRepo(tx, &models.Repo{Did: ownerDid, Name: tc.repoName, Knot: knot, Rkey: tc.repoRkey, RepoDid: tc.did}); err != nil { 44 t.Fatalf("AddRepo: %v", err) 45 } 46 if err := tx.Commit(); err != nil { 47 t.Fatalf("Commit: %v", err) 48 } 49 if err := db.RecordRepoRename(d, ownerDid, tc.alias, tc.did); err != nil { 50 t.Fatalf("RecordRepoRename: %v", err) 51 } 52 53 req := httptest.NewRequest(http.MethodGet, "/"+handle+"/"+tc.reqRepo, nil) 54 rctx := chi.NewRouteContext() 55 rctx.URLParams.Add("user", handle) 56 rctx.URLParams.Add("repo", tc.reqRepo) 57 ctx := context.WithValue(req.Context(), chi.RouteCtxKey, rctx) 58 ctx = context.WithValue(ctx, "resolvedId", identity.Identity{DID: syntax.DID(ownerDid), Handle: syntax.Handle(handle)}) 59 60 rec := httptest.NewRecorder() 61 served := false 62 next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { served = true }) 63 mw := Middleware{db: d, logger: slog.New(slog.NewTextHandler(io.Discard, nil))} 64 mw.ResolveRepo()(next).ServeHTTP(rec, req.WithContext(ctx)) 65 66 if tc.wantLocation == "" { 67 if !served { 68 t.Fatalf("expected repo served, got code=%d Location=%q", rec.Code, rec.Header().Get("Location")) 69 } 70 return 71 } 72 if served { 73 t.Fatal("expected a redirect, but the repo was served") 74 } 75 if rec.Code != http.StatusMovedPermanently { 76 t.Fatalf("got %d, want 301", rec.Code) 77 } 78 if got := rec.Header().Get("Location"); got != tc.wantLocation { 79 t.Errorf("Location = %q, want %q", got, tc.wantLocation) 80 } 81 }) 82 } 83}