diff --git a/internal/commands/auth_list_test.go b/internal/commands/auth_list_test.go new file mode 100644 index 000000000..904bb8c6b --- /dev/null +++ b/internal/commands/auth_list_test.go @@ -0,0 +1,68 @@ +package commands + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/photoprism/photoprism/pkg/capture" +) + +func TestAuthListCommand(t *testing.T) { + t.Run("All", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls"}) + + // Run command with test context. + output := capture.Output(func() { + err = AuthListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "alice") + assert.Contains(t, output, "bob") + assert.Contains(t, output, "visitor") + }) + t.Run("Alice", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "alice"}) + + // Run command with test context. + output := capture.Output(func() { + err = AuthListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "| Session ID |") + assert.Contains(t, output, "alice") + assert.NotContains(t, output, "bob") + assert.NotContains(t, output, "visitor") + }) + t.Run("CSV", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "--csv", "alice"}) + + // Run command with test context. + output := capture.Output(func() { + err = AuthListCommand.Run(ctx) + }) + + // Check command output for plausibility. + t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "Session ID;User;Authentication") + assert.Contains(t, output, "alice") + assert.NotContains(t, output, "bob") + assert.NotContains(t, output, "visitor") + }) +} diff --git a/internal/commands/auth_test.go b/internal/commands/auth_test.go index 969fda582..d52d03484 100644 --- a/internal/commands/auth_test.go +++ b/internal/commands/auth_test.go @@ -5,27 +5,44 @@ import ( "github.com/stretchr/testify/assert" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/pkg/capture" ) -func TestAuthListCommand(t *testing.T) { - var err error +func TestAuthCommands(t *testing.T) { + t.Run("List", func(t *testing.T) { + var err error - ctx := config.CliTestContext() + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"auth", "ls"}) - output := capture.Output(func() { - err = AuthListCommand.Run(ctx) + // Run command with test context. + output := capture.Output(func() { + err = AuthCommands.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "alice") + assert.Contains(t, output, "bob") + assert.Contains(t, output, "visitor") }) + t.Run("ListAlice", func(t *testing.T) { + var err error - if err != nil { - t.Fatal(err) - } + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"auth", "ls", "alice"}) - t.Logf(output) + // Run command with test context. + output := capture.Output(func() { + err = AuthCommands.Run(ctx) + }) - // Check the command output for plausibility. - assert.Contains(t, output, "alice") - assert.Contains(t, output, "bob") - assert.Contains(t, output, "visitor") + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "alice") + assert.NotContains(t, output, "bob") + assert.NotContains(t, output, "visitor") + }) } diff --git a/internal/commands/commands.go b/internal/commands/commands.go index 75078303c..4cc507137 100644 --- a/internal/commands/commands.go +++ b/internal/commands/commands.go @@ -78,6 +78,13 @@ var countFlag = cli.UintFlag{ Value: 100, } +// LogErr logs an error if the argument is not nil. +func LogErr(err error) { + if err != nil { + log.Error(err) + } +} + // childAlreadyRunning tests if a .pid file at filePath is a running process. // it returns the pid value and the running status (true or false). func childAlreadyRunning(filePath string) (pid int, running bool) { diff --git a/internal/commands/commands_test.go b/internal/commands/commands_test.go index fc8fc9e31..1b0dc2900 100644 --- a/internal/commands/commands_test.go +++ b/internal/commands/commands_test.go @@ -1,6 +1,7 @@ package commands import ( + "flag" "os" "testing" @@ -28,3 +29,27 @@ func TestMain(m *testing.M) { os.Exit(code) } + +// NewTestContext creates a new CLI test context with the flags and arguments provided. +func NewTestContext(args []string) *cli.Context { + // Create new command-line app. + app := cli.NewApp() + app.Usage = "PhotoPrism®" + app.Version = "test" + app.Copyright = "(c) 2018-2024 PhotoPrism UG. All rights reserved." + app.EnableBashCompletion = true + app.Flags = config.Flags.Cli() + app.Metadata = map[string]interface{}{ + "Name": "PhotoPrism", + "About": "PhotoPrism®", + "Edition": "ce", + "Version": "test", + } + + // Parse command arguments. + flags := flag.NewFlagSet("test", 0) + LogErr(flags.Parse(args)) + + // Create and return new context. + return cli.NewContext(app, flags, nil) +}