Add test for Put Objects retention status end point (#1542)
This commit is contained in:
committed by
GitHub
parent
df17d31721
commit
07ef32bee3
@@ -523,13 +523,13 @@ func DeleteObject(bucketName string, path string, recursive bool, allVersions bo
|
||||
return response, err
|
||||
}
|
||||
|
||||
func ListObjects(bucketName string, prefix string) (*http.Response, error) {
|
||||
func ListObjects(bucketName string, prefix string, withVersions string) (*http.Response, error) {
|
||||
/*
|
||||
Helper function to list objects in a bucket.
|
||||
GET: {{baseUrl}}/buckets/:bucket_name/objects
|
||||
*/
|
||||
request, err := http.NewRequest("GET",
|
||||
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects?prefix="+prefix,
|
||||
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects?prefix="+prefix+"&with_versions="+withVersions,
|
||||
nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -562,6 +562,31 @@ func SharesAnObjectOnAUrl(bucketName string, prefix string, versionID string, ex
|
||||
return response, err
|
||||
}
|
||||
|
||||
func PutObjectsRetentionStatus(bucketName string, prefix string, versionID string, mode string, expires string, governanceBypass bool) (*http.Response, error) {
|
||||
requestDataAdd := map[string]interface{}{
|
||||
"mode": mode,
|
||||
"expires": expires,
|
||||
"governance_bypass": governanceBypass,
|
||||
}
|
||||
requestDataJSON, _ := json.Marshal(requestDataAdd)
|
||||
requestDataBody := bytes.NewReader(requestDataJSON)
|
||||
request, err := http.NewRequest(
|
||||
"PUT",
|
||||
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/retention?prefix="+prefix+"&version_id="+versionID,
|
||||
requestDataBody,
|
||||
)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
|
||||
request.Header.Add("Content-Type", "application/json")
|
||||
client := &http.Client{
|
||||
Timeout: 2 * time.Second,
|
||||
}
|
||||
response, err := client.Do(request)
|
||||
return response, err
|
||||
}
|
||||
|
||||
func TestAddBucket(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
type args struct {
|
||||
@@ -1229,7 +1254,7 @@ func TestPutObjectTag(t *testing.T) {
|
||||
}
|
||||
|
||||
// 4. Verify the object's tag is set
|
||||
listResponse, listError := ListObjects(bucketName, path)
|
||||
listResponse, listError := ListObjects(bucketName, path, "false")
|
||||
assert.Nil(listError)
|
||||
if listError != nil {
|
||||
log.Println(listError)
|
||||
@@ -1299,7 +1324,7 @@ func TestDeleteMultipleObjects(t *testing.T) {
|
||||
}
|
||||
|
||||
// 4. List the objects, empty list is expected!
|
||||
listResponse, listError := ListObjects(bucketName, "")
|
||||
listResponse, listError := ListObjects(bucketName, "", "false")
|
||||
assert.Nil(listError)
|
||||
if listError != nil {
|
||||
log.Println(listError)
|
||||
@@ -1477,7 +1502,7 @@ func TestDeleteObject(t *testing.T) {
|
||||
}
|
||||
|
||||
// 4. List the objects in the bucket and make sure the object is gone
|
||||
listResponse, listError := ListObjects(bucketName, "")
|
||||
listResponse, listError := ListObjects(bucketName, "", "false")
|
||||
assert.Nil(listError)
|
||||
if listError != nil {
|
||||
log.Println(listError)
|
||||
@@ -1534,7 +1559,7 @@ func TestListObjects(t *testing.T) {
|
||||
}
|
||||
|
||||
// 3. List the object
|
||||
listResponse, listError := ListObjects(bucketName, "")
|
||||
listResponse, listError := ListObjects(bucketName, "", "false")
|
||||
assert.Nil(listError)
|
||||
if listError != nil {
|
||||
log.Println(listError)
|
||||
@@ -1637,3 +1662,104 @@ func TestShareObjectOnURL(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutObjectsRetentionStatus(t *testing.T) {
|
||||
|
||||
// Variables
|
||||
assert := assert.New(t)
|
||||
bucketName := "testputobjectslegalholdstatus"
|
||||
fileName := "testputobjectslegalholdstatus.txt"
|
||||
prefix := "dGVzdHB1dG9iamVjdHNsZWdhbGhvbGRzdGF0dXMudHh0" // encoded base64
|
||||
|
||||
// 1. Create bucket
|
||||
response, err := AddBucket(bucketName, true, true, nil, nil)
|
||||
assert.Nil(err)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
assert.Fail("Error creating the bucket")
|
||||
return
|
||||
}
|
||||
if response != nil {
|
||||
assert.Equal(201, response.StatusCode, inspectHTTPResponse(response))
|
||||
}
|
||||
|
||||
// 2. Add object
|
||||
uploadResponse, uploadError := UploadAnObject(
|
||||
bucketName,
|
||||
fileName,
|
||||
)
|
||||
assert.Nil(uploadError)
|
||||
if uploadError != nil {
|
||||
log.Println(uploadError)
|
||||
return
|
||||
}
|
||||
addObjRsp := inspectHTTPResponse(uploadResponse)
|
||||
if uploadResponse != nil {
|
||||
assert.Equal(
|
||||
200,
|
||||
uploadResponse.StatusCode,
|
||||
addObjRsp,
|
||||
)
|
||||
}
|
||||
|
||||
// Get versionID
|
||||
listResponse, listError := ListObjects(bucketName, prefix, "true")
|
||||
fmt.Println(listError)
|
||||
bodyBytes, _ := ioutil.ReadAll(listResponse.Body)
|
||||
listObjs := models.ListObjectsResponse{}
|
||||
err = json.Unmarshal(bodyBytes, &listObjs)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
assert.Nil(err)
|
||||
}
|
||||
validVersionID := listObjs.Objects[0].VersionID
|
||||
|
||||
type args struct {
|
||||
versionID string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
expectedStatus int
|
||||
args args
|
||||
}{
|
||||
{
|
||||
name: "Valid VersionID when putting object's retention status",
|
||||
expectedStatus: 200,
|
||||
args: args{
|
||||
versionID: validVersionID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Invalid VersionID when putting object's retention status",
|
||||
expectedStatus: 500,
|
||||
args: args{
|
||||
versionID: "*&^###Test1ThisMightBeInvalid555",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 3. Put Objects Legal Status
|
||||
putResponse, putError := PutObjectsRetentionStatus(
|
||||
bucketName,
|
||||
prefix,
|
||||
tt.args.versionID,
|
||||
"compliance",
|
||||
"2033-01-13T23:59:59Z",
|
||||
false,
|
||||
)
|
||||
if putError != nil {
|
||||
log.Println(putError)
|
||||
assert.Fail("Error creating the bucket")
|
||||
}
|
||||
if putResponse != nil {
|
||||
assert.Equal(
|
||||
tt.expectedStatus,
|
||||
putResponse.StatusCode,
|
||||
inspectHTTPResponse(putResponse),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user