160 lines
6.1 KiB
Go
160 lines
6.1 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// onPremEnv is a complete on-prem values set (test names only).
|
|
func onPremEnv(t *testing.T) {
|
|
t.Helper()
|
|
for k, v := range map[string]string{
|
|
"APP_ENV": "onprem",
|
|
"KEYCLOAK_ISSUER": "https://auth.onprem.test/realms/breakpilot",
|
|
"KEYCLOAK_REALM": "breakpilot",
|
|
"KEYCLOAK_ADMIN_URL": "https://auth.onprem.test",
|
|
"KEYCLOAK_CLIENT_ID": "tenant-registry",
|
|
"KEYCLOAK_CLIENT_SECRET": "x",
|
|
"DATABASE_URL": "postgres://registry@postgres/registry",
|
|
"AUTH_ENABLED": "true",
|
|
"AUTHZ_RULES": "portal=*",
|
|
"LICENCE_SIGNING_KEY": "x",
|
|
"PORTAL_URL": "https://app.onprem.test",
|
|
"INVITE_REDIRECT_URI": "https://app.onprem.test/onboard",
|
|
"INVITE_CLIENT_ID": "portal",
|
|
"MAIL_FROM": "breakpilot@onprem.test",
|
|
"PRODUCTS": "pruefwerk,werkpilot",
|
|
"PRODUCT_LAUNCH_URLS": "pruefwerk=https://pruefwerk.onprem.test,werkpilot=https://docs.onprem.test/werkpilot",
|
|
"PRODUCT_PROVISION_URLS": "pruefwerk=https://pruefwerk.onprem.test/api/v1/platform/tenants/{id}/provision",
|
|
"SMTP_HOST": "mail.onprem.test",
|
|
"SMTP_PORT": "25",
|
|
"SMTP_TLS": "none",
|
|
} {
|
|
t.Setenv(k, v)
|
|
}
|
|
for _, k := range []string{"ERP_BASE_URL", "ERP_WEBHOOK_SECRET", "CONTRACT_DOCUMENTS", "LITELLM_MASTER_KEY", "LITELLM_BASE_URL", "SMTP_USER",
|
|
"LEGAL_IMPRINT_URL", "LEGAL_PRIVACY_URL", "MAIL_CONTACT", "MAIL_FROM_NAME"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
}
|
|
|
|
func TestLoad_onPremComplete(t *testing.T) {
|
|
onPremEnv(t)
|
|
c, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !c.OnPrem() || c.CatalogEnv() != "onprem" {
|
|
t.Errorf("OnPrem=%v CatalogEnv=%q", c.OnPrem(), c.CatalogEnv())
|
|
}
|
|
if c.LegalImprintURL != "" || c.LegalPrivacyURL != "" || c.MailContact != "" || c.MailFromName != "Breakpilot" {
|
|
t.Errorf("mail site: %q %q %q %q", c.LegalImprintURL, c.LegalPrivacyURL, c.MailContact, c.MailFromName)
|
|
}
|
|
if c.SMTPTLS != "none" || c.SMTPPort != 25 {
|
|
t.Errorf("smtp = %s:%d", c.SMTPTLS, c.SMTPPort)
|
|
}
|
|
if c.ProvisionURLs["pruefwerk"] == "" || c.LaunchURLs["werkpilot"] == "" || len(c.Products) != 2 {
|
|
t.Errorf("products %v launch %v provision %v", c.Products, c.LaunchURLs, c.ProvisionURLs)
|
|
}
|
|
}
|
|
|
|
func TestLoad_onPremRefusesCloudDefaults(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name, key, value, want string
|
|
}{
|
|
{"no portal URL", "PORTAL_URL", "", "PORTAL_URL"},
|
|
{"no invite redirect", "INVITE_REDIRECT_URI", "", "INVITE_REDIRECT_URI"},
|
|
{"no sender", "MAIL_FROM", "", "MAIL_FROM"},
|
|
{"no licence key", "LICENCE_SIGNING_KEY", "", "LICENCE_SIGNING_KEY"},
|
|
{"no authz rules", "AUTHZ_RULES", "", "AUTHZ_RULES"},
|
|
{"no product list", "PRODUCTS", "", "PRODUCTS"},
|
|
{"no database", "DATABASE_URL", "", "DATABASE_URL"},
|
|
{"auth off", "AUTH_ENABLED", "false", "AUTH_ENABLED"},
|
|
{"an ERP", "ERP_BASE_URL", "https://erp.onprem.test", "no ERP"},
|
|
{"contract documents", "CONTRACT_DOCUMENTS", "on", "contract documents"},
|
|
{"LLM key without gateway", "LITELLM_MASTER_KEY", "x", "LITELLM_BASE_URL"},
|
|
{"product without tile", "PRODUCTS", "pruefwerk,firmwerk", `"firmwerk"`},
|
|
{"our sender", "MAIL_FROM", "noreply@breakpilot.com", "MAIL_FROM points at breakpilot.com"},
|
|
{"our portal", "PORTAL_URL", "https://app.breakpilot.com", "PORTAL_URL points"},
|
|
{"our imprint", "LEGAL_IMPRINT_URL", "https://Breakpilot.com/impressum", "LEGAL_IMPRINT_URL points"},
|
|
{"our privacy notice", "LEGAL_PRIVACY_URL", "https://app.breakpilot.com/datenschutz", "LEGAL_PRIVACY_URL points"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
onPremEnv(t)
|
|
t.Setenv(c.key, c.value)
|
|
_, err := Load()
|
|
if err == nil || !strings.Contains(err.Error(), c.want) {
|
|
t.Errorf("err = %v, want one naming %s", err, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoad_productURLsAreChecked(t *testing.T) {
|
|
for _, c := range []struct{ key, value, want string }{
|
|
{"PRODUCT_LAUNCH_URLS", "pruefwerk", "not key=url"},
|
|
{"PRODUCT_LAUNCH_URLS", "pruefwerk=pruefwerk.onprem.test", "absolute"},
|
|
{"PRODUCT_LAUNCH_URLS", "pruefwerk=https://a.onprem.test,pruefwerk=https://b.onprem.test", "twice"},
|
|
{"PRODUCT_PROVISION_URLS", "pruefwerk=https://pruefwerk.onprem.test/provision", "{id}"},
|
|
} {
|
|
t.Run(c.value, func(t *testing.T) {
|
|
t.Setenv("APP_ENV", "stage")
|
|
t.Setenv("PRODUCT_LAUNCH_URLS", "")
|
|
t.Setenv("PRODUCT_PROVISION_URLS", "")
|
|
t.Setenv(c.key, c.value)
|
|
_, err := Load()
|
|
if err == nil || !strings.Contains(err.Error(), c.want) {
|
|
t.Errorf("err = %v, want %q", err, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The cloud binary is the same: without the new variables nothing changes.
|
|
func TestLoad_cloudUnchangedWithoutTheNewVariables(t *testing.T) {
|
|
for _, env := range []string{"stage", "prod"} {
|
|
t.Run(env, func(t *testing.T) {
|
|
t.Setenv("APP_ENV", env)
|
|
for _, k := range []string{"PRODUCTS", "PRODUCT_LAUNCH_URLS", "PRODUCT_PROVISION_URLS", "SMTP_TLS", "SMTP_PORT", "SMTP_USER",
|
|
"PORTAL_URL", "MAIL_FROM", "INVITE_REDIRECT_URI", "LITELLM_BASE_URL", "AUTH_ENABLED", "LICENCE_SIGNING_KEY"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
c, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.OnPrem() || c.CatalogEnv() != env {
|
|
t.Errorf("OnPrem=%v CatalogEnv=%q", c.OnPrem(), c.CatalogEnv())
|
|
}
|
|
if c.Products != nil || len(c.LaunchURLs) != 0 || len(c.ProvisionURLs) != 0 {
|
|
t.Errorf("overrides set: %v %v %v", c.Products, c.LaunchURLs, c.ProvisionURLs)
|
|
}
|
|
if c.SMTPTLS != "starttls" || c.SMTPPort != 587 {
|
|
t.Errorf("smtp = %s:%d", c.SMTPTLS, c.SMTPPort)
|
|
}
|
|
if c.PortalURL != "https://app.breakpilot.com" || c.MailFrom != "noreply@breakpilot.com" {
|
|
t.Errorf("cloud defaults changed: %s %s", c.PortalURL, c.MailFrom)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoad_smtpTLS(t *testing.T) {
|
|
t.Setenv("APP_ENV", "stage")
|
|
t.Setenv("SMTP_USER", "")
|
|
t.Setenv("SMTP_TLS", "")
|
|
t.Setenv("SMTP_PORT", "465")
|
|
if c, err := Load(); err != nil || c.SMTPTLS != "tls" {
|
|
t.Errorf("port 465: %v %v", c, err)
|
|
}
|
|
t.Setenv("SMTP_TLS", "ssl")
|
|
if _, err := Load(); err == nil {
|
|
t.Error("SMTP_TLS=ssl accepted")
|
|
}
|
|
t.Setenv("SMTP_TLS", "none")
|
|
t.Setenv("SMTP_USER", "relay")
|
|
if _, err := Load(); err == nil || !strings.Contains(err.Error(), "unencrypted") {
|
|
t.Errorf("none + user: %v", err)
|
|
}
|
|
}
|