From c478025513b2a4df3a35200c058ddf60ce1a7701 Mon Sep 17 00:00:00 2001 From: graciousgrey Date: Tue, 16 Jan 2024 18:10:42 +0100 Subject: [PATCH] Tests: Add unit tests #3943 --- internal/form/client_test.go | 22 ++++++++++ internal/query/clients_test.go | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 internal/query/clients_test.go diff --git a/internal/form/client_test.go b/internal/form/client_test.go index 34d024cab..211b4ae22 100644 --- a/internal/form/client_test.go +++ b/internal/form/client_test.go @@ -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()) + }) +} diff --git a/internal/query/clients_test.go b/internal/query/clients_test.go new file mode 100644 index 000000000..e19cf958e --- /dev/null +++ b/internal/query/clients_test.go @@ -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)) + } + }) +}