-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathclient_test.go
268 lines (234 loc) · 7.11 KB
/
client_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package cmd_test
import (
"testing"
"github.com/smartcontractkit/chainlink/cmd"
"github.com/smartcontractkit/chainlink/internal/cltest"
"github.com/smartcontractkit/chainlink/store/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTerminalCookieAuthenticator_AuthenticateWithoutSession(t *testing.T) {
tests := []struct {
name, email, pwd string
}{
{"bad email", "notreal", cltest.Password},
{"bad pwd", cltest.APIEmail, "mostcommonwrongpwdever"},
{"bad both", "notreal", "mostcommonwrongpwdever"},
{"correct", cltest.APIEmail, cltest.Password},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
app, cleanup := cltest.NewApplication()
defer cleanup()
sr := models.SessionRequest{Email: test.email, Password: test.pwd}
store := &cmd.MemoryCookieStore{}
tca := cmd.NewSessionCookieAuthenticator(app.Config, store)
cookie, err := tca.Authenticate(sr)
assert.Error(t, err)
assert.Nil(t, cookie)
cookie, err = store.Retrieve()
assert.NoError(t, err)
assert.Nil(t, cookie)
})
}
}
func TestTerminalCookieAuthenticator_AuthenticateWithSession(t *testing.T) {
app, cleanup := cltest.NewApplication()
defer cleanup()
app.MustSeedUserSession()
tests := []struct {
name, email, pwd string
wantError bool
}{
{"bad email", "notreal", cltest.Password, true},
{"bad pwd", cltest.APIEmail, "mostcommonwrongpwdever", true},
{"bad both", "notreal", "mostcommonwrongpwdever", true},
{"success", cltest.APIEmail, cltest.Password, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
sr := models.SessionRequest{Email: test.email, Password: test.pwd}
store := &cmd.MemoryCookieStore{}
tca := cmd.NewSessionCookieAuthenticator(app.Config, store)
cookie, err := tca.Authenticate(sr)
if test.wantError {
assert.Error(t, err)
assert.Nil(t, cookie)
cookie, err = store.Retrieve()
assert.NoError(t, err)
assert.Nil(t, cookie)
} else {
assert.NoError(t, err)
assert.NotNil(t, cookie)
retrievedCookie, err := store.Retrieve()
assert.NoError(t, err)
assert.Equal(t, cookie, retrievedCookie)
}
})
}
}
func TestDiskCookieStore_Retrieve(t *testing.T) {
tc, cleanup := cltest.NewConfig()
defer cleanup()
config := tc.Config
tests := []struct {
name string
rootDir string
wantError bool
}{
{"missing", config.RootDir(), true},
{"correct fixture", "../internal/fixtures", false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config.Set("RootDir", test.rootDir)
store := cmd.DiskCookieStore{Config: config}
cookie, err := store.Retrieve()
if test.wantError {
assert.Error(t, err)
assert.Nil(t, cookie)
} else {
assert.NoError(t, err)
assert.NotNil(t, cookie)
}
})
}
}
func TestTerminalAPIInitializer_InitializeWithoutAPIUser(t *testing.T) {
tests := []struct {
name string
enteredStrings []string
isTerminal bool
isError bool
}{
{"correct", []string{"good@email.com", "password"}, true, false},
{"incorrect pwd then correct", []string{"good@email.com", "", "good@email.com", "password"}, true, false},
{"incorrect email then correct", []string{"", "password", "good@email.com", "password"}, true, false},
{"not a terminal", []string{}, false, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
store, cleanup := cltest.NewStore()
defer cleanup()
mock := &cltest.MockCountingPrompter{EnteredStrings: test.enteredStrings, NotTerminal: !test.isTerminal}
tai := cmd.NewPromptingAPIInitializer(mock)
user, err := tai.Initialize(store)
if test.isError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, len(test.enteredStrings), mock.Count)
persistedUser, err := store.FindUser()
assert.NoError(t, err)
assert.Equal(t, user.Email, persistedUser.Email)
assert.Equal(t, user.HashedPassword, persistedUser.HashedPassword)
}
})
}
}
func TestTerminalAPIInitializer_InitializeWithExistingAPIUser(t *testing.T) {
store, cleanup := cltest.NewStore()
defer cleanup()
initialUser := cltest.MustUser(cltest.APIEmail, cltest.Password)
require.NoError(t, store.Save(&initialUser))
mock := &cltest.MockCountingPrompter{}
tai := cmd.NewPromptingAPIInitializer(mock)
user, err := tai.Initialize(store)
assert.NoError(t, err)
assert.Equal(t, 0, mock.Count)
assert.Equal(t, initialUser.Email, user.Email)
assert.Equal(t, initialUser.HashedPassword, user.HashedPassword)
}
func TestFileAPIInitializer_InitializeWithoutAPIUser(t *testing.T) {
tests := []struct {
name string
file string
wantError bool
}{
{"correct", "../internal/fixtures/apicredentials", false},
{"no file", "", true},
{"incorrect file", "/tmp/doesnotexist", true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
store, cleanup := cltest.NewStore()
defer cleanup()
tfi := cmd.NewFileAPIInitializer(test.file)
user, err := tfi.Initialize(store)
if test.wantError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, cltest.APIEmail, user.Email)
persistedUser, err := store.FindUser()
assert.NoError(t, err)
assert.Equal(t, persistedUser.Email, user.Email)
}
})
}
}
func TestFileAPIInitializer_InitializeWithExistingAPIUser(t *testing.T) {
store, cleanup := cltest.NewStore()
defer cleanup()
initialUser := cltest.MustUser(cltest.APIEmail, cltest.Password)
require.NoError(t, store.Save(&initialUser))
tests := []struct {
name string
file string
wantError bool
}{
{"correct", "../internal/fixtures/apicredentials", false},
{"no file", "", true},
{"incorrect file", "/tmp/doesnotexist", true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tfi := cmd.NewFileAPIInitializer(test.file)
user, err := tfi.Initialize(store)
assert.NoError(t, err)
assert.Equal(t, initialUser.Email, user.Email)
})
}
}
func TestPromptingSessionRequestBuilder(t *testing.T) {
t.Parallel()
tests := []struct {
email, pwd string
}{
{"correct@input.com", "mypwd"},
}
for _, test := range tests {
t.Run(test.email, func(t *testing.T) {
enteredStrings := []string{test.email, test.pwd}
prompter := &cltest.MockCountingPrompter{EnteredStrings: enteredStrings}
builder := cmd.NewPromptingSessionRequestBuilder(prompter)
sr, err := builder.Build("")
require.NoError(t, err)
assert.Equal(t, test.email, sr.Email)
assert.Equal(t, test.pwd, sr.Password)
})
}
}
func TestFileSessionRequestBuilder(t *testing.T) {
t.Parallel()
builder := cmd.NewFileSessionRequestBuilder()
tests := []struct {
name, file, wantEmail string
wantError bool
}{
{"empty", "", "", true},
{"correct file", "../internal/fixtures/apicredentials", "email@test.net", false},
{"incorrect file", "/tmp/dontexist", "", true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
sr, err := builder.Build(test.file)
assert.Equal(t, test.wantEmail, sr.Email)
if test.wantError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}