s3manager-web/internal/app/s3manager/bucket_view_test.go

183 lines
5.2 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager_test
2017-04-03 22:03:45 +02:00
import (
2021-08-05 11:44:40 +02:00
"context"
2017-04-03 22:03:45 +02:00
"fmt"
2021-02-21 13:57:50 +01:00
"io"
2017-04-03 22:03:45 +02:00
"net/http"
"net/http/httptest"
2021-04-21 11:37:38 +02:00
"os"
2018-03-14 21:53:35 +01:00
"path/filepath"
2018-05-31 18:28:55 +02:00
"strings"
2017-04-03 22:03:45 +02:00
"testing"
2022-03-24 08:46:21 +01:00
"github.com/cloudlena/s3manager/internal/app/s3manager"
"github.com/cloudlena/s3manager/internal/app/s3manager/mocks"
"github.com/gorilla/mux"
2018-05-31 18:28:55 +02:00
"github.com/matryer/is"
2021-08-05 11:44:40 +02:00
"github.com/minio/minio-go/v7"
2017-04-03 22:03:45 +02:00
)
2018-05-31 16:10:41 +02:00
func TestHandleBucketView(t *testing.T) {
2020-12-03 10:28:05 +01:00
t.Parallel()
2019-01-08 08:51:09 +01:00
cases := []struct {
it string
2021-08-05 11:44:40 +02:00
listObjectsFunc func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo
2017-04-18 15:43:13 +02:00
bucketName string
2023-06-13 17:37:57 +02:00
path string
2017-04-18 15:43:13 +02:00
expectedStatusCode int
expectedBodyContains string
2017-04-03 22:03:45 +02:00
}{
2019-01-08 08:51:09 +01:00
{
it: "renders a bucket containing a file",
2021-08-05 11:44:40 +02:00
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
2018-05-08 17:55:26 +02:00
objCh := make(chan minio.ObjectInfo)
go func() {
2022-08-04 22:30:04 +02:00
objCh <- minio.ObjectInfo{Key: "FILE-NAME"}
2018-05-08 17:55:26 +02:00
close(objCh)
}()
2018-04-24 22:35:21 +02:00
return objCh
2017-04-03 22:46:01 +02:00
},
2022-08-04 22:30:04 +02:00
bucketName: "BUCKET-NAME",
2017-04-18 15:43:13 +02:00
expectedStatusCode: http.StatusOK,
2022-08-04 22:30:04 +02:00
expectedBodyContains: "FILE-NAME",
2017-04-03 22:46:01 +02:00
},
2019-01-08 08:51:09 +01:00
{
it: "renders placeholder for an empty bucket",
2021-08-05 11:44:40 +02:00
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
2018-04-24 22:35:21 +02:00
objCh := make(chan minio.ObjectInfo)
2018-05-08 17:55:26 +02:00
close(objCh)
2018-04-24 22:35:21 +02:00
return objCh
2017-04-03 22:19:07 +02:00
},
2022-08-04 22:30:04 +02:00
bucketName: "BUCKET-NAME",
2017-04-18 15:43:13 +02:00
expectedStatusCode: http.StatusOK,
2017-12-21 07:50:59 +01:00
expectedBodyContains: "No objects in",
2017-04-03 22:19:07 +02:00
},
2019-01-08 08:51:09 +01:00
{
it: "renders a bucket containing an archive",
2021-08-05 11:44:40 +02:00
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
2018-05-08 17:55:26 +02:00
objCh := make(chan minio.ObjectInfo)
go func() {
objCh <- minio.ObjectInfo{Key: "archive.tar.gz"}
close(objCh)
}()
2018-04-24 22:35:21 +02:00
return objCh
2017-04-03 22:31:14 +02:00
},
2022-08-04 22:30:04 +02:00
bucketName: "BUCKET-NAME",
2017-04-18 15:43:13 +02:00
expectedStatusCode: http.StatusOK,
expectedBodyContains: "archive",
2017-04-03 22:31:14 +02:00
},
2019-01-08 08:51:09 +01:00
{
it: "renders a bucket containing an image",
2021-08-05 11:44:40 +02:00
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
2018-05-08 17:55:26 +02:00
objCh := make(chan minio.ObjectInfo)
go func() {
2022-08-04 22:30:04 +02:00
objCh <- minio.ObjectInfo{Key: "FILE-NAME.png"}
2018-05-08 17:55:26 +02:00
close(objCh)
}()
2018-04-24 22:35:21 +02:00
return objCh
2017-04-03 22:31:14 +02:00
},
2022-08-04 22:30:04 +02:00
bucketName: "BUCKET-NAME",
2017-04-18 15:43:13 +02:00
expectedStatusCode: http.StatusOK,
expectedBodyContains: "photo",
2017-04-03 22:31:14 +02:00
},
2019-01-08 08:51:09 +01:00
{
it: "renders a bucket containing a sound file",
2021-08-05 11:44:40 +02:00
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
2018-05-08 17:55:26 +02:00
objCh := make(chan minio.ObjectInfo)
go func() {
2022-08-04 22:30:04 +02:00
objCh <- minio.ObjectInfo{Key: "FILE-NAME.mp3"}
2018-05-08 17:55:26 +02:00
close(objCh)
}()
2018-04-24 22:35:21 +02:00
return objCh
2017-04-03 22:31:14 +02:00
},
2022-08-04 22:30:04 +02:00
bucketName: "BUCKET-NAME",
2017-04-18 15:43:13 +02:00
expectedStatusCode: http.StatusOK,
expectedBodyContains: "music_note",
2017-04-03 22:31:14 +02:00
},
2019-01-08 08:51:09 +01:00
{
it: "returns error if the bucket doesn't exist",
2021-08-05 11:44:40 +02:00
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
2018-05-08 17:55:26 +02:00
objCh := make(chan minio.ObjectInfo)
go func() {
2020-05-04 09:46:41 +02:00
objCh <- minio.ObjectInfo{Err: errBucketDoesNotExist}
2018-05-08 17:55:26 +02:00
close(objCh)
}()
2018-04-24 22:35:21 +02:00
return objCh
},
2022-08-04 22:30:04 +02:00
bucketName: "BUCKET-NAME",
2017-04-18 15:43:13 +02:00
expectedStatusCode: http.StatusNotFound,
expectedBodyContains: http.StatusText(http.StatusNotFound),
2017-04-03 22:03:45 +02:00
},
2019-01-08 08:51:09 +01:00
{
it: "returns error if there is an S3 error",
2021-08-05 11:44:40 +02:00
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
2018-05-08 17:55:26 +02:00
objCh := make(chan minio.ObjectInfo)
go func() {
2020-05-04 09:46:41 +02:00
objCh <- minio.ObjectInfo{Err: errS3}
2018-05-08 17:55:26 +02:00
close(objCh)
}()
2018-04-24 22:35:21 +02:00
return objCh
2017-04-03 22:03:45 +02:00
},
2022-08-04 22:30:04 +02:00
bucketName: "BUCKET-NAME",
2017-04-18 15:43:13 +02:00
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
2017-04-03 22:03:45 +02:00
},
2023-06-13 17:37:57 +02:00
{
it: "renders a bucket with folder",
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
objCh := make(chan minio.ObjectInfo)
go func() {
objCh <- minio.ObjectInfo{Key: "AFolder/"}
close(objCh)
}()
return objCh
},
bucketName: "BUCKET-NAME",
expectedStatusCode: http.StatusOK,
expectedBodyContains: "folder",
},
{
it: "renders a bucket with path",
listObjectsFunc: func(context.Context, string, minio.ListObjectsOptions) <-chan minio.ObjectInfo {
objCh := make(chan minio.ObjectInfo)
close(objCh)
return objCh
},
bucketName: "BUCKET-NAME",
path: "abc/def",
expectedStatusCode: http.StatusOK,
expectedBodyContains: "def",
},
2017-04-03 22:03:45 +02:00
}
2019-01-08 08:51:09 +01:00
for _, tc := range cases {
2020-12-03 10:28:05 +01:00
tc := tc
2019-01-08 08:51:09 +01:00
t.Run(tc.it, func(t *testing.T) {
2020-12-03 10:28:05 +01:00
t.Parallel()
2018-05-31 18:28:55 +02:00
is := is.New(t)
2017-12-21 07:50:59 +01:00
2018-08-12 15:13:07 +02:00
s3 := &mocks.S3Mock{
2021-08-05 11:44:40 +02:00
ListObjectsFunc: tc.listObjectsFunc,
2018-04-24 22:35:21 +02:00
}
2021-04-21 11:37:38 +02:00
templates := os.DirFS(filepath.Join("..", "..", "..", "web", "template"))
r := mux.NewRouter()
2023-06-13 17:37:57 +02:00
r.PathPrefix("/buckets/").Handler(s3manager.HandleBucketView(s3, templates, true, true)).Methods(http.MethodGet)
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
2023-06-13 17:37:57 +02:00
resp, err := http.Get(fmt.Sprintf("%s/buckets/%s/%s", ts.URL, tc.bucketName, tc.path))
2018-05-31 18:28:55 +02:00
is.NoErr(err)
defer resp.Body.Close()
2021-02-21 13:57:50 +01:00
body, err := io.ReadAll(resp.Body)
2018-05-31 18:28:55 +02:00
is.NoErr(err)
2017-04-03 22:03:45 +02:00
2018-05-31 18:28:55 +02:00
is.Equal(tc.expectedStatusCode, resp.StatusCode) // status code
is.True(strings.Contains(string(body), tc.expectedBodyContains)) // body
2017-07-31 10:57:22 +02:00
})
2017-04-03 22:03:45 +02:00
}
}