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

77 lines
1.9 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager_test
2017-04-03 22:46:01 +02:00
import (
2018-11-30 23:09:00 +01:00
"io/ioutil"
2017-04-03 22:46:01 +02:00
"net/http"
"net/http/httptest"
2018-03-14 21:53:35 +01:00
"path/filepath"
2018-05-31 18:28:55 +02:00
"strings"
2017-04-03 22:46:01 +02:00
"testing"
2018-03-14 21:53:35 +01:00
"github.com/mastertinner/s3manager/internal/app/s3manager"
2018-08-12 15:13:07 +02:00
"github.com/mastertinner/s3manager/internal/app/s3manager/mocks"
2018-05-31 18:28:55 +02:00
"github.com/matryer/is"
2017-04-03 22:46:01 +02:00
minio "github.com/minio/minio-go"
)
2018-05-31 16:10:41 +02:00
func TestHandleBucketsView(t *testing.T) {
2019-01-08 08:51:09 +01:00
cases := []struct {
it string
2018-04-24 22:35:21 +02:00
listBucketsFunc func() ([]minio.BucketInfo, error)
2017-04-03 22:46:01 +02:00
expectedStatusCode int
expectedBodyContains string
}{
2019-01-08 08:51:09 +01:00
{
it: "renders a list of buckets",
2018-04-24 22:35:21 +02:00
listBucketsFunc: func() ([]minio.BucketInfo, error) {
return []minio.BucketInfo{{Name: "testBucket"}}, nil
2017-04-03 22:46:01 +02:00
},
expectedStatusCode: http.StatusOK,
expectedBodyContains: "testBucket",
},
2019-01-08 08:51:09 +01:00
{
it: "renders placeholder if no buckets",
2018-04-24 22:35:21 +02:00
listBucketsFunc: func() ([]minio.BucketInfo, error) {
return []minio.BucketInfo{}, nil
},
2017-04-03 22:46:01 +02:00
expectedStatusCode: http.StatusOK,
expectedBodyContains: "No buckets yet",
},
2019-01-08 08:51:09 +01:00
{
it: "returns error if there is an S3 error",
2018-04-24 22:35:21 +02:00
listBucketsFunc: func() ([]minio.BucketInfo, error) {
2020-05-04 09:46:41 +02:00
return []minio.BucketInfo{}, errS3
2017-04-03 22:46:01 +02:00
},
expectedStatusCode: http.StatusInternalServerError,
2017-04-09 16:28:57 +02:00
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
2017-04-03 22:46:01 +02:00
},
}
2019-01-08 08:51:09 +01:00
for _, tc := range cases {
t.Run(tc.it, func(t *testing.T) {
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{
2018-04-24 22:35:21 +02:00
ListBucketsFunc: tc.listBucketsFunc,
}
2018-03-14 21:53:35 +01:00
tmplDir := filepath.Join("..", "..", "..", "web", "template")
2017-07-31 10:57:22 +02:00
req, err := http.NewRequest(http.MethodGet, "/buckets", nil)
2018-05-31 18:28:55 +02:00
is.NoErr(err)
2017-04-03 22:46:01 +02:00
2017-07-31 10:57:22 +02:00
rr := httptest.NewRecorder()
2018-05-31 16:10:41 +02:00
handler := s3manager.HandleBucketsView(s3, tmplDir)
2017-04-03 22:46:01 +02:00
2017-07-31 10:57:22 +02:00
handler.ServeHTTP(rr, req)
resp := rr.Result()
2018-11-30 23:09:00 +01:00
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
is.NoErr(err)
2017-04-03 22:46:01 +02:00
2018-11-30 23:09:00 +01: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:46:01 +02:00
}
}