helpers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package testing
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "time"
  7. "git.linuxforward.com/byom/byom-core/config"
  8. "git.linuxforward.com/byom/byom-core/store"
  9. "github.com/sirupsen/logrus"
  10. "github.com/stretchr/testify/require"
  11. "gorm.io/driver/sqlite"
  12. "gorm.io/gorm"
  13. )
  14. // TestDB creates a temporary test database
  15. func TestDB(t *testing.T) *store.DataStore {
  16. t.Helper()
  17. // Create temp directory for test database
  18. tmpDir, err := os.MkdirTemp("", "byom-test-*")
  19. require.NoError(t, err)
  20. // Clean up after test
  21. t.Cleanup(func() {
  22. os.RemoveAll(tmpDir)
  23. })
  24. // Create test database
  25. //dbPath := filepath.Join(tmpDir, "test.db")
  26. dbPath := "../.data/app.db"
  27. db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
  28. require.NoError(t, err)
  29. return store.NewDataStore(db)
  30. }
  31. // TestConfig creates a test configuration
  32. func TestConfig() *config.Config {
  33. return &config.Config{
  34. Server: &config.Server{
  35. ListeningPort: 8080,
  36. TlsConfig: &config.TlsConfig{
  37. Enabled: false,
  38. },
  39. CorsOrigins: []string{"http://localhost:3000"},
  40. },
  41. Database: &config.Database{
  42. Path: "./data/app.db",
  43. MaxOpenConns: 1,
  44. MaxIdleConns: 1,
  45. ConnMaxLifetime: 300,
  46. },
  47. Jwt: &config.Jwt{
  48. JwtSecret: "test-secret",
  49. },
  50. Smtp: &config.Smtp{
  51. Host: "localhost",
  52. Port: 1025,
  53. User: "test",
  54. Pass: "test",
  55. },
  56. Hook: &config.Hook{
  57. BaseURL: "http://localhost:8081",
  58. Domain: "test",
  59. SecretKey: "test-secret",
  60. },
  61. }
  62. }
  63. // TestLogger creates a test logger
  64. func TestLogger() *logrus.Logger {
  65. logger := logrus.New()
  66. logger.SetOutput(os.Stdout)
  67. logger.SetLevel(logrus.DebugLevel)
  68. return logger
  69. }
  70. // TestContext creates a context for testing
  71. func TestContext() (context.Context, context.CancelFunc) {
  72. return context.WithTimeout(context.Background(), 5*time.Second)
  73. }
  74. // MockEmailService is a mock implementation of the email service
  75. type MockEmailService struct {
  76. SendInviteEmailFunc func(email, token, workspaceID string) error
  77. }
  78. func (m *MockEmailService) SendInviteEmail(email, token, workspaceID string) error {
  79. if m.SendInviteEmailFunc != nil {
  80. return m.SendInviteEmailFunc(email, token, workspaceID)
  81. }
  82. return nil
  83. }
  84. func (m *MockEmailService) Close() error {
  85. return nil
  86. }
  87. // MockHookClient is a mock implementation of the hook client
  88. type MockHookClient struct {
  89. SendHookFunc func(event string, payload interface{}) error
  90. }
  91. func (m *MockHookClient) SendHook(event string, payload interface{}) error {
  92. if m.SendHookFunc != nil {
  93. return m.SendHookFunc(event, payload)
  94. }
  95. return nil
  96. }
  97. func (m *MockHookClient) Close() error {
  98. return nil
  99. }