| new file mode 100644 |
| index 000000000..599d9356f |
| --- /dev/null |
| +++ b/internal/console/domainbuy_admission_test.go |
| @@ -0,0 +1,252 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "fmt" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/registrar" |
| +) |
| + |
| +func TestBuyAdmissionNeutralityGuard(t *testing.T) { |
| + for _, tc := range []struct { |
| + name, suffix, key string |
| + status, checks int |
| + premium bool |
| + err error |
| + }{ |
| + {name: "offered", suffix: "com", status: 409, checks: 8}, |
| + {name: "outside-catalog", suffix: "io", key: "notOffered", status: 422}, |
| + {name: "non-registrable", suffix: "shop.com", key: "nameError", status: 422}, |
| + {name: "missing-currency", suffix: "com", key: "notOffered", status: 422}, |
| + {name: "held-premium", suffix: "com", status: 409, checks: 8, premium: true}, |
| + {name: "held-high-cost", suffix: "com", status: 409, checks: 8}, |
| + {name: "registered-premium", suffix: "com", status: 409, checks: 8, premium: true}, |
| + {name: "registrar-unavailable", suffix: "com", status: 409, checks: 8, err: fmt.Errorf("wrapped: %w", registrar.ErrUnavailable)}, |
| + {name: "registrar-error", suffix: "com", key: "unavailable", status: 503, checks: 8, err: registrar.ErrIndeterminate}, |
| + {name: "limited", suffix: "com", key: "rate", status: 429}, |
| + {name: "plan", suffix: "com", status: 409}, |
| + } { |
| + for _, holder := range []string{"apex", "www", "own-apex", "live"} { |
| + t.Run(tc.name+"/"+holder, func(t *testing.T) { |
| + f := newPurchaseFixture(t) |
| + held, unheld := "held."+tc.suffix, "unheld."+tc.suffix |
| + switch holder { |
| + case "live": |
| + if err := f.st.CreateDomainPurchase(DomainPurchase{ID: "guard-live", AccountID: f.acct.ID, Domain: held, App: "kass", State: PurchaseAwaitingPayment}); err != nil { |
| + t.Fatal(err) |
| + } |
| + case "own-apex": |
| + if _, _, err := domains.AttachClaim(context.Background(), f.rel, domains.Claim{Schema: 1, Host: held, Account: f.acct.Sqid, App: "kass"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + default: |
| + host := held |
| + if holder == "www" { |
| + host = "www." + held |
| + } |
| + holdBuyName(t, f, host) |
| + } |
| + for _, name := range []string{held, unheld} { |
| + f.reg.Available[name] = registrar.Availability{Premium: tc.premium, Cost: registrar.Money{Currency: "usd", Minor: 900}} |
| + } |
| + // An offered held name can be free at the registrar. Its answer must |
| + // match an unheld taken name, including when the claim belongs to us. |
| + if tc.name == "offered" || tc.name == "held-premium" || tc.name == "held-high-cost" { |
| + a := f.reg.Available[held] |
| + a.Available = true |
| + if tc.name == "held-high-cost" { |
| + a.Cost.Minor = 1001 |
| + } |
| + f.reg.Available[held] = a |
| + } |
| + if tc.err != nil { |
| + f.srv.cfg.Registrar = admissionErrorRegistrar{f.reg, tc.err} |
| + } |
| + if tc.name == "missing-currency" { |
| + tld := f.srv.cfg.DomainCatalog.TLDs["com"] |
| + tld.Register = map[string]int64{"eur": 1500} |
| + f.srv.cfg.DomainCatalog.TLDs["com"] = tld |
| + } |
| + now := time.Now() |
| + f.srv.domainChecks.now = func() time.Time { return now } |
| + if tc.checks == 0 { |
| + for i := 0; i < domainCheckAccountBudget; i++ { |
| + f.srv.domainChecks.allow(f.acct.ID) |
| + } |
| + } |
| + if tc.name == "plan" { |
| + if _, err := f.st.SetAccountPlan(f.acct.ID, "free", nil, 0, PlanSourceOperator, nil, "test", "operator", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + tok := buyToken(t, f) |
| + browser := newBrowser(t) |
| + f.setUser("owner" + f.acct.Sqid + "@example.com") |
| + signUp(t, f.consoleFixture, browser, "admission-guard") |
| + csrf := sessionToken(t, f.consoleFixture, browser) |
| + wantMessage := hostUnavailableMsg |
| + if tc.key != "" { |
| + wantMessage = domainBuyCopy[tc.key] |
| + } |
| + if tc.name == "plan" { |
| + wantMessage = f.srv.customDomainRefusal(f.acct, unheld).Msg |
| + } |
| + for _, transport := range []string{"cli", "browser"} { |
| + var wantBody string |
| + for _, door := range []string{"check", "purchases"} { |
| + for _, name := range []string{unheld, held} { |
| + var status int |
| + var body string |
| + if transport == "cli" { |
| + payload := strings.ReplaceAll(buyBody(), "shop.com", name) |
| + if door == "check" { |
| + payload = fmt.Sprintf(`{"name":%q}`, name) |
| + } |
| + status, body = buyRequest(t, f, "POST", buyAPIPath(f)+"/"+door, tok, payload) |
| + } else { |
| + path := door |
| + if path == "purchases" { |
| + path = "buy" |
| + } |
| + values := url.Values{"csrf": {csrf}, "name": {name}} |
| + for _, field := range buyContactFields(purchaseContact()) { |
| + values.Set(field.Name, field.Value) |
| + } |
| + status, _, body = postForm(t, browser, f.ts.URL+"/a/"+f.acct.Sqid+"/apps/kass/domains/"+path, values) |
| + } |
| + if status != tc.status || !strings.Contains(body, wantMessage) { |
| + t.Errorf("%s/%s/%s: status %d, want %d and message %q", transport, door, name, status, tc.status, wantMessage) |
| + } |
| + if wantBody == "" { |
| + wantBody = body |
| + } |
| + if body != wantBody { |
| + t.Errorf("%s/%s/%s: refusal bytes differ", transport, door, name) |
| + } |
| + } |
| + } |
| + } |
| + if n := purchaseCalls(f.reg.Calls, "check:"); n != tc.checks { |
| + t.Errorf("registrar calls %d, want %d", n, tc.checks) |
| + } |
| + if n := purchaseCalls(f.reg.Calls, "contact:"); n != 0 { |
| + t.Errorf("refusal created %d contacts", n) |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| +type admissionErrorRegistrar struct { |
| + registrar.Registrar |
| + err error |
| +} |
| + |
| +func (r admissionErrorRegistrar) Check(ctx context.Context, name string) (registrar.Availability, error) { |
| + _, _ = r.Registrar.Check(ctx, name) |
| + return registrar.Availability{}, r.err |
| +} |
| + |
| +func TestBuyAdmissionSharesCheckBudget(t *testing.T) { |
| + f := newPurchaseFixture(t) |
| + now := time.Now() |
| + f.srv.domainChecks.now = func() time.Time { return now } |
| + tok := buyToken(t, f) |
| + browser := newBrowser(t) |
| + f.setUser("owner" + f.acct.Sqid + "@example.com") |
| + signUp(t, f.consoleFixture, browser, "admission-budget") |
| + csrf := sessionToken(t, f.consoleFixture, browser) |
| + holdBuyName(t, f, "held.com") |
| + for i := 0; i < domainCheckAccountBudget+4; i++ { |
| + name := "taken.com" |
| + if i%2 == 0 { |
| + name = "held.com" |
| + } |
| + var status int |
| + switch i % 4 { |
| + case 0: |
| + status, _ = buyRequest(t, f, "POST", buyAPIPath(f)+"/check", tok, fmt.Sprintf(`{"name":%q}`, name)) |
| + case 1: |
| + status, _ = buyRequest(t, f, "POST", buyAPIPath(f)+"/purchases", tok, strings.ReplaceAll(buyBody(), "shop.com", name)) |
| + default: |
| + door := "check" |
| + if i%4 == 3 { |
| + door = "buy" |
| + } |
| + values := url.Values{"csrf": {csrf}, "name": {name}} |
| + for _, field := range buyContactFields(purchaseContact()) { |
| + values.Set(field.Name, field.Value) |
| + } |
| + status, _, _ = postForm(t, browser, f.ts.URL+"/a/"+f.acct.Sqid+"/apps/kass/domains/"+door, values) |
| + } |
| + want := 409 |
| + if i >= domainCheckAccountBudget { |
| + want = 429 |
| + } |
| + if status != want { |
| + t.Errorf("request %d: status %d, want %d", i+1, status, want) |
| + } |
| + } |
| + if n := purchaseCalls(f.reg.Calls, "check:"); n != domainCheckAccountBudget { |
| + t.Errorf("registrar calls %d, want %d", n, domainCheckAccountBudget) |
| + } |
| +} |
| + |
| +func TestBuyAdmissionCatalogRefusals(t *testing.T) { |
| + for _, tc := range []struct { |
| + name, key string |
| + premium bool |
| + cost int64 |
| + status int |
| + }{ |
| + {"premium", "premium", true, 900, 422}, |
| + {"cost", "unavailable", false, 1001, 503}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f := newPurchaseFixture(t) |
| + f.reg.Available["shop.com"] = registrar.Availability{Available: true, Premium: tc.premium, Cost: registrar.Money{Currency: "usd", Minor: tc.cost}} |
| + tok := buyToken(t, f) |
| + browser := newBrowser(t) |
| + f.setUser("owner" + f.acct.Sqid + "@example.com") |
| + signUp(t, f.consoleFixture, browser, "catalog-refusal") |
| + values := url.Values{"csrf": {sessionToken(t, f.consoleFixture, browser)}, "name": {"shop.com"}} |
| + for _, field := range buyContactFields(purchaseContact()) { |
| + values.Set(field.Name, field.Value) |
| + } |
| + for _, transport := range []string{"cli", "browser"} { |
| + var wantBody string |
| + for _, door := range []string{"check", "purchases"} { |
| + var status int |
| + var body string |
| + if transport == "cli" { |
| + payload := buyBody() |
| + if door == "check" { |
| + payload = `{"name":"shop.com"}` |
| + } |
| + status, body = buyRequest(t, f, "POST", buyAPIPath(f)+"/"+door, tok, payload) |
| + } else { |
| + path := door |
| + if path == "purchases" { |
| + path = "buy" |
| + } |
| + status, _, body = postForm(t, browser, f.ts.URL+"/a/"+f.acct.Sqid+"/apps/kass/domains/"+path, values) |
| + } |
| + if status != tc.status || !strings.Contains(body, domainBuyCopy[tc.key]) { |
| + t.Errorf("%s/%s: status %d, want %d and %q", transport, door, status, tc.status, domainBuyCopy[tc.key]) |
| + } |
| + if wantBody == "" { |
| + wantBody = body |
| + } |
| + if body != wantBody { |
| + t.Errorf("%s/%s: refusal bytes differ", transport, door) |
| + } |
| + } |
| + } |
| + }) |
| + } |
| +} |