Auth: Add tests for "photoprism auth ls" terminal command #808
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
parent
f08ef59245
commit
2ce272d60e
4 changed files with 131 additions and 14 deletions
68
internal/commands/auth_list_test.go
Normal file
68
internal/commands/auth_list_test.go
Normal file
|
@ -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")
|
||||
})
|
||||
}
|
|
@ -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")
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue