package controllers_test import ( "context" "database/sql" "fmt" "os" "testing" "time" "gitea.badhouseplants.net/softplayer/softplayer-backend/internal/controllers" "gitea.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/postgres" "github.com/google/uuid" _ "github.com/jackc/pgx/v5/stdlib" "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" ) func newTestDBConnection(ctx context.Context) *sql.DB { connStr, ok := os.LookupEnv("SOFTPLAYER_DB_CONNECTION_STRING") if !ok { // Default connection string connStr = "postgres://softplayer:qwertyu9@localhost:30432/softplayer?sslmode=disable" } db, err := postgres.Open(ctx, connStr) if err != nil { panic(err) } return db } func newTestRedisConnection() *redis.Client { connStr, ok := os.LookupEnv("SOFTPLAYER_REDIS_HOST") if !ok { // Default redis host connStr = "localhost:30379" } return redis.NewClient(&redis.Options{ Addr: connStr, }) } func newTestAccountController(ctx context.Context) *controllers.AccountController { return &controllers.AccountController{ DB: newTestDBConnection(ctx), Redis: newTestRedisConnection(), DevMode: true, HashCost: 3, AccessTokenTTL: time.Second * 10, RefreshTokenTTL: time.Second * 15, JWTSecret: []byte("test-secret"), } } func newTestUniqueEmail(prefix string) string { if prefix == "" { prefix = "test" } return fmt.Sprintf( "%s-%d-%s@example.com", prefix, time.Now().UnixMilli(), uuid.NewString(), ) } func TestIntegrationAccountCreate_Success(t *testing.T) { ctrl := newTestAccountController(t.Context()) accountData := &controllers.AccountData{ Password: "qwertyu9", Email: newTestUniqueEmail("accounts"), } id, err := ctrl.Create(t.Context(), accountData) assert.NoError(t, err) assert.NotEmpty(t, id) } func TestIntegrationAccountCreate_ExistingAccountErr(t *testing.T) { ctrl := newTestAccountController(t.Context()) email := newTestUniqueEmail("accounts") accountData := &controllers.AccountData{ Password: "qwertyu9", Email: email, } id, err := ctrl.Create(t.Context(), accountData) assert.NoError(t, err) assert.NotEmpty(t, id) id, err = ctrl.Create(t.Context(), accountData) assert.Empty(t, id) assert.Error(t, err) assert.ErrorIs(t, err, controllers.ErrEmailUsed) } func TestIntegrationAccountLogin_Success(t *testing.T) { ctrl := newTestAccountController(t.Context()) email := newTestUniqueEmail("accounts") accountData := &controllers.AccountData{ Password: "qwertyu9", Email: email, } id, err := ctrl.Create(t.Context(), accountData) assert.NoError(t, err) assert.NotEmpty(t, id) accountData.UUID = id id, err = ctrl.Login(t.Context(), accountData.Email, accountData.Password) assert.NoError(t, err) assert.NotEmpty(t, id) } func TestIntegrationAccountLogin_WrongPassword(t *testing.T) { ctrl := newTestAccountController(t.Context()) email := newTestUniqueEmail("accounts") accountData := &controllers.AccountData{ Password: "qwertyu9", Email: email, } id, err := ctrl.Create(t.Context(), accountData) assert.NoError(t, err) assert.NotEmpty(t, id) accountData.UUID = id id, err = ctrl.Login(t.Context(), accountData.Email, "Wrong Password") assert.Empty(t, id) assert.Error(t, err) assert.ErrorIs(t, err, controllers.ErrWrongPassword) } func TestIntegrationAccountLogin_WrongEmail(t *testing.T) { ctrl := newTestAccountController(t.Context()) email := newTestUniqueEmail("accounts") accountData := &controllers.AccountData{ Password: "qwertyu9", Email: email, } id, err := ctrl.Create(t.Context(), accountData) assert.NoError(t, err) assert.NotEmpty(t, id) accountData.UUID = id id, err = ctrl.Login(t.Context(), "some@email.com", "Wrong Password") assert.Empty(t, id) assert.Error(t, err) assert.ErrorIs(t, err, controllers.ErrUserNotFound) }