Skip to content

Commit 07ef32b

Browse files
authored
Add test for Put Objects retention status end point (#1542)
1 parent df17d31 commit 07ef32b

File tree

1 file changed

+132
-6
lines changed

1 file changed

+132
-6
lines changed

integration/buckets_test.go

Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,13 @@ func DeleteObject(bucketName string, path string, recursive bool, allVersions bo
523523
return response, err
524524
}
525525

526-
func ListObjects(bucketName string, prefix string) (*http.Response, error) {
526+
func ListObjects(bucketName string, prefix string, withVersions string) (*http.Response, error) {
527527
/*
528528
Helper function to list objects in a bucket.
529529
GET: {{baseUrl}}/buckets/:bucket_name/objects
530530
*/
531531
request, err := http.NewRequest("GET",
532-
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects?prefix="+prefix,
532+
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects?prefix="+prefix+"&with_versions="+withVersions,
533533
nil)
534534
if err != nil {
535535
log.Println(err)
@@ -562,6 +562,31 @@ func SharesAnObjectOnAUrl(bucketName string, prefix string, versionID string, ex
562562
return response, err
563563
}
564564

565+
func PutObjectsRetentionStatus(bucketName string, prefix string, versionID string, mode string, expires string, governanceBypass bool) (*http.Response, error) {
566+
requestDataAdd := map[string]interface{}{
567+
"mode": mode,
568+
"expires": expires,
569+
"governance_bypass": governanceBypass,
570+
}
571+
requestDataJSON, _ := json.Marshal(requestDataAdd)
572+
requestDataBody := bytes.NewReader(requestDataJSON)
573+
request, err := http.NewRequest(
574+
"PUT",
575+
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/retention?prefix="+prefix+"&version_id="+versionID,
576+
requestDataBody,
577+
)
578+
if err != nil {
579+
log.Println(err)
580+
}
581+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
582+
request.Header.Add("Content-Type", "application/json")
583+
client := &http.Client{
584+
Timeout: 2 * time.Second,
585+
}
586+
response, err := client.Do(request)
587+
return response, err
588+
}
589+
565590
func TestAddBucket(t *testing.T) {
566591
assert := assert.New(t)
567592
type args struct {
@@ -1229,7 +1254,7 @@ func TestPutObjectTag(t *testing.T) {
12291254
}
12301255

12311256
// 4. Verify the object's tag is set
1232-
listResponse, listError := ListObjects(bucketName, path)
1257+
listResponse, listError := ListObjects(bucketName, path, "false")
12331258
assert.Nil(listError)
12341259
if listError != nil {
12351260
log.Println(listError)
@@ -1299,7 +1324,7 @@ func TestDeleteMultipleObjects(t *testing.T) {
12991324
}
13001325

13011326
// 4. List the objects, empty list is expected!
1302-
listResponse, listError := ListObjects(bucketName, "")
1327+
listResponse, listError := ListObjects(bucketName, "", "false")
13031328
assert.Nil(listError)
13041329
if listError != nil {
13051330
log.Println(listError)
@@ -1477,7 +1502,7 @@ func TestDeleteObject(t *testing.T) {
14771502
}
14781503

14791504
// 4. List the objects in the bucket and make sure the object is gone
1480-
listResponse, listError := ListObjects(bucketName, "")
1505+
listResponse, listError := ListObjects(bucketName, "", "false")
14811506
assert.Nil(listError)
14821507
if listError != nil {
14831508
log.Println(listError)
@@ -1534,7 +1559,7 @@ func TestListObjects(t *testing.T) {
15341559
}
15351560

15361561
// 3. List the object
1537-
listResponse, listError := ListObjects(bucketName, "")
1562+
listResponse, listError := ListObjects(bucketName, "", "false")
15381563
assert.Nil(listError)
15391564
if listError != nil {
15401565
log.Println(listError)
@@ -1637,3 +1662,104 @@ func TestShareObjectOnURL(t *testing.T) {
16371662
})
16381663
}
16391664
}
1665+
1666+
func TestPutObjectsRetentionStatus(t *testing.T) {
1667+
1668+
// Variables
1669+
assert := assert.New(t)
1670+
bucketName := "testputobjectslegalholdstatus"
1671+
fileName := "testputobjectslegalholdstatus.txt"
1672+
prefix := "dGVzdHB1dG9iamVjdHNsZWdhbGhvbGRzdGF0dXMudHh0" // encoded base64
1673+
1674+
// 1. Create bucket
1675+
response, err := AddBucket(bucketName, true, true, nil, nil)
1676+
assert.Nil(err)
1677+
if err != nil {
1678+
log.Println(err)
1679+
assert.Fail("Error creating the bucket")
1680+
return
1681+
}
1682+
if response != nil {
1683+
assert.Equal(201, response.StatusCode, inspectHTTPResponse(response))
1684+
}
1685+
1686+
// 2. Add object
1687+
uploadResponse, uploadError := UploadAnObject(
1688+
bucketName,
1689+
fileName,
1690+
)
1691+
assert.Nil(uploadError)
1692+
if uploadError != nil {
1693+
log.Println(uploadError)
1694+
return
1695+
}
1696+
addObjRsp := inspectHTTPResponse(uploadResponse)
1697+
if uploadResponse != nil {
1698+
assert.Equal(
1699+
200,
1700+
uploadResponse.StatusCode,
1701+
addObjRsp,
1702+
)
1703+
}
1704+
1705+
// Get versionID
1706+
listResponse, listError := ListObjects(bucketName, prefix, "true")
1707+
fmt.Println(listError)
1708+
bodyBytes, _ := ioutil.ReadAll(listResponse.Body)
1709+
listObjs := models.ListObjectsResponse{}
1710+
err = json.Unmarshal(bodyBytes, &listObjs)
1711+
if err != nil {
1712+
log.Println(err)
1713+
assert.Nil(err)
1714+
}
1715+
validVersionID := listObjs.Objects[0].VersionID
1716+
1717+
type args struct {
1718+
versionID string
1719+
}
1720+
tests := []struct {
1721+
name string
1722+
expectedStatus int
1723+
args args
1724+
}{
1725+
{
1726+
name: "Valid VersionID when putting object's retention status",
1727+
expectedStatus: 200,
1728+
args: args{
1729+
versionID: validVersionID,
1730+
},
1731+
},
1732+
{
1733+
name: "Invalid VersionID when putting object's retention status",
1734+
expectedStatus: 500,
1735+
args: args{
1736+
versionID: "*&^###Test1ThisMightBeInvalid555",
1737+
},
1738+
},
1739+
}
1740+
for _, tt := range tests {
1741+
t.Run(tt.name, func(t *testing.T) {
1742+
// 3. Put Objects Legal Status
1743+
putResponse, putError := PutObjectsRetentionStatus(
1744+
bucketName,
1745+
prefix,
1746+
tt.args.versionID,
1747+
"compliance",
1748+
"2033-01-13T23:59:59Z",
1749+
false,
1750+
)
1751+
if putError != nil {
1752+
log.Println(putError)
1753+
assert.Fail("Error creating the bucket")
1754+
}
1755+
if putResponse != nil {
1756+
assert.Equal(
1757+
tt.expectedStatus,
1758+
putResponse.StatusCode,
1759+
inspectHTTPResponse(putResponse),
1760+
)
1761+
}
1762+
})
1763+
}
1764+
1765+
}

0 commit comments

Comments
 (0)