@@ -19,9 +19,11 @@ import (
1919 ctx "context"
2020 "encoding/json"
2121 "errors"
22+ "github.com/sodafoundation/api/pkg/utils/constants"
2223 "net/http"
2324 "net/http/httptest"
2425 "testing"
26+ "time"
2527
2628 "github.com/astaxie/beego"
2729 "github.com/astaxie/beego/context"
8890 fakeFileShares = []* model.FileShareSpec {fakeFileShare }
8991)
9092
93+ func TestCreateFileShare (t * testing.T ) {
94+ var jsonStr = []byte (`{
95+ "id": "d2975ebe-d82c-430f-b28e-f373746a71ca",
96+ "name": "File_share",
97+ "description": "fake File share",
98+ "size": 1,
99+ "profileId": "b3585ebe-c42c-120g-b28e-f373746a71ca",
100+ "snapshotId": "b7602e18-771e-11e7-8f38-dbd6d291f4eg"
101+ }` )
102+
103+ t .Run ("Should return 202 if everything works well" , func (t * testing.T ) {
104+ fileshare := model.FileShareSpec {BaseModel : & model.BaseModel {
105+ Id : "d2975ebe-d82c-430f-b28e-f373746a71ca" ,
106+ CreatedAt : time .Now ().Format (constants .TimeFormat ),
107+ UpdatedAt : time .Now ().Format (constants .TimeFormat ),
108+ },
109+ Name : "File_share" ,
110+ Description : "fake File share" ,
111+ Status : "creating" ,
112+ Size : int64 (1 ),
113+ AvailabilityZone : "default" ,
114+ ProfileId : "b3585ebe-c42c-120g-b28e-f373746a71ca" ,
115+ SnapshotId : "b7602e18-771e-11e7-8f38-dbd6d291f4eg" ,
116+ }
117+ mockClient := new (dbtest.Client )
118+ mockClient .On ("GetDefaultProfileFileShare" , c .NewAdminContext ()).Return (& SampleProfiles [0 ], nil )
119+ mockClient .On ("GetFileShareSnapshot" , c .NewAdminContext (), fileshare .SnapshotId ).Return (& SampleFileShareSnapshots [0 ], nil )
120+ mockClient .On ("GetFileShare" , c .NewAdminContext (), SampleFileShareSnapshots [0 ].FileShareId ).Return (& SampleFileShares [0 ], nil )
121+ mockClient .On ("GetProfile" , c .NewAdminContext (), fileshare .ProfileId ).Return (& SampleFileShareProfiles [0 ], nil )
122+ mockClient .On ("CreateFileShare" , c .NewAdminContext (), & fileshare ).Return (& SampleFileShares [0 ], nil )
123+ db .C = mockClient
124+
125+ r , _ := http .NewRequest ("POST" , "/v1beta/file/shares" , bytes .NewBuffer (jsonStr ))
126+ w := httptest .NewRecorder ()
127+ r .Header .Set ("Content-Type" , "application/JSON" )
128+ beego .InsertFilter ("*" , beego .BeforeExec , func (httpCtx * context.Context ) {
129+ httpCtx .Input .SetData ("context" , c .NewAdminContext ())
130+ })
131+ beego .BeeApp .Handlers .ServeHTTP (w , r )
132+ var output model.FileShareSpec
133+ json .Unmarshal (w .Body .Bytes (), & output )
134+ assertTestResult (t , w .Code , 202 )
135+ assertTestResult (t , & output , & SampleFileShares [0 ])
136+ })
137+
138+ t .Run ("Should return 500 if create file share with bad request" , func (t * testing.T ) {
139+ fileshare := model.FileShareSpec {BaseModel : & model.BaseModel {}}
140+ json .NewDecoder (bytes .NewBuffer (jsonStr )).Decode (& fileshare )
141+ mockClient := new (dbtest.Client )
142+ mockClient .On ("GetFileShareSnapshot" , c .NewAdminContext (), fileshare .SnapshotId ).Return (& SampleFileShareSnapshots [0 ], nil )
143+ mockClient .On ("GetFileShare" , c .NewAdminContext (), SampleFileShareSnapshots [0 ].FileShareId ).Return (& SampleFileShares [0 ], nil )
144+ mockClient .On ("GetProfile" , c .NewAdminContext (), fileshare .ProfileId ).Return (& SampleFileShareProfiles [0 ], nil )
145+ mockClient .On ("CreateFileShare" , c .NewAdminContext (), & fileshare ).Return (nil , errors .New ("db error" ))
146+ db .C = mockClient
147+
148+ r , _ := http .NewRequest ("POST" , "/v1beta/file/shares" , bytes .NewBuffer (jsonStr ))
149+ w := httptest .NewRecorder ()
150+ r .Header .Set ("Content-Type" , "application/JSON" )
151+ beego .InsertFilter ("*" , beego .BeforeExec , func (httpCtx * context.Context ) {
152+ httpCtx .Input .SetData ("context" , c .NewAdminContext ())
153+ })
154+ beego .BeeApp .Handlers .ServeHTTP (w , r )
155+ assertTestResult (t , w .Code , 500 )
156+ })
157+ }
158+
91159func TestListFileShares (t * testing.T ) {
92160
93161 t .Run ("Should return 200 if everything works well" , func (t * testing.T ) {
0 commit comments