Some checks failed
FreeBSD / Client / Unit (push) Has been cancelled
Linux / Build Cache (push) Has been cancelled
Linux / Client / Unit (386) (push) Has been cancelled
Linux / Client / Unit (amd64) (push) Has been cancelled
Darwin / Client / Unit (push) Has been cancelled
Linux / Client (Docker) / Unit (push) Has been cancelled
Linux / Relay / Unit (386, ) (push) Has been cancelled
Linux / Relay / Unit (amd64, -race) (push) Has been cancelled
Linux / Proxy / Unit (386) (push) Has been cancelled
Linux / Proxy / Unit (amd64) (push) Has been cancelled
Linux / Signal / Unit (386) (push) Has been cancelled
Linux / Signal / Unit (amd64) (push) Has been cancelled
Linux / Management / Unit (amd64, mysql) (push) Has been cancelled
Linux / Management / Unit (amd64, postgres) (push) Has been cancelled
Test installation / test-install-script (false, macos-latest, false) (push) Has been cancelled
Linux / Management / Unit (amd64, sqlite) (push) Has been cancelled
Linux / Management / Benchmark (amd64, postgres) (push) Has been cancelled
Linux / Management / Benchmark (amd64, sqlite) (push) Has been cancelled
Linux / Management / Benchmark (API) (amd64, postgres) (push) Has been cancelled
Linux / Management / Benchmark (API) (amd64, sqlite) (push) Has been cancelled
Linux / Management / Integration (amd64, postgres) (push) Has been cancelled
Linux / Management / Integration (amd64, sqlite) (push) Has been cancelled
Windows / Client / Unit (push) Has been cancelled
Mobile / Android / Build (push) Has been cancelled
Mobile / iOS / Build (push) Has been cancelled
Release / release_ui_darwin (push) Has been cancelled
Release / Windows Installer / Build Test (amd64, amd64) (push) Has been cancelled
Test installation / test-install-script (false, macos-latest, true) (push) Has been cancelled
Test installation / test-install-script (false, ubuntu-latest, false) (push) Has been cancelled
Test installation / test-install-script (false, ubuntu-latest, true) (push) Has been cancelled
Test installation / test-install-script (true, macos-latest, false) (push) Has been cancelled
Test installation / test-install-script (true, macos-latest, true) (push) Has been cancelled
Release / Windows Installer / Build Test (arm64, arm64) (push) Has been cancelled
Release / Comment release artifacts (push) Has been cancelled
Test installation / test-install-script (true, ubuntu-latest, false) (push) Has been cancelled
Test installation / test-install-script (true, ubuntu-latest, true) (push) Has been cancelled
Release / FreeBSD Port / Build & Test (push) Has been cancelled
Release / release (push) Has been cancelled
Release / release_ui (push) Has been cancelled
Test Infrastructure files / test-docker-compose (mysql) (push) Has been cancelled
Test Infrastructure files / test-docker-compose (postgres) (push) Has been cancelled
Test Infrastructure files / test-docker-compose (sqlite) (push) Has been cancelled
Test Infrastructure files / test-getting-started-script (push) Has been cancelled
Release / trigger_signer (push) Has been cancelled
sync main / trigger_sync_main (push) Has been cancelled
Wasm / JS / Lint (push) Has been cancelled
Wasm / JS / Build (push) Has been cancelled
* Migrate to profile ids * Migrate android profile manager * Clean up * Fix review * Add ID type * Fix test and runes in ShortID() * Fix profile switch on up and android comments * Revert android profile to string id * Fix feedback * Fix UI feedback * Fix id assignment * Add renaming of profiles * Fix review * Remove ui binary * Fix getProfileConfigPath not validating id * Change resolve handle order and fix server merge problems * Fix mdm test
152 lines
4.2 KiB
Go
152 lines
4.2 KiB
Go
package profilemanager
|
|
|
|
import (
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func withTempConfigDir(t *testing.T, testFunc func(configDir string)) {
|
|
t.Helper()
|
|
tempDir := t.TempDir()
|
|
t.Setenv("NETBIRD_CONFIG_DIR", tempDir)
|
|
defer os.Unsetenv("NETBIRD_CONFIG_DIR")
|
|
testFunc(tempDir)
|
|
}
|
|
|
|
func withPatchedGlobals(t *testing.T, configDir string, testFunc func()) {
|
|
origDefaultConfigPathDir := DefaultConfigPathDir
|
|
origDefaultConfigPath := DefaultConfigPath
|
|
origActiveProfileStatePath := ActiveProfileStatePath
|
|
origOldDefaultConfigPath := oldDefaultConfigPath
|
|
origConfigDirOverride := ConfigDirOverride
|
|
DefaultConfigPathDir = configDir
|
|
DefaultConfigPath = filepath.Join(configDir, "default.json")
|
|
ActiveProfileStatePath = filepath.Join(configDir, "active_profile.json")
|
|
oldDefaultConfigPath = filepath.Join(configDir, "old_config.json")
|
|
ConfigDirOverride = configDir
|
|
// Clean up any files in the config dir to ensure isolation
|
|
os.RemoveAll(configDir)
|
|
os.MkdirAll(configDir, 0755) //nolint: errcheck
|
|
defer func() {
|
|
DefaultConfigPathDir = origDefaultConfigPathDir
|
|
DefaultConfigPath = origDefaultConfigPath
|
|
ActiveProfileStatePath = origActiveProfileStatePath
|
|
oldDefaultConfigPath = origOldDefaultConfigPath
|
|
ConfigDirOverride = origConfigDirOverride
|
|
}()
|
|
testFunc()
|
|
}
|
|
|
|
func TestServiceManager_CreateAndGetDefaultProfile(t *testing.T) {
|
|
withTempConfigDir(t, func(configDir string) {
|
|
withPatchedGlobals(t, configDir, func() {
|
|
sm := &ServiceManager{}
|
|
err := sm.CreateDefaultProfile()
|
|
assert.NoError(t, err)
|
|
|
|
state, err := sm.GetActiveProfileState()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, defaultProfileName, state.ID.String()) // No active profile state yet
|
|
|
|
err = sm.SetActiveProfileStateToDefault()
|
|
assert.NoError(t, err)
|
|
|
|
active, err := sm.GetActiveProfileState()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "default", active.ID.String())
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestServiceManager_CopyDefaultProfileIfNotExists(t *testing.T) {
|
|
withTempConfigDir(t, func(configDir string) {
|
|
withPatchedGlobals(t, configDir, func() {
|
|
sm := &ServiceManager{}
|
|
|
|
// Case: old default config does not exist
|
|
ok, err := sm.CopyDefaultProfileIfNotExists()
|
|
assert.False(t, ok)
|
|
assert.ErrorIs(t, err, ErrorOldDefaultConfigNotFound)
|
|
|
|
// Case: old default config exists, should be moved
|
|
f, err := os.Create(oldDefaultConfigPath)
|
|
assert.NoError(t, err)
|
|
f.Close()
|
|
|
|
ok, err = sm.CopyDefaultProfileIfNotExists()
|
|
assert.True(t, ok)
|
|
assert.NoError(t, err)
|
|
_, err = os.Stat(DefaultConfigPath)
|
|
assert.NoError(t, err)
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestServiceManager_SetActiveProfileState(t *testing.T) {
|
|
withTempConfigDir(t, func(configDir string) {
|
|
withPatchedGlobals(t, configDir, func() {
|
|
currUser, err := user.Current()
|
|
assert.NoError(t, err)
|
|
sm := &ServiceManager{}
|
|
state := &ActiveProfileState{ID: "foo", Username: currUser.Username}
|
|
err = sm.SetActiveProfileState(state)
|
|
assert.NoError(t, err)
|
|
|
|
// Should error on nil or incomplete state
|
|
err = sm.SetActiveProfileState(nil)
|
|
assert.Error(t, err)
|
|
err = sm.SetActiveProfileState(&ActiveProfileState{ID: "", Username: ""})
|
|
assert.Error(t, err)
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestServiceManager_DefaultProfilePath(t *testing.T) {
|
|
withTempConfigDir(t, func(configDir string) {
|
|
withPatchedGlobals(t, configDir, func() {
|
|
sm := &ServiceManager{}
|
|
assert.Equal(t, DefaultConfigPath, sm.DefaultProfilePath())
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestSanitizeProfileName(t *testing.T) {
|
|
tests := []struct {
|
|
in, want string
|
|
}{
|
|
// unchanged
|
|
{"Alice", "Alice"},
|
|
{"bob123", "bob123"},
|
|
{"under_score", "under_score"},
|
|
{"dash-name", "dash-name"},
|
|
|
|
// spaces and forbidden chars removed
|
|
{"Alice Smith", "AliceSmith"},
|
|
{"bad/char\\name", "badcharname"},
|
|
{"colon:name*?", "colonname"},
|
|
{"quotes\"<>|", "quotes"},
|
|
|
|
// mixed
|
|
{"User_123-Test!@#", "User_123-Test"},
|
|
|
|
// empty and all-bad
|
|
{"", ""},
|
|
{"!@#$%^&*()", ""},
|
|
|
|
// unicode letters and digits
|
|
{"ÜserÇ", "ÜserÇ"},
|
|
{"漢字テスト123", "漢字テスト123"},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
got := sanitizeProfileName(tc.in)
|
|
if got != tc.want {
|
|
t.Errorf("sanitizeProfileName(%q) = %q; want %q", tc.in, got, tc.want)
|
|
}
|
|
}
|
|
}
|