summaryrefslogtreecommitdiff
path: root/internal/cli/profile_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/cli/profile_test.go')
-rw-r--r--internal/cli/profile_test.go125
1 files changed, 125 insertions, 0 deletions
diff --git a/internal/cli/profile_test.go b/internal/cli/profile_test.go
new file mode 100644
index 0000000..d256cb0
--- /dev/null
+++ b/internal/cli/profile_test.go
@@ -0,0 +1,125 @@
+package cli
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestProfileList(t *testing.T) {
+ tmpDir := t.TempDir()
+
+ // Create some dummy profile files
+ profiles := []string{"home.conf", "work.conf", "not-a-conf.txt"}
+ for _, p := range profiles {
+ err := os.WriteFile(filepath.Join(tmpDir, p), []byte("test content"), 0644)
+ if err != nil {
+ t.Fatalf("failed to create test profile %s: %v", p, err)
+ }
+ }
+
+ app := NewApp([]string{"wg-wrap", "profile", "list"})
+ app.ConfigDir = tmpDir
+
+ err := app.Route()
+ if err != nil {
+ t.Errorf("expected no error, got %v", err)
+ }
+}
+
+func TestProfileImport(t *testing.T) {
+ tmpDir := t.TempDir()
+ profilesDir := filepath.Join(tmpDir, "profiles")
+ err := os.MkdirAll(profilesDir, 0755)
+ if err != nil {
+ t.Fatalf("failed to create profiles dir: %v", err)
+ }
+
+ srcFile := filepath.Join(tmpDir, "source.conf")
+ err = os.WriteFile(srcFile, []byte("[Interface]\nPrivateKey = test\n"), 0644)
+ if err != nil {
+ t.Fatalf("failed to create source conf: %v", err)
+ }
+
+ app := NewApp([]string{"wg-wrap", "profile", "import", srcFile})
+ app.ConfigDir = profilesDir
+
+ err = app.Route()
+ if err != nil {
+ t.Errorf("expected no error, got %v", err)
+ }
+
+ // Verify the file was actually copied
+ destFile := filepath.Join(profilesDir, "source.conf")
+ if _, err := os.Stat(destFile); os.IsNotExist(err) {
+ t.Errorf("expected profile to be imported to %s", destFile)
+ }
+}
+
+func TestProfileDelete(t *testing.T) {
+ tmpDir := t.TempDir()
+ profilesDir := filepath.Join(tmpDir, "profiles")
+ err := os.MkdirAll(profilesDir, 0755)
+ if err != nil {
+ t.Fatalf("failed to create profiles dir: %v", err)
+ }
+
+ profileName := "test-profile"
+ profileFile := filepath.Join(profilesDir, profileName+".conf")
+ err = os.WriteFile(profileFile, []byte("[Interface]\nPrivateKey = test\n"), 0644)
+ if err != nil {
+ t.Fatalf("failed to create profile file: %v", err)
+ }
+
+ app := NewApp([]string{"wg-wrap", "profile", "delete", profileName})
+ app.ConfigDir = profilesDir
+
+ err = app.Route()
+ if err != nil {
+ t.Errorf("expected no error, got %v", err)
+ }
+
+ if _, err := os.Stat(profileFile); !os.IsNotExist(err) {
+ t.Errorf("expected profile file %s to be deleted", profileFile)
+ }
+}
+
+func TestProfileDeleteNotFound(t *testing.T) {
+ tmpDir := t.TempDir()
+ app := NewApp([]string{"wg-wrap", "profile", "delete", "non-existent"})
+ app.ConfigDir = tmpDir
+
+ err := app.Route()
+ if err == nil {
+ t.Errorf("expected error when deleting non-existent profile, got nil")
+ }
+}
+
+func TestProfileConfigure(t *testing.T) {
+ // profile configure is intended to modify existing configs.
+ // For now, we just want to ensure it doesn't crash and we can
+ // eventually implement it.
+
+ tmpDir := t.TempDir()
+ profilesDir := filepath.Join(tmpDir, "profiles")
+ err := os.MkdirAll(profilesDir, 0755)
+ if err != nil {
+ t.Fatalf("failed to create profiles dir: %v", err)
+ }
+
+ profileName := "test-profile"
+ profileFile := filepath.Join(profilesDir, profileName+".conf")
+ err = os.WriteFile(profileFile, []byte("[Interface]\nPrivateKey = test\n"), 0644)
+ if err != nil {
+ t.Fatalf("failed to create profile file: %v", err)
+ }
+
+ app := NewApp([]string{"wg-wrap", "profile", "configure", profileName})
+ app.ConfigDir = profilesDir
+
+ err = app.Route()
+ // This will currently return "not yet implemented" error, which is expected for now.
+ if err == nil {
+ t.Errorf("expected 'not yet implemented' error, got nil")
+ }
+}