registrar: structured phone and house number on Contact
Co-Authored-By: Astra (gpt-6-astra) via Codex <noreply@openai.com>
3 files changed,
+72
−4
internal/registrar/registrar.go+14 −0internal/registrar/registrar_test.go+45 −2internal/registrar/registrartest/fake.go+13 −2
diff --git a/internal/registrar/registrar.go b/internal/registrar/registrar.go| index 97bca8d06..ffc4dddf7 100644 |
| --- a/internal/registrar/registrar.go |
| +++ b/internal/registrar/registrar.go |
| @@ -22,6 +22,7 @@ type Availability struct { |
| type Contact struct { |
| // Country ISO 3166-1 alpha-2 |
| FirstName, LastName, Company, Email, Phone, Street, City, State, PostalCode, Country string |
| + PhoneCountryCode, HouseNumber string |
| } |
| type Registration struct { |
| @@ -55,6 +56,7 @@ func (c Contact) Validate() error { |
| {"FirstName", c.FirstName}, |
| {"LastName", c.LastName}, |
| {"Email", c.Email}, |
| + {"PhoneCountryCode", c.PhoneCountryCode}, |
| {"Phone", c.Phone}, |
| {"Street", c.Street}, |
| {"City", c.City}, |
| @@ -65,6 +67,18 @@ func (c Contact) Validate() error { |
| return fmt.Errorf("registrar: %s is required", field.name) |
| } |
| } |
| + for _, field := range []struct{ name, value string }{ |
| + {"PhoneCountryCode", c.PhoneCountryCode}, {"Phone", c.Phone}, |
| + } { |
| + for _, digit := range field.value { |
| + if digit < '0' || digit > '9' { |
| + return fmt.Errorf("registrar: %s must contain ASCII digits only", field.name) |
| + } |
| + } |
| + } |
| + if len(c.PhoneCountryCode) > 3 { |
| + return errors.New("registrar: PhoneCountryCode must contain 1-3 digits") |
| + } |
| if len(c.Country) != 2 || c.Country[0] < 'A' || c.Country[0] > 'Z' || c.Country[1] < 'A' || c.Country[1] > 'Z' { |
| return errors.New("registrar: Country must be two uppercase ASCII letters") |
| } |
diff --git a/internal/registrar/registrar_test.go b/internal/registrar/registrar_test.go| index d18c0127c..ec669885c 100644 |
| --- a/internal/registrar/registrar_test.go |
| +++ b/internal/registrar/registrar_test.go |
| @@ -14,7 +14,7 @@ import ( |
| func fullContact() registrar.Contact { |
| return registrar.Contact{ |
| FirstName: "Alex", LastName: "Example", Email: "alex@example.com", |
| - Phone: "+442079460000", Street: "1 Example Street", City: "London", |
| + PhoneCountryCode: "44", Phone: "2079460000", Street: "1 Example Street", City: "London", |
| PostalCode: "SW1A 1AA", Country: "GB", |
| } |
| } |
| @@ -26,7 +26,7 @@ func TestContactValidateRequiresFields(t *testing.T) { |
| if err := fullContact().Validate(); err != nil { |
| t.Fatalf("full contact: %v", err) |
| } |
| - for _, field := range []string{"FirstName", "LastName", "Email", "Phone", "Street", "City", "PostalCode", "Country"} { |
| + for _, field := range []string{"FirstName", "LastName", "Email", "PhoneCountryCode", "Phone", "Street", "City", "PostalCode", "Country"} { |
| t.Run(field, func(t *testing.T) { |
| for _, blank := range []string{"", " \t\n"} { |
| c := fullContact() |
| @@ -170,3 +170,46 @@ func TestFakeCheckContactLookupAndBalance(t *testing.T) { |
| t.Fatalf("Calls = %v, want %v", f.Calls, wantCalls) |
| } |
| } |
| + |
| +func TestContactStructuredPhone(t *testing.T) { |
| + for _, tc := range []struct{ field, value string }{ |
| + {"PhoneCountryCode", "+44"}, {"PhoneCountryCode", "1234"}, |
| + {"PhoneCountryCode", "44"}, {"PhoneCountryCode", "4x"}, |
| + {"Phone", "+442079460000"}, {"Phone", "207 9460000"}, |
| + {"Phone", "2079460000"}, {"Phone", "123-4567"}, |
| + } { |
| + c := fullContact() |
| + reflect.ValueOf(&c).Elem().FieldByName(tc.field).SetString(tc.value) |
| + if err := c.Validate(); err == nil || !strings.Contains(err.Error(), tc.field) { |
| + t.Errorf("%s=%q: got %v", tc.field, tc.value, err) |
| + } |
| + if _, err := registrartest.New().EnsureContact(context.Background(), "", c); err == nil { |
| + t.Errorf("fake accepted %s=%q", tc.field, tc.value) |
| + } |
| + } |
| + for _, cc := range []string{"1", "44", "353"} { |
| + c := fullContact() |
| + c.PhoneCountryCode, c.HouseNumber = cc, "12A" |
| + if err := c.Validate(); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +func TestFakeContactIdentity(t *testing.T) { |
| + f := registrartest.New() |
| + ctx := context.Background() |
| + c := fullContact() |
| + h, err := f.EnsureContact(ctx, "", c) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + c.Phone, c.HouseNumber = "123456789", "12" |
| + if got, err := f.EnsureContact(ctx, h, c); err != nil || got != h { |
| + t.Fatalf("mutable contact = %q, %v", got, err) |
| + } |
| + c.LastName = "Changed" |
| + if got, err := f.EnsureContact(ctx, h, c); err != nil || got == h || got == "" { |
| + t.Fatalf("new identity = %q, %v", got, err) |
| + } |
| +} |
diff --git a/internal/registrar/registrartest/fake.go b/internal/registrar/registrartest/fake.go| index 7232a9a2f..67b6e0489 100644 |
| --- a/internal/registrar/registrartest/fake.go |
| +++ b/internal/registrar/registrartest/fake.go |
| @@ -19,6 +19,7 @@ type Fake struct { |
| mu sync.Mutex |
| registered map[string]registrar.Registered |
| + contacts map[string]registrar.Contact |
| nextContact int |
| } |
| @@ -29,6 +30,7 @@ func New() *Fake { |
| Available: make(map[string]registrar.Availability), |
| Domains: make(map[string]registrar.Registered), |
| registered: make(map[string]registrar.Registered), |
| + contacts: make(map[string]registrar.Contact), |
| } |
| } |
| @@ -52,10 +54,19 @@ func (f *Fake) EnsureContact(ctx context.Context, existingHandle string, c regis |
| return "", err |
| } |
| if existingHandle != "" { |
| - return existingHandle, nil |
| + previous, ok := f.contacts[existingHandle] |
| + if !ok { |
| + return "", fmt.Errorf("%w: unknown contact", registrar.ErrRefused) |
| + } |
| + if previous.FirstName == c.FirstName && previous.LastName == c.LastName && previous.Company == c.Company { |
| + f.contacts[existingHandle] = c |
| + return existingHandle, nil |
| + } |
| } |
| f.nextContact++ |
| - return fmt.Sprintf("contact-%d", f.nextContact), nil |
| + handle := fmt.Sprintf("contact-%d", f.nextContact) |
| + f.contacts[handle] = c |
| + return handle, nil |
| } |
| func (f *Fake) Register(ctx context.Context, r registrar.Registration) (registrar.Registered, error) { |