2020-01-28 11:04:10 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-07-07 19:12:28 +02:00
|
|
|
func TestAuthMode(t *testing.T) {
|
|
|
|
c := NewConfig(CliTestContext())
|
|
|
|
c.options.Public = true
|
|
|
|
c.options.Demo = false
|
|
|
|
assert.Equal(t, AuthModePublic, c.AuthMode())
|
|
|
|
c.options.Public = false
|
|
|
|
c.options.Demo = false
|
2022-07-11 05:08:15 +02:00
|
|
|
assert.Equal(t, AuthModePasswd, c.AuthMode())
|
2022-07-07 19:12:28 +02:00
|
|
|
c.options.Demo = true
|
|
|
|
assert.Equal(t, AuthModePublic, c.AuthMode())
|
2022-07-11 05:08:15 +02:00
|
|
|
c.options.AuthMode = "pass"
|
|
|
|
assert.Equal(t, AuthModePublic, c.AuthMode())
|
|
|
|
c.options.Demo = false
|
|
|
|
c.options.AuthMode = "pass"
|
|
|
|
assert.Equal(t, AuthModePasswd, c.AuthMode())
|
|
|
|
c.options.AuthMode = "password"
|
|
|
|
assert.Equal(t, AuthModePasswd, c.AuthMode())
|
2022-10-12 18:11:20 +02:00
|
|
|
c.options.Debug = false
|
|
|
|
c.SetAuthMode(AuthModePublic)
|
|
|
|
assert.Equal(t, AuthModePasswd, c.AuthMode())
|
|
|
|
c.options.Debug = true
|
|
|
|
c.SetAuthMode(AuthModePublic)
|
|
|
|
assert.Equal(t, AuthModePublic, c.AuthMode())
|
|
|
|
c.SetAuthMode(AuthModePasswd)
|
|
|
|
assert.Equal(t, AuthModePasswd, c.AuthMode())
|
|
|
|
c.options.Debug = false
|
2022-07-07 19:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuth(t *testing.T) {
|
|
|
|
c := NewConfig(CliTestContext())
|
|
|
|
c.options.Public = true
|
|
|
|
c.options.Demo = false
|
|
|
|
assert.False(t, c.Auth())
|
|
|
|
c.options.Public = false
|
|
|
|
c.options.Demo = false
|
|
|
|
assert.True(t, c.Auth())
|
|
|
|
c.options.Demo = true
|
|
|
|
assert.False(t, c.Auth())
|
|
|
|
}
|
|
|
|
|
2022-10-03 22:59:29 +02:00
|
|
|
func TestSessMaxAge(t *testing.T) {
|
|
|
|
c := NewConfig(CliTestContext())
|
2022-10-25 06:19:56 +02:00
|
|
|
assert.Equal(t, DefaultSessionMaxAge, c.SessionMaxAge())
|
|
|
|
c.options.SessionMaxAge = -1
|
|
|
|
assert.Equal(t, int64(0), c.SessionMaxAge())
|
|
|
|
c.options.SessionMaxAge = 0
|
|
|
|
assert.Equal(t, DefaultSessionMaxAge, c.SessionMaxAge())
|
2022-10-03 22:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessTimeout(t *testing.T) {
|
|
|
|
c := NewConfig(CliTestContext())
|
2022-10-25 06:19:56 +02:00
|
|
|
assert.Equal(t, DefaultSessionTimeout, c.SessionTimeout())
|
|
|
|
c.options.SessionTimeout = -1
|
|
|
|
assert.Equal(t, int64(0), c.SessionTimeout())
|
|
|
|
c.options.SessionTimeout = 0
|
|
|
|
assert.Equal(t, DefaultSessionTimeout, c.SessionTimeout())
|
2022-10-03 22:59:29 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 11:04:10 +01:00
|
|
|
func TestUtils_CheckPassword(t *testing.T) {
|
2020-07-13 20:25:47 +02:00
|
|
|
c := NewConfig(CliTestContext())
|
|
|
|
|
2020-01-28 11:04:10 +01:00
|
|
|
formPassword := "photoprism"
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.AdminPassword = "$2b$10$cRhWIleqJkbaFWhBMp54VOI25RvVubxOooCWzWgdrvl5COFxaBnAy"
|
2020-01-28 11:04:10 +01:00
|
|
|
check := c.CheckPassword(formPassword)
|
|
|
|
assert.True(t, check)
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.AdminPassword = "photoprism"
|
2020-01-28 11:04:10 +01:00
|
|
|
check = c.CheckPassword(formPassword)
|
|
|
|
assert.True(t, check)
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.AdminPassword = "$2b$10$yprZEQzm/Qy7AaePXtKfkem0kANBZgRwl8HbLE4JrjK6/8Pypgi1W"
|
2020-01-28 11:04:10 +01:00
|
|
|
check = c.CheckPassword(formPassword)
|
|
|
|
assert.False(t, check)
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.AdminPassword = "admin"
|
2020-01-28 11:04:10 +01:00
|
|
|
check = c.CheckPassword(formPassword)
|
|
|
|
assert.False(t, check)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUtils_isBcrypt(t *testing.T) {
|
|
|
|
p := "$2b$10$cRhWIleqJkbaFWhBMp54VOI25RvVubxOooCWzWgdrvl5COFxaBnAy"
|
|
|
|
assert.True(t, isBcrypt(p))
|
|
|
|
|
|
|
|
p = "$2b$10$cRhWIleqJkbaFWhBMp54VOI25RvVubxOooCWzWgdrvl5COFxaBnA"
|
|
|
|
assert.False(t, isBcrypt(p))
|
|
|
|
|
|
|
|
p = "admin"
|
|
|
|
assert.False(t, isBcrypt(p))
|
2020-07-13 17:37:43 +02:00
|
|
|
|
|
|
|
p = ""
|
|
|
|
assert.False(t, isBcrypt(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_InvalidDownloadToken(t *testing.T) {
|
2020-07-13 20:25:47 +02:00
|
|
|
c := NewConfig(CliTestContext())
|
|
|
|
|
|
|
|
assert.True(t, c.InvalidDownloadToken("xxx"))
|
2020-07-13 17:37:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_InvalidPreviewToken(t *testing.T) {
|
2020-07-13 20:25:47 +02:00
|
|
|
c := NewConfig(CliTestContext())
|
|
|
|
|
|
|
|
assert.True(t, c.InvalidPreviewToken("xxx"))
|
2020-01-28 11:04:10 +01:00
|
|
|
}
|