Skip to content

Commit

Permalink
Added tests for wrong login/password
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Nov 13, 2024
1 parent 35c2fa4 commit 6a7e550
Showing 1 changed file with 116 additions and 77 deletions.
193 changes: 116 additions & 77 deletions tests/integration/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
})
})
})
Expand Down

0 comments on commit 6a7e550

Please sign in to comment.