/
actionlogin_test.go
301 lines (251 loc) · 6.47 KB
/
actionlogin_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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2016 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/andreaskoch/dee-ns"
"github.com/spf13/afero"
"testing"
)
// The Name function should return "login"
func Test_loginAction_Name_ResultIsLogin(t *testing.T) {
// arrange
credentialStore := testCredentialsStore{saveFunc: func(credentials deens.APICredentials) error {
return nil
}}
login := loginAction{credentialStore}
// act
result := login.Name()
// assert
if result != "login" {
t.Fail()
t.Logf("login.Name() should return %q but returned %q instead", "login", result)
}
}
// The Description function return something
func Test_loginAction_Description_ResultIsNotEmpty(t *testing.T) {
// arrange
credentialStore := testCredentialsStore{saveFunc: func(credentials deens.APICredentials) error {
return nil
}}
login := loginAction{credentialStore}
// act
result := login.Description()
// assert
if isEmpty(result) {
t.Fail()
t.Logf("login.Description() should return something")
}
}
// The Usage function return something
func Test_loginAction_Usage_ResultIsNotEmpty(t *testing.T) {
// arrange
credentialStore := testCredentialsStore{saveFunc: func(credentials deens.APICredentials) error {
return nil
}}
login := loginAction{credentialStore}
// act
result := login.Usage()
// assert
if isEmpty(result) {
t.Fail()
t.Logf("login.Usage() should return something")
}
}
// If invalid arguments are supplied an error should be returned.
func Test_loginAction_InvalidArguments_ErrorIsReturned(t *testing.T) {
// arrange
validArgumentsSet := [][]string{
{
"-e-mail",
"user@example.com",
"-apitoken",
"123456",
},
{
"--eMail",
"user@example.com",
"--apitoken",
"123456",
},
{
"-email=user@example.com",
"-token=123456",
},
{
"--email=user@example.com",
"----apitoken=123456",
},
}
for _, arguments := range validArgumentsSet {
credentialStore := testCredentialsStore{saveFunc: func(credentials deens.APICredentials) error {
return nil
}}
login := loginAction{credentialStore}
// act
_, err := login.Execute(arguments)
// assert
if err == nil {
t.Fail()
t.Logf("login.Execute(%q) should return an error", arguments)
}
}
}
// If valid arguments are supplied to the login method no error should be returned.
func Test_loginAction_ValidArguments_NoErrorIsReturned(t *testing.T) {
// arrange
validArgumentsSet := [][]string{
{
"-email",
"user@example.com",
"-apitoken",
"123456",
},
{
"--email",
"user@example.com",
"--apitoken",
"123456",
},
{
"-email=user@example.com",
"-apitoken=123456",
},
{
"--email=user@example.com",
"--apitoken=123456",
},
}
for _, arguments := range validArgumentsSet {
credentialStore := testCredentialsStore{saveFunc: func(credentials deens.APICredentials) error {
return nil
}}
login := loginAction{credentialStore}
// act
_, err := login.Execute(arguments)
// assert
if err != nil {
t.Fail()
t.Logf("login.Execute(%q) should not return an error: %q", arguments, err.Error())
}
}
}
// If the supplied argument values are invalid an error should be returned
func Test_loginAction_ValidArguments_ButInvalidValues_ErrorIsReturned(t *testing.T) {
// arrange
validArgumentsSet := [][]string{
{
"-email",
"",
"-apitoken",
"123456",
},
{
"--email",
"user@example.com",
"--apitoken",
"",
},
{
"-email= ",
"-apitoken=123456",
},
{
"--email=user@example.com",
"--apitoken= ",
},
}
for _, arguments := range validArgumentsSet {
credentialStore := testCredentialsStore{saveFunc: func(credentials deens.APICredentials) error {
return nil
}}
login := loginAction{credentialStore}
// act
_, err := login.Execute(arguments)
// assert
if err == nil {
t.Fail()
t.Logf("login.Execute(%q) should return an error", arguments)
}
}
}
// Integration test.
func Test_loginAction_ValidArguments_CredentialFileIsCreated(t *testing.T) {
// arrange
filesystem := afero.NewMemMapFs()
credentialStore := newFilesystemCredentialStore(filesystem, "/home/testuser/.dee/credentials.json")
arguments := []string{
"-email",
"user@example.com",
"-apitoken",
"123456",
}
login := loginAction{credentialStore}
// act
login.Execute(arguments)
// assert
fileInfo, err := filesystem.Stat("/home/testuser/.dee/credentials.json")
if fileInfo == nil || err != nil {
t.Fail()
t.Logf("login.Execute(%q) create the credential file and should not return an error: %s", arguments, err.Error())
}
}
func Test_loginAction_Login_InvalidCredentials_ErrorIsReturned(t *testing.T) {
// arrange
var inputs = [][]string{
{"--email=example@example.com", "--apitoken"},
{"--email", "--apitoken=12456"},
{"--email", "--apitoken"},
{"--email=\" \"", "--apitoken=\" \""},
}
loginAction := loginAction{}
// act
for _, arguments := range inputs {
_, err := loginAction.Execute(arguments)
// assert
if err == nil {
t.Fail()
t.Logf("login.Execute(%q) should return an error because the input is invalid.", arguments)
}
}
}
func Test_loginAction_Login_ValidCredentials_CredentialsArePassedToCredentialStore(t *testing.T) {
// arrange
var inputs = [][]string{
{"-email", "example@example.com", "-apitoken", "1234"},
{"-email", "example@example", "-apitoken", "a"},
{"-email", "test+test@example.co.uk", "-apitoken", "ölö23p4k23lö4köl23k4öä"},
}
for _, arguments := range inputs {
credStore := testCredentialsStore{
saveFunc: func(credentials deens.APICredentials) error {
// assert
if credentials.Email != arguments[1] || credentials.Token != arguments[3] {
t.Fail()
t.Logf("Login(%q, %q) passed invalid credentials to the Save function of the credential store: %s", arguments[1], arguments[3], credentials)
}
return nil
},
}
loginAction := loginAction{credStore}
// act
loginAction.Execute(arguments)
}
}
func Test_loginAction_Login_ValidCredentials_CredentialStoreSaveFails_ErrorIsReturned(t *testing.T) {
// arrange
credStore := testCredentialsStore{
saveFunc: func(credentials deens.APICredentials) error {
return fmt.Errorf("Save failed")
},
}
loginAction := loginAction{credStore}
// act
_, err := loginAction.Execute([]string{"-email", "example@example.com", "-apitoken", "1234"})
// assert
if err == nil {
t.Fail()
t.Logf("If the save at the credential store fails Login should return an error.")
}
}