From bbeb92f6182cc5da64696dbd9a1bc963f2e019c7 Mon Sep 17 00:00:00 2001 From: Erwan Guyader Date: Thu, 21 Nov 2024 17:56:42 +0100 Subject: [PATCH] WIP: setup required config in WithManager helper --- model/instance/instance_test.go | 5 ---- model/instance/manager.go | 4 +++ model/oauth/client_test.go | 8 +----- tests/testutils/test_utils.go | 44 +++++++++++++++++++++++++-------- web/apps/apps_test.go | 7 +----- web/settings/settings_test.go | 7 +----- 6 files changed, 41 insertions(+), 34 deletions(-) diff --git a/model/instance/instance_test.go b/model/instance/instance_test.go index 0b455b79d6a..d2c8837c56c 100644 --- a/model/instance/instance_test.go +++ b/model/instance/instance_test.go @@ -59,11 +59,6 @@ func TestInstance(t *testing.T) { was := cfg.Contexts defer func() { cfg.Contexts = was }() - cfg.Clouderies = map[string]config.ClouderyConfig{ - "context": { - API: config.ClouderyAPI{URL: "http://manager.example.org", Token: ""}, - }, - } cfg.Contexts = map[string]interface{}{ "context": map[string]interface{}{ "logos": map[string]interface{}{ diff --git a/model/instance/manager.go b/model/instance/manager.go index 61bdc61054f..f19445b0dec 100644 --- a/model/instance/manager.go +++ b/model/instance/manager.go @@ -28,20 +28,24 @@ const ( func (i *Instance) ManagerURL(k ManagerURLKind) (string, error) { c := clouderyConfig(i) if c == nil { + fmt.Printf("NO CLOUDERY CONFIG") return "", nil } if i.UUID == "" { + fmt.Printf("NO INSTANCE UUID") return "", nil } base := c.API.URL if base == "" { + fmt.Printf("EMPTY API URL") return "", nil } baseURL, err := url.Parse(base) if err != nil { + fmt.Printf("INVALID API URL") return "", err } diff --git a/model/oauth/client_test.go b/model/oauth/client_test.go index 18355329fc2..952d9d0c4a3 100644 --- a/model/oauth/client_test.go +++ b/model/oauth/client_test.go @@ -32,12 +32,6 @@ func TestClient(t *testing.T) { } config.UseTestFile(t) - conf := config.GetConfig() - conf.Clouderies = map[string]config.ClouderyConfig{ - config.DefaultInstanceContext: { - API: config.ClouderyAPI{URL: "http://manager.example.org", Token: ""}, - }, - } setup := testutils.NewSetup(t, t.Name()) testInstance := setup.GetTestInstance() @@ -201,7 +195,7 @@ func TestClient(t *testing.T) { premiumLink := assertClientsLimitAlertMailWasSent(t, testInstance, "notificationWithoutPremium", 1) assert.Empty(t, premiumLink) - testutils.WithManager(t, testInstance) + testutils.WithManager(t, testInstance, testutils.ManagerConfig{URL: "http://manager.example.org", WithPremiumLinks: true}) notificationWithPremium = &oauth.Client{ ClientName: "notificationWithPremium", diff --git a/tests/testutils/test_utils.go b/tests/testutils/test_utils.go index 16f9b31712a..49d5ea87735 100644 --- a/tests/testutils/test_utils.go +++ b/tests/testutils/test_utils.go @@ -3,6 +3,7 @@ package testutils import ( "bytes" "context" + "encoding/json" "errors" "flag" "fmt" @@ -469,7 +470,14 @@ func compress(content string) []byte { return buf.Bytes() } -func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool) { +type ManagerConfig struct { + URL string + WithPremiumLinks bool +} + +func WithManager(t *testing.T, inst *instance.Instance, managerConfig ManagerConfig) (shouldRemoveUUID bool) { + require.NotEmpty(t, managerConfig.URL, "Could not enable test instance manager: cloudery API URL is required") + if inst.UUID == "" { uuid, err := uuid.NewV7() require.NoError(t, err, "Could not enable test instance manager") @@ -477,18 +485,34 @@ func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool) shouldRemoveUUID = true } - config, ok := inst.SettingsContext() - require.True(t, ok, "Could not enable test instance manager: could not fetch test instance settings context") + contextName := inst.ContextName + if contextName == "" { + contextName = config.DefaultInstanceContext + } - managerURL, err := inst.ManagerURL(instance.ManagerBaseURL) - require.NoError(t, err, "Could not enable test instance manager: cloudery config is required") - require.NotEmpty(t, managerURL, "Could not enable test instance manager: cloudery API URL is required") + cfg := config.GetConfig() + originalCfg, _ := json.Marshal(cfg) - was := config["enable_premium_links"] - config["enable_premium_links"] = true + if cfg.Clouderies == nil { + cfg.Clouderies = map[string]config.ClouderyConfig{} + } + if _, ok := cfg.Clouderies[contextName]; !ok { + cfg.Clouderies[contextName] = config.ClouderyConfig{} + } + cloudery := cfg.Clouderies[contextName] + cloudery.API = config.ClouderyAPI{URL: managerConfig.URL, Token: ""} + + if cfg.Contexts == nil { + cfg.Contexts = map[string]interface{}{} + } + if _, ok := cfg.Contexts[contextName]; !ok { + cfg.Contexts[contextName] = map[string]interface{}{} + } + context := cfg.Contexts[contextName].(map[string]interface{}) + context["enable_premium_links"] = managerConfig.WithPremiumLinks t.Cleanup(func() { - config["enable_premium_links"] = was + json.Unmarshal(originalCfg, cfg) if shouldRemoveUUID { inst.UUID = "" @@ -496,7 +520,7 @@ func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool) } }) - err = instance.Update(inst) + err := instance.Update(inst) require.NoError(t, err, "Could not enable test instance manager") return shouldRemoveUUID diff --git a/web/apps/apps_test.go b/web/apps/apps_test.go index 80dce3cf5e7..17c9f7b10e0 100644 --- a/web/apps/apps_test.go +++ b/web/apps/apps_test.go @@ -66,11 +66,6 @@ func TestApps(t *testing.T) { Host: "localhost", Path: tempdir, } - cfg.Clouderies = map[string]config.ClouderyConfig{ - config.DefaultInstanceContext: { - API: config.ClouderyAPI{URL: "http://manager.example.org", Token: ""}, - }, - } was := cfg.Subdomains cfg.Subdomains = config.NestedSubdomains defer func() { cfg.Subdomains = was }() @@ -209,7 +204,7 @@ func TestApps(t *testing.T) { // TOS not signed warning - testutils.WithManager(t, testInstance) + testutils.WithManager(t, testInstance, testutils.ManagerConfig{URL: "http://manager.example.org"}) tosSigned := testInstance.TOSSigned tosLatest := testInstance.TOSLatest diff --git a/web/settings/settings_test.go b/web/settings/settings_test.go index f526dc9b595..2dce2686385 100644 --- a/web/settings/settings_test.go +++ b/web/settings/settings_test.go @@ -73,11 +73,6 @@ func TestSettings(t *testing.T) { config.UseTestFile(t) conf := config.GetConfig() conf.Assets = "../../assets" - conf.Clouderies = map[string]config.ClouderyConfig{ - config.DefaultInstanceContext: { - API: config.ClouderyAPI{URL: "http://manager.example.org", Token: ""}, - }, - } conf.Contexts[config.DefaultInstanceContext] = map[string]interface{}{ "logos": map[string]interface{}{ "home": map[string]interface{}{ @@ -1025,7 +1020,7 @@ func TestSettings(t *testing.T) { Contains("/#/connectedDevices"). NotContains("http://manager.example.org") - testutils.WithManager(t, testInstance) + testutils.WithManager(t, testInstance, testutils.ManagerConfig{URL: "http://manager.example.org", WithPremiumLinks: true}) e.GET("/settings/clients/limit-exceeded"). WithCookie(sessCookie, "connected").