mirror of
https://github.com/twofas/2fas-server.git
synced 2024-11-24 19:30:51 +01:00
validate blank browser extension name
This commit is contained in:
parent
6f9b7871cd
commit
bffc5418ed
@ -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),
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
10
internal/common/validation/validators.go
Normal file
10
internal/common/validation/validators.go
Normal file
@ -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()) != ""
|
||||
}
|
@ -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)
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user