2022-07-06 23:19:05 +02:00
|
|
|
package models_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pocketbase/pocketbase/models"
|
2022-10-30 10:28:14 +02:00
|
|
|
"github.com/pocketbase/pocketbase/tools/types"
|
2022-07-06 23:19:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdminTableName(t *testing.T) {
|
2024-01-03 10:58:25 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
m := models.Admin{}
|
|
|
|
if m.TableName() != "_admins" {
|
|
|
|
t.Fatalf("Unexpected table name, got %q", m.TableName())
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 10:28:14 +02:00
|
|
|
|
|
|
|
func TestAdminValidatePassword(t *testing.T) {
|
2024-01-03 10:58:25 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
scenarios := []struct {
|
|
|
|
admin models.Admin
|
|
|
|
password string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// empty passwordHash + empty pass
|
|
|
|
models.Admin{},
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// empty passwordHash + nonempty pass
|
|
|
|
models.Admin{},
|
|
|
|
"123456",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// nonempty passwordHash + empty pass
|
|
|
|
models.Admin{PasswordHash: "$2a$10$SKk/Y/Yc925PBtsSYBvq3Ous9Jy18m4KTn6b/PQQ.Y9QVjy3o/Fv."},
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// nonempty passwordHash + wrong pass
|
|
|
|
models.Admin{PasswordHash: "$2a$10$SKk/Y/Yc925PBtsSYBvq3Ous9Jy18m4KTn6b/PQQ.Y9QVjy3o/Fv."},
|
|
|
|
"654321",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// nonempty passwordHash + correct pass
|
|
|
|
models.Admin{PasswordHash: "$2a$10$SKk/Y/Yc925PBtsSYBvq3Ous9Jy18m4KTn6b/PQQ.Y9QVjy3o/Fv."},
|
|
|
|
"123456",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range scenarios {
|
|
|
|
result := s.admin.ValidatePassword(s.password)
|
|
|
|
if result != s.expected {
|
|
|
|
t.Errorf("(%d) Expected %v, got %v", i, s.expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdminSetPassword(t *testing.T) {
|
2024-01-03 10:58:25 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
m := models.Admin{
|
|
|
|
// 123456
|
|
|
|
PasswordHash: "$2a$10$SKk/Y/Yc925PBtsSYBvq3Ous9Jy18m4KTn6b/PQQ.Y9QVjy3o/Fv.",
|
|
|
|
LastResetSentAt: types.NowDateTime(),
|
|
|
|
TokenKey: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty pass
|
|
|
|
err1 := m.SetPassword("")
|
|
|
|
if err1 == nil {
|
|
|
|
t.Fatal("Expected empty password error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err2 := m.SetPassword("654321")
|
|
|
|
if err2 != nil {
|
|
|
|
t.Fatalf("Expected nil, got error %v", err2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !m.ValidatePassword("654321") {
|
|
|
|
t.Fatalf("Password is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.TokenKey == "test" {
|
|
|
|
t.Fatalf("Expected TokenKey to change, got %v", m.TokenKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !m.LastResetSentAt.IsZero() {
|
|
|
|
t.Fatalf("Expected LastResetSentAt to be zero datetime, got %v", m.LastResetSentAt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdminRefreshTokenKey(t *testing.T) {
|
2024-01-03 10:58:25 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
m := models.Admin{TokenKey: "test"}
|
|
|
|
|
|
|
|
m.RefreshTokenKey()
|
|
|
|
|
|
|
|
// empty pass
|
|
|
|
if m.TokenKey == "" || m.TokenKey == "test" {
|
|
|
|
t.Fatalf("Expected TokenKey to change, got %q", m.TokenKey)
|
|
|
|
}
|
|
|
|
}
|