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

66 lines
1.6 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager_test
import (
2021-08-05 11:44:40 +02:00
"context"
"net/http"
"net/http/httptest"
2018-05-31 18:28:55 +02:00
"strings"
"testing"
2021-12-30 12:09:42 +01:00
"github.com/cloudlena/s3manager/internal/app/s3manager"
"github.com/cloudlena/s3manager/internal/app/s3manager/mocks"
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"
)
2018-05-31 16:10:41 +02:00
func TestHandleDeleteObject(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
removeObjectFunc func(context.Context, string, string, minio.RemoveObjectOptions) error
2017-04-09 16:28:57 +02:00
expectedStatusCode int
expectedBodyContains string
}{
2019-01-08 08:51:09 +01:00
{
it: "deletes an existing object",
2021-08-05 11:44:40 +02:00
removeObjectFunc: func(context.Context, string, string, minio.RemoveObjectOptions) error {
2018-04-24 22:35:21 +02:00
return nil
},
2017-04-09 16:28:57 +02:00
expectedStatusCode: http.StatusNoContent,
2017-04-10 14:19:27 +02:00
expectedBodyContains: "",
},
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
removeObjectFunc: func(context.Context, string, string, minio.RemoveObjectOptions) error {
2020-05-04 09:46:41 +02:00
return errS3
},
2017-04-09 16:28:57 +02:00
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
},
}
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{
2018-04-24 22:35:21 +02:00
RemoveObjectFunc: tc.removeObjectFunc,
}
2017-07-31 10:57:22 +02:00
req, err := http.NewRequest(http.MethodDelete, "/api/buckets/bucketName/objects/objectName", nil)
2018-05-31 18:28:55 +02:00
is.NoErr(err)
2017-07-31 10:57:22 +02:00
rr := httptest.NewRecorder()
2018-05-31 16:10:41 +02:00
handler := s3manager.HandleDeleteObject(s3)
2017-07-31 10:57:22 +02:00
handler.ServeHTTP(rr, req)
2018-05-31 18:28:55 +02:00
is.Equal(tc.expectedStatusCode, rr.Code) // status code
is.True(strings.Contains(rr.Body.String(), tc.expectedBodyContains)) // body
2017-07-31 10:57:22 +02:00
})
}
}