diff --git a/internal/commands/auth_list_test.go b/internal/commands/auth_list_test.go index 7a124769c..1deeeb8c7 100644 --- a/internal/commands/auth_list_test.go +++ b/internal/commands/auth_list_test.go @@ -45,6 +45,7 @@ func TestAuthListCommand(t *testing.T) { assert.Contains(t, output, "alice") assert.NotContains(t, output, "bob") assert.NotContains(t, output, "visitor") + assert.NotContains(t, output, "| Preview Token |") }) t.Run("CSV", func(t *testing.T) { var err error @@ -58,11 +59,63 @@ func TestAuthListCommand(t *testing.T) { }) // Check command output for plausibility. - t.Logf(output) + //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("Tokens", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "--tokens", "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, "| Preview Token |") + assert.Contains(t, output, "alice") + assert.NotContains(t, output, "bob") + assert.NotContains(t, output, "visitor") + }) + t.Run("NoResult", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "--tokens", "notexisting"}) + + // 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.Empty(t, output) + }) + t.Run("Error", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "--xyz", "alice"}) + + // Run command with test context. + output := capture.Output(func() { + err = AuthListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.Error(t, err) + assert.Empty(t, output) + }) } diff --git a/internal/commands/auth_show_test.go b/internal/commands/auth_show_test.go new file mode 100644 index 000000000..c03dc36e8 --- /dev/null +++ b/internal/commands/auth_show_test.go @@ -0,0 +1,44 @@ +package commands + +import ( + "github.com/photoprism/photoprism/pkg/capture" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestAuthShowCommand(t *testing.T) { + t.Run("All", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"show", "sess34q3hael"}) + + // Run command with test context. + output := capture.Output(func() { + err = AuthShowCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "alice") + assert.Contains(t, output, "access_token") + assert.Contains(t, output, "client") + }) + t.Run("NoResult", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"show", "sess34qxxxxx"}) + + // Run command with test context. + output := capture.Output(func() { + err = AuthShowCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.Error(t, err) + assert.Empty(t, output) + }) +} diff --git a/internal/commands/clients_list_test.go b/internal/commands/clients_list_test.go new file mode 100644 index 000000000..44f34daf5 --- /dev/null +++ b/internal/commands/clients_list_test.go @@ -0,0 +1,103 @@ +package commands + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/photoprism/photoprism/pkg/capture" +) + +func TestClientsListCommand(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 = ClientsListCommand.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, "Monitoring") + assert.NotContains(t, output, "visitor") + }) + t.Run("Monitoring", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "monitoring"}) + + // Run command with test context. + output := capture.Output(func() { + err = ClientsListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "| Scope |") + assert.Contains(t, output, "Monitoring") + assert.NotContains(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", "monitoring"}) + + // Run command with test context. + output := capture.Output(func() { + err = ClientsListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "Client ID;Client Name;Authentication;Scope;") + assert.Contains(t, output, "Monitoring") + assert.Contains(t, output, "metrics") + assert.NotContains(t, output, "bob") + assert.NotContains(t, output, "alice") + }) + t.Run("NoResult", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "notexisting"}) + + // Run command with test context. + output := capture.Output(func() { + err = ClientsListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Empty(t, output) + }) + t.Run("Error", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "--xyz", "monitoring"}) + + // Run command with test context. + output := capture.Output(func() { + err = ClientsListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.Error(t, err) + assert.Empty(t, output) + }) +} diff --git a/internal/commands/clients_show_test.go b/internal/commands/clients_show_test.go new file mode 100644 index 000000000..0f907fb8d --- /dev/null +++ b/internal/commands/clients_show_test.go @@ -0,0 +1,44 @@ +package commands + +import ( + "github.com/photoprism/photoprism/pkg/capture" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestClientsShowCommand(t *testing.T) { + t.Run("All", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"show", "cs5gfen1bgxz7s9i"}) + + // Run command with test context. + output := capture.Output(func() { + err = ClientsShowCommand.Run(ctx) + }) + + // Check command output for plausibility. + //t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "Alice") + assert.Contains(t, output, "oauth2") + assert.Contains(t, output, "confidential") + }) + t.Run("NoResult", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"show", "cs5gfen1bgxzxxxx"}) + + // Run command with test context. + output := capture.Output(func() { + err = ClientsShowCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.Error(t, err) + assert.Empty(t, output) + }) +} diff --git a/internal/commands/users_list_test.go b/internal/commands/users_list_test.go new file mode 100644 index 000000000..687c881b5 --- /dev/null +++ b/internal/commands/users_list_test.go @@ -0,0 +1,119 @@ +package commands + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/photoprism/photoprism/pkg/capture" +) + +func TestUsersListCommand(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 = UsersListCommand.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.NotContains(t, output, "Monitoring") + assert.NotContains(t, output, "visitor") + }) + t.Run("Friend", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "friend"}) + + // Run command with test context. + output := capture.Output(func() { + err = UsersListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "| Last Login |") + assert.Contains(t, output, "friend") + assert.NotContains(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", "friend"}) + + // Run command with test context. + output := capture.Output(func() { + err = UsersListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "UID;Username;Role;Authentication;Super Admin;Web Login;") + assert.Contains(t, output, "friend") + assert.Contains(t, output, "uqxqg7i1kperxvu7") + assert.NotContains(t, output, "bob") + assert.NotContains(t, output, "alice") + }) + t.Run("NoResult", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "notexisting"}) + + // Run command with test context. + output := capture.Output(func() { + err = UsersListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.NoError(t, err) + assert.Empty(t, output) + }) + t.Run("InvalidFlag", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "--xyz", "friend"}) + + // Run command with test context. + output := capture.Output(func() { + err = UsersListCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.Error(t, err) + assert.Empty(t, output) + }) + t.Run("InvalidValue", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"ls", "-n=-1", "friend"}) + + // Run command with test context. + output := capture.Output(func() { + err = UsersListCommand.Run(ctx) + }) + + // Check command output for plausibility. + //t.Logf(output) + assert.Error(t, err) + assert.Empty(t, output) + }) +} diff --git a/internal/commands/users_show_test.go b/internal/commands/users_show_test.go new file mode 100644 index 000000000..b494c0b54 --- /dev/null +++ b/internal/commands/users_show_test.go @@ -0,0 +1,44 @@ +package commands + +import ( + "github.com/photoprism/photoprism/pkg/capture" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestUsersShowCommand(t *testing.T) { + t.Run("All", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"show", "alice"}) + + // Run command with test context. + output := capture.Output(func() { + err = UsersShowCommand.Run(ctx) + }) + + // Check command output for plausibility. + //t.Logf(output) + assert.NoError(t, err) + assert.Contains(t, output, "Alice") + assert.Contains(t, output, "admin") + assert.Contains(t, output, "alice@example.com") + }) + t.Run("NoResult", func(t *testing.T) { + var err error + + // Create test context with flags and arguments. + ctx := NewTestContext([]string{"show", "notexisting"}) + + // Run command with test context. + output := capture.Output(func() { + err = UsersShowCommand.Run(ctx) + }) + + // Check command output for plausibility. + // t.Logf(output) + assert.Error(t, err) + assert.Empty(t, output) + }) +} diff --git a/pkg/rnd/ref_id_test.go b/pkg/rnd/ref_id_test.go index 18a9769a6..32afe609b 100644 --- a/pkg/rnd/ref_id_test.go +++ b/pkg/rnd/ref_id_test.go @@ -48,6 +48,13 @@ func TestRefID(t *testing.T) { assert.False(t, InvalidRefID(id6)) assert.Len(t, id6, 12) + id7 := RefID("abcdef") + t.Logf("RefID 7: %s", id7) + assert.NotEmpty(t, id7) + assert.True(t, IsRefID(id7)) + assert.False(t, InvalidRefID(id7)) + assert.Len(t, id7, 12) + for n := 7; n < 20; n++ { id := RefID("test") t.Logf("RefID %d: %s", n, id) diff --git a/pkg/rnd/type_test.go b/pkg/rnd/type_test.go index 6f0c66725..44b34df91 100644 --- a/pkg/rnd/type_test.go +++ b/pkg/rnd/type_test.go @@ -23,3 +23,48 @@ func TestUidType(t *testing.T) { assert.Equal(t, byte('l'), prefix) }) } + +func TestType_Equal(t *testing.T) { + assert.True(t, TypeSHA1.Equal("SHA1")) + assert.False(t, TypeSHA1.Equal("SHA256")) +} + +func TestType_NotEqual(t *testing.T) { + assert.False(t, TypeSHA1.NotEqual("SHA1")) + assert.True(t, TypeSHA1.NotEqual("SHA256")) +} + +func TestType_EntityID(t *testing.T) { + assert.True(t, TypeUID.EntityID()) + assert.False(t, TypeSHA384.EntityID()) +} + +func TestType_SessionID(t *testing.T) { + assert.True(t, TypeSessionID.SessionID()) + assert.False(t, TypeRefID.SessionID()) +} + +func TestType_CrcToken(t *testing.T) { + assert.True(t, TypeCrcToken.CrcToken()) + assert.False(t, TypeMixed.CrcToken()) +} + +func TestType_Hash(t *testing.T) { + assert.True(t, TypeMD5.Hash()) + assert.True(t, TypeSHA384.Hash()) + assert.False(t, TypeUUID.Hash()) +} + +func TestType_SHA(t *testing.T) { + assert.True(t, TypeSHA1.SHA()) + assert.True(t, TypeSHA384.SHA()) + assert.True(t, TypeSHA224.SHA()) + assert.False(t, TypeUID.SHA()) +} + +func TestType_Unknown(t *testing.T) { + assert.True(t, TypeUnknown.Unknown()) + assert.False(t, TypeSHA384.Unknown()) + assert.False(t, TypeRefID.Unknown()) + +}