diff --git a/pkg/api/controllers/fileshare_test.go b/pkg/api/controllers/fileshare_test.go index d40fd6b07..9c327e83f 100644 --- a/pkg/api/controllers/fileshare_test.go +++ b/pkg/api/controllers/fileshare_test.go @@ -19,9 +19,11 @@ import ( ctx "context" "encoding/json" "errors" + "github.com/sodafoundation/api/pkg/utils/constants" "net/http" "net/http/httptest" "testing" + "time" "github.com/astaxie/beego" "github.com/astaxie/beego/context" @@ -88,6 +90,72 @@ var ( fakeFileShares = []*model.FileShareSpec{fakeFileShare} ) +func TestCreateFileShare(t *testing.T) { + var jsonStr = []byte(`{ + "id": "d2975ebe-d82c-430f-b28e-f373746a71ca", + "name": "File_share", + "description": "fake File share", + "size": 1, + "profileId": "b3585ebe-c42c-120g-b28e-f373746a71ca", + "snapshotId": "b7602e18-771e-11e7-8f38-dbd6d291f4eg" + }`) + + t.Run("Should return 202 if everything works well", func(t *testing.T) { + fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{ + Id: "d2975ebe-d82c-430f-b28e-f373746a71ca", + CreatedAt: time.Now().Format(constants.TimeFormat), + UpdatedAt: time.Now().Format(constants.TimeFormat), + }, + Name: "File_share", + Description: "fake File share", + Status: "creating", + Size: int64(1), + AvailabilityZone: "default", + ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca", + SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg", + } + mockClient := new(dbtest.Client) + mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&SampleProfiles[0], nil) + mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), fileshare.SnapshotId).Return(&SampleFileShareSnapshots[0], nil) + mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil) + mockClient.On("GetProfile", c.NewAdminContext(), fileshare.ProfileId).Return(&SampleFileShareProfiles[0], nil) + mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(&SampleFileShares[0], nil) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/file/shares", bytes.NewBuffer(jsonStr)) + w := httptest.NewRecorder() + r.Header.Set("Content-Type", "application/JSON") + beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) { + httpCtx.Input.SetData("context", c.NewAdminContext()) + }) + beego.BeeApp.Handlers.ServeHTTP(w, r) + var output model.FileShareSpec + json.Unmarshal(w.Body.Bytes(), &output) + assertTestResult(t, w.Code, 202) + assertTestResult(t, &output, &SampleFileShares[0]) + }) + + t.Run("Should return 500 if create file share with bad request", func(t *testing.T) { + fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare) + mockClient := new(dbtest.Client) + mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), fileshare.SnapshotId).Return(&SampleFileShareSnapshots[0], nil) + mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil) + mockClient.On("GetProfile", c.NewAdminContext(), fileshare.ProfileId).Return(&SampleFileShareProfiles[0], nil) + mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(nil, errors.New("db error")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/file/shares", bytes.NewBuffer(jsonStr)) + w := httptest.NewRecorder() + r.Header.Set("Content-Type", "application/JSON") + beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) { + httpCtx.Input.SetData("context", c.NewAdminContext()) + }) + beego.BeeApp.Handlers.ServeHTTP(w, r) + assertTestResult(t, w.Code, 500) + }) +} + func TestListFileShares(t *testing.T) { t.Run("Should return 200 if everything works well", func(t *testing.T) {