forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_test.go
104 lines (90 loc) · 2.2 KB
/
admin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package models_test
import (
"testing"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/types"
)
func TestAdminTableName(t *testing.T) {
m := models.Admin{}
if m.TableName() != "_admins" {
t.Fatalf("Unexpected table name, got %q", m.TableName())
}
}
func TestAdminValidatePassword(t *testing.T) {
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) {
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) {
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)
}
}