| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/domains" |
| 12 | "github.com/carlosframework/platform/internal/provision" |
| 13 | ) |
| 14 | |
| 15 | // firstRouteFixture gives titogo a first-route claim on woodstar.app — the |
| 16 | // claim `carlos deploy --kind static --host` makes, with the static record |
| 17 | // it declared at the same host. That record is titogo's only instance |
| 18 | // record, which is exactly what used to make a route-less re-attach default |
| 19 | // to the host itself. |
| 20 | func firstRouteFixture(t *testing.T, code string) (*consoleFixture, string) { |
| 21 | t.Helper() |
| 22 | fx, member := instanceFixture(t, code) |
| 23 | ctx := context.Background() |
| 24 | if _, _, err := domains.AttachClaim(ctx, fx.rel, domains.Claim{ |
| 25 | Host: "woodstar.app", Account: "bab", App: "titogo", CreatedAt: time.Now().UTC().Format(time.RFC3339), |
| 26 | }); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if err := provision.CreateRecord(ctx, fx.rel, provision.Record{ |
| 30 | Schema: 1, Host: "woodstar.app", App: "titogo", Account: "bab", Channel: "edge", Kind: "static", |
| 31 | CreatedAt: time.Now().UTC(), |
| 32 | }); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | return fx, member |
| 36 | } |
| 37 | |
| 38 | func TestAttachKeepsAFirstRouteClaimFirstRoute(t *testing.T) { |
| 39 | const zone = "acme.oncarlos.com" |
| 40 | for name, body := range map[string]string{ |
| 41 | "no route": `{"host":"woodstar.app"}`, |
| 42 | "route is the host": `{"host":"woodstar.app","route":"WoodStar.app"}`, |
| 43 | "wildcard flip": `{"host":"woodstar.app","wildcard":true}`, |
| 44 | "wildcard and catchall": `{"host":"woodstar.app","wildcard":true,"catchall":true}`, |
| 45 | } { |
| 46 | t.Run(name, func(t *testing.T) { |
| 47 | fx, member := firstRouteFixture(t, "code-dom-fr-"+strings.ReplaceAll(name, " ", "-")) |
| 48 | fx.srv.delegationZone = zone |
| 49 | fx.srv.lookupCNAME = func(_ context.Context, n string) (string, error) { |
| 50 | if n == "_acme-challenge.woodstar.app" { |
| 51 | return "_acme-challenge.woodstar.app." + zone + ".", nil |
| 52 | } |
| 53 | return "", errors.New("not found") |
| 54 | } |
| 55 | code, _, out := rawReq(t, http.MethodPost, domainsURL(fx, "bab", "titogo"), member, body) |
| 56 | if code != http.StatusOK || !strings.Contains(out, `"route":"woodstar.app"`) || !strings.Contains(out, `"created":false`) { |
| 57 | t.Fatalf("re-attach = %d %s", code, out) |
| 58 | } |
| 59 | c := claimOf(t, fx, "woodstar.app") |
| 60 | if c.Route != "" { |
| 61 | t.Fatalf("claim route = %q, want the first-route claim's empty route", c.Route) |
| 62 | } |
| 63 | if strings.Contains(body, "wildcard") && !c.Wildcard { |
| 64 | t.Fatalf("claim = %+v, want Wildcard set", *c) |
| 65 | } |
| 66 | if strings.Contains(body, "catchall") && !c.Catchall { |
| 67 | t.Fatalf("claim = %+v, want Catchall set", *c) |
| 68 | } |
| 69 | // Detach still takes the first-route branch: the record goes too. |
| 70 | code, _, out = rawReq(t, http.MethodDelete, domainsURL(fx, "bab", "titogo")+"/woodstar.app", member, "") |
| 71 | if code != http.StatusOK || !strings.Contains(out, `"removed":true`) { |
| 72 | t.Fatalf("detach = %d %s", code, out) |
| 73 | } |
| 74 | if _, err := provision.GetRecord(context.Background(), fx.rel, "bab", "titogo", "woodstar.app"); err == nil { |
| 75 | t.Fatal("the static record outlived its first-route claim") |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Another holder's claim does not take the branch: the ordinary path answers |
| 82 | // with its neutral refusal, exactly as before. |
| 83 | func TestAttachFirstRouteBranchIsOwnClaimsOnly(t *testing.T) { |
| 84 | fx, member := instanceFixture(t, "code-dom-fr-other") |
| 85 | if _, _, err := domains.AttachClaim(context.Background(), fx.rel, domains.Claim{ |
| 86 | Host: "woodstar.app", Account: "cdef", App: "widgets", CreatedAt: time.Now().UTC().Format(time.RFC3339), |
| 87 | }); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | code, _, out := rawReq(t, http.MethodPost, domainsURL(fx, "bab", "titogo"), member, `{"host":"woodstar.app","route":"go.tito.io"}`) |
| 91 | if code != http.StatusConflict || strings.TrimSpace(out) != `{"error":"hostname unavailable"}` { |
| 92 | t.Fatalf("attach over another holder's first-route claim = %d %s, want the neutral 409", code, out) |
| 93 | } |
| 94 | if c := claimOf(t, fx, "woodstar.app"); c.Account != "cdef" || c.Route != "" { |
| 95 | t.Fatalf("holder's claim changed: %+v", *c) |
| 96 | } |
| 97 | } |
| 98 | |