mirror of
https://github.com/gofiber/storage.git
synced 2025-10-06 09:07:14 +08:00
chore: proper creation of the config in tests
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ const (
|
|||||||
postgresDatabase string = "fiber"
|
postgresDatabase string = "fiber"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newTestStore(t testing.TB) (*Storage, error) {
|
func newTestConfig(t testing.TB) (Config, error) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -41,19 +42,41 @@ func newTestStore(t testing.TB) (*Storage, error) {
|
|||||||
testcontainers.CleanupContainer(t, c)
|
testcontainers.CleanupContainer(t, c)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var cfg Config
|
||||||
conn, err := c.ConnectionString(ctx, "sslmode=disable")
|
conn, err := c.ConnectionString(ctx, "sslmode=disable")
|
||||||
|
if err != nil {
|
||||||
|
return cfg, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg = Config{
|
||||||
|
ConnectionURI: conn,
|
||||||
|
Reset: true,
|
||||||
|
}
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestStore(t testing.TB) (*Storage, error) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
cfg, err := newTestConfig(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return New(Config{
|
return newTestStoreWithConfig(t, cfg)
|
||||||
ConnectionURI: conn,
|
}
|
||||||
Reset: true,
|
|
||||||
}), nil
|
func newTestStoreWithConfig(t testing.TB, cfg Config) (*Storage, error) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
return New(cfg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoCreateUser(t *testing.T) {
|
func TestNoCreateUser(t *testing.T) {
|
||||||
testStore, err := newTestStore(t)
|
cfg, err := newTestConfig(t)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testStore, err := newTestStoreWithConfig(t, cfg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
|
||||||
@@ -83,6 +106,9 @@ func TestNoCreateUser(t *testing.T) {
|
|||||||
_, err = conn.Exec(ctx, "REVOKE CREATE ON SCHEMA public FROM "+username)
|
_, err = conn.Exec(ctx, "REVOKE CREATE ON SCHEMA public FROM "+username)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// new connection with the user and password without privileges
|
||||||
|
unpriviledgedURI := strings.Replace(cfg.ConnectionURI, postgresUser+":"+postgresPass, username+":"+password, 1)
|
||||||
|
|
||||||
t.Run("should panic if limited user tries to create table", func(t *testing.T) {
|
t.Run("should panic if limited user tries to create table", func(t *testing.T) {
|
||||||
tableThatDoesNotExist := "public.table_does_not_exists_" + strconv.Itoa(int(time.Now().UnixNano()))
|
tableThatDoesNotExist := "public.table_does_not_exists_" + strconv.Itoa(int(time.Now().UnixNano()))
|
||||||
|
|
||||||
@@ -91,23 +117,23 @@ func TestNoCreateUser(t *testing.T) {
|
|||||||
require.NotNil(t, r, "Expected a panic when creating a table without permissions")
|
require.NotNil(t, r, "Expected a panic when creating a table without permissions")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
panicCfg := Config{
|
||||||
|
ConnectionURI: unpriviledgedURI,
|
||||||
|
Reset: true,
|
||||||
|
Table: tableThatDoesNotExist,
|
||||||
|
}
|
||||||
|
|
||||||
// This should panic since the user doesn't have CREATE permissions
|
// This should panic since the user doesn't have CREATE permissions
|
||||||
New(Config{
|
New(panicCfg)
|
||||||
Database: postgresDatabase,
|
|
||||||
Username: username,
|
|
||||||
Password: password,
|
|
||||||
Reset: true,
|
|
||||||
Table: tableThatDoesNotExist,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
limitedCfg := Config{
|
||||||
|
ConnectionURI: unpriviledgedURI,
|
||||||
|
Reset: false,
|
||||||
|
}
|
||||||
|
|
||||||
// connect to an existing table using an unprivileged user
|
// connect to an existing table using an unprivileged user
|
||||||
limitedStore := New(Config{
|
limitedStore := New(limitedCfg)
|
||||||
Database: os.Getenv("POSTGRES_DATABASE"),
|
|
||||||
Username: username,
|
|
||||||
Password: password,
|
|
||||||
Reset: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
limitedStore.Close()
|
limitedStore.Close()
|
||||||
|
Reference in New Issue
Block a user