-
Notifications
You must be signed in to change notification settings - Fork 13
/
auth_mutations.resolvers_test.go
732 lines (707 loc) · 22.7 KB
/
auth_mutations.resolvers_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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
package resolver_test
import (
"context"
"fmt"
"go-template/daos"
"reflect"
"strings"
"testing"
"github.com/volatiletech/null/v8"
fm "go-template/gqlmodels"
"go-template/internal/config"
"go-template/internal/jwt"
"go-template/internal/service"
"go-template/models"
"go-template/pkg/utl/resultwrapper"
"go-template/pkg/utl/secure"
"go-template/resolver"
"go-template/testutls"
"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"
)
const (
UserRoleName = "UserRole"
SuperAdminRoleName = "SuperAdminRole"
ErrorFromRedisCache = "RedisCache Error"
ErrorFromGetRole = "RedisCache GetRole Error"
ErrorUnauthorizedUser = "Unauthorized User"
ErrorFromCreateRole = "CreateRole Error"
ErrorPasswordValidation = "Fail on PasswordValidation"
ErrorActiveStatus = "Fail on ActiveStatus"
ErrorInsecurePassword = "Insecure password"
ErrorInvalidToken = "Fail on FindByToken"
ErrorUpdateUser = "User Update Error"
ErrorDeleteUser = "User Delete Error"
ErrorFromConfig = "Config Error"
ErrorFromBool = "Boolean Error"
ErrorMsgFromConfig = "error in loading config"
ErrorMsginvalidToken = "error from FindByToken"
ErrorMsgFindingUser = "error in finding the user"
ErrorMsgFromJwt = "error in creating auth service"
ErrorMsgfromUpdateUser = "error while updating user"
ErrorMsgPasswordValidation = "username or password does not exist "
TestPasswordHash = "$2a$10$dS5vK8hHmG5"
OldPasswordHash = "$2a$10$dS5vK8hHmG5gzwV8f7TK5.WHviMBqmYQLYp30a3XvqhCW9Wvl2tOS"
SuccessCase = "Success"
ErrorFindingUser = "Fail on finding user"
ErrorFromCreateUser = "Fail on Create User"
ErrorFromThrottleCheck = "Throttle error"
ErrorFromJwt = "Jwt Error"
ErrorFromGenerateToken = "Token Error"
OldPassword = "adminuser"
NewPassword = "adminuser!A9@"
TestPassword = "pass123"
TestUsername = "wednesday"
TestToken = "refreshToken"
ReqToken = "refresh_token"
)
type loginArgs struct {
UserName string
Password string
}
type loginType struct {
name string
req loginArgs
wantResp *fm.LoginResponse
wantErr bool
err error
init func() *gomonkey.Patches
}
func errorFindingUserCase() loginType {
return loginType{
name: ErrorFindingUser,
req: loginArgs{
UserName: TestUsername,
Password: TestPassword,
},
wantErr: true,
err: fmt.Errorf("%s", ErrorMsgFindingUser),
init: func() *gomonkey.Patches {
return gomonkey.ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
return nil, fmt.Errorf("%s", ErrorMsgFindingUser)
})
},
}
}
func errorPasswordValidationCase() loginType {
return loginType{
name: ErrorPasswordValidation,
req: loginArgs{
UserName: testutls.MockEmail,
Password: TestPassword,
},
wantErr: true,
err: fmt.Errorf("%s", ErrorMsgPasswordValidation),
init: func() *gomonkey.Patches {
tg := jwt.Service{}
sec := secure.Service{}
return gomonkey.ApplyFunc(service.Secure, func(cfg *config.Configuration) secure.Service {
return sec
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, nil
}).ApplyFunc(config.Load, func() (*config.Configuration, error) {
return &config.Configuration{}, nil
}).ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, nil
}).ApplyFunc(tg.GenerateToken, func(u *models.User) (string, error) {
return "", fmt.Errorf("%s", ErrorMsgPasswordValidation)
})
},
}
}
func errorActiveStatusCase() loginType {
return loginType{
name: ErrorActiveStatus,
req: loginArgs{
UserName: testutls.MockEmail,
Password: OldPassword,
},
wantErr: true,
err: resultwrapper.ErrUnauthorized,
init: func() *gomonkey.Patches {
tg := jwt.Service{}
sec := secure.Service{}
// mock FindUserByUserName with the proper password, and active state
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, nil
}).
ApplyFunc(service.Secure, func(cfg *config.Configuration) secure.Service {
return sec
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, nil
}).ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, nil
}).ApplyFunc(tg.GenerateToken, func(u *models.User) (string, error) {
return "", nil
})
},
}
}
func errorFromConfigCase() loginType {
return loginType{
name: ErrorFromConfig,
req: loginArgs{
UserName: testutls.MockEmail,
Password: OldPassword,
},
wantErr: true,
err: fmt.Errorf("%s", ErrorMsgFromConfig),
init: func() *gomonkey.Patches {
return gomonkey.ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, nil
}).
ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, fmt.Errorf("error in loading config")
})
},
}
}
func errorWhileGeneratingToken() loginType {
return loginType{
name: ErrorFromJwt,
req: loginArgs{
UserName: testutls.MockEmail,
Password: OldPassword,
},
wantErr: true,
err: fmt.Errorf("%s", ErrorMsgFromJwt),
init: func() *gomonkey.Patches {
tg := jwt.Service{}
sec := secure.Service{}
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, nil
}).ApplyMethod(reflect.TypeOf(tg), "GenerateToken",
func(jwt.Service, *models.User) (string, error) {
return "", nil
}).ApplyFunc(service.Secure, func(cfg *config.Configuration) secure.Service {
return sec
}).ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPassword)
user.Active = null.BoolFrom(true)
return user, nil
}).ApplyMethod(reflect.TypeOf(sec), "HashMatchesPassword", func(sec secure.Service, hash string, password string) bool {
return true
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, fmt.Errorf("%s", ErrorMsgFromJwt)
})
},
}
}
func errorUpdateUserCase() loginType {
err := fmt.Errorf("%s", ErrorMsgfromUpdateUser)
return loginType{
name: ErrorUpdateUser,
req: loginArgs{
UserName: testutls.MockEmail,
Password: OldPassword,
},
wantErr: true,
err: err,
init: func() *gomonkey.Patches {
tg := jwt.Service{}
sec := secure.Service{}
return gomonkey.ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, nil
}).ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(true)
return user, nil
}).
ApplyFunc(service.Secure, func(cfg *config.Configuration) secure.Service {
return sec
}).
ApplyMethod(reflect.TypeOf(tg), "GenerateToken",
func(jwt.Service, *models.User) (string, error) {
return "", nil
}).ApplyMethod(reflect.TypeOf(sec), "HashMatchesPassword",
func(sec secure.Service, hash string, password string) bool {
return true
}).
ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, nil
}).
ApplyMethod(reflect.TypeOf(sec), "Token",
func(secure.Service, string) string {
return ""
}).
ApplyFunc(daos.UpdateUser,
func(u models.User, ctx context.Context) (models.User, error) {
return models.User{}, err
}).ApplyMethod(reflect.TypeOf(sec), "Token",
func(secure.Service, string) string {
return TestToken
})
},
}
}
func loginSuccessCase() loginType {
jwtToken := "jwttokenstring"
return loginType{
name: SuccessCase,
req: loginArgs{
UserName: testutls.MockEmail,
Password: OldPassword,
},
wantResp: &fm.LoginResponse{
Token: jwtToken,
RefreshToken: TestToken,
},
init: func() *gomonkey.Patches {
tg := jwt.Service{}
sec := secure.Service{}
return gomonkey.ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(true)
return user, nil
}).
ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, nil
}).
ApplyFunc(service.Secure, func(cfg *config.Configuration) secure.Service {
return sec
}).
ApplyMethod(reflect.TypeOf(tg), "GenerateToken",
func(jwt.Service, *models.User) (string, error) {
return jwtToken, nil
}).
ApplyMethod(reflect.TypeOf(sec), "Token",
func(secure.Service, string) string {
return TestToken
}).
ApplyFunc(daos.UpdateUser,
func(u models.User, ctx context.Context) (models.User, error) {
return *testutls.MockUser(), nil
}).ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, nil
})
},
}
}
func errorWhileCreatingJWTService() loginType {
err := fmt.Errorf("error in creating auth service")
return loginType{
name: "Error while creating a JWT Service",
req: loginArgs{
UserName: testutls.MockEmail,
Password: OldPassword,
},
wantErr: true,
err: err,
init: func() *gomonkey.Patches {
sec := secure.Service{}
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, nil
}).ApplyFunc(service.Secure, func(cfg *config.Configuration) secure.Service {
return sec
}).ApplyFunc(daos.FindUserByUserName,
func(username string, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, nil
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
// mock service.JWT
return jwt.Service{}, err
})
},
}
}
func loadLoginTestCases() []loginType {
return []loginType{
errorWhileGeneratingToken(),
errorFindingUserCase(),
errorFromConfigCase(),
errorPasswordValidationCase(),
errorActiveStatusCase(),
errorWhileCreatingJWTService(),
errorUpdateUserCase(),
loginSuccessCase(),
}
}
func TestLogin(
t *testing.T,
) {
cases := loadLoginTestCases()
// Create a new instance of the resolver
resolver1 := resolver.Resolver{}
for _, tt := range cases {
t.Run(
tt.name,
func(t *testing.T) {
patch := tt.init()
c := context.Background()
// Call the login mutation with the given arguments and check the response and error against the expected values
response, err := resolver1.Mutation().Login(c, tt.req.UserName, tt.req.Password)
if tt.wantResp != nil &&
response != nil {
// Assert that the expected response matches the actual response
assert.Equal(t, tt.wantResp, response)
} else {
// Assert that the expected error value matches the actual error value
assert.Equal(t, true, strings.Contains(err.Error(), tt.err.Error()))
assert.Equal(t, tt.wantErr, err != nil)
}
if patch != nil {
patch.Reset()
}
},
)
}
}
type changeReq struct {
OldPassword string
NewPassword string
}
type changePasswordType struct {
name string
req changeReq
wantResp *fm.ChangePasswordResponse
wantErr bool
init func() *gomonkey.Patches
}
func changePasswordErrorFindingUserCase() changePasswordType {
return changePasswordType{
name: ErrorFindingUser,
req: changeReq{
OldPassword: TestPassword,
NewPassword: NewPassword,
},
wantErr: true,
init: func() *gomonkey.Patches {
return gomonkey.ApplyFunc(daos.FindUserByID,
func(userID int, ctx context.Context) (*models.User, error) {
return nil, fmt.Errorf("%s", ErrorMsgFindingUser)
})
},
}
}
func changePasswordErrorPasswordValidationcase() changePasswordType {
return changePasswordType{
name: ErrorPasswordValidation,
req: changeReq{
OldPassword: TestPassword,
NewPassword: NewPassword,
},
wantErr: true,
init: func() *gomonkey.Patches {
// tg := jwt.Service{}
sec := secure.Service{}
return gomonkey.ApplyFunc(daos.FindUserByID,
func(userID int, ctx context.Context) (*models.User, error) {
return nil, fmt.Errorf("%s", ErrorMsgFindingUser)
}).ApplyMethod(reflect.TypeOf(sec), "HashMatchesPassword",
func(sec secure.Service, hash string, password string) bool {
return false
})
},
}
}
func changePasswordErrorInsecurePasswordCase() changePasswordType {
return changePasswordType{
name: ErrorInsecurePassword,
req: changeReq{
OldPassword: OldPassword,
NewPassword: testutls.MockEmail,
},
wantErr: true,
init: func() *gomonkey.Patches {
sec := secure.Service{}
// mock FindUserByUserName with the proper password, and active state
return gomonkey.ApplyFunc(daos.FindUserByID,
func(userID int, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, nil
}).ApplyMethod(reflect.TypeOf(sec), "HashMatchesPassword",
func(sec secure.Service, hash string, password string) bool {
return true
}).ApplyMethod(reflect.TypeOf(sec), "Password",
func(sec secure.Service, pass string, inputs ...string) bool {
return false
})
},
}
}
func changePasswordErrorUpdateUserCase() changePasswordType {
return changePasswordType{
name: ErrorUpdateUser,
req: changeReq{
OldPassword: OldPassword,
NewPassword: NewPassword,
},
wantErr: true,
init: func() *gomonkey.Patches {
return gomonkey.ApplyFunc(daos.FindUserByID,
func(userID int, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, fmt.Errorf("%s", ErrorInsecurePassword)
}).ApplyFunc(daos.UpdateUser,
func(user models.User, ctx context.Context) (*models.User, error) {
return nil, fmt.Errorf("%s", ErrorUpdateUser)
})
},
}
}
func changePasswordErrorFromConfigCase() changePasswordType {
return changePasswordType{
name: ErrorFromConfig,
req: changeReq{
OldPassword: OldPassword,
NewPassword: testutls.MockEmail,
},
wantErr: true,
init: func() *gomonkey.Patches {
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, fmt.Errorf("error in loading config")
}).ApplyFunc(daos.FindUserByID,
func(userID int, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, nil
})
},
}
}
func changePasswordSuccessCase() changePasswordType {
return changePasswordType{
name: SuccessCase,
req: changeReq{
OldPassword: OldPassword,
NewPassword: NewPassword,
},
wantResp: &fm.ChangePasswordResponse{
Ok: true,
},
wantErr: false,
init: func() *gomonkey.Patches {
sec := secure.Service{}
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, nil
}).ApplyFunc(service.Secure, func(cfg *config.Configuration) secure.Service {
return sec
}).ApplyMethod(reflect.TypeOf(sec), "Password", func(secure.Service, string, ...string) bool {
return true
}).ApplyFunc(daos.FindUserByID,
func(userID int, ctx context.Context) (*models.User, error) {
user := testutls.MockUser()
user.Password = null.StringFrom(OldPasswordHash)
user.Active = null.BoolFrom(false)
return user, nil
}).ApplyFunc(daos.UpdateUser,
func(user models.User, ctx context.Context) (models.User, error) {
return *testutls.MockUser(), nil
})
},
}
}
func loadChangePasswordTestCases() []changePasswordType {
return []changePasswordType{
changePasswordErrorFindingUserCase(),
changePasswordErrorPasswordValidationcase(),
changePasswordErrorInsecurePasswordCase(),
changePasswordErrorUpdateUserCase(),
changePasswordErrorFromConfigCase(),
changePasswordSuccessCase(),
}
}
func TestChangePassword(
t *testing.T,
) {
// Define a struct to represent the change password request
cases := loadChangePasswordTestCases()
// Create a new instance of the resolver
resolver1 := resolver.Resolver{}
for _, tt := range cases {
t.Run(
tt.name,
func(t *testing.T) {
// Handle the case where there is an error while loading the configuration
patches := tt.init()
// Set up the context with the mock user
c := context.Background()
ctx := context.WithValue(c, testutls.UserKey, testutls.MockUser())
// Call the ChangePassword mutation and check the response and error against the expected values
response, err := resolver1.Mutation().ChangePassword(ctx, tt.req.OldPassword, tt.req.NewPassword)
if tt.wantResp != nil {
// Assert that the expected response matches the actual response
assert.Equal(t, tt.wantResp, response)
}
// Assert that the expected error value matches the actual error value
assert.Equal(t, tt.wantErr, err != nil)
if patches != nil {
patches.Reset()
}
},
)
}
}
type refereshTokenType struct {
name string
req string
wantResp *fm.RefreshTokenResponse
wantErr bool
err error
init func() *gomonkey.Patches
}
func refreshTokenInvalidCase() refereshTokenType {
return refereshTokenType{
name: ErrorInvalidToken,
req: TestToken,
wantErr: true,
err: fmt.Errorf("%s", ErrorMsginvalidToken),
init: func() *gomonkey.Patches {
tg := jwt.Service{}
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return &config.Configuration{}, nil
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, nil
}).ApplyMethod(reflect.TypeOf(tg), "GenerateToken",
func(jwt.Service, *models.User) (string, error) {
return "", fmt.Errorf("%s", ErrorMsginvalidToken)
}).ApplyFunc(daos.FindUserByToken, func(token string, ctx context.Context) (*models.User, error) {
return testutls.MockUser(), nil
})
},
}
}
func refreshTokenErrorFromConfigCase() refereshTokenType {
return refereshTokenType{
name: ErrorFromConfig,
req: ReqToken,
wantErr: true,
err: fmt.Errorf("%s", ErrorMsgFromConfig),
init: func() *gomonkey.Patches {
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, fmt.Errorf("%s", ErrorFromConfig)
}).ApplyFunc(daos.FindUserByToken, func(token string, ctx context.Context) (*models.User, error) {
return testutls.MockUser(), nil
})
},
}
}
func refereshTokenerrorWhileGeneratingToken() refereshTokenType {
return refereshTokenType{
name: ErrorFromJwt,
req: ReqToken,
wantErr: true,
err: fmt.Errorf("%s", ErrorMsgFromJwt),
init: func() *gomonkey.Patches {
tg := jwt.Service{}
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return nil, nil
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, fmt.Errorf("%s", ErrorMsgFromJwt)
}).ApplyFunc(daos.FindUserByToken, func(token string, ctx context.Context) (*models.User, error) {
return testutls.MockUser(), nil
})
},
}
}
func refereshTokenErrorFromGenerateTokenCase() refereshTokenType {
return refereshTokenType{
name: ErrorFromGenerateToken,
req: ReqToken,
wantErr: true,
err: fmt.Errorf("%s", ErrorFromGenerateToken),
init: func() *gomonkey.Patches {
tg := jwt.Service{}
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return &config.Configuration{}, nil
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, nil
}).ApplyMethod(reflect.TypeOf(tg), "GenerateToken",
func(jwt.Service, *models.User) (string, error) {
return "", fmt.Errorf("%s", ErrorFromGenerateToken)
}).ApplyFunc(daos.FindUserByToken, func(token string, ctx context.Context) (*models.User, error) {
return testutls.MockUser(), nil
})
},
}
}
func refreshTokenSuccessCase() refereshTokenType {
return refereshTokenType{
name: SuccessCase,
req: ReqToken,
wantResp: &fm.RefreshTokenResponse{
Token: testutls.MockToken,
},
wantErr: false,
init: func() *gomonkey.Patches {
tg := jwt.Service{}
return gomonkey.ApplyFunc(config.Load, func() (*config.Configuration, error) {
return &config.Configuration{}, nil
}).ApplyFunc(service.JWT, func(cfg *config.Configuration) (jwt.Service, error) {
return tg, nil
}).ApplyMethod(reflect.TypeOf(tg), "GenerateToken",
func(jwt.Service, *models.User) (string, error) {
return "", nil
}).ApplyFunc(daos.FindUserByToken, func(token string, ctx context.Context) (*models.User, error) {
return testutls.MockUser(), nil
})
},
}
}
func loadRefereshTokenCases() []refereshTokenType {
return []refereshTokenType{
refreshTokenInvalidCase(),
refreshTokenErrorFromConfigCase(),
refereshTokenerrorWhileGeneratingToken(),
refereshTokenErrorFromGenerateTokenCase(),
refreshTokenSuccessCase(),
}
}
func TestRefreshToken(t *testing.T) {
cases := loadRefereshTokenCases()
// Create a new instance of the resolver
resolver1 := resolver.Resolver{}
for _, tt := range cases {
t.Run(
tt.name,
func(t *testing.T) {
// Handle the case where authentication token is invalid
patches := tt.init()
// Set up the context with the mock user
c := context.Background()
ctx := context.WithValue(c, testutls.UserKey, testutls.MockUser())
// Call the refresh token mutation with the given arguments and check the response and error against the expected values
response, err := resolver1.Mutation().
RefreshToken(ctx, tt.req)
if tt.wantResp != nil &&
response != nil {
tt.wantResp.Token = response.Token
// Assert that the expected response matches the actual response
assert.Equal(t, tt.wantResp, response)
} else {
// Assert that the expected error value matches the actual error value
assert.Equal(t, true, strings.Contains(err.Error(), tt.err.Error()))
}
if patches != nil {
patches.Reset()
}
},
)
}
}