From 6a7e550d8ee25eddf66403f2326544c215cbb701 Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Wed, 13 Nov 2024 12:07:47 +0300 Subject: [PATCH] Added tests for wrong login/password --- tests/integration/driver_test.go | 193 +++++++++++++++++++------------ 1 file changed, 116 insertions(+), 77 deletions(-) diff --git a/tests/integration/driver_test.go b/tests/integration/driver_test.go index 9121cec06..af950ce37 100644 --- a/tests/integration/driver_test.go +++ b/tests/integration/driver_test.go @@ -30,6 +30,7 @@ import ( "github.com/ydb-platform/ydb-go-sdk/v3" "github.com/ydb-platform/ydb-go-sdk/v3/config" + "github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials" "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta" "github.com/ydb-platform/ydb-go-sdk/v3/internal/version" "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" @@ -193,94 +194,132 @@ func TestDriver(sourceTest *testing.T) { require.NoError(t, err) }() t.RunSynced("UsingConnectionString", func(t *xtest.SyncedTest) { - u, err := url.Parse(os.Getenv("YDB_CONNECTION_STRING")) - require.NoError(t, err) - u.User = url.UserPassword("test", "password") - t.Log(u.String()) - db, err := ydb.Open(ctx, - u.String(), - ) - require.NoError(t, err) - defer func() { - _ = db.Close(ctx) - }() - row, err := db.Query().QueryRow(ctx, `SELECT 1`) - require.NoError(t, err) - var v int - err = row.Scan(&v) - require.NoError(t, err) - tableName := path.Join(db.Name(), t.Name(), "test") - t.RunSynced("CreateTable", func(t *xtest.SyncedTest) { - err := db.Query().Exec(ctx, fmt.Sprintf(` - CREATE TABLE IF NOT EXISTS %s ( - id Uint64, - value Utf8, - PRIMARY KEY (id) - )`, "`"+tableName+"`"), - ) + t.RunSynced("HappyWay", func(t *xtest.SyncedTest) { + u, err := url.Parse(os.Getenv("YDB_CONNECTION_STRING")) require.NoError(t, err) - }) - t.RunSynced("DescribeTable", func(t *xtest.SyncedTest) { - var d options.Description - err := db.Table().Do(ctx, func(ctx context.Context, s table.Session) error { - d, err = s.DescribeTable(ctx, tableName) - if err != nil { - return err - } + u.User = url.UserPassword("test", "password") + t.Log(u.String()) + db, err := ydb.Open(ctx, u.String()) + require.NoError(t, err) + defer func() { + _ = db.Close(ctx) + }() + row, err := db.Query().QueryRow(ctx, `SELECT 1`) + require.NoError(t, err) + var v int + err = row.Scan(&v) + require.NoError(t, err) + tableName := path.Join(db.Name(), t.Name(), "test") + t.RunSynced("CreateTable", func(t *xtest.SyncedTest) { + err := db.Query().Exec(ctx, fmt.Sprintf(` + CREATE TABLE IF NOT EXISTS %s ( + id Uint64, + value Utf8, + PRIMARY KEY (id) + )`, "`"+tableName+"`"), + ) + require.NoError(t, err) + }) + t.RunSynced("DescribeTable", func(t *xtest.SyncedTest) { + var d options.Description + err := db.Table().Do(ctx, func(ctx context.Context, s table.Session) error { + d, err = s.DescribeTable(ctx, tableName) + if err != nil { + return err + } - return nil + return nil + }) + require.NoError(t, err) + require.Equal(t, "test", d.Name) + require.Equal(t, 2, len(d.Columns)) + require.Equal(t, "id", d.Columns[0].Name) + require.Equal(t, "value", d.Columns[1].Name) + require.Equal(t, []string{"id"}, d.PrimaryKey) }) + }) + t.RunSynced("WrongLogin", func(t *xtest.SyncedTest) { + u, err := url.Parse(os.Getenv("YDB_CONNECTION_STRING")) + require.NoError(t, err) + u.User = url.UserPassword("wrong_login", "password") + db, err := ydb.Open(ctx, u.String()) + require.Error(t, err) + require.Nil(t, db) + require.True(t, credentials.IsAccessError(err)) + }) + t.RunSynced("WrongPassword", func(t *xtest.SyncedTest) { + u, err := url.Parse(os.Getenv("YDB_CONNECTION_STRING")) require.NoError(t, err) - require.Equal(t, "test", d.Name) - require.Equal(t, 2, len(d.Columns)) - require.Equal(t, "id", d.Columns[0].Name) - require.Equal(t, "value", d.Columns[1].Name) - require.Equal(t, []string{"id"}, d.PrimaryKey) + u.User = url.UserPassword("test", "wrong_password") + db, err := ydb.Open(ctx, u.String()) + require.Error(t, err) + require.Nil(t, db) + require.True(t, credentials.IsAccessError(err)) }) }) t.RunSynced("UsingExplicitStaticCredentials", func(t *xtest.SyncedTest) { - db, err := ydb.Open(ctx, - os.Getenv("YDB_CONNECTION_STRING"), - ydb.WithStaticCredentials("test", "password"), - ) - require.NoError(t, err) - defer func() { - _ = db.Close(ctx) - }() - tableName := path.Join(db.Name(), t.Name(), "test") - t.RunSynced("CreateTable", func(t *xtest.SyncedTest) { - err := db.Query().Exec(ctx, fmt.Sprintf(` - CREATE TABLE IF NOT EXISTS %s ( - id Uint64, - value Utf8, - PRIMARY KEY (id) - )`, "`"+tableName+"`"), + t.RunSynced("HappyWay", func(t *xtest.SyncedTest) { + db, err := ydb.Open(ctx, + os.Getenv("YDB_CONNECTION_STRING"), + ydb.WithStaticCredentials("test", "password"), ) require.NoError(t, err) - }) - t.RunSynced("Query", func(t *xtest.SyncedTest) { - row, err := db.Query().QueryRow(ctx, `SELECT 1`) - require.NoError(t, err) - var v int - err = row.Scan(&v) - require.NoError(t, err) - }) - t.RunSynced("DescribeTable", func(t *xtest.SyncedTest) { - var d options.Description - err := db.Table().Do(ctx, func(ctx context.Context, s table.Session) error { - d, err = s.DescribeTable(ctx, tableName) - if err != nil { - return err - } + defer func() { + _ = db.Close(ctx) + }() + tableName := path.Join(db.Name(), t.Name(), "test") + t.RunSynced("CreateTable", func(t *xtest.SyncedTest) { + err := db.Query().Exec(ctx, fmt.Sprintf(` + CREATE TABLE IF NOT EXISTS %s ( + id Uint64, + value Utf8, + PRIMARY KEY (id) + )`, "`"+tableName+"`"), + ) + require.NoError(t, err) + }) + t.RunSynced("Query", func(t *xtest.SyncedTest) { + row, err := db.Query().QueryRow(ctx, `SELECT 1`) + require.NoError(t, err) + var v int + err = row.Scan(&v) + require.NoError(t, err) + }) + t.RunSynced("DescribeTable", func(t *xtest.SyncedTest) { + var d options.Description + err := db.Table().Do(ctx, func(ctx context.Context, s table.Session) error { + d, err = s.DescribeTable(ctx, tableName) + if err != nil { + return err + } - return nil + return nil + }) + require.NoError(t, err) + require.Equal(t, "test", d.Name) + require.Equal(t, 2, len(d.Columns)) + require.Equal(t, "id", d.Columns[0].Name) + require.Equal(t, "value", d.Columns[1].Name) + require.Equal(t, []string{"id"}, d.PrimaryKey) }) - require.NoError(t, err) - require.Equal(t, "test", d.Name) - require.Equal(t, 2, len(d.Columns)) - require.Equal(t, "id", d.Columns[0].Name) - require.Equal(t, "value", d.Columns[1].Name) - require.Equal(t, []string{"id"}, d.PrimaryKey) + }) + t.RunSynced("WrongLogin", func(t *xtest.SyncedTest) { + db, err := ydb.Open(ctx, + os.Getenv("YDB_CONNECTION_STRING"), + ydb.WithStaticCredentials("wrong_user", "password"), + ) + require.Error(t, err) + require.Nil(t, db) + require.True(t, credentials.IsAccessError(err)) + }) + t.RunSynced("WrongPassword", func(t *xtest.SyncedTest) { + db, err := ydb.Open(ctx, + os.Getenv("YDB_CONNECTION_STRING"), + ydb.WithStaticCredentials("test", "wrong_password"), + ) + require.Error(t, err) + require.Nil(t, db) + require.True(t, credentials.IsAccessError(err)) }) }) })