This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
main_test.go
122 lines (94 loc) · 2.38 KB
/
main_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package staticbackend
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/config"
"github.com/staticbackendhq/core/model"
)
const (
dbName = "unittest"
admEmail = "[email protected]"
password = "my_unittest_pw"
userEmail = "[email protected]"
userPassword = "another_fake_password"
)
var (
extexec *extras
funexec *functions
wsURL string
pubKey string
adminToken string
userToken string
rootToken string
testAccountID string
mship *membership
db *Database
acct *accounts
)
func TestMain(m *testing.M) {
config.Current = config.LoadConfig()
backend.Setup(config.Current)
db = &Database{cache: backend.Cache, log: backend.Log}
acct = &accounts{log: backend.Log}
mship = &membership{log: backend.Log}
deleteAndSetupTestAccount()
//TODO: re-enable this when we move back to WebSocket instead of SSE
/*
hub := newHub(backend.Cache)
go hub.run()
*/
ws := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//TODO: re-enable this when we move back to WebSocket instead of SSE
//serveWs(backend.Log, hub, w, r)
}))
defer ws.Close()
wsURL = "ws" + strings.TrimPrefix(ws.URL, "http")
funexec = &functions{datastore: backend.DB, dbName: dbName}
extexec = &extras{}
os.Exit(m.Run())
}
func deleteAndSetupTestAccount() {
if err := backend.DB.DeleteTenant(dbName, admEmail); err != nil {
log.Fatal(err)
}
cus := model.Tenant{
Email: admEmail,
}
cus, err := backend.DB.CreateTenant(cus)
if err != nil {
log.Fatal(err)
}
base := model.DatabaseConfig{
ID: dbName,
TenantID: cus.ID,
Name: dbName,
AllowedDomain: []string{"localhost"},
IsActive: true,
Created: time.Now(),
}
base, err = backend.DB.CreateDatabase(base)
if err != nil {
log.Fatal(err)
}
pubKey = base.ID
usrSvc := backend.Membership(base)
token, dbToken, err := usrSvc.CreateAccountAndUser(admEmail, password, 100)
if err != nil {
log.Fatal(err)
}
adminToken = string(token)
rootToken = fmt.Sprintf("%s|%s|%s", dbToken.ID, dbToken.AccountID, dbToken.Token)
testAccountID = dbToken.AccountID
token, _, err = usrSvc.CreateUser(dbToken.AccountID, userEmail, userPassword, 0)
if err != nil {
log.Fatal(err)
}
userToken = string(token)
}