Tests: Add unit tests

This commit is contained in:
graciousgrey
2024-01-19 15:48:07 +01:00
parent 62ddac3142
commit d3a67a6694
8 changed files with 245 additions and 6 deletions

View File

@@ -1,9 +1,10 @@
package commands package commands
import ( import (
"testing"
"github.com/photoprism/photoprism/pkg/capture" "github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
func TestAuthShowCommand(t *testing.T) { func TestAuthShowCommand(t *testing.T) {
@@ -23,7 +24,7 @@ func TestAuthShowCommand(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, output, "alice") assert.Contains(t, output, "alice")
assert.Contains(t, output, "access_token") assert.Contains(t, output, "access_token")
assert.Contains(t, output, "client") assert.Contains(t, output, "Client")
}) })
t.Run("NoResult", func(t *testing.T) { t.Run("NoResult", func(t *testing.T) {
var err error var err error

View File

@@ -62,7 +62,7 @@ func TestClientsListCommand(t *testing.T) {
// Check command output for plausibility. // Check command output for plausibility.
// t.Logf(output) // t.Logf(output)
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, output, "Client ID;Client Name;Authentication;Scope;") assert.Contains(t, output, "Client ID;Client Name;Client User;Authentication Method;Role;Scope;")
assert.Contains(t, output, "Monitoring") assert.Contains(t, output, "Monitoring")
assert.Contains(t, output, "metrics") assert.Contains(t, output, "metrics")
assert.NotContains(t, output, "bob") assert.NotContains(t, output, "bob")

View File

@@ -1,9 +1,10 @@
package commands package commands
import ( import (
"testing"
"github.com/photoprism/photoprism/pkg/capture" "github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
func TestClientsShowCommand(t *testing.T) { func TestClientsShowCommand(t *testing.T) {

View File

@@ -0,0 +1,61 @@
package commands
import (
"testing"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert"
)
func TestUsersAddCommand(t *testing.T) {
t.Run("AddUserThatAlreadyExists", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
ctx := NewTestContext([]string{"add", "--name=Alice", "--email=jane@test.de", "--password=test1234", "--role=admin", "alice"})
// Run command with test context.
output := capture.Output(func() {
err = UsersAddCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
})
t.Run("AddDeletedUser", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
ctx := NewTestContext([]string{"add", "--name=deleted", "--password=test1234", "deleted"})
// Run command with test context.
output := capture.Output(func() {
err = UsersAddCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
})
t.Run("AddUsernameMissing", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
ctx := NewTestContext([]string{"add", "--name=noname", "--password=test1234", "/##"})
// Run command with test context.
output := capture.Output(func() {
err = UsersAddCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
})
}

View File

@@ -0,0 +1,42 @@
package commands
import (
"github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert"
"testing"
)
func TestUsersModCommand(t *testing.T) {
t.Run("ModNotExistingUser", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
ctx := NewTestContext([]string{"mod", "--name=New", "--email=new@test.de", "uqxqg7i1kperxxx0"})
// Run command with test context.
output := capture.Output(func() {
err = UsersModCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
})
t.Run("ModDeletedUser", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
ctx := NewTestContext([]string{"mod", "--name=New", "--email=new@test.de", "deleted"})
// Run command with test context.
output := capture.Output(func() {
err = UsersModCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
})
}

View File

@@ -0,0 +1,42 @@
package commands
import (
"github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert"
"testing"
)
func TestUsersRemoveCommand(t *testing.T) {
t.Run("RemoveNotExistingUser", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
ctx := NewTestContext([]string{"rm", "uqxqg7i1kperxxx0"})
// Run command with test context.
output := capture.Output(func() {
err = UsersRemoveCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
})
t.Run("RemoveDeletedUser", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
ctx := NewTestContext([]string{"rm", "deleted"})
// Run command with test context.
output := capture.Output(func() {
err = UsersRemoveCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
})
}

View File

@@ -1,13 +1,14 @@
package commands package commands
import ( import (
"testing"
"github.com/photoprism/photoprism/pkg/capture" "github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
func TestUsersShowCommand(t *testing.T) { func TestUsersShowCommand(t *testing.T) {
t.Run("All", func(t *testing.T) { t.Run("Alice", func(t *testing.T) {
var err error var err error
// Create test context with flags and arguments. // Create test context with flags and arguments.

View File

@@ -0,0 +1,91 @@
package commands
import (
"github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert"
"testing"
)
func TestUsersCommand(t *testing.T) {
t.Run("AddModifyAndRemoveJohn", func(t *testing.T) {
var err error
//Add John
ctx := NewTestContext([]string{"add", "--name=John", "--email=john@test.de", "--password=test1234", "--role=admin", "john"})
// Run command with test context.
output := capture.Output(func() {
err = UsersAddCommand.Run(ctx)
})
// Check command output for plausibility.
// t.Logf(output)
assert.NoError(t, err)
assert.Empty(t, output)
ctx2 := NewTestContext([]string{"show", "john"})
// Run command with test context.
output2 := capture.Output(func() {
err = UsersShowCommand.Run(ctx2)
})
//t.Logf(output2)
assert.NoError(t, err)
assert.Contains(t, output2, "John")
assert.Contains(t, output2, "admin")
assert.Contains(t, output2, "john@test.de")
//Modify John
// Create test context with flags and arguments.
ctx3 := NewTestContext([]string{"mod", "--name=Johnny", "--email=johnnny@test.de", "--password=test12345", "john"})
// Run command with test context.
output3 := capture.Output(func() {
err = UsersModCommand.Run(ctx3)
})
// Check command output for plausibility.
// t.Logf(output3)
assert.NoError(t, err)
assert.Empty(t, output3)
output4 := capture.Output(func() {
err = UsersShowCommand.Run(ctx2)
})
//t.Logf(output4)
assert.NoError(t, err)
assert.Contains(t, output4, "Johnny")
assert.Contains(t, output4, "admin")
assert.Contains(t, output4, "johnnny@test.de")
assert.Contains(t, output4, "| DeletedAt | <nil>")
//Remove John
// Create test context with flags and arguments.
ctx5 := NewTestContext([]string{"rm", "--force", "john"})
// Run command with test context.
output5 := capture.Output(func() {
err = UsersRemoveCommand.Run(ctx5)
})
// Check command output for plausibility.
// t.Logf(output5)
assert.NoError(t, err)
assert.Empty(t, output5)
output6 := capture.Output(func() {
err = UsersShowCommand.Run(ctx2)
})
//t.Logf(output6)
assert.NoError(t, err)
assert.Contains(t, output6, "Johnny")
assert.Contains(t, output6, "admin")
assert.Contains(t, output6, "johnnny@test.de")
assert.Contains(t, output6, "| DeletedAt | time.Date")
assert.NotContains(t, output6, "| DeletedAt | <nil>")
})
}