From 06e1592b548668058e6a41232bdbff42458e104a Mon Sep 17 00:00:00 2001 From: Cesar Celis Hernandez Date: Mon, 31 Jan 2022 10:25:35 -0600 Subject: [PATCH] Added test for downloading an object (#1480) --- integration/buckets_test.go | 106 ++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/integration/buckets_test.go b/integration/buckets_test.go index b8bf10b0c..eb565d515 100644 --- a/integration/buckets_test.go +++ b/integration/buckets_test.go @@ -18,7 +18,9 @@ package integration import ( "bytes" + b64 "encoding/base64" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -41,6 +43,14 @@ import ( var token string +func encodeBase64(fileName string) string { + /* + Helper function to encode in base64 the file name so we can get the path + */ + path := b64.StdEncoding.EncodeToString([]byte(fileName)) + return path +} + func inspectHTTPResponse(httpResponse *http.Response) string { /* Helper function to inspect the content of a HTTP response. @@ -359,6 +369,29 @@ func GetBucketRetention(bucketName string) (*http.Response, error) { return response, err } +func DownloadObject(bucketName string, path string) (*http.Response, error) { + /* + Helper function to download an object from a bucket. + GET: {{baseUrl}}/buckets/bucketName/objects/download?prefix=file + */ + request, err := http.NewRequest( + "GET", + "http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/download?prefix="+ + path, + nil, + ) + 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 UploadAnObject(bucketName string, fileName string) (*http.Response, error) { /* Helper function to upload a file to a bucket for testing. @@ -1023,6 +1056,79 @@ func TestBucketRetention(t *testing.T) { assert.Equal(expected, finalResponse, finalResponse) } +func TestDownloadObject(t *testing.T) { + /* + Test to download an object from a given bucket. + */ + + // Vars + assert := assert.New(t) + bucketName := "testdownloadobjbucketone" + fileName := "testdownloadobjectfilenameone" + path := encodeBase64(fileName) + workingDirectory, getWdErr := os.Getwd() + if getWdErr != nil { + assert.Fail("Couldn't get the directory") + } + + // 1. Create the 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. Upload an object to the bucket + uploadResponse, uploadError := UploadAnObject(bucketName, fileName) + assert.Nil(uploadError) + if uploadError != nil { + log.Println(uploadError) + return + } + if uploadResponse != nil { + assert.Equal( + 200, + uploadResponse.StatusCode, + inspectHTTPResponse(uploadResponse), + ) + } + + // 3. Download the object from the bucket + downloadResponse, downloadError := DownloadObject(bucketName, path) + assert.Nil(downloadError) + if downloadError != nil { + log.Println(downloadError) + assert.Fail("Error downloading the object") + return + } + finalResponse := inspectHTTPResponse(downloadResponse) + if downloadResponse != nil { + assert.Equal( + 200, + downloadResponse.StatusCode, + finalResponse, + ) + } + + // 4. Verify the file was downloaded + files, err := ioutil.ReadDir(workingDirectory) + if err != nil { + log.Fatal(err) + } + for _, file := range files { + fmt.Println(file.Name(), file.IsDir()) + } + if _, err := os.Stat(workingDirectory); errors.Is(err, os.ErrNotExist) { + // path/to/whatever does not exist + assert.Fail("File wasn't downloaded") + } +} + func TestUploadObjectToBucket(t *testing.T) { /* Function to test the upload of an object to a bucket.