2017-05-08 23:07:07 +02:00
|
|
|
package s3manager_test
|
2017-04-03 22:03:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-03-14 21:53:35 +01:00
|
|
|
"path/filepath"
|
2017-04-03 22:03:45 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-03-14 21:53:35 +01:00
|
|
|
"github.com/mastertinner/s3manager/internal/app/s3manager"
|
2017-04-03 22:19:07 +02:00
|
|
|
minio "github.com/minio/minio-go"
|
2017-04-03 22:03:45 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBucketViewHandler(t *testing.T) {
|
2017-05-08 23:07:07 +02:00
|
|
|
cases := map[string]struct {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func func(string, string, bool, <-chan struct{}) <-chan minio.ObjectInfo
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName string
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedBodyContains string
|
2017-04-03 22:03:45 +02:00
|
|
|
}{
|
2017-12-21 07:50:59 +01:00
|
|
|
"renders a bucket containing a file": {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func: func(bucketName string, objectPrefix string, recursive bool, doneCh <-chan struct{}) <-chan minio.ObjectInfo {
|
|
|
|
objCh := make(chan minio.ObjectInfo, 1)
|
|
|
|
defer close(objCh)
|
|
|
|
objCh <- minio.ObjectInfo{Key: "testFile"}
|
|
|
|
return objCh
|
2017-04-03 22:46:01 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
2018-04-24 22:35:21 +02:00
|
|
|
expectedBodyContains: "testFile",
|
2017-04-03 22:46:01 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"renders placeholder for an empty bucket": {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func: func(bucketName string, objectPrefix string, recursive bool, doneCh <-chan struct{}) <-chan minio.ObjectInfo {
|
|
|
|
objCh := make(chan minio.ObjectInfo)
|
|
|
|
defer close(objCh)
|
|
|
|
return objCh
|
2017-04-03 22:19:07 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
2017-12-21 07:50:59 +01:00
|
|
|
expectedBodyContains: "No objects in",
|
2017-04-03 22:19:07 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"renders a bucket containing an archive": {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func: func(bucketName string, objectPrefix string, recursive bool, doneCh <-chan struct{}) <-chan minio.ObjectInfo {
|
|
|
|
objCh := make(chan minio.ObjectInfo, 1)
|
|
|
|
defer close(objCh)
|
|
|
|
objCh <- minio.ObjectInfo{Key: "archive.tar.gz"}
|
|
|
|
return objCh
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "archive",
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"renders a bucket containing an image": {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func: func(bucketName string, objectPrefix string, recursive bool, doneCh <-chan struct{}) <-chan minio.ObjectInfo {
|
|
|
|
objCh := make(chan minio.ObjectInfo, 1)
|
|
|
|
defer close(objCh)
|
|
|
|
objCh <- minio.ObjectInfo{Key: "testImage.png"}
|
|
|
|
return objCh
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "photo",
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"renders a bucket containing a sound file": {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func: func(bucketName string, objectPrefix string, recursive bool, doneCh <-chan struct{}) <-chan minio.ObjectInfo {
|
|
|
|
objCh := make(chan minio.ObjectInfo, 1)
|
|
|
|
defer close(objCh)
|
|
|
|
objCh <- minio.ObjectInfo{Key: "testSound.mp3"}
|
|
|
|
return objCh
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "music_note",
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"returns error if the bucket doesn't exist": {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func: func(bucketName string, objectPrefix string, recursive bool, doneCh <-chan struct{}) <-chan minio.ObjectInfo {
|
|
|
|
objCh := make(chan minio.ObjectInfo, 1)
|
|
|
|
defer close(objCh)
|
|
|
|
objCh <- minio.ObjectInfo{Err: s3manager.ErrBucketDoesNotExist}
|
|
|
|
return objCh
|
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusNotFound,
|
|
|
|
expectedBodyContains: http.StatusText(http.StatusNotFound),
|
2017-04-03 22:03:45 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"returns error if there is an S3 error": {
|
2018-04-24 22:35:21 +02:00
|
|
|
listObjectsV2Func: func(bucketName string, objectPrefix string, recursive bool, doneCh <-chan struct{}) <-chan minio.ObjectInfo {
|
|
|
|
objCh := make(chan minio.ObjectInfo, 1)
|
|
|
|
defer close(objCh)
|
|
|
|
objCh <- minio.ObjectInfo{Err: errors.New("mocked S3 error")}
|
|
|
|
return objCh
|
2017-04-03 22:03:45 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
|
2017-04-03 22:03:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-08 23:07:07 +02:00
|
|
|
for tcID, tc := range cases {
|
2017-07-31 10:57:22 +02:00
|
|
|
t.Run(tcID, func(t *testing.T) {
|
2017-12-21 07:50:59 +01:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2018-04-24 22:35:21 +02:00
|
|
|
s3 := &S3Mock{
|
|
|
|
ListObjectsV2Func: tc.listObjectsV2Func,
|
|
|
|
}
|
|
|
|
|
2018-03-14 21:53:35 +01:00
|
|
|
tmplDir := filepath.Join("..", "..", "..", "web", "template")
|
2017-07-31 10:57:22 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.
|
|
|
|
Methods(http.MethodGet).
|
|
|
|
Path("/buckets/{bucketName}").
|
2018-04-24 22:35:21 +02:00
|
|
|
Handler(s3manager.BucketViewHandler(s3, tmplDir))
|
2017-04-03 22:03:45 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
ts := httptest.NewServer(r)
|
|
|
|
defer ts.Close()
|
2017-04-03 22:03:45 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
url := fmt.Sprintf("%s/buckets/%s", ts.URL, tc.bucketName)
|
|
|
|
resp, err := http.Get(url)
|
2017-04-19 14:18:58 +02:00
|
|
|
assert.NoError(err, tcID)
|
2018-04-14 01:24:20 +02:00
|
|
|
defer func() {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}()
|
2017-04-03 22:03:45 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
assert.NoError(err, tcID)
|
2017-04-03 22:03:45 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
assert.Equal(tc.expectedStatusCode, resp.StatusCode, tcID)
|
|
|
|
assert.Contains(string(body), tc.expectedBodyContains, tcID)
|
|
|
|
})
|
2017-04-03 22:03:45 +02:00
|
|
|
}
|
|
|
|
}
|