Files
jcoffey-dev 628616ff6a
ci / test (pull_request) Successful in 54s
certs: order on the domain that covers the mail host (#82)
certs retried the order on the install's own domain only. The server
orders one certificate per domain, and a domain's default names include
the server's hostname only when it is inside that domain, so a mail host
on another domain was never covered: certs waited three minutes and
failed.

planOrder now picks the domain whose name is the closest suffix of the
mail host. On another domain it orders there with the install's ACME
account: adding a certificate-only domain (no DKIM keys, nothing
published) when the server has none, as the console's Order a
certificate does, or adding the host to names entered in the console. A
domain on the default names whose server hostname is still another name
gets the place to change it instead of a three-minute wait. certs also
stops early when the managed Caddy serves another mail host, since the
challenge can't reach the server then.

RetryCertificates no longer resets a domain's names to the default set
on every retry: names entered in the console are kept.
2026-11-18 09:34:11 -07:00

64 lines
3.3 KiB
Go

// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
// SPDX-License-Identifier: AGPL-3.0-or-later
package apply
import (
"reflect"
"strings"
"testing"
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/jmap"
)
func auto(id, name string, names ...string) jmap.DomainCert {
return jmap.DomainCert{ID: id, Name: name, Kind: "Automatic", ProviderID: "acme1", Names: names}
}
// #82: the domain that orders a certificate naming the mail host.
func TestPlanOrder(t *testing.T) {
install := auto("d1", "example.com")
cases := []struct {
name string
domains []jmap.DomainCert
host, srv string
want orderPlan
wantErrHas string
}{
{"same domain, default names, server already called that", []jmap.DomainCert{install}, "mail.example.com", "mail.example.com",
orderPlan{Action: "retry", DomainID: "d1", DomainName: "example.com"}, ""},
{"same domain, default names, server still has its old name", []jmap.DomainCert{install}, "mx.example.com", "mail.example.com",
orderPlan{}, "set the hostname to mx.example.com"},
{"another domain the server doesn't have: added for certificates", []jmap.DomainCert{install}, "mail.example.net", "",
orderPlan{Action: "add", DomainName: "example.net", ProviderID: "acme1", Names: []string{"mail.example.net"}}, ""},
{"another domain with manual certificates: the install's account, just that name",
[]jmap.DomainCert{install, {ID: "d2", Name: "example.net", Kind: "Manual"}}, "mail.example.net", "",
orderPlan{Action: "set", DomainID: "d2", DomainName: "example.net", ProviderID: "acme1", Names: []string{"mail.example.net"}}, ""},
{"names entered in the console are kept, and the host added",
[]jmap.DomainCert{install, auto("d2", "example.net", "autoconfig.example.net")}, "mail.example.net", "",
orderPlan{Action: "set", DomainID: "d2", DomainName: "example.net", ProviderID: "acme1", Names: []string{"autoconfig.example.net", "mail.example.net"}}, ""},
{"names that already include it: just order again",
[]jmap.DomainCert{install, auto("d2", "example.net", "mail.example.net")}, "MAIL.example.net.", "",
orderPlan{Action: "retry", DomainID: "d2", DomainName: "example.net"}, ""},
{"the closest domain wins", []jmap.DomainCert{install, auto("d2", "example.net"), {ID: "d3", Name: "eu.example.net", Kind: "Manual"}},
"mail.eu.example.net", "", orderPlan{Action: "set", DomainID: "d3", DomainName: "eu.example.net", ProviderID: "acme1", Names: []string{"mail.eu.example.net"}}, ""},
{"install domain on manual certificates: turn ACME on, as before", []jmap.DomainCert{{ID: "d1", Name: "example.com", Kind: "Manual"}},
"mail.example.com", "", orderPlan{Action: "enable", DomainID: "d1", DomainName: "example.com"}, ""},
{"another domain with no ACME account anywhere", []jmap.DomainCert{{ID: "d1", Name: "example.com", Kind: "Manual"}},
"mail.example.net", "", orderPlan{}, "no ACME account"},
{"install domain missing", nil, "mail.example.com", "", orderPlan{}, "does not exist"},
}
for _, c := range cases {
got, err := planOrder(c.domains, "example.com", c.host, c.srv)
if c.wantErrHas != "" {
if err == nil || !strings.Contains(err.Error(), c.wantErrHas) {
t.Errorf("%s: err = %v, want one saying %q", c.name, err, c.wantErrHas)
}
continue
}
if err != nil || !reflect.DeepEqual(got, c.want) {
t.Errorf("%s:\n got %+v, %v\nwant %+v", c.name, got, err, c.want)
}
}
}