Tests: Add unit tests

This commit is contained in:
graciousgrey 2024-01-18 14:00:52 +01:00
parent e5cf00e050
commit 62ddac3142
8 changed files with 460 additions and 1 deletions

View file

@ -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)
})
}

View file

@ -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)
})
}

View file

@ -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)
})
}

View file

@ -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)
})
}

View file

@ -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)
})
}

View file

@ -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)
})
}

View file

@ -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)

View file

@ -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())
}