onasty/e2e/e2e_test.go(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
package e2e_test
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/pgx"
"github.com/jackc/pgx/v5/stdlib"
"github.com/olexsmir/onasty/internal/config"
"github.com/olexsmir/onasty/internal/hasher"
"github.com/olexsmir/onasty/internal/jwtutil"
"github.com/olexsmir/onasty/internal/logger"
"github.com/olexsmir/onasty/internal/mailer"
"github.com/olexsmir/onasty/internal/service/notesrv"
"github.com/olexsmir/onasty/internal/service/usersrv"
"github.com/olexsmir/onasty/internal/store/psql/noterepo"
"github.com/olexsmir/onasty/internal/store/psql/sessionrepo"
"github.com/olexsmir/onasty/internal/store/psql/userepo"
"github.com/olexsmir/onasty/internal/store/psql/vertokrepo"
"github.com/olexsmir/onasty/internal/store/psqlutil"
"github.com/olexsmir/onasty/internal/store/rdb"
"github.com/olexsmir/onasty/internal/store/rdb/usercache"
httptransport "github.com/olexsmir/onasty/internal/transport/http"
"github.com/olexsmir/onasty/internal/transport/http/ratelimit"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/testcontainers/testcontainers-go"
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
tcredis "github.com/testcontainers/testcontainers-go/modules/redis"
"github.com/testcontainers/testcontainers-go/wait"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
type (
stopFunc func()
AppTestSuite struct {
suite.Suite
ctx context.Context
require *require.Assertions
postgresDB *psqlutil.DB
stopPostgres stopFunc
redisDB *rdb.DB
stopRedis stopFunc
router http.Handler
hasher hasher.Hasher
jwtTokenizer jwtutil.JWTTokenizer
mailer *mailer.TestMailer
}
errorResponse struct {
Message string `json:"message"`
}
)
func TestAppSuite(t *testing.T) {
if testing.Short() {
t.Skip()
}
// gin output is too verbose(and annoying) in tests
gin.SetMode(gin.TestMode)
suite.Run(t, new(AppTestSuite))
}
func (e *AppTestSuite) SetupSuite() {
e.ctx = context.Background()
e.require = e.Require()
e.postgresDB, e.stopPostgres = e.prepPostgres()
e.redisDB, e.stopRedis = e.prepRedis()
e.initDeps()
}
func (e *AppTestSuite) TearDownSuite() {
e.stopPostgres()
e.stopRedis()
}
// initDeps initializes the dependencies for the app
// and sets up the router for tests
func (e *AppTestSuite) initDeps() {
cfg := e.getConfig()
logger, err := logger.NewCustomLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogShowLine)
e.require.NoError(err)
slog.SetDefault(logger)
e.hasher = hasher.NewSHA256Hasher(cfg.PasswordSalt)
e.jwtTokenizer = jwtutil.NewJWTUtil(cfg.JwtSigningKey, time.Hour)
e.mailer = mailer.NewTestMailer()
sessionrepo := sessionrepo.New(e.postgresDB)
vertokrepo := vertokrepo.New(e.postgresDB)
userepo := userepo.New(e.postgresDB)
usercache := usercache.New(e.redisDB, cfg.CacheUsersTTL)
usersrv := usersrv.New(
userepo,
sessionrepo,
vertokrepo,
e.hasher,
e.jwtTokenizer,
e.mailer,
usercache,
cfg.JwtRefreshTokenTTL,
cfg.VerificationTokenTTL,
cfg.AppURL,
)
noterepo := noterepo.New(e.postgresDB)
notesrv := notesrv.New(noterepo)
// for testing purposes, it's ok to have high values ig
ratelimitCfg := ratelimit.Config{
RPS: 1000,
TTL: time.Millisecond,
Burst: 1000,
}
handler := httptransport.NewTransport(usersrv, notesrv, ratelimitCfg)
e.router = handler.Handler()
}
func (e *AppTestSuite) prepPostgres() (*psqlutil.DB, stopFunc) {
dbCredential := "testing"
postgresContainer, err := tcpostgres.Run(e.ctx,
"postgres:16-alpine",
tcpostgres.WithUsername(dbCredential),
tcpostgres.WithPassword(dbCredential),
tcpostgres.WithDatabase(dbCredential),
testcontainers.WithWaitStrategy(wait.ForListeningPort("5432/tcp")))
e.require.NoError(err)
stop := func() { e.require.NoError(postgresContainer.Terminate(e.ctx)) }
// connect to the db
host, err := postgresContainer.Host(e.ctx)
e.require.NoError(err)
port, err := postgresContainer.MappedPort(e.ctx, "5432/tcp")
e.require.NoError(err)
db, err := psqlutil.Connect(e.ctx, fmt.Sprintf( //nolint:nosprintfhostport
"postgres://%s:%s@%s:%s/%s",
dbCredential,
dbCredential,
host,
port.Port(),
dbCredential,
))
e.require.NoError(err)
// run migrations
sdb := stdlib.OpenDBFromPool(db.Pool)
driver, err := pgx.WithInstance(sdb, &pgx.Config{}) //nolint:exhaustruct
e.require.NoError(err)
m, err := migrate.NewWithDatabaseInstance(
"file://../migrations/",
"pgxv5", driver,
)
e.require.NoError(err)
e.require.NoError(m.Up())
e.require.NoError(driver.Close())
return db, stop
}
func (e *AppTestSuite) prepRedis() (*rdb.DB, stopFunc) {
redisContainer, err := tcredis.Run(e.ctx, "redis:7.4-alpine")
e.require.NoError(err)
stop := func() { e.require.NoError(redisContainer.Terminate(e.ctx)) }
uri, err := redisContainer.ConnectionString(e.ctx)
e.require.NoError(err)
connOpts, err := redis.ParseURL(uri)
e.require.NoError(err)
redis, err := rdb.Connect(e.ctx, connOpts.Addr, connOpts.Password, connOpts.DB)
e.require.NoError(err)
return redis, stop
}
func (e *AppTestSuite) getConfig() *config.Config {
return &config.Config{ //nolint:exhaustruct
AppEnv: "testing",
AppURL: "",
ServerPort: "3000",
PasswordSalt: "salty-password",
JwtSigningKey: "jwt-key",
JwtAccessTokenTTL: time.Hour,
JwtRefreshTokenTTL: 24 * time.Hour,
VerificationTokenTTL: 24 * time.Hour,
LogShowLine: os.Getenv("LOG_SHOW_LINE") == "true",
LogFormat: "text",
LogLevel: "debug",
CacheUsersTTL: time.Second,
}
}
|