-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_test.go
336 lines (273 loc) · 8.97 KB
/
user_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"context"
"strconv"
"testing"
"time"
"github.com/Microkubes/microservice-security/auth"
"github.com/Microkubes/microservice-user/app"
"github.com/Microkubes/microservice-user/app/test"
"github.com/Microkubes/microservice-user/store"
"github.com/keitaroinc/goa"
)
var db = store.NewDB()
var (
service = goa.New("user-test")
ctrl = NewUserController(service, db, nil)
ID = "5df2103b5f1b640001142d3c"
notFoundID = "5df2103b5f1b640001142d4c"
notFonundEmail = "[email protected]"
notFoundToken = "not-found-token"
badID = "bad-id"
badEmail = "[email protected]"
internalErrID = "internal-err-id"
internalErrEmail = "[email protected]"
internalErrToken = "internal-error-token"
)
func TestGetUserOK(t *testing.T) {
// Call generated test helper, this checks that the returned media type is of the
// correct type (i.e. uses view "default") and validates the media type.
// Also, it ckecks the returned status code
_, user := test.GetUserOK(t, context.Background(), service, ctrl, ID)
if user == nil {
t.Fatal("Nil user")
}
if user.ID != ID {
t.Errorf("Invalid user ID, expected %s, got %s", ID, user.ID)
}
}
// The test helper takes care of validating the status code for us
func TestGetUserNotFound(t *testing.T) {
test.GetUserNotFound(t, context.Background(), service, ctrl, notFoundID)
}
func TestGetUserBadRequest(t *testing.T) {
test.GetUserBadRequest(t, context.Background(), service, ctrl, badID)
}
func TestGetUserInternalServerError(t *testing.T) {
test.GetUserInternalServerError(t, context.Background(), service, ctrl, internalErrID)
}
func TestGetMeUserOK(t *testing.T) {
ctx := context.Background()
authObj := &auth.Auth{UserID: ID}
ctx = auth.SetAuth(ctx, authObj)
_, user := test.GetMeUserOK(t, ctx, service, ctrl)
if user == nil {
t.Fatal("Nil user")
}
if user.ID != ID {
t.Errorf("Invalid user ID: expected %s, got %s", ID, user.ID)
}
}
func TestGetMeUserNotFound(t *testing.T) {
ctx := context.Background()
authObj := &auth.Auth{UserID: notFoundID}
ctx = auth.SetAuth(ctx, authObj)
test.GetMeUserNotFound(t, ctx, service, ctrl)
}
func TestGetMeUserBadRequest(t *testing.T) {
ctx := context.Background()
authObj := &auth.Auth{UserID: badID}
ctx = auth.SetAuth(ctx, authObj)
test.GetMeUserBadRequest(t, ctx, service, ctrl)
}
func TestGetMeUserInternalServerError(t *testing.T) {
ctx := context.Background()
authObj := &auth.Auth{UserID: internalErrID}
ctx = auth.SetAuth(ctx, authObj)
test.GetMeUserInternalServerError(t, ctx, service, ctrl)
}
func TestCreateUserOK(t *testing.T) {
roles := []string{"admin", "user"}
password := "keitaro"
extID := "qwerc461f9f8eb02aae053f3"
CreateUserPayload := &app.CreateUserPayload{
Email: "[email protected]",
Password: &password,
ExternalID: &extID,
Roles: roles,
}
//CreateUserCreated
_, user := test.CreateUserCreated(t, context.Background(), service, ctrl, CreateUserPayload)
if user == nil {
t.Fatal("User not created")
}
}
func TestCreateUserBadRequest(t *testing.T) {
CreateUserPayload := &app.CreateUserPayload{
Email: "[email protected]",
Roles: []string{"admin", "user"},
}
test.CreateUserBadRequest(t, context.Background(), service, ctrl, CreateUserPayload)
}
func TestCreateUserInternalServerError(t *testing.T) {
password := "keitaro"
extID := "qwerc461f9f8eb02aae053f3"
CreateUserPayload := &app.CreateUserPayload{
Password: &password,
Email: "[email protected]",
ExternalID: &extID,
Roles: []string{"admin", "user"},
}
test.CreateUserInternalServerError(t, context.Background(), service, ctrl, CreateUserPayload)
}
func TestUpdateUserOK(t *testing.T) {
roles := []string{"admin"}
UpdateUserPayload := &app.UpdateUserPayload{
Roles: roles,
}
_, users := test.UpdateUserOK(t, context.Background(), service, ctrl, ID, UpdateUserPayload)
if users == nil {
t.Fatal("Expected the update user data.")
}
}
func TestUpdateUserNotFound(t *testing.T) {
UpdateUserPayload := &app.UpdateUserPayload{
Roles: []string{"admin", "user"},
}
test.UpdateUserNotFound(t, context.Background(), service, ctrl, notFoundID, UpdateUserPayload)
}
func TestUpdateUserBadRequest(t *testing.T) {
UpdateUserPayload := &app.UpdateUserPayload{
Roles: []string{"admin", "user"},
}
test.UpdateUserBadRequest(t, context.Background(), service, ctrl, badID, UpdateUserPayload)
}
func TestUpdateUserInternalServerError(t *testing.T) {
UpdateUserPayload := &app.UpdateUserPayload{
Roles: []string{"admin", "user"},
}
test.UpdateUserInternalServerError(t, context.Background(), service, ctrl, internalErrID, UpdateUserPayload)
}
func TestFindUserBadRequest(t *testing.T) {
payload := &app.Credentials{
Email: "",
Password: "",
}
test.FindUserBadRequest(t, context.Background(), service, ctrl, payload)
}
func TestFindUserInternalServerError(t *testing.T) {
payload := &app.Credentials{
Email: internalErrEmail,
Password: "the-pass",
}
test.FindUserInternalServerError(t, context.Background(), service, ctrl, payload)
}
func TestFindUserNotFound(t *testing.T) {
payload := &app.Credentials{
Email: "[email protected]",
Password: "the-pass",
}
test.FindUserNotFound(t, context.Background(), service, ctrl, payload)
}
func TestFindUserOK(t *testing.T) {
payload := &app.Credentials{
Email: "[email protected]",
Password: "keitaro",
}
_, user := test.FindUserOK(t, context.Background(), service, ctrl, payload)
if user == nil {
t.Fatal("Expected user")
}
}
func TestFindByEmailUserOK(t *testing.T) {
payload := &app.EmailPayload{
Email: "[email protected]",
}
_, user := test.FindByEmailUserOK(t, context.Background(), service, ctrl, payload)
if user == nil {
t.Fatal("Nil user")
}
}
func TestFindByEmailUserNotFound(t *testing.T) {
payload := &app.EmailPayload{
Email: notFonundEmail,
}
test.FindByEmailUserNotFound(t, context.Background(), service, ctrl, payload)
}
func TestFindByEmailUserInternalServerError(t *testing.T) {
payload := &app.EmailPayload{
Email: internalErrEmail,
}
test.FindByEmailUserInternalServerError(t, context.Background(), service, ctrl, payload)
}
func TestResetVerificationTokenUserOK(t *testing.T) {
test.ResetVerificationTokenUserOK(t, context.Background(), service, ctrl, &app.EmailPayload{
Email: "[email protected]",
})
}
func TestResetVerificationTokenUserNotFound(t *testing.T) {
test.ResetVerificationTokenUserNotFound(t, context.Background(), service, ctrl, &app.EmailPayload{
Email: notFonundEmail,
})
}
func TestResetVerificationTokenUserBadRequest(t *testing.T) {
test.ResetVerificationTokenUserBadRequest(t, context.Background(), service, ctrl, &app.EmailPayload{
Email: badEmail,
})
test.ResetVerificationTokenUserBadRequest(t, context.Background(), service, ctrl, &app.EmailPayload{})
}
func TestResetVerificationTokenUserInternalServerError(t *testing.T) {
test.ResetVerificationTokenUserInternalServerError(t, context.Background(), service, ctrl, &app.EmailPayload{
Email: internalErrEmail,
})
}
func TestVerifyUserOK(t *testing.T) {
token := "sdaewefdc234erfdd123erfdxc23edx"
test.VerifyUserOK(t, context.Background(), service, ctrl, &token)
}
func TestVerifyUserNotFound(t *testing.T) {
token := notFoundToken
test.VerifyUserNotFound(t, context.Background(), service, ctrl, &token)
}
func TestVerifyUserInternalServerError(t *testing.T) {
token := internalErrToken
test.VerifyUserInternalServerError(t, context.Background(), service, ctrl, &token)
}
func TestGenerateToken(t *testing.T) {
token := generateToken(40)
if len(token) != 56 {
t.Errorf("Expected token length was 56, got %d", len(token))
}
}
func TestGenerateExpDate(t *testing.T) {
expDate := generateExpDate()
time := strconv.Itoa(int(time.Now().UTC().Unix()/60) + (60 * 24))
if len(expDate) != len(time) {
t.Errorf("expDate wrong format")
}
if expDate != time {
t.Error("expDate value is not expected value")
}
}
func TestCheckExpDate(t *testing.T) {
expDate := strconv.Itoa(int(time.Now().UTC().Unix()/60) + (60 * 24))
if !checkExpDate(expDate) {
t.Errorf("expDate is expired, Expected value: [true]")
}
expDate = strconv.Itoa(int(time.Now().UTC().Unix()/60) - (60 * 24))
if checkExpDate(expDate) {
t.Errorf("expDate is not expired, Expected value [false]")
}
}
func TestStringToBcryptHash(t *testing.T) {
_, err := stringToBcryptHash("keitaro")
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
}
func TestGetAllUserOK(t *testing.T) {
ctx := context.Background()
authObj := &auth.Auth{UserID: ID}
ctx = auth.SetAuth(ctx, authObj)
test.GetAllUserOK(t, ctx, service, ctrl, nil, nil, nil, nil)
}
func TestGetAllUserNotFound(t *testing.T) {
ctx := context.Background()
authObj := &auth.Auth{UserID: ID}
ctx = auth.SetAuth(ctx, authObj)
offset := 5
test.GetAllUserNotFound(t, ctx, service, ctrl, nil, &offset, nil, nil)
}
func TestGetAllUserInternalServerError(t *testing.T) {
test.GetAllUserInternalServerError(t, context.Background(), service, ctrl, nil, nil, nil, nil)
}