Skip to content

Commit

Permalink
WIP: setup required config in WithManager helper
Browse files Browse the repository at this point in the history
  • Loading branch information
taratatach committed Nov 26, 2024
1 parent b4347c6 commit d849f1f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 34 deletions.
5 changes: 0 additions & 5 deletions model/instance/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
4 changes: 4 additions & 0 deletions model/instance/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ const (
func (i *Instance) ManagerURL(k ManagerURLKind) (string, error) {
c := clouderyConfig(i)
if c == nil {
fmt.Printf("NO CLOUDERY CONFIG")

Check failure on line 31 in model/instance/manager.go

View workflow job for this annotation

GitHub Actions / lint

use of `fmt.Printf` forbidden by pattern `fmt.Printf` (forbidigo)
return "", nil
}

if i.UUID == "" {
fmt.Printf("NO INSTANCE UUID")

Check failure on line 36 in model/instance/manager.go

View workflow job for this annotation

GitHub Actions / lint

use of `fmt.Printf` forbidden by pattern `fmt.Printf` (forbidigo)
return "", nil
}

base := c.API.URL
if base == "" {
fmt.Printf("EMPTY API URL")

Check failure on line 42 in model/instance/manager.go

View workflow job for this annotation

GitHub Actions / lint

use of `fmt.Printf` forbidden by pattern `fmt.Printf` (forbidigo)
return "", nil
}

baseURL, err := url.Parse(base)
if err != nil {
fmt.Printf("INVALID API URL")
return "", err
}

Expand Down
8 changes: 1 addition & 7 deletions model/oauth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand Down
44 changes: 34 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,34 +470,57 @@ 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")
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 = ""
require.NoError(t, instance.Update(inst))
}
})

err = instance.Update(inst)
err := instance.Update(inst)
require.NoError(t, err, "Could not enable test instance manager")

return shouldRemoveUUID
Expand Down
7 changes: 1 addition & 6 deletions web/apps/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }()
Expand Down Expand Up @@ -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", WithPremiumLinks: true})

tosSigned := testInstance.TOSSigned
tosLatest := testInstance.TOSLatest
Expand Down
9 changes: 3 additions & 6 deletions web/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down Expand Up @@ -116,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 @@ -1025,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 d849f1f

Please sign in to comment.