@@ -461,12 +461,24 @@ func ListObjects(bucketName, prefix, withVersions string) (*http.Response, error
461
461
return response , err
462
462
}
463
463
464
- func SharesAnObjectOnAUrl (bucketName , prefix , versionID , expires string ) (* http.Response , error ) {
465
- // Helper function to share an object on a url
464
+ func SharesAnObjectOnAUrl (bucketName , prefix , versionID , expires , accessKey , secretKey string ) (* http.Response , error ) {
465
+ // Helper function to share an object on an url
466
+
467
+ requestDataAdd := map [string ]interface {}{
468
+ "prefix" : prefix ,
469
+ "version_id" : versionID ,
470
+ "expires" : expires ,
471
+ "access_key" : accessKey ,
472
+ "secret_key" : secretKey ,
473
+ }
474
+
475
+ requestDataJSON , _ := json .Marshal (requestDataAdd )
476
+ requestDataBody := bytes .NewReader (requestDataJSON )
477
+
466
478
request , err := http .NewRequest (
467
- "GET " ,
468
- "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects/share?prefix=" + prefix + "&version_id=" + versionID + "&expires=" + expires ,
469
- nil ,
479
+ "POST " ,
480
+ "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects/share" ,
481
+ requestDataBody ,
470
482
)
471
483
if err != nil {
472
484
log .Println (err )
@@ -743,6 +755,39 @@ func PutObjectsLegalholdStatus(bucketName, prefix, status, versionID string) (*h
743
755
return response , err
744
756
}
745
757
758
+ func PostServiceAccountCredentials (accessKey , secretKey , policy string ) (* http.Response , error ) {
759
+ /*
760
+ Helper function to create a service account
761
+ POST: {{baseUrl}}/service-account-credentials
762
+ {
763
+ "accessKey":"testsa",
764
+ "secretKey":"secretsa",
765
+ "policy":""
766
+ }
767
+ */
768
+ requestDataAdd := map [string ]interface {}{
769
+ "accessKey" : accessKey ,
770
+ "secretKey" : secretKey ,
771
+ "policy" : policy ,
772
+ }
773
+ requestDataJSON , _ := json .Marshal (requestDataAdd )
774
+ requestDataBody := bytes .NewReader (requestDataJSON )
775
+
776
+ request , err := http .NewRequest ("POST" ,
777
+ "http://localhost:9090/api/v1/service-account-credentials" ,
778
+ requestDataBody )
779
+ if err != nil {
780
+ log .Println (err )
781
+ }
782
+ request .Header .Add ("Cookie" , fmt .Sprintf ("token=%s" , token ))
783
+ request .Header .Add ("Content-Type" , "application/json" )
784
+ client := & http.Client {
785
+ Timeout : 2 * time .Second ,
786
+ }
787
+ response , err := client .Do (request )
788
+ return response , err
789
+ }
790
+
746
791
func TestPutObjectsLegalholdStatus (t * testing.T ) {
747
792
// Variables
748
793
assert := assert .New (t )
@@ -1514,6 +1559,8 @@ func TestShareObjectOnURL(t *testing.T) {
1514
1559
tags := make (map [string ]string )
1515
1560
tags ["tag" ] = "testputobjecttagbucketonetagone"
1516
1561
versionID := "null"
1562
+ accessKey := "testaccesskey"
1563
+ secretKey := "secretAccessKey"
1517
1564
1518
1565
// 1. Create the bucket
1519
1566
if ! setupBucket (bucketName , false , false , nil , nil , assert , 200 ) {
@@ -1534,6 +1581,21 @@ func TestShareObjectOnURL(t *testing.T) {
1534
1581
inspectHTTPResponse (uploadResponse ),
1535
1582
)
1536
1583
}
1584
+ // 2. Create Access Key
1585
+ accKeyRsp , createError := PostServiceAccountCredentials (accessKey , secretKey , "" )
1586
+
1587
+ if createError != nil {
1588
+ log .Println (createError )
1589
+ return
1590
+ }
1591
+
1592
+ if accKeyRsp != nil {
1593
+ assert .Equal (
1594
+ 201 ,
1595
+ accKeyRsp .StatusCode ,
1596
+ inspectHTTPResponse (accKeyRsp ),
1597
+ )
1598
+ }
1537
1599
1538
1600
type args struct {
1539
1601
prefix string
@@ -1561,7 +1623,7 @@ func TestShareObjectOnURL(t *testing.T) {
1561
1623
for _ , tt := range tests {
1562
1624
t .Run (tt .name , func (t * testing.T ) {
1563
1625
// 3. Share the object on a URL
1564
- shareResponse , shareError := SharesAnObjectOnAUrl (bucketName , tt .args .prefix , versionID , "604800s" )
1626
+ shareResponse , shareError := SharesAnObjectOnAUrl (bucketName , tt .args .prefix , versionID , "604800s" , accessKey , secretKey )
1565
1627
assert .Nil (shareError )
1566
1628
if shareError != nil {
1567
1629
log .Println (shareError )
0 commit comments