@@ -25,6 +25,7 @@ import (
25
25
"time"
26
26
27
27
"github.com/stretchr/testify/assert"
28
+ "github.com/stretchr/testify/require"
28
29
"go.uber.org/zap/zaptest"
29
30
"golang.org/x/crypto/bcrypt"
30
31
"google.golang.org/grpc/metadata"
@@ -64,9 +65,7 @@ func TestNewAuthStoreRevision(t *testing.T) {
64
65
defer as .Close ()
65
66
new := as .Revision ()
66
67
67
- if old != new {
68
- t .Fatalf ("expected revision %d, got %d" , old , new )
69
- }
68
+ require .Equalf (t , old , new , "expected revision %d, got %d" , old , new )
70
69
}
71
70
72
71
// TestNewAuthStoreBcryptCost ensures that NewAuthStore uses default when given bcrypt-cost is invalid
@@ -80,9 +79,7 @@ func TestNewAuthStoreBcryptCost(t *testing.T) {
80
79
for _ , invalidCost := range invalidCosts {
81
80
as := NewAuthStore (zaptest .NewLogger (t ), newBackendMock (), tp , invalidCost )
82
81
defer as .Close ()
83
- if as .BcryptCost () != bcrypt .DefaultCost {
84
- t .Fatalf ("expected DefaultCost when bcryptcost is invalid" )
85
- }
82
+ require .Equalf (t , bcrypt .DefaultCost , as .BcryptCost (), "expected DefaultCost when bcryptcost is invalid" )
86
83
}
87
84
}
88
85
@@ -162,22 +159,17 @@ func TestUserAdd(t *testing.T) {
162
159
const userName = "foo"
163
160
ua := & pb.AuthUserAddRequest {Name : userName , Options : & authpb.UserAddOptions {NoPassword : false }}
164
161
_ , err := as .UserAdd (ua ) // add an existing user
165
- if err == nil {
166
- t .Fatalf ("expected %v, got %v" , ErrUserAlreadyExist , err )
167
- }
168
- if ! errors .Is (err , ErrUserAlreadyExist ) {
169
- t .Fatalf ("expected %v, got %v" , ErrUserAlreadyExist , err )
170
- }
162
+ require .Errorf (t , err , "expected %v, got %v" , ErrUserAlreadyExist , err )
163
+ require .ErrorIsf (t , err , ErrUserAlreadyExist , "expected %v, got %v" , ErrUserAlreadyExist , err )
171
164
172
165
ua = & pb.AuthUserAddRequest {Name : "" , Options : & authpb.UserAddOptions {NoPassword : false }}
173
166
_ , err = as .UserAdd (ua ) // add a user with empty name
174
167
if ! errors .Is (err , ErrUserEmpty ) {
175
168
t .Fatal (err )
176
169
}
177
170
178
- if _ , ok := as .rangePermCache [userName ]; ! ok {
179
- t .Fatalf ("user %s should be added but it doesn't exist in rangePermCache" , userName )
180
- }
171
+ _ , ok := as .rangePermCache [userName ]
172
+ require .Truef (t , ok , "user %s should be added but it doesn't exist in rangePermCache" , userName )
181
173
}
182
174
183
175
func TestRecover (t * testing.T ) {
@@ -188,9 +180,7 @@ func TestRecover(t *testing.T) {
188
180
as .enabled = false
189
181
as .Recover (as .be )
190
182
191
- if ! as .IsAuthEnabled () {
192
- t .Fatalf ("expected auth enabled got disabled" )
193
- }
183
+ require .Truef (t , as .IsAuthEnabled (), "expected auth enabled got disabled" )
194
184
}
195
185
196
186
func TestRecoverWithEmptyRangePermCache (t * testing.T ) {
@@ -202,19 +192,13 @@ func TestRecoverWithEmptyRangePermCache(t *testing.T) {
202
192
as .rangePermCache = map [string ]* unifiedRangePermissions {}
203
193
as .Recover (as .be )
204
194
205
- if ! as .IsAuthEnabled () {
206
- t .Fatalf ("expected auth enabled got disabled" )
207
- }
195
+ require .Truef (t , as .IsAuthEnabled (), "expected auth enabled got disabled" )
208
196
209
- if len (as .rangePermCache ) != 3 {
210
- t .Fatalf ("rangePermCache should have permission information for 3 users (\" root\" and \" foo\" ,\" foo-no-user-options\" ), but has %d information" , len (as .rangePermCache ))
211
- }
212
- if _ , ok := as .rangePermCache ["root" ]; ! ok {
213
- t .Fatal ("user \" root\" should be created by setupAuthStore() but doesn't exist in rangePermCache" )
214
- }
215
- if _ , ok := as .rangePermCache ["foo" ]; ! ok {
216
- t .Fatal ("user \" foo\" should be created by setupAuthStore() but doesn't exist in rangePermCache" )
217
- }
197
+ require .Lenf (t , as .rangePermCache , 3 , "rangePermCache should have permission information for 3 users (\" root\" and \" foo\" ,\" foo-no-user-options\" ), but has %d information" , len (as .rangePermCache ))
198
+ _ , ok := as .rangePermCache ["root" ]
199
+ require .Truef (t , ok , "user \" root\" should be created by setupAuthStore() but doesn't exist in rangePermCache" )
200
+ _ , ok = as .rangePermCache ["foo" ]
201
+ require .Truef (t , ok , "user \" foo\" should be created by setupAuthStore() but doesn't exist in rangePermCache" )
218
202
}
219
203
220
204
func TestCheckPassword (t * testing.T ) {
@@ -223,12 +207,8 @@ func TestCheckPassword(t *testing.T) {
223
207
224
208
// auth a non-existing user
225
209
_ , err := as .CheckPassword ("foo-test" , "bar" )
226
- if err == nil {
227
- t .Fatalf ("expected %v, got %v" , ErrAuthFailed , err )
228
- }
229
- if ! errors .Is (err , ErrAuthFailed ) {
230
- t .Fatalf ("expected %v, got %v" , ErrAuthFailed , err )
231
- }
210
+ require .Errorf (t , err , "expected %v, got %v" , ErrAuthFailed , err )
211
+ require .ErrorIsf (t , err , ErrAuthFailed , "expected %v, got %v" , ErrAuthFailed , err )
232
212
233
213
// auth an existing user with correct password
234
214
_ , err = as .CheckPassword ("foo" , "bar" )
@@ -238,12 +218,8 @@ func TestCheckPassword(t *testing.T) {
238
218
239
219
// auth an existing user but with wrong password
240
220
_ , err = as .CheckPassword ("foo" , "" )
241
- if err == nil {
242
- t .Fatalf ("expected %v, got %v" , ErrAuthFailed , err )
243
- }
244
- if ! errors .Is (err , ErrAuthFailed ) {
245
- t .Fatalf ("expected %v, got %v" , ErrAuthFailed , err )
246
- }
221
+ require .Errorf (t , err , "expected %v, got %v" , ErrAuthFailed , err )
222
+ require .ErrorIsf (t , err , ErrAuthFailed , "expected %v, got %v" , ErrAuthFailed , err )
247
223
}
248
224
249
225
func TestUserDelete (t * testing.T ) {
@@ -260,16 +236,11 @@ func TestUserDelete(t *testing.T) {
260
236
261
237
// delete a non-existing user
262
238
_ , err = as .UserDelete (ud )
263
- if err == nil {
264
- t .Fatalf ("expected %v, got %v" , ErrUserNotFound , err )
265
- }
266
- if ! errors .Is (err , ErrUserNotFound ) {
267
- t .Fatalf ("expected %v, got %v" , ErrUserNotFound , err )
268
- }
239
+ require .Errorf (t , err , "expected %v, got %v" , ErrUserNotFound , err )
240
+ require .ErrorIsf (t , err , ErrUserNotFound , "expected %v, got %v" , ErrUserNotFound , err )
269
241
270
- if _ , ok := as .rangePermCache [userName ]; ok {
271
- t .Fatalf ("user %s should be deleted but it exists in rangePermCache" , userName )
272
- }
242
+ _ , ok := as .rangePermCache [userName ]
243
+ require .Falsef (t , ok , "user %s should be deleted but it exists in rangePermCache" , userName )
273
244
}
274
245
275
246
func TestUserDeleteAndPermCache (t * testing.T ) {
@@ -286,13 +257,10 @@ func TestUserDeleteAndPermCache(t *testing.T) {
286
257
287
258
// delete a non-existing user
288
259
_ , err = as .UserDelete (ud )
289
- if ! errors .Is (err , ErrUserNotFound ) {
290
- t .Fatalf ("expected %v, got %v" , ErrUserNotFound , err )
291
- }
260
+ require .ErrorIsf (t , err , ErrUserNotFound , "expected %v, got %v" , ErrUserNotFound , err )
292
261
293
- if _ , ok := as .rangePermCache [deletedUserName ]; ok {
294
- t .Fatalf ("user %s should be deleted but it exists in rangePermCache" , deletedUserName )
295
- }
262
+ _ , ok := as .rangePermCache [deletedUserName ]
263
+ require .Falsef (t , ok , "user %s should be deleted but it exists in rangePermCache" , deletedUserName )
296
264
297
265
// add a new user
298
266
const newUser = "bar"
@@ -302,9 +270,8 @@ func TestUserDeleteAndPermCache(t *testing.T) {
302
270
t .Fatal (err )
303
271
}
304
272
305
- if _ , ok := as .rangePermCache [newUser ]; ! ok {
306
- t .Fatalf ("user %s should exist but it doesn't exist in rangePermCache" , deletedUserName )
307
- }
273
+ _ , ok = as .rangePermCache [newUser ]
274
+ require .Truef (t , ok , "user %s should exist but it doesn't exist in rangePermCache" , deletedUserName )
308
275
}
309
276
310
277
func TestUserChangePassword (t * testing.T ) {
@@ -330,12 +297,8 @@ func TestUserChangePassword(t *testing.T) {
330
297
331
298
// change a non-existing user
332
299
_ , err = as .UserChangePassword (& pb.AuthUserChangePasswordRequest {Name : "foo-test" , HashedPassword : encodePassword ("bar" )})
333
- if err == nil {
334
- t .Fatalf ("expected %v, got %v" , ErrUserNotFound , err )
335
- }
336
- if ! errors .Is (err , ErrUserNotFound ) {
337
- t .Fatalf ("expected %v, got %v" , ErrUserNotFound , err )
338
- }
300
+ require .Errorf (t , err , "expected %v, got %v" , ErrUserNotFound , err )
301
+ require .ErrorIsf (t , err , ErrUserNotFound , "expected %v, got %v" , ErrUserNotFound , err )
339
302
340
303
// change a user(user option is nil) password
341
304
_ , err = as .UserChangePassword (& pb.AuthUserChangePasswordRequest {Name : "foo-no-user-options" , HashedPassword : encodePassword ("bar" )})
@@ -393,21 +356,15 @@ func TestHasRole(t *testing.T) {
393
356
394
357
// checks role reflects correctly
395
358
hr := as .HasRole ("foo" , "role-test" )
396
- if ! hr {
397
- t .Fatal ("expected role granted, got false" )
398
- }
359
+ require .Truef (t , hr , "expected role granted, got false" )
399
360
400
361
// checks non existent role
401
362
hr = as .HasRole ("foo" , "non-existent-role" )
402
- if hr {
403
- t .Fatal ("expected role not found, got true" )
404
- }
363
+ require .Falsef (t , hr , "expected role not found, got true" )
405
364
406
365
// checks non existent user
407
366
hr = as .HasRole ("nouser" , "role-test" )
408
- if hr {
409
- t .Fatal ("expected user not found got true" )
410
- }
367
+ require .Falsef (t , hr , "expected user not found got true" )
411
368
}
412
369
413
370
func TestIsOpPermitted (t * testing.T ) {
@@ -470,9 +427,7 @@ func TestGetUser(t *testing.T) {
470
427
if err != nil {
471
428
t .Fatal (err )
472
429
}
473
- if u == nil {
474
- t .Fatal ("expect user not nil, got nil" )
475
- }
430
+ require .NotNilf (t , u , "expect user not nil, got nil" )
476
431
expected := []string {"role-test" }
477
432
478
433
assert .Equal (t , expected , u .Roles )
@@ -801,18 +756,13 @@ func TestUserRevokePermission(t *testing.T) {
801
756
t .Fatal (err )
802
757
}
803
758
804
- if _ , ok := as .rangePermCache [userName ]; ! ok {
805
- t .Fatalf ("User %s should have its entry in rangePermCache" , userName )
806
- }
759
+ _ , ok := as .rangePermCache [userName ]
760
+ require .Truef (t , ok , "User %s should have its entry in rangePermCache" , userName )
807
761
unifiedPerm := as .rangePermCache [userName ]
808
762
pt1 := adt .NewBytesAffinePoint ([]byte ("WriteKeyBegin" ))
809
- if ! unifiedPerm .writePerms .Contains (pt1 ) {
810
- t .Fatal ("rangePermCache should contain WriteKeyBegin" )
811
- }
763
+ require .Truef (t , unifiedPerm .writePerms .Contains (pt1 ), "rangePermCache should contain WriteKeyBegin" )
812
764
pt2 := adt .NewBytesAffinePoint ([]byte ("OutOfRange" ))
813
- if unifiedPerm .writePerms .Contains (pt2 ) {
814
- t .Fatal ("rangePermCache should not contain OutOfRange" )
815
- }
765
+ require .Falsef (t , unifiedPerm .writePerms .Contains (pt2 ), "rangePermCache should not contain OutOfRange" )
816
766
817
767
u , err := as .UserGet (& pb.AuthUserGetRequest {Name : userName })
818
768
if err != nil {
@@ -1003,12 +953,8 @@ func TestRecoverFromSnapshot(t *testing.T) {
1003
953
1004
954
ua := & pb.AuthUserAddRequest {Name : "foo" , Options : & authpb.UserAddOptions {NoPassword : false }}
1005
955
_ , err := as .UserAdd (ua ) // add an existing user
1006
- if err == nil {
1007
- t .Fatalf ("expected %v, got %v" , ErrUserAlreadyExist , err )
1008
- }
1009
- if ! errors .Is (err , ErrUserAlreadyExist ) {
1010
- t .Fatalf ("expected %v, got %v" , ErrUserAlreadyExist , err )
1011
- }
956
+ require .Errorf (t , err , "expected %v, got %v" , ErrUserAlreadyExist , err )
957
+ require .ErrorIsf (t , err , ErrUserAlreadyExist , "expected %v, got %v" , ErrUserAlreadyExist , err )
1012
958
1013
959
ua = & pb.AuthUserAddRequest {Name : "" , Options : & authpb.UserAddOptions {NoPassword : false }}
1014
960
_ , err = as .UserAdd (ua ) // add a user with empty name
@@ -1025,9 +971,7 @@ func TestRecoverFromSnapshot(t *testing.T) {
1025
971
as2 := NewAuthStore (zaptest .NewLogger (t ), as .be , tp , bcrypt .MinCost )
1026
972
defer as2 .Close ()
1027
973
1028
- if ! as2 .IsAuthEnabled () {
1029
- t .Fatal ("recovering authStore from existing backend failed" )
1030
- }
974
+ require .Truef (t , as2 .IsAuthEnabled (), "recovering authStore from existing backend failed" )
1031
975
1032
976
ul , err := as .UserList (& pb.AuthUserListRequest {})
1033
977
if err != nil {
@@ -1167,9 +1111,7 @@ func testAuthInfoFromCtxWithRoot(t *testing.T, opts string) {
1167
1111
if aerr != nil {
1168
1112
t .Fatal (err )
1169
1113
}
1170
- if ai == nil {
1171
- t .Fatal ("expected non-nil *AuthInfo" )
1172
- }
1114
+ require .NotNilf (t , ai , "expected non-nil *AuthInfo" )
1173
1115
if ai .Username != "root" {
1174
1116
t .Errorf ("expected user name 'root', got %+v" , ai )
1175
1117
}
@@ -1188,9 +1130,7 @@ func TestUserNoPasswordAdd(t *testing.T) {
1188
1130
1189
1131
ctx := context .WithValue (context .WithValue (context .TODO (), AuthenticateParamIndex {}, uint64 (1 )), AuthenticateParamSimpleTokenPrefix {}, "dummy" )
1190
1132
_ , err = as .Authenticate (ctx , username , "" )
1191
- if ! errors .Is (err , ErrAuthFailed ) {
1192
- t .Fatalf ("expected %v, got %v" , ErrAuthFailed , err )
1193
- }
1133
+ require .ErrorIsf (t , err , ErrAuthFailed , "expected %v, got %v" , ErrAuthFailed , err )
1194
1134
}
1195
1135
1196
1136
func TestUserAddWithOldLog (t * testing.T ) {
@@ -1227,10 +1167,6 @@ func TestUserChangePasswordWithOldLog(t *testing.T) {
1227
1167
1228
1168
// change a non-existing user
1229
1169
_ , err = as .UserChangePassword (& pb.AuthUserChangePasswordRequest {Name : "foo-test" , HashedPassword : encodePassword ("bar" )})
1230
- if err == nil {
1231
- t .Fatalf ("expected %v, got %v" , ErrUserNotFound , err )
1232
- }
1233
- if ! errors .Is (err , ErrUserNotFound ) {
1234
- t .Fatalf ("expected %v, got %v" , ErrUserNotFound , err )
1235
- }
1170
+ require .Errorf (t , err , "expected %v, got %v" , ErrUserNotFound , err )
1171
+ require .ErrorIsf (t , err , ErrUserNotFound , "expected %v, got %v" , ErrUserNotFound , err )
1236
1172
}
0 commit comments