2017-05-08 23:07:07 +02:00
|
|
|
package s3manager_test
|
2017-04-03 22:46:01 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-03-14 21:53:35 +01:00
|
|
|
"path/filepath"
|
2017-04-03 22:46:01 +02:00
|
|
|
"testing"
|
|
|
|
|
2018-03-14 21:53:35 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/mastertinner/s3manager/internal/app/s3manager"
|
2017-04-03 22:46:01 +02:00
|
|
|
minio "github.com/minio/minio-go"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBucketsViewHandler(t *testing.T) {
|
2017-05-08 23:07:07 +02:00
|
|
|
cases := map[string]struct {
|
2018-03-14 21:53:35 +01:00
|
|
|
s3 s3manager.S3
|
2017-04-03 22:46:01 +02:00
|
|
|
expectedStatusCode int
|
|
|
|
expectedBodyContains string
|
|
|
|
}{
|
2017-12-21 07:50:59 +01:00
|
|
|
"renders a list of buckets": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{
|
2017-04-03 22:46:01 +02:00
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "testBucket",
|
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"renders placeholder if no buckets": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{},
|
2017-04-03 22:46:01 +02:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "No buckets yet",
|
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"returns error if there is an S3 error": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{
|
2017-04-03 23:52:41 +02:00
|
|
|
Err: errors.New("mocked S3 error"),
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-03-14 21:53:35 +01:00
|
|
|
tmplDir := filepath.Join("..", "..", "..", "web", "template")
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.
|
|
|
|
Methods(http.MethodGet).
|
|
|
|
Path("/buckets/{bucketName}").
|
|
|
|
Handler(s3manager.BucketViewHandler(tc.s3, tmplDir))
|
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "/buckets", nil)
|
|
|
|
assert.NoError(err, tcID)
|
2017-04-03 22:46:01 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
rr := httptest.NewRecorder()
|
2018-03-14 21:53:35 +01:00
|
|
|
handler := s3manager.BucketsViewHandler(tc.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()
|
2017-04-03 22:46:01 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
assert.Equal(tc.expectedStatusCode, resp.StatusCode, tcID)
|
|
|
|
assert.Contains(rr.Body.String(), tc.expectedBodyContains, tcID)
|
|
|
|
})
|
2017-04-03 22:46:01 +02:00
|
|
|
}
|
|
|
|
}
|