-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
140 lines (121 loc) · 3.52 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package sncli
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/jonhadfield/gosn-v2/auth"
"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
"github.com/jonhadfield/gosn-v2/session"
)
var (
testSession *cache.Session
gTtestSession *session.Session
testUserEmail string
testUserPassword string
)
func localTestMain() {
localServer := "http://ramea:3000"
testUserEmail = fmt.Sprintf("ramea-%s", strconv.FormatInt(time.Now().UnixNano(), 16))
testUserPassword = "secretsanta"
rInput := auth.RegisterInput{
Password: testUserPassword,
Email: testUserEmail,
APIServer: localServer,
Version: "004",
Debug: true,
}
_, err := rInput.Register()
if err != nil {
panic(fmt.Sprintf("failed to register with: %s", localServer))
}
signIn(localServer, testUserEmail, testUserPassword)
}
func signIn(server, email, password string) {
ts, err := auth.CliSignIn(email, password, server, false)
if err != nil {
log.Fatal(err)
}
if server == "" {
server = SNServerURL
}
httpClient := common.NewHTTPClient()
httpClient.Logger = nil
gTtestSession = &session.Session{
Debug: false,
HTTPClient: httpClient,
SchemaValidation: false,
Server: server,
FilesServerUrl: ts.FilesServerUrl,
Token: "",
MasterKey: ts.MasterKey,
ItemsKeys: nil,
DefaultItemsKey: session.SessionItemsKey{},
KeyParams: auth.KeyParams{},
AccessToken: ts.AccessToken,
RefreshToken: ts.RefreshToken,
AccessExpiration: ts.AccessExpiration,
RefreshExpiration: ts.RefreshExpiration,
ReadOnlyAccess: ts.ReadOnlyAccess,
PasswordNonce: ts.PasswordNonce,
Schemas: nil,
}
testSession = &cache.Session{
Session: gTtestSession,
CacheDB: nil,
CacheDBPath: "",
}
}
func TestMain(m *testing.M) {
// if os.Getenv("SN_SERVER") == "" || strings.Contains(os.Getenv("SN_SERVER"), "ramea") {
if strings.Contains(os.Getenv("SN_SERVER"), "ramea") {
localTestMain()
} else {
signIn(SNServerURL, os.Getenv("SN_EMAIL"), os.Getenv("SN_PASSWORD"))
}
if _, err := items.Sync(items.SyncInput{Session: gTtestSession}); err != nil {
log.Fatal(err)
}
if gTtestSession.DefaultItemsKey.ItemsKey == "" {
panic("failed in TestMain due to empty default items key")
}
if strings.TrimSpace(gTtestSession.Server) == "" {
panic("failed in TestMain due to empty server")
}
var err error
testSession, err = cache.ImportSession(&auth.SignInResponseDataSession{
Debug: gTtestSession.Debug,
HTTPClient: gTtestSession.HTTPClient,
SchemaValidation: false,
Server: gTtestSession.Server,
FilesServerUrl: gTtestSession.FilesServerUrl,
Token: "",
MasterKey: gTtestSession.MasterKey,
KeyParams: gTtestSession.KeyParams,
AccessToken: gTtestSession.AccessToken,
RefreshToken: gTtestSession.RefreshToken,
AccessExpiration: gTtestSession.AccessExpiration,
RefreshExpiration: gTtestSession.RefreshExpiration,
ReadOnlyAccess: gTtestSession.ReadOnlyAccess,
PasswordNonce: gTtestSession.PasswordNonce,
}, "")
if err != nil {
return
}
testSession.CacheDBPath, err = cache.GenCacheDBPath(*testSession, "", common.LibName)
if err != nil {
panic(err)
}
os.Exit(m.Run())
}
// prevent throttling when using official server.
func testDelay() {
if strings.Contains(os.Getenv("SN_SERVER"), "api.standardnotes.com") {
time.Sleep(2 * time.Second)
}
}