Skip to content

Commit c6c2afd

Browse files
improve: use context.Background instead of context.TODO (#651)
* improve: use context.Background instead of context.TODO * refactor --------- Co-authored-by: Piotr Fus <[email protected]>
1 parent c219d9d commit c6c2afd

10 files changed

+66
-65
lines changed

auth_test.go

+27-26
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,27 @@ func TestUnitPostAuth(t *testing.T) {
2929
bodyCreator := func() ([]byte, error) {
3030
return []byte{0x12, 0x34}, nil
3131
}
32-
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
32+
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
3333
if err != nil {
3434
t.Fatalf("err: %v", err)
3535
}
3636
sr.FuncAuthPost = postAuthTestError
37-
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
37+
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
3838
if err == nil {
3939
t.Fatal("should have failed to auth for unknown reason")
4040
}
4141
sr.FuncAuthPost = postAuthTestAppBadGatewayError
42-
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
42+
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
4343
if err == nil {
4444
t.Fatal("should have failed to auth for unknown reason")
4545
}
4646
sr.FuncAuthPost = postAuthTestAppForbiddenError
47-
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
47+
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
4848
if err == nil {
4949
t.Fatal("should have failed to auth for unknown reason")
5050
}
5151
sr.FuncAuthPost = postAuthTestAppUnexpectedError
52-
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
52+
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
5353
if err == nil {
5454
t.Fatal("should have failed to auth for unknown reason")
5555
}
@@ -131,7 +131,8 @@ func postAuthCheckOAuth(
131131
_ *http.Client,
132132
_ *url.Values, _ map[string]string,
133133
bodyCreator bodyCreatorType,
134-
_ time.Duration) (*authResponse, error) {
134+
_ time.Duration,
135+
) (*authResponse, error) {
135136
var ar authRequest
136137
jsonBody, _ := bodyCreator()
137138
if err := json.Unmarshal(jsonBody, &ar); err != nil {
@@ -408,7 +409,7 @@ func TestUnitAuthenticateWithTokenAccessor(t *testing.T) {
408409
sc.rest = sr
409410

410411
// FuncPostAuth is set to fail, but AuthTypeTokenAccessor should not even make a call to FuncPostAuth
411-
resp, err := authenticate(context.TODO(), sc, []byte{}, []byte{})
412+
resp, err := authenticate(context.Background(), sc, []byte{}, []byte{})
412413
if err != nil {
413414
t.Fatalf("should not have failed, err %v", err)
414415
}
@@ -449,7 +450,7 @@ func TestUnitAuthenticate(t *testing.T) {
449450
}
450451
sc.rest = sr
451452

452-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
453+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
453454
if err == nil {
454455
t.Fatal("should have failed.")
455456
}
@@ -458,7 +459,7 @@ func TestUnitAuthenticate(t *testing.T) {
458459
t.Fatalf("Snowflake error is expected. err: %v", driverErr)
459460
}
460461
sr.FuncPostAuth = postAuthFailWrongAccount
461-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
462+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
462463
if err == nil {
463464
t.Fatal("should have failed.")
464465
}
@@ -467,7 +468,7 @@ func TestUnitAuthenticate(t *testing.T) {
467468
t.Fatalf("Snowflake error is expected. err: %v", driverErr)
468469
}
469470
sr.FuncPostAuth = postAuthFailUnknown
470-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
471+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
471472
if err == nil {
472473
t.Fatal("should have failed.")
473474
}
@@ -477,7 +478,7 @@ func TestUnitAuthenticate(t *testing.T) {
477478
}
478479
ta.SetTokens("bad-token", "bad-master-token", 1)
479480
sr.FuncPostAuth = postAuthSuccessWithErrorCode
480-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
481+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
481482
if err == nil {
482483
t.Fatal("should have failed.")
483484
}
@@ -491,7 +492,7 @@ func TestUnitAuthenticate(t *testing.T) {
491492
}
492493
ta.SetTokens("bad-token", "bad-master-token", 1)
493494
sr.FuncPostAuth = postAuthSuccessWithInvalidErrorCode
494-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
495+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
495496
if err == nil {
496497
t.Fatal("should have failed.")
497498
}
@@ -501,7 +502,7 @@ func TestUnitAuthenticate(t *testing.T) {
501502
}
502503
sr.FuncPostAuth = postAuthSuccess
503504
var resp *authResponseMain
504-
resp, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
505+
resp, err = authenticate(context.Background(), sc, []byte{}, []byte{})
505506
if err != nil {
506507
t.Fatalf("failed to auth. err: %v", err)
507508
}
@@ -533,7 +534,7 @@ func TestUnitAuthenticateSaml(t *testing.T) {
533534
Host: "blah.okta.com",
534535
}
535536
sc.rest = sr
536-
_, err = authenticate(context.TODO(), sc, []byte("HTML data in bytes from"), []byte{})
537+
_, err = authenticate(context.Background(), sc, []byte("HTML data in bytes from"), []byte{})
537538
if err != nil {
538539
t.Fatalf("failed to run. err: %v", err)
539540
}
@@ -550,7 +551,7 @@ func TestUnitAuthenticateOAuth(t *testing.T) {
550551
sc.cfg.Token = "oauthToken"
551552
sc.cfg.Authenticator = AuthTypeOAuth
552553
sc.rest = sr
553-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
554+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
554555
if err != nil {
555556
t.Fatalf("failed to run. err: %v", err)
556557
}
@@ -566,14 +567,14 @@ func TestUnitAuthenticatePasscode(t *testing.T) {
566567
sc.cfg.Passcode = "987654321"
567568
sc.rest = sr
568569

569-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
570+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
570571
if err != nil {
571572
t.Fatalf("failed to run. err: %v", err)
572573
}
573574
sr.FuncPostAuth = postAuthCheckPasscodeInPassword
574575
sc.rest = sr
575576
sc.cfg.PasscodeInPassword = true
576-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
577+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
577578
if err != nil {
578579
t.Fatalf("failed to run. err: %v", err)
579580
}
@@ -594,7 +595,7 @@ func TestUnitAuthenticateJWT(t *testing.T) {
594595
sc.rest = sr
595596

596597
// A valid JWT token should pass
597-
if _, err = authenticate(context.TODO(), sc, []byte{}, []byte{}); err != nil {
598+
if _, err = authenticate(context.Background(), sc, []byte{}, []byte{}); err != nil {
598599
t.Fatalf("failed to run. err: %v", err)
599600
}
600601

@@ -604,7 +605,7 @@ func TestUnitAuthenticateJWT(t *testing.T) {
604605
t.Error(err)
605606
}
606607
sc.cfg.PrivateKey = invalidPrivateKey
607-
if _, err = authenticate(context.TODO(), sc, []byte{}, []byte{}); err == nil {
608+
if _, err = authenticate(context.Background(), sc, []byte{}, []byte{}); err == nil {
608609
t.Fatalf("invalid token passed")
609610
}
610611
}
@@ -619,20 +620,20 @@ func TestUnitAuthenticateUsernamePasswordMfa(t *testing.T) {
619620
sc.cfg.Authenticator = AuthTypeUsernamePasswordMFA
620621
sc.cfg.ClientRequestMfaToken = ConfigBoolTrue
621622
sc.rest = sr
622-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
623+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
623624
if err != nil {
624625
t.Fatalf("failed to run. err: %v", err)
625626
}
626627

627628
sr.FuncPostAuth = postAuthCheckUsernamePasswordMfaToken
628629
sc.cfg.MfaToken = "mockedMfaToken"
629-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
630+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
630631
if err != nil {
631632
t.Fatalf("failed to run. err: %v", err)
632633
}
633634

634635
sr.FuncPostAuth = postAuthCheckUsernamePasswordMfaFailed
635-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
636+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
636637
if err == nil {
637638
t.Fatal("should have failed")
638639
}
@@ -648,7 +649,7 @@ func TestUnitAuthenticateWithConfigMFA(t *testing.T) {
648649
sc.cfg.Authenticator = AuthTypeUsernamePasswordMFA
649650
sc.cfg.ClientRequestMfaToken = ConfigBoolTrue
650651
sc.rest = sr
651-
sc.ctx = context.TODO()
652+
sc.ctx = context.Background()
652653
err = authenticateWithConfig(sc)
653654
if err != nil {
654655
t.Fatalf("failed to run. err: %v", err)
@@ -665,20 +666,20 @@ func TestUnitAuthenticateExternalBrowser(t *testing.T) {
665666
sc.cfg.Authenticator = AuthTypeExternalBrowser
666667
sc.cfg.ClientStoreTemporaryCredential = ConfigBoolTrue
667668
sc.rest = sr
668-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
669+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
669670
if err != nil {
670671
t.Fatalf("failed to run. err: %v", err)
671672
}
672673

673674
sr.FuncPostAuth = postAuthCheckExternalBrowserToken
674675
sc.cfg.IDToken = "mockedIDToken"
675-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
676+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
676677
if err != nil {
677678
t.Fatalf("failed to run. err: %v", err)
678679
}
679680

680681
sr.FuncPostAuth = postAuthCheckExternalBrowserFailed
681-
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
682+
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
682683
if err == nil {
683684
t.Fatal("should have failed")
684685
}

authexternalbrowser_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ func TestUnitAuthenticateByExternalBrowser(t *testing.T) {
9191
FuncPostAuthSAML: postAuthExternalBrowserError,
9292
TokenAccessor: getSimpleTokenAccessor(),
9393
}
94-
_, _, err := authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
94+
_, _, err := authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
9595
if err == nil {
9696
t.Fatal("should have failed.")
9797
}
9898
sr.FuncPostAuthSAML = postAuthExternalBrowserFail
99-
_, _, err = authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
99+
_, _, err = authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
100100
if err == nil {
101101
t.Fatal("should have failed.")
102102
}
103103
sr.FuncPostAuthSAML = postAuthExternalBrowserFailWithCode
104-
_, _, err = authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
104+
_, _, err = authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
105105
if err == nil {
106106
t.Fatal("should have failed.")
107107
}
@@ -128,7 +128,7 @@ func TestAuthenticationTimeout(t *testing.T) {
128128
FuncPostAuthSAML: postAuthExternalBrowserError,
129129
TokenAccessor: getSimpleTokenAccessor(),
130130
}
131-
_, _, err := authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
131+
_, _, err := authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
132132
if err.Error() != "authentication timed out" {
133133
t.Fatal("should have timed out")
134134
}

authokta_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ func TestUnitPostAuthSAML(t *testing.T) {
6464
TokenAccessor: getSimpleTokenAccessor(),
6565
}
6666
var err error
67-
_, err = postAuthSAML(context.TODO(), sr, make(map[string]string), []byte{}, 0)
67+
_, err = postAuthSAML(context.Background(), sr, make(map[string]string), []byte{}, 0)
6868
if err == nil {
6969
t.Fatal("should have failed.")
7070
}
7171
sr.FuncPost = postTestAppBadGatewayError
72-
_, err = postAuthSAML(context.TODO(), sr, make(map[string]string), []byte{}, 0)
72+
_, err = postAuthSAML(context.Background(), sr, make(map[string]string), []byte{}, 0)
7373
if err == nil {
7474
t.Fatal("should have failed.")
7575
}
7676
sr.FuncPost = postTestSuccessButInvalidJSON
77-
_, err = postAuthSAML(context.TODO(), sr, make(map[string]string), []byte{0x12, 0x34}, 0)
77+
_, err = postAuthSAML(context.Background(), sr, make(map[string]string), []byte{0x12, 0x34}, 0)
7878
if err == nil {
7979
t.Fatalf("should have failed to post")
8080
}
@@ -86,17 +86,17 @@ func TestUnitPostAuthOKTA(t *testing.T) {
8686
TokenAccessor: getSimpleTokenAccessor(),
8787
}
8888
var err error
89-
_, err = postAuthOKTA(context.TODO(), sr, make(map[string]string), []byte{}, "hahah", 0)
89+
_, err = postAuthOKTA(context.Background(), sr, make(map[string]string), []byte{}, "hahah", 0)
9090
if err == nil {
9191
t.Fatal("should have failed.")
9292
}
9393
sr.FuncPost = postTestAppBadGatewayError
94-
_, err = postAuthOKTA(context.TODO(), sr, make(map[string]string), []byte{}, "hahah", 0)
94+
_, err = postAuthOKTA(context.Background(), sr, make(map[string]string), []byte{}, "hahah", 0)
9595
if err == nil {
9696
t.Fatal("should have failed.")
9797
}
9898
sr.FuncPost = postTestSuccessButInvalidJSON
99-
_, err = postAuthOKTA(context.TODO(), sr, make(map[string]string), []byte{0x12, 0x34}, "haha", 0)
99+
_, err = postAuthOKTA(context.Background(), sr, make(map[string]string), []byte{0x12, 0x34}, "haha", 0)
100100
if err == nil {
101101
t.Fatal("should have failed to run post request after the renewal")
102102
}
@@ -108,17 +108,17 @@ func TestUnitGetSSO(t *testing.T) {
108108
TokenAccessor: getSimpleTokenAccessor(),
109109
}
110110
var err error
111-
_, err = getSSO(context.TODO(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
111+
_, err = getSSO(context.Background(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
112112
if err == nil {
113113
t.Fatal("should have failed.")
114114
}
115115
sr.FuncGet = getTestAppBadGatewayError
116-
_, err = getSSO(context.TODO(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
116+
_, err = getSSO(context.Background(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
117117
if err == nil {
118118
t.Fatal("should have failed.")
119119
}
120120
sr.FuncGet = getTestHTMLSuccess
121-
_, err = getSSO(context.TODO(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
121+
_, err = getSSO(context.Background(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
122122
if err != nil {
123123
t.Fatalf("failed to get HTML content. err: %v", err)
124124
}
@@ -194,17 +194,17 @@ func TestUnitAuthenticateBySAML(t *testing.T) {
194194
TokenAccessor: getSimpleTokenAccessor(),
195195
}
196196
var err error
197-
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
197+
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
198198
if err == nil {
199199
t.Fatal("should have failed.")
200200
}
201201
sr.FuncPostAuthSAML = postAuthSAMLAuthFail
202-
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
202+
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
203203
if err == nil {
204204
t.Fatal("should have failed.")
205205
}
206206
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccessButInvalidURL
207-
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
207+
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
208208
if err == nil {
209209
t.Fatal("should have failed.")
210210
}
@@ -217,23 +217,23 @@ func TestUnitAuthenticateBySAML(t *testing.T) {
217217
}
218218
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccess
219219
sr.FuncPostAuthOKTA = postAuthOKTAError
220-
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
220+
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
221221
if err == nil {
222222
t.Fatal("should have failed.")
223223
}
224224
sr.FuncPostAuthOKTA = postAuthOKTASuccess
225225
sr.FuncGetSSO = getSSOError
226-
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
226+
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
227227
if err == nil {
228228
t.Fatal("should have failed.")
229229
}
230230
sr.FuncGetSSO = getSSOSuccessButInvalidURL
231-
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
231+
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
232232
if err == nil {
233233
t.Fatal("should have failed.")
234234
}
235235
sr.FuncGetSSO = getSSOSuccess
236-
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
236+
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
237237
if err != nil {
238238
t.Fatalf("failed. err: %v", err)
239239
}

connection_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func TestServiceName(t *testing.T) {
191191

192192
expectServiceName := serviceNameStub
193193
for i := 0; i < 5; i++ {
194-
sc.exec(context.TODO(), "", false, /* noResult */
194+
sc.exec(context.Background(), "", false, /* noResult */
195195
false /* isInternal */, false /* describeOnly */, nil)
196196
if actualServiceName, ok := sc.cfg.Params[serviceName]; ok {
197197
if *actualServiceName != expectServiceName {

ctx_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func TestCtxVal(t *testing.T) {
4040

4141
func TestLogEntryCtx(t *testing.T) {
4242
var log = logger
43-
var ctx1 = context.WithValue(context.TODO(), SFSessionIDKey, "sessID1")
44-
var ctx2 = context.WithValue(context.TODO(), SFSessionUserKey, "admin")
43+
var ctx1 = context.WithValue(context.Background(), SFSessionIDKey, "sessID1")
44+
var ctx2 = context.WithValue(context.Background(), SFSessionUserKey, "admin")
4545

4646
fs1 := context2Fields(ctx1)
4747
fs2 := context2Fields(ctx2)

driver.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type SnowflakeDriver struct{}
1818
// Open creates a new connection.
1919
func (d SnowflakeDriver) Open(dsn string) (driver.Conn, error) {
2020
logger.Info("Open")
21-
ctx := context.TODO()
21+
ctx := context.Background()
2222
cfg, err := ParseDSN(dsn)
2323
if err != nil {
2424
return nil, err

0 commit comments

Comments
 (0)