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