From df4c63e2a3c8b898b206d75103e289b772967e0e Mon Sep 17 00:00:00 2001 From: Cesar Celis Hernandez Date: Fri, 21 Jan 2022 13:20:30 -0500 Subject: [PATCH] Make bucket Integration Test (#1424) Co-authored-by: cniackz --- integration/buckets_test.go | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/integration/buckets_test.go b/integration/buckets_test.go index d1bda44d1..71fed9ed3 100644 --- a/integration/buckets_test.go +++ b/integration/buckets_test.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "io/ioutil" "log" "net/http" @@ -208,6 +209,109 @@ func TestAddBucket(t *testing.T) { } } +func TestAddBucketLocking(t *testing.T) { + /* + This function is to test that locking can't be activated if versioning + is not enabled. + Then, locking will be activated because versioning is activated as well. + */ + assert := assert.New(t) + + client := &http.Client{ + Timeout: 2 * time.Second, + } + + /* + This is invalid, versioning has to be true for locking to be true, but + test will see and make sure this is not allowed and that we get proper + error for this scenario. + */ + requestDataAdd := map[string]interface{}{ + "name": "test1", + "versioning": false, + "locking": true, + } + + requestDataJSON, _ := json.Marshal(requestDataAdd) + + requestDataBody := bytes.NewReader(requestDataJSON) + + // create an object locking bucket without versioning flag + request, err := http.NewRequest( + "POST", "http://localhost:9090/api/v1/buckets", requestDataBody) + if err != nil { + log.Println(err) + return + } + + request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) + request.Header.Add("Content-Type", "application/json") + + response, err := client.Do(request) + assert.Nil(err) + if err != nil { + log.Println(err) + return + } + + if response != nil { + assert.Equal(400, response.StatusCode, "400 is expected for this test") + } + + msg := "TestAddBucketLocking(): Valid scenario versioning true locking true" + fmt.Println(msg) + + /* + This is valid, versioning is true, then locking can be true as well. + */ + requestDataAdd = map[string]interface{}{ + "name": "thujan", + "versioning": true, + "locking": true, + } + + requestDataJSON, _ = json.Marshal(requestDataAdd) + + requestDataBody = bytes.NewReader(requestDataJSON) + + // create an object locking bucket with versioning flag + request, err = http.NewRequest( + "POST", "http://localhost:9090/api/v1/buckets", requestDataBody) + if err != nil { + log.Println(err) + return + } + + request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) + request.Header.Add("Content-Type", "application/json") + + response, err = client.Do(request) + assert.Nil(err) + if err != nil { + log.Println(err) + return + } + + // Verification part, bucket should be created with versioning enabled and + // locking enabled, we expect 201 when created. + if response != nil { + assert.Equal(201, response.StatusCode, "201 is expected for this test") + } + + defer response.Body.Close() + + /* + To convert an HTTP response body to a string in Go, so you can read the + error from the API in case the bucket is invalid for some reason + */ + b, err := io.ReadAll(response.Body) + if err != nil { + log.Fatalln(err) + } + fmt.Println(string(b)) + +} + func TestGetBucket(t *testing.T) { assert := assert.New(t)