Skip to content

Commit

Permalink
Merge pull request #960 from Shruthi-1MN/fix-924
Browse files Browse the repository at this point in the history
Fix-#924 fileshare description validation
  • Loading branch information
leonwanghui committed Jul 11, 2019
2 parents b0132e1 + 71aed06 commit 63a1b38
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/api/util/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"net"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -163,6 +164,20 @@ func CreateFileShareDBEntry(ctx *c.Context, in *model.FileShareSpec) (*model.Fil
log.Error(errMsg)
return nil, errors.New(errMsg)
}

// validate the description
reg, err := regexp.Compile("[^a-zA-Z0-9 ]+")
if err != nil {
errMsg := fmt.Sprintf("regex compilation for file share description validation failed")
log.Error(errMsg)
return nil, errors.New(errMsg)
}
if reg.MatchString(in.Description) {
errMsg := fmt.Sprintf("invalid fileshare description and it has some special characters")
log.Error(errMsg)
return nil, errors.New(errMsg)
}

in.UserId = ctx.UserId
in.Status = model.FileShareCreating
// Store the fileshare meadata into database.
Expand Down
80 changes: 80 additions & 0 deletions pkg/api/util/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package util
import (
"fmt"
"reflect"
"strconv"
"testing"

"github.com/opensds/opensds/pkg/context"
Expand Down Expand Up @@ -475,3 +476,82 @@ func TestCreateFileShareSnapshotDBEntry(t *testing.T) {
})

}

func TestCreateFileShareDBEntry(t *testing.T) {
var in = &model.FileShareSpec{
BaseModel: &model.BaseModel{},
Name: "sample-fileshare-01",
Description: "This is a sample fileshare for testing",
Size: int64(1),
ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca",
Status: model.FileShareCreating,
}

t.Run("Everything should work well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
db.C = mockClient

var expected = &SampleFileShares[0]
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
if err != nil {
t.Errorf("failed to create fileshare err is %v\n", err)
}
assertTestResult(t, result, expected)
})

t.Run("The size of fileshare created should be greater than zero", func(t *testing.T) {
in.Size = int64(-2)
mockClient := new(dbtest.Client)
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
db.C = mockClient

_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
expectedError := fmt.Sprintf("invalid fileshare size: %d", in.Size)
assertTestResult(t, err.Error(), expectedError)
})

t.Run("The profile id should not be empty", func(t *testing.T) {
in.ProfileId = ""
mockClient := new(dbtest.Client)
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
db.C = mockClient

_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
expectedError := "profile id can not be empty when creating fileshare in db!"
assertTestResult(t, err.Error(), expectedError)
})

t.Run("Empty file share name is allowed", func(t *testing.T) {
in.Size, in.Name, in.ProfileId = int64(1),"", "b3585ebe-c42c-120g-b28e-f373746a71ca"
mockClient := new(dbtest.Client)
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
db.C = mockClient

_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
expectedError := "empty fileshare name is not allowed. Please give valid name."
assertTestResult(t, err.Error(), expectedError)
})

t.Run("File share name length more than 128 characters are not allowed", func(t *testing.T) {
in.Size, in.Name, in.ProfileId = int64(1),"kjxdpvfuenecpyhbxdumwibipjkigcjykabuhghjghjgjgjgjgjghhkkkkkkkkkkkkkkklsccdesnkgxnjnhfjusigceorogdgjlyciemscgvncerucokfekdiviuyplbly", "b3585ebe-c42c-120g-b28e-f373746a71ca"
mockClient := new(dbtest.Client)
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
db.C = mockClient

_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
expectedError := "fileshare name length should not more than 128 characters. current length is : "+strconv.Itoa(len(in.Name))
assertTestResult(t, err.Error(), expectedError)
})

t.Run("Special characters in file share description are not allowed", func(t *testing.T) {
in.Size, in.Name, in.ProfileId, in.Description = int64(1),"sample-fileshare-01", "b3585ebe-c42c-120g-b28e-f373746a71ca", "#FileShare Code!$! test"
mockClient := new(dbtest.Client)
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
db.C = mockClient

_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
expectedError := "invalid fileshare description and it has some special characters"
assertTestResult(t, err.Error(), expectedError)
})
}

0 comments on commit 63a1b38

Please sign in to comment.