diff --git a/backend/app/services/common_test.go b/backend/app/services/common_test.go new file mode 100644 index 0000000..e6794d3 --- /dev/null +++ b/backend/app/services/common_test.go @@ -0,0 +1,86 @@ +package services + +import ( + "database/sql" + "errors" + "testing" + + "quyun/v2/app/commands/testx" + "quyun/v2/app/errorx" + common_dto "quyun/v2/app/http/v1/dto" + + . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/suite" + "go.ipao.vip/atom/contracts" + "go.uber.org/dig" +) + +type CommonTestSuiteInjectParams struct { + dig.In + + DB *sql.DB + Initials []contracts.Initial `group:"initials"` +} + +type CommonTestSuite struct { + suite.Suite + CommonTestSuiteInjectParams +} + +func Test_Common(t *testing.T) { + providers := testx.Default().With(Provide) + + testx.Serve(providers, t, func(p CommonTestSuiteInjectParams) { + suite.Run(t, &CommonTestSuite{CommonTestSuiteInjectParams: p}) + }) +} + +func (s *CommonTestSuite) Test_AbortUpload() { + Convey("AbortUpload", s.T(), func() { + ctx := s.T().Context() + tenantID := int64(1) + ownerID := int64(1001) + + newUpload := func() string { + form := &common_dto.UploadInitForm{ + Filename: "sample.mp4", + Type: "video", + MimeType: "video/mp4", + } + resp, err := Common.InitUpload(ctx, tenantID, ownerID, form) + So(err, ShouldBeNil) + So(resp.UploadID, ShouldNotBeBlank) + return resp.UploadID + } + + Convey("should allow owner to abort", func() { + uploadID := newUpload() + + err := Common.AbortUpload(ctx, tenantID, ownerID, uploadID) + So(err, ShouldBeNil) + }) + + Convey("should reject abort by other user", func() { + uploadID := newUpload() + otherID := int64(2002) + + err := Common.AbortUpload(ctx, tenantID, otherID, uploadID) + So(err, ShouldNotBeNil) + + var appErr *errorx.AppError + So(errors.As(err, &appErr), ShouldBeTrue) + So(appErr.Code, ShouldEqual, errorx.ErrForbidden.Code) + + So(Common.AbortUpload(ctx, tenantID, ownerID, uploadID), ShouldBeNil) + }) + + Convey("should return not found for missing upload", func() { + err := Common.AbortUpload(ctx, tenantID, ownerID, "missing-upload") + So(err, ShouldNotBeNil) + + var appErr *errorx.AppError + So(errors.As(err, &appErr), ShouldBeTrue) + So(appErr.Code, ShouldEqual, errorx.ErrRecordNotFound.Code) + }) + }) +}