forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_stubs.go
147 lines (129 loc) · 3.45 KB
/
dynamic_stubs.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package tests
import (
"strconv"
"time"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/types"
)
func StubOTPRecords(app core.App) error {
superuser2, err := app.FindAuthRecordByEmail(core.CollectionNameSuperusers, "[email protected]")
if err != nil {
return err
}
superuser2.SetRaw("stubId", "superuser2")
superuser3, err := app.FindAuthRecordByEmail(core.CollectionNameSuperusers, "[email protected]")
if err != nil {
return err
}
superuser3.SetRaw("stubId", "superuser3")
user1, err := app.FindAuthRecordByEmail("users", "[email protected]")
if err != nil {
return err
}
user1.SetRaw("stubId", "user1")
now := types.NowDateTime()
old := types.NowDateTime().Add(-1 * time.Hour)
stubs := map[*core.Record][]types.DateTime{
superuser2: {now, now.Add(-1 * time.Millisecond), old, now.Add(-2 * time.Millisecond), old.Add(-1 * time.Millisecond)},
superuser3: {now.Add(-3 * time.Millisecond), now.Add(-2 * time.Minute)},
user1: {old},
}
for record, idDates := range stubs {
for i, date := range idDates {
otp := core.NewOTP(app)
otp.Id = record.GetString("stubId") + "_" + strconv.Itoa(i)
otp.SetRecordRef(record.Id)
otp.SetCollectionRef(record.Collection().Id)
otp.SetPassword("test123")
otp.SetRaw("created", date)
if err := app.SaveNoValidate(otp); err != nil {
return err
}
}
}
return nil
}
func StubMFARecords(app core.App) error {
superuser2, err := app.FindAuthRecordByEmail(core.CollectionNameSuperusers, "[email protected]")
if err != nil {
return err
}
superuser2.SetRaw("stubId", "superuser2")
superuser3, err := app.FindAuthRecordByEmail(core.CollectionNameSuperusers, "[email protected]")
if err != nil {
return err
}
superuser3.SetRaw("stubId", "superuser3")
user1, err := app.FindAuthRecordByEmail("users", "[email protected]")
if err != nil {
return err
}
user1.SetRaw("stubId", "user1")
now := types.NowDateTime()
old := types.NowDateTime().Add(-1 * time.Hour)
type mfaData struct {
method string
date types.DateTime
}
stubs := map[*core.Record][]mfaData{
superuser2: {
{core.MFAMethodOTP, now},
{core.MFAMethodOTP, old},
{core.MFAMethodPassword, now.Add(-2 * time.Minute)},
{core.MFAMethodPassword, now.Add(-1 * time.Millisecond)},
{core.MFAMethodOAuth2, old.Add(-1 * time.Millisecond)},
},
superuser3: {
{core.MFAMethodOAuth2, now.Add(-3 * time.Millisecond)},
{core.MFAMethodPassword, now.Add(-3 * time.Minute)},
},
user1: {
{core.MFAMethodOAuth2, old},
},
}
for record, idDates := range stubs {
for i, data := range idDates {
otp := core.NewMFA(app)
otp.Id = record.GetString("stubId") + "_" + strconv.Itoa(i)
otp.SetRecordRef(record.Id)
otp.SetCollectionRef(record.Collection().Id)
otp.SetMethod(data.method)
otp.SetRaw("created", data.date)
if err := app.SaveNoValidate(otp); err != nil {
return err
}
}
}
return nil
}
func StubLogsData(app *TestApp) error {
_, err := app.AuxDB().NewQuery(`
delete from {{_logs}};
insert into {{_logs}} (
[[id]],
[[level]],
[[message]],
[[data]],
[[created]],
[[updated]]
)
values
(
"873f2133-9f38-44fb-bf82-c8f53b310d91",
0,
"test_message1",
'{"status":200}',
"2022-05-01 10:00:00.123Z",
"2022-05-01 10:00:00.123Z"
),
(
"f2133873-44fb-9f38-bf82-c918f53b310d",
8,
"test_message2",
'{"status":400}',
"2022-05-02 10:00:00.123Z",
"2022-05-02 10:00:00.123Z"
);
`).Execute()
return err
}