photoprism/internal/entity/auth_session_client_test.go
Michael Mayer 305e7bac68 OAuth2: Refactor "client add" and "client mod" CLI commands #808 #3943
Signed-off-by: Michael Mayer <michael@photoprism.app>
2024-01-29 13:54:50 +01:00

98 lines
1.8 KiB
Go

package entity
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/photoprism/photoprism/pkg/unix"
)
func TestNewClientAuthentication(t *testing.T) {
t.Run("Anonymous", func(t *testing.T) {
sess := NewClientAuthentication("Anonymous", unix.Day, "metrics", nil)
if sess == nil {
t.Fatal("session must not be nil")
}
t.Logf("sess: %#v", sess)
})
t.Run("Alice", func(t *testing.T) {
user := FindUserByName("alice")
if user == nil {
t.Fatal("user must not be nil")
}
sess := NewClientAuthentication("alice", unix.Day, "metrics", user)
if sess == nil {
t.Fatal("session must not be nil")
}
t.Logf("sess: %#v", sess)
})
t.Run("NoScope", func(t *testing.T) {
user := FindUserByName("alice")
if user == nil {
t.Fatal("user must not be nil")
}
sess := NewClientAuthentication("alice", unix.Day, "", user)
if sess == nil {
t.Fatal("session must not be nil")
}
t.Logf("sess: %#v", sess)
})
t.Run("NoLifetime", func(t *testing.T) {
user := FindUserByName("alice")
if user == nil {
t.Fatal("user must not be nil")
}
sess := NewClientAuthentication("", 0, "metrics", user)
if sess == nil {
t.Fatal("session must not be nil")
}
t.Logf("sess: %#v", sess)
})
}
func TestAddClientAuthentication(t *testing.T) {
t.Run("Anonymous", func(t *testing.T) {
sess, err := AddClientAuthentication("", unix.Day, "metrics", nil)
assert.NoError(t, err)
if sess == nil {
t.Fatal("session must not be nil")
}
t.Logf("sess: %#v", sess)
})
t.Run("Alice", func(t *testing.T) {
user := FindUserByName("alice")
if user == nil {
t.Fatal("user must not be nil")
}
sess, err := AddClientAuthentication("My Client App Token", unix.Day, "metrics", user)
assert.NoError(t, err)
if sess == nil {
t.Fatal("session must not be nil")
}
t.Logf("sess: %#v", sess)
})
}