diff --git a/services/collaboration/pkg/connector/connector.go b/services/collaboration/pkg/connector/connector.go index 200c131051..8cccc7dfbe 100644 --- a/services/collaboration/pkg/connector/connector.go +++ b/services/collaboration/pkg/connector/connector.go @@ -12,6 +12,68 @@ type ConnectorResponse struct { Body interface{} } +// NewResponse creates a new ConnectorResponse with just the specified status. +// Headers and Body will be nil +func NewResponse(status int) *ConnectorResponse { + return &ConnectorResponse{Status: status} +} + +// NewResponse creates a new ConnectorResponse with the specified status +// and the "X-WOPI-Lock" header having the value in the lockID parameter. +// +// This is usually used for conflict responses where the current lock id needs +// to be returned, although the `GetLock` method also uses this method for a +// successful response (with the lock id included) +func NewResponseWithLock(status int, lockID string) *ConnectorResponse { + return &ConnectorResponse{ + Status: status, + Headers: map[string]string{ + HeaderWopiLock: lockID, + }, + } +} + +// NewResponseSuccessBody creates a new ConnectorResponse with a fixed 200 +// (success) status and the specified body. The headers will be nil. +// +// This is used for the `CheckFileInfo` method in order to return the fileinfo +func NewResponseSuccessBody(body interface{}) *ConnectorResponse { + return &ConnectorResponse{ + Status: 200, + Body: body, + } +} + +// NewResponseSuccessBodyName creates a new ConnectorResponse with a fixed 200 +// (success) status and a "map[string]interface{}" body. The body will contain +// a "Name" key with the supplied name as value. +// +// This is used for the `RenameFile` method in order to return the final name +// of the renamed file if the operation is successful +func NewResponseSuccessBodyName(name string) *ConnectorResponse { + return &ConnectorResponse{ + Status: 200, + Body: map[string]interface{}{ + "Name": name, + }, + } +} + +// NewResponseSuccessBodyNameUrl creates a new ConnectorResponse with a fixed +// 200 (success) status and a "map[string]interface{}" body. The body will +// contain "Name" and "Url" keys with their respective suplied values +// +// This is used in the `PutRelativeFile` methods (both suggested and relative). +func NewResponseSuccessBodyNameUrl(name, url string) *ConnectorResponse { + return &ConnectorResponse{ + Status: 200, + Body: map[string]interface{}{ + "Name": name, + "Url": url, + }, + } +} + // ConnectorError defines an error in the connector. It contains an error code // and a message. // For convenience, the error code can be used as HTTP error code, although diff --git a/services/collaboration/pkg/connector/contentconnector.go b/services/collaboration/pkg/connector/contentconnector.go index d816f4029c..9812b11c92 100644 --- a/services/collaboration/pkg/connector/contentconnector.go +++ b/services/collaboration/pkg/connector/contentconnector.go @@ -223,7 +223,7 @@ func (c *ContentConnector) PutFile(ctx context.Context, stream io.Reader, stream Str("StatusCode", statRes.GetStatus().GetCode().String()). Str("StatusMsg", statRes.GetStatus().GetMessage()). Msg("PutFile: stat failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } // If there is a lock and it mismatches, return 409 @@ -232,12 +232,7 @@ func (c *ContentConnector) PutFile(ctx context.Context, stream io.Reader, stream Str("LockID", statRes.GetInfo().GetLock().GetLockId()). Msg("PutFile: wrong lock") // onlyoffice says it's required to send the current lockId, MS doesn't say anything - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: statRes.GetInfo().GetLock().GetLockId(), - }, - }, nil + return NewResponseWithLock(409, statRes.GetInfo().GetLock().GetLockId()), nil } // only unlocked uploads can go through if the target file is empty, @@ -247,12 +242,7 @@ func (c *ContentConnector) PutFile(ctx context.Context, stream io.Reader, stream if lockID == "" && statRes.GetInfo().GetLock() == nil && statRes.GetInfo().GetSize() > 0 { logger.Error().Msg("PutFile: file must be locked first") // onlyoffice says to send an empty string if the file is unlocked, MS doesn't say anything - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: "", - }, - }, nil + return NewResponseWithLock(409, ""), nil } // Prepare the data to initiate the upload @@ -288,7 +278,7 @@ func (c *ContentConnector) PutFile(ctx context.Context, stream io.Reader, stream Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("UploadHelper: InitiateFileUpload failed with wrong status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } // if the content length is greater than 0, we need to upload the content to the @@ -313,7 +303,7 @@ func (c *ContentConnector) PutFile(ctx context.Context, stream io.Reader, stream Str("Endpoint", uploadEndpoint). Bool("HasUploadToken", hasUploadToken). Msg("UploadHelper: Upload endpoint or token is missing") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } httpClient := http.Client{ @@ -369,10 +359,10 @@ func (c *ContentConnector) PutFile(ctx context.Context, stream io.Reader, stream Bool("HasUploadToken", hasUploadToken). Int("HttpCode", httpResp.StatusCode). Msg("UploadHelper: Put request to the upload endpoint failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } logger.Debug().Msg("PutFile: success") - return &ConnectorResponse{Status: 200}, nil + return NewResponse(200), nil } diff --git a/services/collaboration/pkg/connector/fileconnector.go b/services/collaboration/pkg/connector/fileconnector.go index df77254d36..316e3d1364 100644 --- a/services/collaboration/pkg/connector/fileconnector.go +++ b/services/collaboration/pkg/connector/fileconnector.go @@ -139,7 +139,7 @@ func (f *FileConnector) GetLock(ctx context.Context) (*ConnectorResponse, error) Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("GetLock failed with unexpected status") // TODO: Should we be more strict? There could be more causes for the failure - return &ConnectorResponse{Status: 404}, nil + return NewResponse(404), nil } lockID := "" @@ -152,12 +152,7 @@ func (f *FileConnector) GetLock(ctx context.Context) (*ConnectorResponse, error) Str("LockID", lockID). Msg("GetLock success") - return &ConnectorResponse{ - Status: 200, - Headers: map[string]string{ - HeaderWopiLock: lockID, - }, - }, nil + return NewResponseWithLock(200, lockID), nil } // Lock returns a WOPI lock or performs an unlock and relock @@ -192,7 +187,7 @@ func (f *FileConnector) Lock(ctx context.Context, lockID, oldLockID string) (*Co if lockID == "" { logger.Error().Msg("Lock failed due to empty lockID") - return &ConnectorResponse{Status: 400}, nil + return NewResponse(400), nil } var setOrRefreshStatus *rpcv1beta1.Status @@ -246,7 +241,7 @@ func (f *FileConnector) Lock(ctx context.Context, lockID, oldLockID string) (*Co switch setOrRefreshStatus.GetCode() { case rpcv1beta1.Code_CODE_OK: logger.Debug().Msg("SetLock successful") - return &ConnectorResponse{Status: 200}, nil + return NewResponse(200), nil case rpcv1beta1.Code_CODE_FAILED_PRECONDITION, rpcv1beta1.Code_CODE_ABORTED: // Code_CODE_FAILED_PRECONDITION -> Lock operation mismatched lock @@ -276,12 +271,7 @@ func (f *FileConnector) Lock(ctx context.Context, lockID, oldLockID string) (*Co logger.Warn(). Str("LockID", resp.GetLock().GetLockId()). Msg("SetLock conflict") - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: resp.GetLock().GetLockId(), - }, - }, nil + return NewResponseWithLock(409, resp.GetLock().GetLockId()), nil } // TODO: according to the spec we need to treat this as a RefreshLock @@ -292,22 +282,22 @@ func (f *FileConnector) Lock(ctx context.Context, lockID, oldLockID string) (*Co logger.Warn(). Str("LockID", resp.GetLock().GetLockId()). Msg("SetLock lock refreshed instead") - return &ConnectorResponse{Status: 200}, nil // no need to send the lockID for a 200 code + return NewResponse(200), nil // no need to send the lockID for a 200 code } logger.Error().Msg("SetLock failed and could not refresh") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil case rpcv1beta1.Code_CODE_NOT_FOUND: logger.Error().Msg("SetLock failed, file not found") - return &ConnectorResponse{Status: 404}, nil + return NewResponse(404), nil default: logger.Error(). Str("StatusCode", setOrRefreshStatus.GetCode().String()). Str("StatusMsg", setOrRefreshStatus.GetMessage()). Msg("SetLock failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } @@ -337,7 +327,7 @@ func (f *FileConnector) RefreshLock(ctx context.Context, lockID string) (*Connec if lockID == "" { logger.Error().Msg("RefreshLock failed due to empty lockID") - return &ConnectorResponse{Status: 400}, nil + return NewResponse(400), nil } req := &providerv1beta1.RefreshLockRequest{ @@ -361,14 +351,14 @@ func (f *FileConnector) RefreshLock(ctx context.Context, lockID string) (*Connec switch resp.GetStatus().GetCode() { case rpcv1beta1.Code_CODE_OK: logger.Debug().Msg("RefreshLock successful") - return &ConnectorResponse{Status: 200}, nil + return NewResponse(200), nil case rpcv1beta1.Code_CODE_NOT_FOUND: logger.Error(). Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("RefreshLock failed, file reference not found") - return &ConnectorResponse{Status: 404}, nil + return NewResponse(404), nil case rpcv1beta1.Code_CODE_ABORTED: logger.Error(). @@ -393,7 +383,7 @@ func (f *FileConnector) RefreshLock(ctx context.Context, lockID string) (*Connec Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("RefreshLock failed, tried to get the current lock failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } if resp.GetLock() == nil { @@ -401,12 +391,7 @@ func (f *FileConnector) RefreshLock(ctx context.Context, lockID string) (*Connec Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("RefreshLock failed, no lock on file") - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: "", - }, - }, nil + return NewResponseWithLock(409, ""), nil } else { // lock is different than the one requested, otherwise we wouldn't reached this point logger.Error(). @@ -414,19 +399,14 @@ func (f *FileConnector) RefreshLock(ctx context.Context, lockID string) (*Connec Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("RefreshLock failed, lock mismatch") - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: resp.GetLock().GetLockId(), - }, - }, nil + return NewResponseWithLock(409, resp.GetLock().GetLockId()), nil } default: logger.Error(). Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("RefreshLock failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } @@ -456,7 +436,7 @@ func (f *FileConnector) UnLock(ctx context.Context, lockID string) (*ConnectorRe if lockID == "" { logger.Error().Msg("Unlock failed due to empty lockID") - return &ConnectorResponse{Status: 400}, nil + return NewResponse(400), nil } req := &providerv1beta1.UnlockRequest{ @@ -476,16 +456,11 @@ func (f *FileConnector) UnLock(ctx context.Context, lockID string) (*ConnectorRe switch resp.GetStatus().GetCode() { case rpcv1beta1.Code_CODE_OK: logger.Debug().Msg("Unlock successful") - return &ConnectorResponse{Status: 200}, nil + return NewResponse(200), nil case rpcv1beta1.Code_CODE_ABORTED: // File isn't locked. Need to return 409 with empty lock logger.Error().Err(err).Msg("Unlock failed, file isn't locked") - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: "", - }, - }, nil + return NewResponseWithLock(409, ""), nil case rpcv1beta1.Code_CODE_LOCKED: // We need to return 409 with the current lock req := &providerv1beta1.GetLockRequest{ @@ -503,7 +478,7 @@ func (f *FileConnector) UnLock(ctx context.Context, lockID string) (*ConnectorRe Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("Unlock failed, tried to get the current lock failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } var outLockId string @@ -522,18 +497,13 @@ func (f *FileConnector) UnLock(ctx context.Context, lockID string) (*ConnectorRe Msg("Unlock failed, lock mismatch") outLockId = resp.GetLock().GetLockId() } - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: outLockId, - }, - }, nil + return NewResponseWithLock(409, outLockId), nil default: logger.Error(). Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("Unlock failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } @@ -588,7 +558,7 @@ func (f *FileConnector) PutRelativeFileSuggested(ctx context.Context, ccs Conten Str("StatusCode", oldStatRes.GetStatus().GetCode().String()). Str("StatusMsg", oldStatRes.GetStatus().GetMessage()). Msg("PutRelativeFileSuggested: stat failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } if strings.HasPrefix(target, ".") { @@ -634,7 +604,7 @@ func (f *FileConnector) PutRelativeFileSuggested(ctx context.Context, ccs Conten // TODO: code 400 might happen, what to do? // in other cases, just return the error newLogger.Error().Msg("PutRelativeFileSuggested: put file failed with unhandled status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } @@ -653,13 +623,7 @@ func (f *FileConnector) PutRelativeFileSuggested(ctx context.Context, ccs Conten Str("FinalReference", wopiContext.FileReference.String()). Msg("PutRelativeFileSuggested: success") - return &ConnectorResponse{ - Status: 200, - Body: map[string]interface{}{ - "Name": finalTarget, - "Url": wopiSrcURL.String(), - }, - }, nil + return NewResponseSuccessBodyNameUrl(finalTarget, wopiSrcURL.String()), nil } // PutRelativeFileRelative upload a file using the provided target name @@ -711,7 +675,7 @@ func (f *FileConnector) PutRelativeFileRelative(ctx context.Context, ccs Content Str("StatusCode", oldStatRes.GetStatus().GetCode().String()). Str("StatusMsg", oldStatRes.GetStatus().GetMessage()). Msg("PutRelativeFileRelative: stat failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } targetPath := utils.MakeRelativePath(target) @@ -761,6 +725,7 @@ func (f *FileConnector) PutRelativeFileRelative(ctx context.Context, ccs Content Str("LockID", lockID). Msg("PutRelativeFileRelative: error conflict") + // need to build the response ourselves return &ConnectorResponse{ Status: 409, Headers: map[string]string{ @@ -776,7 +741,7 @@ func (f *FileConnector) PutRelativeFileRelative(ctx context.Context, ccs Content newLogger.Error(). Str("LockID", lockID). Msg("PutRelativeFileRelative: put file failed with unhandled status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } if err := f.adjustWopiReference(ctx, &wopiContext, newLogger); err != nil { @@ -791,13 +756,7 @@ func (f *FileConnector) PutRelativeFileRelative(ctx context.Context, ccs Content newLogger.Debug().Msg("PutRelativeFileRelative: success") - return &ConnectorResponse{ - Status: 200, - Body: map[string]interface{}{ - "Name": target, - "Url": wopiSrcURL.String(), - }, - }, nil + return NewResponseSuccessBodyNameUrl(target, wopiSrcURL.String()), nil } // DeleteFile will delete the requested file @@ -865,7 +824,7 @@ func (f *FileConnector) DeleteFile(ctx context.Context, lockID string) (*Connect if deleteRes.GetStatus().GetCode() == rpcv1beta1.Code_CODE_NOT_FOUND { // don't bother to check for locks of a missing file logger.Error().Msg("DeleteFile: tried to delete a missing file") - return &ConnectorResponse{Status: 404}, nil + return NewResponse(404), nil } // check if the file is locked to return a proper lockID @@ -884,27 +843,22 @@ func (f *FileConnector) DeleteFile(ctx context.Context, lockID string) (*Connect Str("StatusCode", resp.GetStatus().GetCode().String()). Str("StatusMsg", resp.GetStatus().GetMessage()). Msg("DeleteFile: GetLock failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } if resp.GetLock() != nil { logger.Error(). Str("LockID", resp.GetLock().GetLockId()). Msg("DeleteFile: file is locked") - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: resp.GetLock().GetLockId(), - }, - }, nil + return NewResponseWithLock(409, resp.GetLock().GetLockId()), nil } else { // return the original error since the file isn't locked logger.Error().Msg("DeleteFile: delete failed on unlocked file") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } logger.Debug().Msg("DeleteFile: success") - return &ConnectorResponse{Status: 200}, nil + return NewResponse(200), nil } // RenameFile will rename the requested file @@ -944,13 +898,13 @@ func (f *FileConnector) RenameFile(ctx context.Context, lockID, target string) ( if oldStatRes.GetStatus().GetCode() != rpcv1beta1.Code_CODE_OK { if oldStatRes.GetStatus().GetCode() == rpcv1beta1.Code_CODE_NOT_FOUND { logger.Error().Msg("RenameFile: file not found") - return &ConnectorResponse{Status: 404}, nil + return NewResponse(404), nil } else { logger.Error(). Str("StatusCode", oldStatRes.GetStatus().GetCode().String()). Str("StatusMsg", oldStatRes.GetStatus().GetMessage()). Msg("RenameFile: stat failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } @@ -989,12 +943,7 @@ func (f *FileConnector) RenameFile(ctx context.Context, lockID, target string) ( Str("StatusCode", moveRes.GetStatus().GetCode().String()). Str("StatusMsg", moveRes.GetStatus().GetMessage()). Msg("RenameFile: conflict") - return &ConnectorResponse{ - Status: 409, - Headers: map[string]string{ - HeaderWopiLock: currentLockID, - }, - }, nil + return NewResponseWithLock(409, currentLockID), nil } if moveRes.GetStatus().GetCode() == rpcv1beta1.Code_CODE_ALREADY_EXISTS { @@ -1009,7 +958,7 @@ func (f *FileConnector) RenameFile(ctx context.Context, lockID, target string) ( Str("StatusMsg", moveRes.GetStatus().GetMessage()). Msg("RenameFile: move failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } } else { // if the put is successful, exit the loop and move on @@ -1019,13 +968,8 @@ func (f *FileConnector) RenameFile(ctx context.Context, lockID, target string) ( } logger.Debug().Msg("RenameFile: success") - return &ConnectorResponse{ - Status: 200, - Body: map[string]interface{}{ - "Name": strings.TrimSuffix(path.Base(finalTarget), path.Ext(finalTarget)), // return the final filename without extension - }, - }, nil - + // return the final filename without extension + return NewResponseSuccessBodyName(strings.TrimSuffix(path.Base(finalTarget), path.Ext(finalTarget))), nil } // CheckFileInfo returns information about the requested file and capabilities of the wopi server @@ -1058,7 +1002,7 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (*ConnectorResponse, Str("StatusCode", statRes.GetStatus().GetCode().String()). Str("StatusMsg", statRes.GetStatus().GetMessage()). Msg("CheckFileInfo: stat failed with unexpected status") - return &ConnectorResponse{Status: 500}, nil + return NewResponse(500), nil } // If a not known app name is used, consider "Microsoft" as default. @@ -1146,10 +1090,7 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (*ConnectorResponse, info.SetProperties(infoMap) logger.Debug().Interface("FileInfo", info).Msg("CheckFileInfo: success") - return &ConnectorResponse{ - Status: 200, - Body: info, - }, nil + return NewResponseSuccessBody(info), nil } func (f *FileConnector) watermarkText(user *userv1beta1.User) string { diff --git a/services/collaboration/pkg/connector/fileconnector_test.go b/services/collaboration/pkg/connector/fileconnector_test.go index aeb303bc11..2f74ebb141 100644 --- a/services/collaboration/pkg/connector/fileconnector_test.go +++ b/services/collaboration/pkg/connector/fileconnector_test.go @@ -791,7 +791,7 @@ var _ = Describe("FileConnector", func() { }, }, nil) - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponse(200), nil) stat2ParamMatcher := mock.MatchedBy(func(statReq *providerv1beta1.StatRequest) bool { if statReq.Ref.ResourceId.StorageId == "storageid" && @@ -847,7 +847,7 @@ var _ = Describe("FileConnector", func() { }, }, nil) - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponse(200), nil) stat2ParamMatcher := mock.MatchedBy(func(statReq *providerv1beta1.StatRequest) bool { if statReq.Ref.ResourceId.StorageId == "storageid" && @@ -908,8 +908,8 @@ var _ = Describe("FileConnector", func() { // first call will fail with conflict, second call succeeds. // we're only interested on whether the file is locked or not, the actual lockID is irrelevant - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 409}, nil).Once() - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil).Once() + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponse(409), nil).Once() + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponse(200), nil).Once() newFilePath := new(string) stat2ParamMatcher := mock.MatchedBy(func(statReq *providerv1beta1.StatRequest) bool { @@ -971,7 +971,7 @@ var _ = Describe("FileConnector", func() { }, }, nil) - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 500}, nil) + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponse(500), nil) response, err := fc.PutRelativeFileSuggested(ctx, ccs, stream, int64(stream.Len()), ".pdf") Expect(err).To(Succeed()) @@ -1043,7 +1043,7 @@ var _ = Describe("FileConnector", func() { }, }, nil) - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponse(200), nil) stat2ParamMatcher := mock.MatchedBy(func(statReq *providerv1beta1.StatRequest) bool { if statReq.Ref.ResourceId.StorageId == "storageid" && @@ -1098,7 +1098,7 @@ var _ = Describe("FileConnector", func() { }, }, nil) - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 409, Headers: map[string]string{connector.HeaderWopiLock: "zzz999"}}, nil) + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponseWithLock(409, "zzz999"), nil) stat2ParamMatcher := mock.MatchedBy(func(statReq *providerv1beta1.StatRequest) bool { if statReq.Ref.ResourceId.StorageId == "storageid" && @@ -1158,7 +1158,7 @@ var _ = Describe("FileConnector", func() { }, }, nil) - ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(&connector.ConnectorResponse{Status: 500}, nil) + ccs.On("PutFile", mock.Anything, stream, int64(stream.Len()), "").Times(1).Return(connector.NewResponse(500), nil) response, err := fc.PutRelativeFileRelative(ctx, ccs, stream, int64(stream.Len()), "convFile.pdf") Expect(err).To(Succeed()) diff --git a/services/collaboration/pkg/connector/httpadapter_test.go b/services/collaboration/pkg/connector/httpadapter_test.go index 59d9f6fd56..64de4409b1 100644 --- a/services/collaboration/pkg/connector/httpadapter_test.go +++ b/services/collaboration/pkg/connector/httpadapter_test.go @@ -60,7 +60,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("GetLock", mock.Anything).Times(1).Return(&connector.ConnectorResponse{Status: 404}, nil) + fc.On("GetLock", mock.Anything).Times(1).Return(connector.NewResponse(404), nil) httpAdapter.GetLock(w, req) resp := w.Result() @@ -73,7 +73,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("GetLock", mock.Anything).Times(1).Return(&connector.ConnectorResponse{Status: 200, Headers: map[string]string{connector.HeaderWopiLock: "zzz111"}}, nil) + fc.On("GetLock", mock.Anything).Times(1).Return(connector.NewResponseWithLock(200, "zzz111"), nil) httpAdapter.GetLock(w, req) resp := w.Result() @@ -87,7 +87,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("GetLock", mock.Anything).Times(1).Return(&connector.ConnectorResponse{Status: 200, Headers: map[string]string{connector.HeaderWopiLock: ""}}, nil) + fc.On("GetLock", mock.Anything).Times(1).Return(connector.NewResponseWithLock(200, ""), nil) httpAdapter.GetLock(w, req) resp := w.Result() @@ -119,7 +119,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("Lock", mock.Anything, "", "").Times(1).Return(&connector.ConnectorResponse{Status: 400}, nil) + fc.On("Lock", mock.Anything, "", "").Times(1).Return(connector.NewResponse(400), nil) httpAdapter.Lock(w, req) resp := w.Result() @@ -133,7 +133,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("Lock", mock.Anything, "abc123", "").Times(1).Return(&connector.ConnectorResponse{Status: 409, Headers: map[string]string{connector.HeaderWopiLock: "zzz111"}}, nil) + fc.On("Lock", mock.Anything, "abc123", "").Times(1).Return(connector.NewResponseWithLock(409, "zzz111"), nil) httpAdapter.Lock(w, req) resp := w.Result() @@ -148,7 +148,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("Lock", mock.Anything, "abc123", "").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + fc.On("Lock", mock.Anything, "abc123", "").Times(1).Return(connector.NewResponse(200), nil) httpAdapter.Lock(w, req) resp := w.Result() @@ -180,7 +180,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("Lock", mock.Anything, "", "").Times(1).Return(&connector.ConnectorResponse{Status: 400}, nil) + fc.On("Lock", mock.Anything, "", "").Times(1).Return(connector.NewResponse(400), nil) httpAdapter.Lock(w, req) resp := w.Result() @@ -195,7 +195,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("Lock", mock.Anything, "abc123", "qwerty").Times(1).Return(&connector.ConnectorResponse{Status: 409, Headers: map[string]string{connector.HeaderWopiLock: "zzz111"}}, nil) + fc.On("Lock", mock.Anything, "abc123", "qwerty").Times(1).Return(connector.NewResponseWithLock(409, "zzz111"), nil) httpAdapter.Lock(w, req) resp := w.Result() @@ -211,7 +211,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("Lock", mock.Anything, "abc123", "qwerty").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + fc.On("Lock", mock.Anything, "abc123", "qwerty").Times(1).Return(connector.NewResponse(200), nil) httpAdapter.Lock(w, req) resp := w.Result() @@ -242,7 +242,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("RefreshLock", mock.Anything, "").Times(1).Return(&connector.ConnectorResponse{Status: 400}, nil) + fc.On("RefreshLock", mock.Anything, "").Times(1).Return(connector.NewResponse(400), nil) httpAdapter.RefreshLock(w, req) resp := w.Result() @@ -256,7 +256,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("RefreshLock", mock.Anything, "abc123").Times(1).Return(&connector.ConnectorResponse{Status: 409, Headers: map[string]string{connector.HeaderWopiLock: "zzz111"}}, nil) + fc.On("RefreshLock", mock.Anything, "abc123").Times(1).Return(connector.NewResponseWithLock(409, "zzz111"), nil) httpAdapter.RefreshLock(w, req) resp := w.Result() @@ -271,7 +271,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("RefreshLock", mock.Anything, "abc123").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + fc.On("RefreshLock", mock.Anything, "abc123").Times(1).Return(connector.NewResponse(200), nil) httpAdapter.RefreshLock(w, req) resp := w.Result() @@ -301,7 +301,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("UnLock", mock.Anything, "").Times(1).Return(&connector.ConnectorResponse{Status: 400}, nil) + fc.On("UnLock", mock.Anything, "").Times(1).Return(connector.NewResponse(400), nil) httpAdapter.UnLock(w, req) resp := w.Result() @@ -315,7 +315,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("UnLock", mock.Anything, "abc123").Times(1).Return(&connector.ConnectorResponse{Status: 409, Headers: map[string]string{connector.HeaderWopiLock: "zzz111"}}, nil) + fc.On("UnLock", mock.Anything, "abc123").Times(1).Return(connector.NewResponseWithLock(409, "zzz111"), nil) httpAdapter.UnLock(w, req) resp := w.Result() @@ -330,7 +330,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("UnLock", mock.Anything, "abc123").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + fc.On("UnLock", mock.Anything, "abc123").Times(1).Return(connector.NewResponse(200), nil) httpAdapter.UnLock(w, req) resp := w.Result() @@ -358,7 +358,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - fc.On("CheckFileInfo", mock.Anything).Times(1).Return(&connector.ConnectorResponse{Status: 404}, nil) + fc.On("CheckFileInfo", mock.Anything).Times(1).Return(connector.NewResponse(404), nil) httpAdapter.CheckFileInfo(w, req) resp := w.Result() @@ -375,7 +375,7 @@ var _ = Describe("HttpAdapter", func() { Size: 123456789, BreadcrumbDocName: "testy.docx", } - fc.On("CheckFileInfo", mock.Anything).Times(1).Return(&connector.ConnectorResponse{Status: 200, Body: finfo}, nil) + fc.On("CheckFileInfo", mock.Anything).Times(1).Return(connector.NewResponseSuccessBody(finfo), nil) httpAdapter.CheckFileInfo(w, req) resp := w.Result() @@ -458,7 +458,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - cc.On("PutFile", mock.Anything, mock.Anything, int64(len(contentBody)), "abc123").Times(1).Return(&connector.ConnectorResponse{Status: 409, Headers: map[string]string{connector.HeaderWopiLock: "zzz111"}}, nil) + cc.On("PutFile", mock.Anything, mock.Anything, int64(len(contentBody)), "abc123").Times(1).Return(connector.NewResponseWithLock(409, "zzz111"), nil) httpAdapter.PutFile(w, req) resp := w.Result() @@ -473,7 +473,7 @@ var _ = Describe("HttpAdapter", func() { w := httptest.NewRecorder() - cc.On("PutFile", mock.Anything, mock.Anything, int64(len(contentBody)), "abc123").Times(1).Return(&connector.ConnectorResponse{Status: 200}, nil) + cc.On("PutFile", mock.Anything, mock.Anything, int64(len(contentBody)), "abc123").Times(1).Return(connector.NewResponse(200), nil) httpAdapter.PutFile(w, req) resp := w.Result()