Tests: Add unit tests #3943
This commit is contained in:
parent
0f8d5073dd
commit
04e8dfe6cb
3 changed files with 243 additions and 0 deletions
33
internal/entity/auth_client_add_test.go
Normal file
33
internal/entity/auth_client_add_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package entity
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/form"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_AddClient(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
m := form.Client{ClientName: "test", AuthMethod: "basic", AuthScope: "all"}
|
||||
|
||||
c, err := AddClient(m)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "test", c.ClientName)
|
||||
})
|
||||
t.Run("ClientNameEmpty", func(t *testing.T) {
|
||||
m := form.Client{ClientName: "", AuthMethod: "basic", AuthScope: "all"}
|
||||
|
||||
c, err := AddClient(m)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("error expected")
|
||||
}
|
||||
|
||||
assert.Equal(t, "", c.ClientName)
|
||||
})
|
||||
}
|
39
internal/entity/auth_client_fixtures_test.go
Normal file
39
internal/entity/auth_client_fixtures_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package entity
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestClientMap_Get(t *testing.T) {
|
||||
t.Run("Alice", func(t *testing.T) {
|
||||
r := ClientFixtures.Get("alice")
|
||||
assert.Equal(t, "Alice", r.ClientName)
|
||||
assert.Equal(t, "cs5gfen1bgxz7s9i", r.ClientUID)
|
||||
assert.IsType(t, Client{}, r)
|
||||
})
|
||||
|
||||
t.Run("Invalid", func(t *testing.T) {
|
||||
r := ClientFixtures.Get("xxx")
|
||||
assert.Equal(t, "", r.ClientName)
|
||||
assert.Equal(t, "", r.ClientUID)
|
||||
assert.IsType(t, Client{}, r)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClientMap_Pointer(t *testing.T) {
|
||||
t.Run("Alice", func(t *testing.T) {
|
||||
r := ClientFixtures.Pointer("alice")
|
||||
assert.Equal(t, "cs5gfen1bgxz7s9i", r.ClientUID)
|
||||
assert.Equal(t, "Alice", r.ClientName)
|
||||
assert.IsType(t, &Client{}, r)
|
||||
})
|
||||
|
||||
t.Run("Invalid", func(t *testing.T) {
|
||||
r := ClientFixtures.Pointer("xxx")
|
||||
assert.Equal(t, "", r.ClientName)
|
||||
assert.Equal(t, "", r.ClientUID)
|
||||
assert.IsType(t, &Client{}, r)
|
||||
})
|
||||
}
|
|
@ -3,6 +3,9 @@ package entity
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/form"
|
||||
"github.com/photoprism/photoprism/pkg/authn"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -241,6 +244,35 @@ func TestClient_NewSecret(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestClient_Method(t *testing.T) {
|
||||
t.Run("Alice", func(t *testing.T) {
|
||||
alice := ClientFixtures.Get("alice")
|
||||
assert.Equal(t, alice.Method(), authn.MethodOAuth2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_UpdateLastActive(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
var m = Client{ClientName: "Anne", ClientUID: "cs5cpu17n6gj2fff"}
|
||||
if err := m.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Empty(t, m.LastActive)
|
||||
|
||||
c := m.UpdateLastActive()
|
||||
|
||||
assert.NotEmpty(t, c.LastActive)
|
||||
})
|
||||
t.Run("EmptyUID", func(t *testing.T) {
|
||||
var m = Client{ClientName: "No UUID"}
|
||||
|
||||
c := m.UpdateLastActive()
|
||||
|
||||
assert.Empty(t, c.LastActive)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_HasPassword(t *testing.T) {
|
||||
t.Run("Alice", func(t *testing.T) {
|
||||
expected := ClientFixtures.Get("alice")
|
||||
|
@ -281,3 +313,142 @@ func TestClient_HasPassword(t *testing.T) {
|
|||
assert.NotEmpty(t, m.UpdatedAt)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_Expires(t *testing.T) {
|
||||
t.Run("Metrics", func(t *testing.T) {
|
||||
m := ClientFixtures.Get("metrics")
|
||||
|
||||
r := m.Expires()
|
||||
|
||||
assert.Equal(t, r.String(), "1h0m0s")
|
||||
})
|
||||
t.Run("Alice", func(t *testing.T) {
|
||||
m := ClientFixtures.Get("alice")
|
||||
|
||||
r := m.Expires()
|
||||
|
||||
assert.Equal(t, r.String(), "24h0m0s")
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_Report(t *testing.T) {
|
||||
t.Run("Metrics", func(t *testing.T) {
|
||||
m := ClientFixtures.Get("metrics")
|
||||
|
||||
rows, _ := m.Report(true)
|
||||
assert.NotEmpty(t, rows)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_SetFormValues(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
var m = Client{ClientName: "Neo", ClientUID: "cs5cpu17n6gj3aab"}
|
||||
|
||||
if err := m.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var values = form.Client{ClientName: "New Name", AuthMethod: authn.MethodOAuth2.String(),
|
||||
AuthScope: "test",
|
||||
AuthExpires: 4000,
|
||||
AuthTokens: 3,
|
||||
AuthEnabled: false}
|
||||
|
||||
c := m.SetFormValues(values)
|
||||
|
||||
assert.Equal(t, "New Name", c.ClientName)
|
||||
assert.Equal(t, int64(4000), c.AuthExpires)
|
||||
assert.Equal(t, int64(3), c.AuthTokens)
|
||||
assert.Equal(t, false, c.AuthEnabled)
|
||||
})
|
||||
t.Run("Success2", func(t *testing.T) {
|
||||
var m = Client{ClientName: "Neo", ClientUID: "cs5cpu17n6gj3aab", AuthTokens: -4}
|
||||
|
||||
if err := m.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var values = form.Client{ClientName: "Annika", AuthMethod: authn.MethodBasic.String(),
|
||||
AuthScope: "metrics",
|
||||
AuthExpires: -4000,
|
||||
AuthTokens: -5,
|
||||
AuthEnabled: true}
|
||||
|
||||
c := m.SetFormValues(values)
|
||||
|
||||
assert.Equal(t, "Annika", c.ClientName)
|
||||
assert.Equal(t, int64(3600), c.AuthExpires)
|
||||
assert.Equal(t, int64(-1), c.AuthTokens)
|
||||
assert.Equal(t, true, c.AuthEnabled)
|
||||
})
|
||||
t.Run("Success3", func(t *testing.T) {
|
||||
var m = Client{ClientName: "Neo", ClientUID: "cs5cpu17n6gj3aab"}
|
||||
|
||||
if err := m.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var values = form.Client{ClientName: "Friend",
|
||||
AuthMethod: authn.MethodBasic.String(),
|
||||
AuthScope: "test",
|
||||
AuthExpires: 4000000,
|
||||
AuthTokens: 3000000000,
|
||||
AuthEnabled: true,
|
||||
UserUID: "uqxqg7i1kperxvu7"}
|
||||
|
||||
c := m.SetFormValues(values)
|
||||
|
||||
assert.Equal(t, "Friend", c.ClientName)
|
||||
assert.Equal(t, int64(2678400), c.AuthExpires)
|
||||
assert.Equal(t, int64(2147483647), c.AuthTokens)
|
||||
assert.Equal(t, true, c.AuthEnabled)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_Validate(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
m := Client{ClientName: "test", ClientType: "test", AuthMethod: "basic", AuthScope: "all"}
|
||||
|
||||
err := m.Validate()
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
t.Run("ClientNameEmpty", func(t *testing.T) {
|
||||
m := Client{ClientName: "", ClientType: "test", AuthMethod: "basic", AuthScope: "all"}
|
||||
|
||||
err := m.Validate()
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("error expected")
|
||||
}
|
||||
})
|
||||
t.Run("ClientTypeEmpty", func(t *testing.T) {
|
||||
m := Client{ClientName: "test", ClientType: "", AuthMethod: "basic", AuthScope: "all"}
|
||||
|
||||
err := m.Validate()
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("error expected")
|
||||
}
|
||||
})
|
||||
t.Run("AuthMethodEmpty", func(t *testing.T) {
|
||||
m := Client{ClientName: "test", ClientType: "test", AuthMethod: "", AuthScope: "all"}
|
||||
|
||||
err := m.Validate()
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("error expected")
|
||||
}
|
||||
})
|
||||
t.Run("AuthScopeEmpty", func(t *testing.T) {
|
||||
m := Client{ClientName: "test", ClientType: "test", AuthMethod: "basic", AuthScope: ""}
|
||||
|
||||
err := m.Validate()
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("error expected")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue