github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/credentials/access_error_test.go (about)

     1  package credentials
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
    11  	grpcCodes "google.golang.org/grpc/codes"
    12  	grpcStatus "google.golang.org/grpc/status"
    13  
    14  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    15  )
    16  
    17  var _ Credentials = customCredentials{}
    18  
    19  type customCredentials struct {
    20  	token string
    21  }
    22  
    23  func (c customCredentials) Token(context.Context) (string, error) {
    24  	return c.token, nil
    25  }
    26  
    27  func TestAccessError(t *testing.T) {
    28  	for _, tt := range []struct {
    29  		err         error
    30  		errorString string
    31  	}{
    32  		{
    33  			err: AccessError(
    34  				"something went wrong",
    35  				errors.New("test"),
    36  				WithEndpoint("grps://localhost:2135"),
    37  				WithDatabase("/local"),
    38  				WithCredentials(NewAnonymousCredentials(WithSourceInfo(""))),
    39  			),
    40  			errorString: "something went wrong (" +
    41  				"endpoint:\"grps://localhost:2135\"," +
    42  				"database:\"/local\"," +
    43  				"credentials:\"Anonymous{}\"" +
    44  				"): test " +
    45  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:33)`", //nolint:lll
    46  		},
    47  		{
    48  			err: AccessError(
    49  				"something went wrong",
    50  				errors.New("test"),
    51  				WithEndpoint("grps://localhost:2135"),
    52  				WithDatabase("/local"),
    53  				WithCredentials(NewAnonymousCredentials(WithSourceInfo(t.Name()))),
    54  			),
    55  			errorString: "something went wrong (" +
    56  				"endpoint:\"grps://localhost:2135\"," +
    57  				"database:\"/local\"," +
    58  				"credentials:\"Anonymous{From:\\\"TestAccessError\\\"}\"" +
    59  				"): test " +
    60  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:48)`", //nolint:lll
    61  		},
    62  		{
    63  			err: AccessError(
    64  				"something went wrong",
    65  				errors.New("test"),
    66  				WithEndpoint("grps://localhost:2135"),
    67  				WithDatabase("/local"),
    68  				WithCredentials(NewAccessTokenCredentials("SECRET_TOKEN", WithSourceInfo(""))),
    69  			),
    70  			errorString: "something went wrong (" +
    71  				"endpoint:\"grps://localhost:2135\"," +
    72  				"database:\"/local\"," +
    73  				"credentials:\"AccessToken{Token:\\\"****(CRC-32c: 9B7801F4)\\\"}\"" +
    74  				"): test " +
    75  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:63)`", //nolint:lll
    76  		},
    77  		{
    78  			err: AccessError(
    79  				"something went wrong",
    80  				errors.New("test"),
    81  				WithEndpoint("grps://localhost:2135"),
    82  				WithDatabase("/local"),
    83  				WithCredentials(NewAccessTokenCredentials("SECRET_TOKEN", WithSourceInfo(t.Name()))),
    84  			),
    85  			errorString: "something went wrong (" +
    86  				"endpoint:\"grps://localhost:2135\"," +
    87  				"database:\"/local\"," +
    88  				"credentials:\"AccessToken{Token:\\\"****(CRC-32c: 9B7801F4)\\\",From:\\\"TestAccessError\\\"}\"" +
    89  				"): test " +
    90  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:78)`", //nolint:lll
    91  		},
    92  		{
    93  			err: AccessError(
    94  				"something went wrong",
    95  				errors.New("test"),
    96  				WithEndpoint("grps://localhost:2135"),
    97  				WithDatabase("/local"),
    98  				WithCredentials(
    99  					NewStaticCredentials("USER", "SECRET_PASSWORD", "auth.endpoint:2135",
   100  						WithSourceInfo(""),
   101  					),
   102  				),
   103  			),
   104  			errorString: "something went wrong (" +
   105  				"endpoint:\"grps://localhost:2135\"," +
   106  				"database:\"/local\"," +
   107  				"credentials:\"Static{User:\\\"USER\\\",Password:\\\"SEC**********RD\\\",Token:\\\"****(CRC-32c: 00000000)\\\"}\"" + //nolint:lll
   108  				"): test " +
   109  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:93)`", //nolint:lll
   110  		},
   111  		{
   112  			err: AccessError(
   113  				"something went wrong",
   114  				errors.New("test"),
   115  				WithEndpoint("grps://localhost:2135"),
   116  				WithDatabase("/local"),
   117  				WithCredentials(
   118  					NewStaticCredentials("USER", "SECRET_PASSWORD", "auth.endpoint:2135",
   119  						WithSourceInfo(t.Name()),
   120  					),
   121  				),
   122  			),
   123  			errorString: "something went wrong (" +
   124  				"endpoint:\"grps://localhost:2135\"," +
   125  				"database:\"/local\"," +
   126  				"credentials:\"Static{User:\\\"USER\\\",Password:\\\"SEC**********RD\\\",Token:\\\"****(CRC-32c: 00000000)\\\",From:\\\"TestAccessError\\\"}\"" + //nolint:lll
   127  				"): test " +
   128  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:112)`", //nolint:lll
   129  		},
   130  		{
   131  			err: AccessError(
   132  				"something went wrong",
   133  				errors.New("test"),
   134  				WithEndpoint("grps://localhost:2135"),
   135  				WithDatabase("/local"),
   136  				WithCredentials(customCredentials{token: "SECRET_TOKEN"}),
   137  			),
   138  			errorString: "something went wrong (" +
   139  				"endpoint:\"grps://localhost:2135\"," +
   140  				"database:\"/local\"," +
   141  				"credentials:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.customCredentials\"" +
   142  				"): test " +
   143  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:131)`", //nolint:lll
   144  		},
   145  		{
   146  			err: AccessError(
   147  				"something went wrong",
   148  				errors.New("test"),
   149  				WithEndpoint("grps://localhost:2135"),
   150  				WithDatabase("/local"),
   151  				WithCredentials(NewAnonymousCredentials(WithSourceInfo(""))),
   152  			),
   153  			errorString: "something went wrong (" +
   154  				"endpoint:\"grps://localhost:2135\"," +
   155  				"database:\"/local\"," +
   156  				"credentials:\"Anonymous{}\"" +
   157  				"): test " +
   158  				"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:146)`", //nolint:lll
   159  		},
   160  	} {
   161  		t.Run("", func(t *testing.T) {
   162  			require.Equal(t, tt.errorString, tt.err.Error())
   163  		})
   164  	}
   165  }
   166  
   167  func TestWrongStringifyCustomCredentials(t *testing.T) {
   168  	require.Equal(t, "&{\"SECRET_TOKEN\"}", fmt.Sprintf("%q", &customCredentials{token: "SECRET_TOKEN"}))
   169  }
   170  
   171  func TestIsAccessError(t *testing.T) {
   172  	for _, tt := range []struct {
   173  		error error
   174  		is    bool
   175  	}{
   176  		{
   177  			error: grpcStatus.Error(grpcCodes.PermissionDenied, ""),
   178  			is:    true,
   179  		},
   180  		{
   181  			error: grpcStatus.Error(grpcCodes.Unauthenticated, ""),
   182  			is:    true,
   183  		},
   184  		{
   185  			error: xerrors.Transport(grpcStatus.Error(grpcCodes.PermissionDenied, "")),
   186  			is:    true,
   187  		},
   188  		{
   189  			error: xerrors.Transport(grpcStatus.Error(grpcCodes.Unauthenticated, "")),
   190  			is:    true,
   191  		},
   192  		{
   193  			error: xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_UNAUTHORIZED)),
   194  			is:    true,
   195  		},
   196  		{
   197  			error: errors.New("some error"),
   198  			is:    false,
   199  		},
   200  	} {
   201  		t.Run("", func(t *testing.T) {
   202  			require.Equal(t, tt.is, IsAccessError(tt.error), tt.error)
   203  		})
   204  	}
   205  }