github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/driver/postgres/privilege_util_test.go (about) 1 package postgres_test 2 3 import ( 4 "testing" 5 6 . "github.com/onsi/gomega" 7 8 "github.com/machinefi/w3bstream/pkg/depends/base/types" 9 confpostgres "github.com/machinefi/w3bstream/pkg/depends/conf/postgres" 10 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx" 11 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/driver/postgres" 12 ) 13 14 var d *confpostgres.Endpoint 15 16 func init() { 17 d = &confpostgres.Endpoint{ 18 Master: types.Endpoint{ 19 Hostname: "localhost", 20 Port: 15432, 21 Base: "postgres", 22 Username: "root", 23 Password: "test_passwd", 24 Param: map[string][]string{"sslmode": {"disable"}}, 25 }, 26 Database: sqlx.NewDatabase("postgres"), 27 } 28 d.SetDefault() 29 30 if err := d.Init(); err != nil { 31 panic(err) 32 } 33 } 34 35 func TestCurrentUser(t *testing.T) { 36 usename, err := postgres.CurrentUser(d) 37 NewWithT(t).Expect(err).To(BeNil()) 38 NewWithT(t).Expect(usename).To(Equal("root")) 39 } 40 41 func TestCreateUserIfNotExists(t *testing.T) { 42 var ( 43 usename = "notexist" 44 passwd = "nopasswd" 45 ) 46 47 err, isCreated := postgres.CreateUserIfNotExists(d, usename, passwd) 48 NewWithT(t).Expect(err).To(BeNil()) 49 NewWithT(t).Expect(isCreated).To(BeTrue()) 50 51 err, isCreated = postgres.CreateUserIfNotExists(d, usename, passwd) 52 NewWithT(t).Expect(err).To(BeNil()) 53 NewWithT(t).Expect(isCreated).To(BeFalse()) 54 55 err = postgres.DropUser(d, usename) 56 NewWithT(t).Expect(err).To(BeNil()) 57 } 58 59 func TestDropUser(t *testing.T) { 60 err := postgres.DropUser(d, "root") 61 NewWithT(t).Expect(err).To(Equal(postgres.ErrDropCurrentUser)) 62 63 var ( 64 usename = "notexist" 65 passwd = "nopasswd" 66 ) 67 err, isCreated := postgres.CreateUserIfNotExists(d, usename, passwd) 68 NewWithT(t).Expect(err).To(BeNil()) 69 NewWithT(t).Expect(isCreated).To(BeTrue()) 70 71 err = postgres.DropUser(d, usename) 72 NewWithT(t).Expect(err).To(BeNil()) 73 } 74 75 func TestGrantAllPrivileges(t *testing.T) { 76 err := postgres.GrantAllPrivileges(d, postgres.PrivilegeDomainDatabase, "postgres", "root") 77 NewWithT(t).Expect(err).To(BeNil()) 78 } 79 80 func TestAlterUserConnectionLimit(t *testing.T) { 81 err := postgres.AlterUserConnectionLimit(d, "root", 100) 82 NewWithT(t).Expect(err).To(BeNil()) 83 }