diff --git a/internal/api/app.go b/internal/api/app.go index e86e2d4..0947956 100644 --- a/internal/api/app.go +++ b/internal/api/app.go @@ -13,6 +13,7 @@ import ( "github.com/twofas/2fas-server/internal/common/api" "github.com/twofas/2fas-server/internal/common/db" "github.com/twofas/2fas-server/internal/common/redis" + "github.com/twofas/2fas-server/internal/common/validation" ) var validate *validator.Validate @@ -38,11 +39,13 @@ func NewApplication(config config.Configuration) *Application { database := db.NewDbConnection(config) redisClient := redis.New(config.Redis.ServiceUrl, config.Redis.Port) + validate.RegisterValidation("not_blank", validation.NotBlank) + modules := []Module{ health.NewHealthModule(config, redisClient), support.NewSupportModule(config, gorm, database, validate), icons.NewIconsModule(config, gorm, database, validate), - extension.NewBrowserExtensionModule(config, gorm, database, redisClient), + extension.NewBrowserExtensionModule(config, gorm, database, redisClient, validate), mobile.NewMobileModule(config, gorm, database, validate, redisClient), } diff --git a/internal/api/browser_extension/ports/http.go b/internal/api/browser_extension/ports/http.go index 707032b..7c095c3 100644 --- a/internal/api/browser_extension/ports/http.go +++ b/internal/api/browser_extension/ports/http.go @@ -20,9 +20,7 @@ type RoutesHandler struct { validator *validator.Validate } -func NewRoutesHandler(cqrs *app.Cqrs) *RoutesHandler { - validate := validator.New() - +func NewRoutesHandler(cqrs *app.Cqrs, validate *validator.Validate) *RoutesHandler { return &RoutesHandler{ cqrs: cqrs, validator: validate, diff --git a/internal/api/browser_extension/service/service.go b/internal/api/browser_extension/service/service.go index 7a6992c..035b780 100644 --- a/internal/api/browser_extension/service/service.go +++ b/internal/api/browser_extension/service/service.go @@ -3,6 +3,7 @@ package service import ( "database/sql" "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" "github.com/go-redis/redis/v8" "github.com/twofas/2fas-server/config" "github.com/twofas/2fas-server/internal/api/browser_extension/adapters" @@ -27,7 +28,13 @@ type BrowserExtensionModule struct { Config config.Configuration } -func NewBrowserExtensionModule(config config.Configuration, gorm *gorm.DB, database *sql.DB, redisClient *redis.Client) *BrowserExtensionModule { +func NewBrowserExtensionModule( + config config.Configuration, + gorm *gorm.DB, + database *sql.DB, + redisClient *redis.Client, + validate *validator.Validate, +) *BrowserExtensionModule { queryBuilder := db.NewQueryBuilder(database) browserExtensionsMysqlRepository := adapters.NewBrowserExtensionsMysqlRepository(gorm) @@ -102,7 +109,7 @@ func NewBrowserExtensionModule(config config.Configuration, gorm *gorm.DB, datab }, } - routesHandler := ports.NewRoutesHandler(cqrs) + routesHandler := ports.NewRoutesHandler(cqrs, validate) module := &BrowserExtensionModule{ Cqrs: cqrs, diff --git a/internal/common/validation/validators.go b/internal/common/validation/validators.go new file mode 100644 index 0000000..4575266 --- /dev/null +++ b/internal/common/validation/validators.go @@ -0,0 +1,10 @@ +package validation + +import ( + "github.com/go-playground/validator/v10" + "strings" +) + +func NotBlank(fl validator.FieldLevel) bool { + return strings.TrimSpace(fl.Field().String()) != "" +} diff --git a/tests/browser_extension/browser_extension_test.go b/tests/browser_extension/browser_extension_test.go index 98606e1..353100c 100644 --- a/tests/browser_extension/browser_extension_test.go +++ b/tests/browser_extension/browser_extension_test.go @@ -1,10 +1,13 @@ package tests import ( + "fmt" "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" + "github.com/twofas/2fas-server/internal/common/crypto" "github.com/twofas/2fas-server/tests" + "net/http" "testing" ) @@ -20,6 +23,28 @@ func (s *BrowserExtensionTestSuite) SetupTest() { tests.DoSuccessDelete(s.T(), "/browser_extensions") } +func (s *BrowserExtensionTestSuite) TestCreateBrowserExtension() { + type testCase struct { + extensionName string + expectedHttpCode int + } + + testsCases := []testCase{ + {extensionName: "", expectedHttpCode: 400}, + {extensionName: " ", expectedHttpCode: 400}, + {extensionName: " ", expectedHttpCode: 400}, + {extensionName: "abc", expectedHttpCode: 200}, + {extensionName: "efg ", expectedHttpCode: 200}, + {extensionName: " ab123 ", expectedHttpCode: 200}, + } + + for _, tc := range testsCases { + response := createBrowserExtension(tc.extensionName) + + assert.Equal(s.T(), tc.expectedHttpCode, response.StatusCode) + } +} + func (s *BrowserExtensionTestSuite) TestUpdateBrowserExtension() { browserExt := tests.CreateBrowserExtension(s.T(), "go-test") @@ -49,3 +74,14 @@ func (s *BrowserExtensionTestSuite) TestDoNotFindNotExistingExtension() { assert.Equal(s.T(), 404, response.StatusCode) } + +func createBrowserExtension(name string) *http.Response { + keyPair := crypto.GenerateKeyPair(2048) + + pubKey := crypto.PublicKeyToBase64(keyPair.PublicKey) + + payload := []byte(fmt.Sprintf(`{"name":"%s","browser_name":"go-browser","browser_version":"0.1","public_key":"%s"}`, name, pubKey)) + + return tests.DoPost("/browser_extensions", payload, nil) + +}