Skip to content

Commit

Permalink
Merge branch 'master' into SNOW-870356-increase-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jl authored Oct 20, 2023
2 parents ad2e93c + debf383 commit 95d337a
Show file tree
Hide file tree
Showing 24 changed files with 647 additions and 147 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
env:
PARAMETERS_SECRET: ${{ secrets.PARAMETERS_SECRET }}
CLOUD_PROVIDER: ${{ matrix.cloud }}
GORACE: history_size=7
run: ./ci/test.sh
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jira_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
echo ::set-output name=jira::$jira
- name: Comment on issue
uses: atlassian/gajira-comment@master
if: startsWith(steps.extract.outputs.jira, 'SNOW-')
if: startsWith(steps.extract.outputs.jira, 'SNOW-') && github.event.comment.user.login != 'codecov[bot]'
with:
issue: "${{ steps.extract.outputs.jira }}"
comment: "${{ github.event.comment.user.login }} commented:\n\n${{ github.event.comment.body }}\n\n${{ github.event.comment.html_url }}"
98 changes: 98 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.

package gosnowflake

import (
"fmt"
"reflect"
"strings"
"testing"
)

func assertNilF(t *testing.T, actual any, descriptions ...string) {
fatalOnNonEmpty(t, validateNil(actual, descriptions...))
}

func assertNotNilF(t *testing.T, actual any, descriptions ...string) {
fatalOnNonEmpty(t, validateNotNil(actual, descriptions...))
}

func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}

func assertEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
fatalOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}

func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
}

func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) {
errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...))
}

func fatalOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Fatal(errMsg)
}
}

func errorOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Error(errMsg)
}
}

func validateNil(actual any, descriptions ...string) string {
if isNil(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc)
}

func validateNotNil(actual any, descriptions ...string) string {
if !isNil(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected to be not nil but was not. %s", desc)
}

func validateEqual(actual any, expected any, descriptions ...string) string {
if expected == actual {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
}

func validateStringContains(actual string, expectedToContain string, descriptions ...string) string {
if strings.Contains(actual, expectedToContain) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc)
}

func validateHasPrefix(actual string, expectedPrefix string, descriptions ...string) string {
if strings.HasPrefix(actual, expectedPrefix) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc)
}

func joinDescriptions(descriptions ...string) string {
return strings.Join(descriptions, " ")
}

