This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
155 lines (120 loc) · 4.04 KB
/
session_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package gosn
import (
"fmt"
"os"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
type MockKeyRingDodgy struct {
}
func (k MockKeyRingDodgy) Set(user, service, password string) error {
return fmt.Errorf("failed to set session")
}
func (k MockKeyRingDodgy) Get(service, user string) (r string, err error) {
return "an invalid session", nil
}
func (k MockKeyRingDodgy) Delete(service, user string) error {
return nil
}
type MockKeyRingDefined struct {
}
func (k MockKeyRingDefined) Set(user, service, password string) error {
return nil
}
func (k MockKeyRingDefined) Get(service, user string) (r string, err error) {
return "someone@example.com;https://sync.standardnotes.org;eyJhbGciOiJKUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c;8f0f5166841ca4dee2975c74cc7e0a4345ce24b54d7b215677a3d540303aa203;6d5ffc6f8e337e6e3ae6d0c3201d9e2d00ffee64672bc4fe1886ad31770c19f1", nil
}
func (k MockKeyRingDefined) Delete(service, user string) error {
return nil
}
type MockKeyRingUnDefined struct {
}
func (k MockKeyRingUnDefined) Set(user, service, password string) error {
return nil
}
func (k MockKeyRingUnDefined) Get(service, user string) (r string, err error) {
return "", nil
}
func (k MockKeyRingUnDefined) Delete(service, user string) error {
return nil
}
var (
testSessionEmail = "me@home.com"
testSessionServer = "https://sync.server.com"
testSessionToken = "testsessiontoken"
testSessionAk = "testsessionak"
testSessionMk = "testsessionmk"
testSession = fmt.Sprintf("%s;%s;%s;%s;%s", testSessionEmail, testSessionServer,
testSessionToken, testSessionAk, testSessionMk)
)
func TestMakeSessionString(t *testing.T) {
sess := Session{
Token: testSessionToken,
Mk: testSessionMk,
Ak: testSessionAk,
Server: testSessionServer,
}
ss := makeSessionString(testSessionEmail, sess)
assert.Equal(t, testSession, ss)
}
func TestWriteSession(t *testing.T) {
var kEmpty MockKeyRingDodgy
assert.Error(t, writeSession("example", kEmpty))
var kDefined MockKeyRingDefined
assert.NoError(t, SessionExists(kDefined))
}
func TestAddSession(t *testing.T) {
viper.SetEnvPrefix("sn")
assert.NoError(t, viper.BindEnv("email"))
assert.NoError(t, viper.BindEnv("password"))
assert.NoError(t, viper.BindEnv("server"))
serverURL := os.Getenv("SN_SERVER")
if serverURL == "" {
serverURL = SNServerURL
}
_, err := AddSession(serverURL, "", MockKeyRingUnDefined{})
assert.NoError(t, err)
}
func TestSessionExists(t *testing.T) {
var kEmpty MockKeyRingUnDefined
assert.Error(t, SessionExists(kEmpty))
var kDefined MockKeyRingDefined
assert.NoError(t, SessionExists(kDefined))
}
func TestRemoveSession(t *testing.T) {
var kUndefined MockKeyRingUnDefined
assert.Contains(t, RemoveSession(kUndefined), "failed")
var kDefined MockKeyRingDefined
assert.Contains(t, RemoveSession(kDefined), "success")
}
func TestSessionStatus(t *testing.T) {
// if session is undefined then session value should
// be empty and error returned to reflect that
var kUndefined MockKeyRingUnDefined
s, err := SessionStatus("", kUndefined)
assert.Error(t, err)
assert.Contains(t, err.Error(), "empty")
assert.Empty(t, s)
// if session is not empty but a value is found then
// assume session is not encrypted
var kDefined MockKeyRingDefined
s, err = SessionStatus("", kDefined)
assert.NoError(t, err)
assert.Contains(t, s, "session found: someone@example.com")
// if stored session value is not immediately valid
// then session is assumed to be encrypted so ensure
// a key, if not provided, is flagged
var kDodgy MockKeyRingDodgy
s, err = SessionStatus("", kDodgy)
assert.Error(t, err)
assert.Contains(t, err.Error(), "key required")
assert.Empty(t, s)
// if stored session value is not immediately valid
// then session is assumed to be encrypted so ensure
// session that cannot be encrypted is flagged
s, err = SessionStatus("somekey", kDodgy)
assert.Error(t, err)
assert.Contains(t, err.Error(), "corrupt")
assert.Empty(t, s)
}