Skip to content

Commit

Permalink
WIP: test: WithManager sets up config
Browse files Browse the repository at this point in the history
  • Loading branch information
taratatach committed Nov 26, 2024
1 parent 1fd2613 commit ae0b118
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
4 changes: 1 addition & 3 deletions model/oauth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
42 changes: 32 additions & 10 deletions tests/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutils
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -469,26 +470,34 @@ 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")
inst.UUID = uuid.String()
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 = ""
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions web/apps/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }()
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions web/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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").
Expand Down

0 comments on commit ae0b118

Please sign in to comment.