func isNil(value any) bool {
if value == nil {
return true
}
val := reflect.ValueOf(value)
return val.Kind() == reflect.Pointer && val.IsNil()
}
2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func postAuth(
var respd authResponse
err = json.NewDecoder(resp.Body).Decode(&respd)
if err != nil {
logger.Error("failed to decode JSON. err: %v", err)
logger.Errorf("failed to decode JSON. err: %v", err)
return nil, err
}
return &respd, nil
Expand Down
53 changes: 27 additions & 26 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ func TestUnitPostAuth(t *testing.T) {
bodyCreator := func() ([]byte, error) {
return []byte{0x12, 0x34}, nil
}
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
if err != nil {
t.Fatalf("err: %v", err)
}
sr.FuncAuthPost = postAuthTestError
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
if err == nil {
t.Fatal("should have failed to auth for unknown reason")
}
sr.FuncAuthPost = postAuthTestAppBadGatewayError
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
if err == nil {
t.Fatal("should have failed to auth for unknown reason")
}
sr.FuncAuthPost = postAuthTestAppForbiddenError
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
if err == nil {
t.Fatal("should have failed to auth for unknown reason")
}
sr.FuncAuthPost = postAuthTestAppUnexpectedError
_, err = postAuth(context.TODO(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
_, err = postAuth(context.Background(), sr, sr.Client, &url.Values{}, make(map[string]string), bodyCreator, 0)
if err == nil {
t.Fatal("should have failed to auth for unknown reason")
}
Expand Down Expand Up @@ -131,7 +131,8 @@ func postAuthCheckOAuth(
_ *http.Client,
_ *url.Values, _ map[string]string,
bodyCreator bodyCreatorType,
_ time.Duration) (*authResponse, error) {
_ time.Duration,
) (*authResponse, error) {
var ar authRequest
jsonBody, _ := bodyCreator()
if err := json.Unmarshal(jsonBody, &ar); err != nil {
Expand Down Expand Up @@ -408,7 +409,7 @@ func TestUnitAuthenticateWithTokenAccessor(t *testing.T) {
sc.rest = sr

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

_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err == nil {
t.Fatal("should have failed.")
}
Expand All @@ -458,7 +459,7 @@ func TestUnitAuthenticate(t *testing.T) {
t.Fatalf("Snowflake error is expected. err: %v", driverErr)
}
sr.FuncPostAuth = postAuthFailWrongAccount
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err == nil {
t.Fatal("should have failed.")
}
Expand All @@ -467,7 +468,7 @@ func TestUnitAuthenticate(t *testing.T) {
t.Fatalf("Snowflake error is expected. err: %v", driverErr)
}
sr.FuncPostAuth = postAuthFailUnknown
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err == nil {
t.Fatal("should have failed.")
}
Expand All @@ -477,7 +478,7 @@ func TestUnitAuthenticate(t *testing.T) {
}
ta.SetTokens("bad-token", "bad-master-token", 1)
sr.FuncPostAuth = postAuthSuccessWithErrorCode
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err == nil {
t.Fatal("should have failed.")
}
Expand All @@ -491,7 +492,7 @@ func TestUnitAuthenticate(t *testing.T) {
}
ta.SetTokens("bad-token", "bad-master-token", 1)
sr.FuncPostAuth = postAuthSuccessWithInvalidErrorCode
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err == nil {
t.Fatal("should have failed.")
}
Expand All @@ -501,7 +502,7 @@ func TestUnitAuthenticate(t *testing.T) {
}
sr.FuncPostAuth = postAuthSuccess
var resp *authResponseMain
resp, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
resp, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to auth. err: %v", err)
}
Expand Down Expand Up @@ -533,7 +534,7 @@ func TestUnitAuthenticateSaml(t *testing.T) {
Host: "blah.okta.com",
}
sc.rest = sr
_, err = authenticate(context.TODO(), sc, []byte("HTML data in bytes from"), []byte{})
_, err = authenticate(context.Background(), sc, []byte("HTML data in bytes from"), []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}
Expand All @@ -550,7 +551,7 @@ func TestUnitAuthenticateOAuth(t *testing.T) {
sc.cfg.Token = "oauthToken"
sc.cfg.Authenticator = AuthTypeOAuth
sc.rest = sr
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}
Expand All @@ -566,14 +567,14 @@ func TestUnitAuthenticatePasscode(t *testing.T) {
sc.cfg.Passcode = "987654321"
sc.rest = sr

_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}
sr.FuncPostAuth = postAuthCheckPasscodeInPassword
sc.rest = sr
sc.cfg.PasscodeInPassword = true
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}
Expand All @@ -594,7 +595,7 @@ func TestUnitAuthenticateJWT(t *testing.T) {
sc.rest = sr

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

Expand All @@ -604,7 +605,7 @@ func TestUnitAuthenticateJWT(t *testing.T) {
t.Error(err)
}
sc.cfg.PrivateKey = invalidPrivateKey
if _, err = authenticate(context.TODO(), sc, []byte{}, []byte{}); err == nil {
if _, err = authenticate(context.Background(), sc, []byte{}, []byte{}); err == nil {
t.Fatalf("invalid token passed")
}
}
Expand All @@ -619,20 +620,20 @@ func TestUnitAuthenticateUsernamePasswordMfa(t *testing.T) {
sc.cfg.Authenticator = AuthTypeUsernamePasswordMFA
sc.cfg.ClientRequestMfaToken = ConfigBoolTrue
sc.rest = sr
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}

sr.FuncPostAuth = postAuthCheckUsernamePasswordMfaToken
sc.cfg.MfaToken = "mockedMfaToken"
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}

sr.FuncPostAuth = postAuthCheckUsernamePasswordMfaFailed
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err == nil {
t.Fatal("should have failed")
}
Expand All @@ -648,7 +649,7 @@ func TestUnitAuthenticateWithConfigMFA(t *testing.T) {
sc.cfg.Authenticator = AuthTypeUsernamePasswordMFA
sc.cfg.ClientRequestMfaToken = ConfigBoolTrue
sc.rest = sr
sc.ctx = context.TODO()
sc.ctx = context.Background()
err = authenticateWithConfig(sc)
if err != nil {
t.Fatalf("failed to run. err: %v", err)
Expand Down Expand Up @@ -698,20 +699,20 @@ func TestUnitAuthenticateExternalBrowser(t *testing.T) {
sc.cfg.Authenticator = AuthTypeExternalBrowser
sc.cfg.ClientStoreTemporaryCredential = ConfigBoolTrue
sc.rest = sr
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}

sr.FuncPostAuth = postAuthCheckExternalBrowserToken
sc.cfg.IDToken = "mockedIDToken"
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err != nil {
t.Fatalf("failed to run. err: %v", err)
}

sr.FuncPostAuth = postAuthCheckExternalBrowserFailed
_, err = authenticate(context.TODO(), sc, []byte{}, []byte{})
_, err = authenticate(context.Background(), sc, []byte{}, []byte{})
if err == nil {
t.Fatal("should have failed")
}
Expand Down
8 changes: 4 additions & 4 deletions authexternalbrowser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ func TestUnitAuthenticateByExternalBrowser(t *testing.T) {
FuncPostAuthSAML: postAuthExternalBrowserError,
TokenAccessor: getSimpleTokenAccessor(),
}
_, _, err := authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
_, _, err := authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPostAuthSAML = postAuthExternalBrowserFail
_, _, err = authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
_, _, err = authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPostAuthSAML = postAuthExternalBrowserFailWithCode
_, _, err = authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
_, _, err = authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
if err == nil {
t.Fatal("should have failed.")
}
Expand All @@ -128,7 +128,7 @@ func TestAuthenticationTimeout(t *testing.T) {
FuncPostAuthSAML: postAuthExternalBrowserError,
TokenAccessor: getSimpleTokenAccessor(),
}
_, _, err := authenticateByExternalBrowser(context.TODO(), sr, authenticator, application, account, user, password, timeout)
_, _, err := authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout)
if err.Error() != "authentication timed out" {
t.Fatal("should have timed out")
}
Expand Down
Loading

0 comments on commit 95d337a

Please sign in to comment.