2017-05-08 23:07:07 +02:00
|
|
|
package s3manager_test
|
2017-04-02 17:10:36 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-05-31 18:28:55 +02:00
|
|
|
"strings"
|
2017-04-02 17:10:36 +02:00
|
|
|
"testing"
|
|
|
|
|
2018-03-14 21:53:35 +01:00
|
|
|
"github.com/mastertinner/s3manager/internal/app/s3manager"
|
2018-05-31 18:28:55 +02:00
|
|
|
"github.com/matryer/is"
|
2017-04-02 17:10:36 +02:00
|
|
|
)
|
|
|
|
|
2018-05-31 16:10:41 +02:00
|
|
|
func TestHandleCreateBucket(t *testing.T) {
|
2017-05-08 23:07:07 +02:00
|
|
|
cases := map[string]struct {
|
2018-04-24 22:35:21 +02:00
|
|
|
makeBucketFunc func(string, string) error
|
2017-04-09 16:28:57 +02:00
|
|
|
body string
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedBodyContains string
|
2017-04-02 17:10:36 +02:00
|
|
|
}{
|
2017-12-21 07:50:59 +01:00
|
|
|
"creates a new bucket": {
|
2018-05-22 22:56:01 +02:00
|
|
|
makeBucketFunc: func(string, string) error {
|
2018-04-24 22:35:21 +02:00
|
|
|
return nil
|
|
|
|
},
|
2018-05-22 22:56:01 +02:00
|
|
|
body: `{"name":"myBucket"}`,
|
2017-04-09 16:28:57 +02:00
|
|
|
expectedStatusCode: http.StatusCreated,
|
2018-05-22 22:56:01 +02:00
|
|
|
expectedBodyContains: `{"name":"myBucket","creationDate":"0001-01-01T00:00:00Z"}`,
|
2017-04-02 17:10:36 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"returns error for empty request": {
|
2018-05-22 22:56:01 +02:00
|
|
|
makeBucketFunc: func(string, string) error {
|
2018-04-24 22:35:21 +02:00
|
|
|
return nil
|
|
|
|
},
|
2017-04-09 16:28:57 +02:00
|
|
|
body: "",
|
|
|
|
expectedStatusCode: http.StatusUnprocessableEntity,
|
|
|
|
expectedBodyContains: http.StatusText(http.StatusUnprocessableEntity),
|
2017-04-02 17:10:36 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"returns error for malformed request": {
|
2018-05-22 22:56:01 +02:00
|
|
|
makeBucketFunc: func(string, string) error {
|
2018-04-24 22:35:21 +02:00
|
|
|
return nil
|
|
|
|
},
|
2017-04-09 16:28:57 +02:00
|
|
|
body: "}",
|
|
|
|
expectedStatusCode: http.StatusUnprocessableEntity,
|
|
|
|
expectedBodyContains: http.StatusText(http.StatusUnprocessableEntity),
|
2017-04-02 17:10:36 +02:00
|
|
|
},
|
2017-12-21 07:50:59 +01:00
|
|
|
"returns error if there is an S3 error": {
|
2018-05-22 22:56:01 +02:00
|
|
|
makeBucketFunc: func(string, string) error {
|
2018-04-24 22:35:21 +02:00
|
|
|
return errors.New("mocked S3 error")
|
2017-04-02 17:10:36 +02:00
|
|
|
},
|
2018-05-22 22:56:01 +02:00
|
|
|
body: `{"name":"myBucket"}`,
|
2017-04-09 16:28:57 +02:00
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
|
2017-04-02 17:10:36 +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) {
|
2018-05-31 18:28:55 +02:00
|
|
|
is := is.New(t)
|
2017-12-21 07:50:59 +01:00
|
|
|
|
2018-04-24 22:35:21 +02:00
|
|
|
s3 := &S3Mock{
|
|
|
|
MakeBucketFunc: tc.makeBucketFunc,
|
|
|
|
}
|
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
req, err := http.NewRequest(http.MethodPost, "/api/buckets", bytes.NewBufferString(tc.body))
|
2018-05-31 18:28:55 +02:00
|
|
|
is.NoErr(err)
|
2017-04-02 17:10:36 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
rr := httptest.NewRecorder()
|
2018-05-31 16:10:41 +02:00
|
|
|
handler := s3manager.HandleCreateBucket(s3)
|
2017-04-02 17:10:36 +02:00
|
|
|
|
2017-07-31 10:57:22 +02:00
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
resp := rr.Result()
|
2017-04-02 17:10:36 +02:00
|
|
|
|
2018-05-31 18:28:55 +02:00
|
|
|
is.Equal(tc.expectedStatusCode, resp.StatusCode) // status code
|
|
|
|
is.True(strings.Contains(rr.Body.String(), tc.expectedBodyContains)) // body
|
2017-07-31 10:57:22 +02:00
|
|
|
})
|
2017-04-02 17:10:36 +02:00
|
|
|
}
|
|
|
|
}
|