mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Tests: Add unit tests
This commit is contained in:
@@ -28,19 +28,4 @@ func TestOIDCLogin(t *testing.T) {
|
||||
r := PerformRequest(app, http.MethodGet, "/api/v1/oidc/login")
|
||||
assert.Equal(t, http.StatusTemporaryRedirect, r.Code)
|
||||
})
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
conf.SetAuthMode(config.AuthModePasswd)
|
||||
defer conf.SetAuthMode(config.AuthModePublic)
|
||||
|
||||
conf.Options().OIDCUri = "https://keycloak.localssl.dev/realms/master"
|
||||
conf.Options().SiteUrl = "https://app.localssl.dev/"
|
||||
conf.Options().OIDCClient = "photoprism-develop"
|
||||
conf.Options().OIDCSecret = "9d8351a0-ca01-4556-9c37-85eb634869b9"
|
||||
|
||||
OIDCLogin(router)
|
||||
|
||||
r := PerformRequest(app, http.MethodGet, "/api/v1/oidc/login")
|
||||
assert.Equal(t, http.StatusFound, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestOIDCRedirect(t *testing.T) {
|
||||
conf.SetAuthMode(config.AuthModePasswd)
|
||||
defer conf.SetAuthMode(config.AuthModePublic)
|
||||
|
||||
conf.Options().OIDCUri = "https://keycloak.localssl.dev/realms/master"
|
||||
conf.Options().OIDCUri = "http://dummy-oidc:9998"
|
||||
conf.Options().SiteUrl = "https://app.localssl.dev/"
|
||||
conf.Options().OIDCClient = "photoprism-develop"
|
||||
conf.Options().OIDCSecret = "9d8351a0-ca01-4556-9c37-85eb634869b9"
|
||||
|
||||
@@ -72,7 +72,7 @@ func TestCreateSession(t *testing.T) {
|
||||
CreateSession(router)
|
||||
|
||||
r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "admin", "password": "photoprism"}`)
|
||||
t.Logf("Response Body: %s", r.Body.String())
|
||||
//t.Logf("Response Body: %s", r.Body.String())
|
||||
userName := gjson.Get(r.Body.String(), "user.Name").String()
|
||||
assert.Equal(t, "admin", userName)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
@@ -191,6 +191,50 @@ func TestCreateSession(t *testing.T) {
|
||||
assert.Equal(t, i18n.Msg(i18n.ErrInvalidCredentials), val.String())
|
||||
assert.Equal(t, http.StatusUnauthorized, r.Code)
|
||||
})
|
||||
t.Run("2FAPasscodeRequired", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
conf.SetAuthMode(config.AuthModePasswd)
|
||||
defer conf.SetAuthMode(config.AuthModePublic)
|
||||
|
||||
CreateSession(router)
|
||||
|
||||
r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "2fa", "password": "2fa-123!"}`)
|
||||
userEmail := gjson.Get(r.Body.String(), "user.Email")
|
||||
userName := gjson.Get(r.Body.String(), "user.Name")
|
||||
assert.Equal(t, "", userEmail.String())
|
||||
assert.Equal(t, "", userName.String())
|
||||
assert.Equal(t, http.StatusUnauthorized, r.Code)
|
||||
})
|
||||
t.Run("2FAInvalidPasscode", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
conf.SetAuthMode(config.AuthModePasswd)
|
||||
defer conf.SetAuthMode(config.AuthModePublic)
|
||||
|
||||
CreateSession(router)
|
||||
|
||||
r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "2fa", "password": "2fa-123!", "code": "123456"}`)
|
||||
|
||||
userEmail := gjson.Get(r.Body.String(), "user.Email")
|
||||
userName := gjson.Get(r.Body.String(), "user.Name")
|
||||
assert.Equal(t, "", userEmail.String())
|
||||
assert.Equal(t, "", userName.String())
|
||||
assert.Equal(t, http.StatusUnauthorized, r.Code)
|
||||
})
|
||||
t.Run("2FAUseRecoveryCode", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
conf.SetAuthMode(config.AuthModePasswd)
|
||||
defer conf.SetAuthMode(config.AuthModePublic)
|
||||
|
||||
CreateSession(router)
|
||||
|
||||
r := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", `{"username": "2fa", "password": "2fa-123!", "code": "0wg68oc6jgo54"}`)
|
||||
|
||||
userEmail := gjson.Get(r.Body.String(), "user.Email")
|
||||
userName := gjson.Get(r.Body.String(), "user.Name")
|
||||
assert.Equal(t, "2FA@example.com", userEmail.String())
|
||||
assert.Equal(t, "2fa", userName.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetSession(t *testing.T) {
|
||||
|
||||
@@ -3,11 +3,11 @@ package api
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/photoprism/photoprism/internal/entity"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/entity"
|
||||
"github.com/photoprism/photoprism/internal/form"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -64,6 +64,7 @@ func TestUpdateUser(t *testing.T) {
|
||||
|
||||
f := form.User{
|
||||
DisplayName: "Alicia",
|
||||
UploadPath: "uploads-alice",
|
||||
}
|
||||
|
||||
if userForm, err := json.Marshal(f); err != nil {
|
||||
@@ -73,6 +74,7 @@ func TestUpdateUser(t *testing.T) {
|
||||
string(userForm), sessId)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
assert.Contains(t, r.Body.String(), "\"DisplayName\":\"Alicia\"")
|
||||
assert.Contains(t, r.Body.String(), "\"UploadPath\":\"uploads-alice\"")
|
||||
}
|
||||
})
|
||||
|
||||
@@ -85,6 +87,8 @@ func TestUpdateUser(t *testing.T) {
|
||||
|
||||
f := form.User{
|
||||
DisplayName: "Bobby",
|
||||
WebDAV: false,
|
||||
UploadPath: "uploads-bob",
|
||||
}
|
||||
|
||||
if userForm, err := json.Marshal(f); err != nil {
|
||||
@@ -94,6 +98,7 @@ func TestUpdateUser(t *testing.T) {
|
||||
string(userForm), sessId)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
assert.Contains(t, r.Body.String(), "\"DisplayName\":\"Bobby\"")
|
||||
assert.Contains(t, r.Body.String(), "\"UploadPath\":\"uploads-bob\"")
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -44,4 +44,67 @@ func TestNewClient(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.IsType(t, &Client{}, client)
|
||||
})
|
||||
t.Run("EmptyScopes", func(t *testing.T) {
|
||||
uri, err := url.Parse("http://dummy-oidc:9998")
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
client, err := NewClient(
|
||||
uri,
|
||||
"csg6yqvykh0780f9",
|
||||
"nd09wkee0ElsMvzLGkgWS9wJAttHwF2h",
|
||||
"",
|
||||
"https://app.localssl.dev/",
|
||||
true,
|
||||
)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.IsType(t, &Client{}, client)
|
||||
})
|
||||
t.Run("IssuerUriMissing", func(t *testing.T) {
|
||||
client, err := NewClient(
|
||||
nil,
|
||||
"csg6yqvykh0780f9",
|
||||
"nd09wkee0ElsMvzLGkgWS9wJAttHwF2h",
|
||||
authn.OidcDefaultScopes,
|
||||
"https://app.localssl.dev/",
|
||||
true,
|
||||
)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, client)
|
||||
})
|
||||
t.Run("EmptyRedirectUrl", func(t *testing.T) {
|
||||
uri, parseErr := url.Parse("http://dummy-oidc:9998")
|
||||
|
||||
assert.NoError(t, parseErr)
|
||||
|
||||
client, _ := NewClient(
|
||||
uri,
|
||||
"csg6yqvykh0780f9",
|
||||
"nd09wkee0ElsMvzLGkgWS9wJAttHwF2h",
|
||||
authn.OidcDefaultScopes,
|
||||
"",
|
||||
true,
|
||||
)
|
||||
|
||||
assert.Nil(t, client)
|
||||
})
|
||||
t.Run("ServiceDiscoveryFails", func(t *testing.T) {
|
||||
uri, err := url.Parse("https://dummy-oidc:9998")
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
client, err := NewClient(
|
||||
uri,
|
||||
"csg6yqvykh0780f9",
|
||||
"nd09wkee0ElsMvzLGkgWS9wJAttHwF2h",
|
||||
authn.OidcDefaultScopes,
|
||||
"https://app.localssl.dev/",
|
||||
true,
|
||||
)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, client)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user