Skip to content

Commit ce989e3

Browse files
authored
Add test to add tag to an object (#1481)
1 parent 63d3c72 commit ce989e3

File tree

1 file changed

+107
-5
lines changed

1 file changed

+107
-5
lines changed

integration/buckets_test.go

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,37 @@ func GetBucketRetention(bucketName string) (*http.Response, error) {
369369
return response, err
370370
}
371371

372+
func PutObjectTags(bucketName string, prefix string, tags map[string]string, versionID string) (*http.Response, error) {
373+
/*
374+
Helper function to put object's tags.
375+
PUT: /buckets/{bucket_name}/objects/tags?prefix=prefix
376+
{
377+
"tags": {}
378+
}
379+
*/
380+
requestDataAdd := map[string]interface{}{
381+
"tags": tags,
382+
}
383+
requestDataJSON, _ := json.Marshal(requestDataAdd)
384+
requestDataBody := bytes.NewReader(requestDataJSON)
385+
request, err := http.NewRequest(
386+
"PUT",
387+
"http://localhost:9090/api/v1/buckets/"+
388+
bucketName+"/objects/tags?prefix="+prefix+"&version_id="+versionID,
389+
requestDataBody,
390+
)
391+
if err != nil {
392+
log.Println(err)
393+
}
394+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
395+
request.Header.Add("Content-Type", "application/json")
396+
client := &http.Client{
397+
Timeout: 2 * time.Second,
398+
}
399+
response, err := client.Do(request)
400+
return response, err
401+
}
402+
372403
func DownloadObject(bucketName string, path string) (*http.Response, error) {
373404
/*
374405
Helper function to download an object from a bucket.
@@ -453,13 +484,13 @@ func DeleteObject(bucketName string, path string, recursive bool, allVersions bo
453484
return response, err
454485
}
455486

456-
func ListObjects(bucketName string) (*http.Response, error) {
487+
func ListObjects(bucketName string, prefix string) (*http.Response, error) {
457488
/*
458-
Helper function to list objects in a bucket.
459-
GET: {{baseUrl}}/buckets/:bucket_name/objects
489+
Helper function to list objects in a bucket.
490+
GET: {{baseUrl}}/buckets/:bucket_name/objects
460491
*/
461492
request, err := http.NewRequest("GET",
462-
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects",
493+
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects?prefix="+prefix,
463494
nil)
464495
if err != nil {
465496
log.Println(err)
@@ -1056,6 +1087,77 @@ func TestBucketRetention(t *testing.T) {
10561087
assert.Equal(expected, finalResponse, finalResponse)
10571088
}
10581089

1090+
func TestPutObjectTag(t *testing.T) {
1091+
/*
1092+
Test to put a tag to an object
1093+
*/
1094+
1095+
// Vars
1096+
assert := assert.New(t)
1097+
bucketName := "testputobjecttagbucketone"
1098+
fileName := "testputobjecttagbucketone.txt"
1099+
path := encodeBase64(fileName)
1100+
tags := make(map[string]string)
1101+
tags["tag"] = "testputobjecttagbucketonetagone"
1102+
versionID := "null"
1103+
1104+
// 1. Create the bucket
1105+
response, err := AddBucket(bucketName, false, false, nil, nil)
1106+
assert.Nil(err)
1107+
if err != nil {
1108+
log.Println(err)
1109+
return
1110+
}
1111+
if response != nil {
1112+
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
1113+
}
1114+
1115+
// 2. Upload the object to the bucket
1116+
uploadResponse, uploadError := UploadAnObject(bucketName, fileName)
1117+
assert.Nil(uploadError)
1118+
if uploadError != nil {
1119+
log.Println(uploadError)
1120+
return
1121+
}
1122+
if uploadResponse != nil {
1123+
assert.Equal(
1124+
200,
1125+
uploadResponse.StatusCode,
1126+
inspectHTTPResponse(uploadResponse),
1127+
)
1128+
}
1129+
1130+
// 3. Put a tag to the object
1131+
putTagResponse, putTagError := PutObjectTags(
1132+
bucketName, path, tags, versionID)
1133+
assert.Nil(putTagError)
1134+
if putTagError != nil {
1135+
log.Println(putTagError)
1136+
return
1137+
}
1138+
putObjectTagresult := inspectHTTPResponse(putTagResponse)
1139+
if putTagResponse != nil {
1140+
assert.Equal(
1141+
200, putTagResponse.StatusCode, putObjectTagresult)
1142+
}
1143+
1144+
// 4. Verify the object's tag is set
1145+
listResponse, listError := ListObjects(bucketName, path)
1146+
assert.Nil(listError)
1147+
if listError != nil {
1148+
log.Println(listError)
1149+
return
1150+
}
1151+
finalResponse := inspectHTTPResponse(listResponse)
1152+
if listResponse != nil {
1153+
assert.Equal(200, listResponse.StatusCode,
1154+
finalResponse)
1155+
}
1156+
assert.True(
1157+
strings.Contains(finalResponse, tags["tag"]),
1158+
finalResponse)
1159+
}
1160+
10591161
func TestDownloadObject(t *testing.T) {
10601162
/*
10611163
Test to download an object from a given bucket.
@@ -1217,7 +1319,7 @@ func TestDeleteObject(t *testing.T) {
12171319
}
12181320

12191321
// 4. List the objects in the bucket and make sure the object is gone
1220-
listResponse, listError := ListObjects(bucketName)
1322+
listResponse, listError := ListObjects(bucketName, "")
12211323
assert.Nil(listError)
12221324
if listError != nil {
12231325
log.Println(listError)

0 commit comments

Comments
 (0)