2017-04-03 22:03:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
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) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := map[string]struct {
|
2017-04-03 22:19:07 +02:00
|
|
|
s3 S3Client
|
|
|
|
bucketName string
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedBodyCountains string
|
2017-04-03 22:03:45 +02:00
|
|
|
}{
|
2017-04-03 22:46:01 +02:00
|
|
|
"success (empty bucket)": {
|
|
|
|
s3: &S3ClientMock{
|
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyCountains: "No objects in",
|
|
|
|
},
|
|
|
|
"success (with file)": {
|
2017-04-03 22:19:07 +02:00
|
|
|
s3: &S3ClientMock{
|
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "testFile"},
|
|
|
|
},
|
2017-04-03 22:19:07 +02:00
|
|
|
},
|
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyCountains: "testBucket",
|
|
|
|
},
|
2017-04-03 22:31:14 +02:00
|
|
|
"success (archive)": {
|
|
|
|
s3: &S3ClientMock{
|
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "archive.tar.gz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyCountains: "archive",
|
|
|
|
},
|
|
|
|
"success (image)": {
|
|
|
|
s3: &S3ClientMock{
|
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "testImage.png"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyCountains: "photo",
|
|
|
|
},
|
|
|
|
"success (sound)": {
|
|
|
|
s3: &S3ClientMock{
|
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "testSound.mp3"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyCountains: "music_note",
|
|
|
|
},
|
2017-04-03 22:03:45 +02:00
|
|
|
"bucket doesn't exist": {
|
2017-04-03 22:19:07 +02:00
|
|
|
s3: &S3ClientMock{},
|
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusNotFound,
|
|
|
|
expectedBodyCountains: "bucket not found\n",
|
2017-04-03 22:03:45 +02:00
|
|
|
},
|
|
|
|
"s3 error": {
|
|
|
|
s3: &S3ClientMock{
|
2017-04-03 23:52:41 +02:00
|
|
|
Err: errors.New("mocked S3 error"),
|
2017-04-03 22:03:45 +02:00
|
|
|
},
|
2017-04-03 22:19:07 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
expectedBodyCountains: "error listing objects\n",
|
2017-04-03 22:03:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.
|
|
|
|
Methods("GET").
|
|
|
|
Path("/buckets/{bucketName}").
|
|
|
|
Handler(BucketViewHandler(tc.s3))
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/buckets/%s", ts.URL, tc.bucketName)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
assert.NoError(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
assert.Equal(tc.expectedStatusCode, resp.StatusCode)
|
2017-04-03 22:19:07 +02:00
|
|
|
assert.Contains(string(body), tc.expectedBodyCountains)
|
2017-04-03 22:03:45 +02:00
|
|
|
}
|
|
|
|
}
|