chore: build
ci-api / test (push) Successful in 23m50s
ci-web / test (push) Successful in 13m43s

This commit is contained in:
Xor290
2026-10-04 12:52:16 +02:00
parent 8d857faa45
commit 4bb8c143e2
29 changed files with 2256 additions and 1561 deletions
@@ -20,6 +20,8 @@ type fakeProv struct {
state demos.ResourceState
stateErr error
stateCalls []string
domainSpecs []demos.ProjectDomainSpec
domainErr error
provisioned chan struct{}
}
@@ -46,12 +48,19 @@ func (f *fakeProv) ProjectState(_ context.Context, ns string) (demos.ResourceSta
return f.state, f.stateErr
}
func (f *fakeProv) UpdateProjectDomain(s demos.ProjectDomainSpec) error {
f.mu.Lock()
defer f.mu.Unlock()
f.domainSpecs = append(f.domainSpecs, s)
return f.domainErr
}
var t0 = time.Date(2026, time.September, 20, 12, 0, 0, 0, time.UTC)
func newTestService(t *testing.T) (*Service, *fakeProv, *time.Time) {
t.Helper()
prov := newFakeProv()
svc := NewService(NewMemStore(), prov, Config{Domain: "vitrine-omnex.club"})
svc := NewService(NewMemStore(), prov, Config{Domain: "vitrine-omnex.club", ReservedDomains: []string{"omnex.sbs"}})
now := t0
svc.now = func() time.Time { return now }
return svc, prov, &now
@@ -333,3 +342,107 @@ func TestState_UnknownProjectAndClusterError(t *testing.T) {
t.Error("erreur du cluster avalée")
}
}
func TestSetDomain_SetsServedDomainAndKeepsDefaultHost(t *testing.T) {
svc, prov, _ := newTestService(t)
p := createReady(t, svc, prov, validInput())
got, err := svc.SetDomain(p.ID, " Boutique.Client.FR ")
if err != nil {
t.Fatal(err)
}
if got.CustomDomain != "boutique.client.fr" || got.URL != "https://boutique.client.fr" {
t.Errorf("domaine=%q url=%q", got.CustomDomain, got.URL)
}
if got.Host != p.Host {
t.Errorf("l'hôte par défaut a changé : %q", got.Host)
}
want := demos.ProjectDomainSpec{Namespace: p.Namespace, Host: p.Host, CustomDomain: "boutique.client.fr", URL: "https://boutique.client.fr"}
if len(prov.domainSpecs) != 1 || prov.domainSpecs[0] != want {
t.Errorf("spec envoyée au cluster : %+v", prov.domainSpecs)
}
stored, _ := svc.Get(p.ID)
if stored.CustomDomain != "boutique.client.fr" {
t.Errorf("non persisté : %+v", stored)
}
// Domaine vide : retour à l'adresse par défaut.
got, err = svc.SetDomain(p.ID, "")
if err != nil || got.CustomDomain != "" || got.URL != p.URL {
t.Errorf("retrait : err=%v domaine=%q url=%q (attendu %q)", err, got.CustomDomain, got.URL, p.URL)
}
}
func TestSetDomain_RejectsInvalidAndReservedDomains(t *testing.T) {
svc, prov, _ := newTestService(t)
p := createReady(t, svc, prov, validInput())
other := createReady(t, svc, prov, validInput())
invalid := []string{"pas un domaine", "http://x.fr", "x.fr/chemin", "localhost", "*.client.fr", "10.0.0.1", "-a.fr"}
for _, d := range invalid {
if _, err := svc.SetDomain(p.ID, d); !errors.Is(err, demos.ErrInvalidDomain) {
t.Errorf("%q : erreur %v, attendu ErrInvalidDomain", d, err)
}
}
reserved := []string{"vitrine-omnex.club", other.Host, "x.vitrine-omnex.club", "omnex.sbs", "demo-1.omnex.sbs"}
for _, d := range reserved {
if _, err := svc.SetDomain(p.ID, d); !errors.Is(err, ErrDomainReserved) {
t.Errorf("%q : erreur %v, attendu ErrDomainReserved", d, err)
}
}
// Un domaine qui finit par la même chaîne sans être un sous-domaine reste permis.
if _, err := svc.SetDomain(p.ID, "monomnex.sbs"); err != nil {
t.Errorf("monomnex.sbs refusé à tort : %v", err)
}
if len(prov.domainSpecs) != 1 {
t.Errorf("le cluster ne doit être appelé que pour le domaine valide : %d appels", len(prov.domainSpecs))
}
}
func TestSetDomain_DomainAlreadyUsedByAnotherProject(t *testing.T) {
svc, prov, _ := newTestService(t)
a := createReady(t, svc, prov, validInput())
b := createReady(t, svc, prov, validInput())
if _, err := svc.SetDomain(a.ID, "boutique.client.fr"); err != nil {
t.Fatal(err)
}
if _, err := svc.SetDomain(b.ID, "boutique.client.fr"); !errors.Is(err, ErrDomainTaken) {
t.Errorf("erreur %v, attendu ErrDomainTaken", err)
}
// Le même projet peut le redéfinir (idempotent).
if _, err := svc.SetDomain(a.ID, "boutique.client.fr"); err != nil {
t.Errorf("redéfinition par le propriétaire : %v", err)
}
// Une fois le premier projet supprimé, le domaine est libre.
if _, err := svc.Delete(a.ID); err != nil {
t.Fatal(err)
}
if _, err := svc.SetDomain(b.ID, "boutique.client.fr"); err != nil {
t.Errorf("domaine d'un projet supprimé toujours bloqué : %v", err)
}
}
func TestSetDomain_StateAndClusterErrors(t *testing.T) {
svc, prov, _ := newTestService(t)
if _, err := svc.SetDomain("inconnu", "x.client.fr"); !errors.Is(err, ErrNotFound) {
t.Errorf("erreur %v, attendu ErrNotFound", err)
}
p := createReady(t, svc, prov, validInput())
prov.domainErr = errors.New("helm indisponible")
if _, err := svc.SetDomain(p.ID, "x.client.fr"); err == nil {
t.Fatal("erreur du cluster avalée")
}
if got, _ := svc.Get(p.ID); got.CustomDomain != "" || got.URL != p.URL {
t.Errorf("échec du cluster mais projet modifié : %+v", got)
}
prov.domainErr = nil
if _, err := svc.Delete(p.ID); err != nil {
t.Fatal(err)
}
if _, err := svc.SetDomain(p.ID, "x.client.fr"); !errors.Is(err, ErrNotReady) {
t.Errorf("projet supprimé : erreur %v, attendu ErrNotReady", err)
}
}