Tests: Add unit tests #3943
This commit is contained in:
parent
2bf65737d3
commit
c478025513
2 changed files with 96 additions and 0 deletions
|
@ -1,8 +1,11 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"testing"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
|
||||
"github.com/photoprism/photoprism/pkg/authn"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -16,3 +19,22 @@ func TestNewClient(t *testing.T) {
|
|||
assert.Equal(t, "", client.Name())
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewClientFromCli(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
globalSet := flag.NewFlagSet("test", 0)
|
||||
globalSet.String("name", "Test", "")
|
||||
globalSet.String("scope", "*", "")
|
||||
globalSet.String("method", "totp", "")
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Version = "0.0.0"
|
||||
|
||||
c := cli.NewContext(app, globalSet, nil)
|
||||
|
||||
client := NewClientFromCli(c)
|
||||
assert.Equal(t, authn.Method2FA, client.Method())
|
||||
assert.Equal(t, "webdav", client.Scope())
|
||||
assert.Equal(t, "Test", client.Name())
|
||||
})
|
||||
}
|
||||
|
|
74
internal/query/clients_test.go
Normal file
74
internal/query/clients_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package query
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestClients(t *testing.T) {
|
||||
t.Run("Default", func(t *testing.T) {
|
||||
if results, err := Clients(0, 0, "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
assert.LessOrEqual(t, 4, len(results))
|
||||
}
|
||||
})
|
||||
t.Run("Limit", func(t *testing.T) {
|
||||
if results, err := Clients(2, 0, "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
assert.LessOrEqual(t, 2, len(results))
|
||||
}
|
||||
})
|
||||
t.Run("Offset", func(t *testing.T) {
|
||||
if results, err := Clients(3, 1, "", "all"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
assert.LessOrEqual(t, 3, len(results))
|
||||
}
|
||||
})
|
||||
t.Run("SearchAliceByName", func(t *testing.T) {
|
||||
if results, err := Clients(100, 0, "", "alice"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
assert.LessOrEqual(t, 1, len(results))
|
||||
if len(results) > 0 {
|
||||
assert.Equal(t, "cs5gfen1bgxz7s9i", results[0].ClientUID)
|
||||
assert.Equal(t, "uqxetse3cy5eo9z2", results[0].UserUID)
|
||||
assert.Equal(t, "alice", results[0].UserName)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("SearchAliceByClientUID", func(t *testing.T) {
|
||||
if results, err := Clients(100, 0, "", "cs5gfen1bgxz7s9i"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
assert.LessOrEqual(t, 1, len(results))
|
||||
if len(results) > 0 {
|
||||
assert.Equal(t, "cs5gfen1bgxz7s9i", results[0].ClientUID)
|
||||
assert.Equal(t, "uqxetse3cy5eo9z2", results[0].UserUID)
|
||||
assert.Equal(t, "alice", results[0].UserName)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("SearchAliceByUserUID", func(t *testing.T) {
|
||||
if results, err := Clients(100, 0, "", "uqxetse3cy5eo9z2"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
assert.LessOrEqual(t, 1, len(results))
|
||||
if len(results) > 0 {
|
||||
assert.Equal(t, "cs5gfen1bgxz7s9i", results[0].ClientUID)
|
||||
assert.Equal(t, "uqxetse3cy5eo9z2", results[0].UserUID)
|
||||
assert.Equal(t, "alice", results[0].UserName)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("SortByCreated", func(t *testing.T) {
|
||||
if results, err := Clients(100, 0, "created_at", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
assert.LessOrEqual(t, 4, len(results))
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue