From ae0b1186b64960b678e67e0b0fdf5e05ef1e971e Mon Sep 17 00:00:00 2001 From: Erwan Guyader Date: Tue, 26 Nov 2024 17:12:05 +0100 Subject: [PATCH] WIP: test: WithManager sets up config --- model/oauth/client_test.go | 4 +--- pkg/config/config/config.go | 1 + tests/testutils/test_utils.go | 42 ++++++++++++++++++++++++++--------- web/apps/apps_test.go | 3 +-- web/settings/settings_test.go | 5 +++-- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/model/oauth/client_test.go b/model/oauth/client_test.go index e3d7cbb6f7a..952d9d0c4a3 100644 --- a/model/oauth/client_test.go +++ b/model/oauth/client_test.go @@ -32,8 +32,6 @@ func TestClient(t *testing.T) { } config.UseTestFile(t) - conf := config.GetConfig() - conf.Contexts[config.DefaultInstanceContext] = map[string]interface{}{"manager_url": "http://manager.example.org"} setup := testutils.NewSetup(t, t.Name()) testInstance := setup.GetTestInstance() @@ -197,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/pkg/config/config/config.go b/pkg/config/config/config.go index 8199fb1c122..0b9e31f412b 100644 --- a/pkg/config/config/config.go +++ b/pkg/config/config/config.go @@ -1150,6 +1150,7 @@ func createTestViper() *viper.Viper { v.SetDefault("log.level", "info") v.SetDefault("assets_polling_disabled", false) v.SetDefault("assets_polling_interval", 2*time.Minute) + v.SetDefault("contexts", map[string]interface{}{DefaultInstanceContext: map[string]interface{}{}}) applyDefaults(v) return v } diff --git a/tests/testutils/test_utils.go b/tests/testutils/test_utils.go index 66ff3f5b274..34ca8db5f98 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,19 @@ 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") - - managerURL, ok := config["manager_url"].(string) - require.True(t, ok, "Could not enable test instance manager: manager_url config is required") - require.NotEmpty(t, managerURL, "Could not enable test instance manager: manager_url config is required") + cfg := config.GetConfig() + originalCfg, _ := json.Marshal(cfg) - was := config["enable_premium_links"] - config["enable_premium_links"] = true + if cfg.Contexts == nil { + cfg.Contexts = map[string]interface{}{} + } + context, contextName := getContext(inst, cfg) + context["manager_url"] = managerConfig.URL + context["enable_premium_links"] = managerConfig.WithPremiumLinks + cfg.Contexts[contextName] = context t.Cleanup(func() { - config["enable_premium_links"] = was + json.Unmarshal(originalCfg, cfg) if shouldRemoveUUID { inst.UUID = "" @@ -502,6 +511,19 @@ func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool) return shouldRemoveUUID } +// getContext returns the most relevant context config depending on the +// instance context name or creates a new one if none exist. +// It also returns the name of the context associated with the config. +func getContext(inst *instance.Instance, cfg *config.Config) (map[string]interface{}, string) { + if context, ok := cfg.Contexts[inst.ContextName].(map[string]interface{}); ok { + return context, inst.ContextName + } + if context, ok := cfg.Contexts[config.DefaultInstanceContext].(map[string]interface{}); ok { + return context, config.DefaultInstanceContext + } + return map[string]interface{}{}, config.DefaultInstanceContext +} + func DisableManager(inst *instance.Instance, shouldRemoveUUID bool) error { config, ok := inst.SettingsContext() if !ok { diff --git a/web/apps/apps_test.go b/web/apps/apps_test.go index c8b02d83dd8..e85523cea5b 100644 --- a/web/apps/apps_test.go +++ b/web/apps/apps_test.go @@ -66,7 +66,6 @@ func TestApps(t *testing.T) { Host: "localhost", Path: tempdir, } - cfg.Contexts[config.DefaultInstanceContext] = map[string]interface{}{"manager_url": "http://manager.example.org"} was := cfg.Subdomains cfg.Subdomains = config.NestedSubdomains defer func() { cfg.Subdomains = was }() @@ -205,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", WithPremiumLinks: true}) tosSigned := testInstance.TOSSigned tosLatest := testInstance.TOSLatest diff --git a/web/settings/settings_test.go b/web/settings/settings_test.go index b476a39dff7..50b2dd5d374 100644 --- a/web/settings/settings_test.go +++ b/web/settings/settings_test.go @@ -74,7 +74,6 @@ func TestSettings(t *testing.T) { conf := config.GetConfig() conf.Assets = "../../assets" conf.Contexts[config.DefaultInstanceContext] = map[string]interface{}{ - "manager_url": "http://manager.example.org", "logos": map[string]interface{}{ "home": map[string]interface{}{ "light": []interface{}{ @@ -112,6 +111,8 @@ func TestSettings(t *testing.T) { t.Run("GetContext", func(t *testing.T) { e := testutils.CreateTestClient(t, tsURL) + testutils.WithManager(t, testInstance, testutils.ManagerConfig{URL: "http://manager.example.org"}) + obj := e.GET("/settings/context"). WithCookie(sessCookie, "connected"). WithHeader("Accept", "application/vnd.api+json"). @@ -1021,7 +1022,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").