Skip to content

Commit 3d70427

Browse files
authored
Add test for Shares an Object on a url end point (#1535)
1 parent c29ac61 commit 3d70427

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

integration/buckets_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,25 @@ func ListObjects(bucketName string, prefix string) (*http.Response, error) {
543543
return response, err
544544
}
545545

546+
func SharesAnObjectOnAUrl(bucketName string, prefix string, versionID string, expires string) (*http.Response, error) {
547+
// Helper function to share an object on a url
548+
request, err := http.NewRequest(
549+
"GET",
550+
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/share?prefix="+prefix+"&version_id="+versionID+"&expires="+expires,
551+
nil,
552+
)
553+
if err != nil {
554+
log.Println(err)
555+
}
556+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
557+
request.Header.Add("Content-Type", "application/json")
558+
client := &http.Client{
559+
Timeout: 2 * time.Second,
560+
}
561+
response, err := client.Do(request)
562+
return response, err
563+
}
564+
546565
func TestAddBucket(t *testing.T) {
547566
assert := assert.New(t)
548567
type args struct {
@@ -1532,3 +1551,89 @@ func TestListObjects(t *testing.T) {
15321551
strings.Contains(finalResponse, "testlistobjecttobucket1"),
15331552
finalResponse)
15341553
}
1554+
1555+
func TestShareObjectOnURL(t *testing.T) {
1556+
/*
1557+
Test to share an object via URL
1558+
*/
1559+
1560+
// Vars
1561+
assert := assert.New(t)
1562+
bucketName := "testshareobjectonurl"
1563+
fileName := "testshareobjectonurl.txt"
1564+
validPrefix := encodeBase64(fileName)
1565+
tags := make(map[string]string)
1566+
tags["tag"] = "testputobjecttagbucketonetagone"
1567+
versionID := "null"
1568+
1569+
// 1. Create the bucket
1570+
response, err := AddBucket(bucketName, false, false, nil, nil)
1571+
assert.Nil(err)
1572+
if err != nil {
1573+
log.Println(err)
1574+
return
1575+
}
1576+
if response != nil {
1577+
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
1578+
}
1579+
1580+
// 2. Upload the object to the bucket
1581+
uploadResponse, uploadError := UploadAnObject(bucketName, fileName)
1582+
assert.Nil(uploadError)
1583+
if uploadError != nil {
1584+
log.Println(uploadError)
1585+
return
1586+
}
1587+
if uploadResponse != nil {
1588+
assert.Equal(
1589+
200,
1590+
uploadResponse.StatusCode,
1591+
inspectHTTPResponse(uploadResponse),
1592+
)
1593+
}
1594+
1595+
type args struct {
1596+
prefix string
1597+
}
1598+
tests := []struct {
1599+
name string
1600+
expectedStatus int
1601+
args args
1602+
}{
1603+
{
1604+
name: "Share File with valid prefix",
1605+
expectedStatus: 200,
1606+
args: args{
1607+
prefix: validPrefix,
1608+
},
1609+
},
1610+
{
1611+
name: "Share file with invalid prefix",
1612+
expectedStatus: 500,
1613+
args: args{
1614+
prefix: "invalidprefix",
1615+
},
1616+
},
1617+
}
1618+
for _, tt := range tests {
1619+
t.Run(tt.name, func(t *testing.T) {
1620+
1621+
// 3. Share the object on a URL
1622+
shareResponse, shareError := SharesAnObjectOnAUrl(bucketName, tt.args.prefix, versionID, "604800s")
1623+
assert.Nil(shareError)
1624+
if shareError != nil {
1625+
log.Println(shareError)
1626+
return
1627+
}
1628+
finalResponse := inspectHTTPResponse(shareResponse)
1629+
if shareResponse != nil {
1630+
assert.Equal(
1631+
tt.expectedStatus,
1632+
shareResponse.StatusCode,
1633+
finalResponse,
1634+
)
1635+
}
1636+
1637+
})
1638+
}
1639+
}

0 commit comments

Comments
 (0)