Skip to content

Commit df4c63e

Browse files
cniackzcniackz
andauthored
Make bucket Integration Test (#1424)
Co-authored-by: cniackz <cniackz@cniackzs-MacBook-Air.local>
1 parent 51ce548 commit df4c63e

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

integration/buckets_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
"io"
2324
"io/ioutil"
2425
"log"
2526
"net/http"
@@ -208,6 +209,109 @@ func TestAddBucket(t *testing.T) {
208209
}
209210
}
210211

212+
func TestAddBucketLocking(t *testing.T) {
213+
/*
214+
This function is to test that locking can't be activated if versioning
215+
is not enabled.
216+
Then, locking will be activated because versioning is activated as well.
217+
*/
218+
assert := assert.New(t)
219+
220+
client := &http.Client{
221+
Timeout: 2 * time.Second,
222+
}
223+
224+
/*
225+
This is invalid, versioning has to be true for locking to be true, but
226+
test will see and make sure this is not allowed and that we get proper
227+
error for this scenario.
228+
*/
229+
requestDataAdd := map[string]interface{}{
230+
"name": "test1",
231+
"versioning": false,
232+
"locking": true,
233+
}
234+
235+
requestDataJSON, _ := json.Marshal(requestDataAdd)
236+
237+
requestDataBody := bytes.NewReader(requestDataJSON)
238+
239+
// create an object locking bucket without versioning flag
240+
request, err := http.NewRequest(
241+
"POST", "http://localhost:9090/api/v1/buckets", requestDataBody)
242+
if err != nil {
243+
log.Println(err)
244+
return
245+
}
246+
247+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
248+
request.Header.Add("Content-Type", "application/json")
249+
250+
response, err := client.Do(request)
251+
assert.Nil(err)
252+
if err != nil {
253+
log.Println(err)
254+
return
255+
}
256+
257+
if response != nil {
258+
assert.Equal(400, response.StatusCode, "400 is expected for this test")
259+
}
260+
261+
msg := "TestAddBucketLocking(): Valid scenario versioning true locking true"
262+
fmt.Println(msg)
263+
264+
/*
265+
This is valid, versioning is true, then locking can be true as well.
266+
*/
267+
requestDataAdd = map[string]interface{}{
268+
"name": "thujan",
269+
"versioning": true,
270+
"locking": true,
271+
}
272+
273+
requestDataJSON, _ = json.Marshal(requestDataAdd)
274+
275+
requestDataBody = bytes.NewReader(requestDataJSON)
276+
277+
// create an object locking bucket with versioning flag
278+
request, err = http.NewRequest(
279+
"POST", "http://localhost:9090/api/v1/buckets", requestDataBody)
280+
if err != nil {
281+
log.Println(err)
282+
return
283+
}
284+
285+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
286+
request.Header.Add("Content-Type", "application/json")
287+
288+
response, err = client.Do(request)
289+
assert.Nil(err)
290+
if err != nil {
291+
log.Println(err)
292+
return
293+
}
294+
295+
// Verification part, bucket should be created with versioning enabled and
296+
// locking enabled, we expect 201 when created.
297+
if response != nil {
298+
assert.Equal(201, response.StatusCode, "201 is expected for this test")
299+
}
300+
301+
defer response.Body.Close()
302+
303+
/*
304+
To convert an HTTP response body to a string in Go, so you can read the
305+
error from the API in case the bucket is invalid for some reason
306+
*/
307+
b, err := io.ReadAll(response.Body)
308+
if err != nil {
309+
log.Fatalln(err)
310+
}
311+
fmt.Println(string(b))
312+
313+
}
314+
211315
func TestGetBucket(t *testing.T) {
212316
assert := assert.New(t)
213317

0 commit comments

Comments
 (0)