shareId = this.get('linkShare').id;
// note: update can only update a single value at a time
- call = this.updateShare(shareId, attributes);
+ call = this.updateShare(shareId, attributes, options);
} else {
attributes = _.defaults(attributes, {
password: '',
shareType: OC.Share.SHARE_TYPE_LINK
});
- call = this.addShare(attributes);
+ call = this.addShare(attributes, options);
}
return call;
}
}
});
- }).fail(function(result) {
+ }).fail(function(xhr) {
var msg = t('core', 'Error');
+ var result = xhr.responseJSON;
if (result.ocs && result.ocs.meta) {
msg = result.ocs.meta.message;
}
});
},
- updateShare: function(shareId, attrs) {
+ updateShare: function(shareId, attrs, options) {
var self = this;
+ options = options || {};
return $.ajax({
type: 'PUT',
url: this._getUrl('shares/' + encodeURIComponent(shareId)),
data: attrs,
dataType: 'json'
}).done(function() {
- self.fetch();
+ self.fetch({
+ success: function() {
+ if (_.isFunction(options.success)) {
+ options.success(self);
+ }
+ }
+ });
+ }).fail(function(xhr) {
+ var msg = t('core', 'Error');
+ var result = xhr.responseJSON;
+ if (result.ocs && result.ocs.meta) {
+ msg = result.ocs.meta.message;
+ }
+
+ if (_.isFunction(options.error)) {
+ options.error(self, msg);
+ } else {
+ OC.dialogs.alert(msg, t('core', 'Error while sharing'));
+ }
});
},
* @param {int} shareId share id
* @return {jQuery}
*/
- removeShare: function(shareId) {
+ removeShare: function(shareId, options) {
var self = this;
+ options = options || {};
return $.ajax({
type: 'DELETE',
url: this._getUrl('shares/' + encodeURIComponent(shareId)),
}).done(function() {
- self.fetch();
+ self.fetch({
+ success: function() {
+ if (_.isFunction(options.success)) {
+ options.success(self);
+ }
+ }
+ });
+ }).fail(function(xhr) {
+ var msg = t('core', 'Error');
+ var result = xhr.responseJSON;
+ if (result.ocs && result.ocs.meta) {
+ msg = result.ocs.meta.message;
+ }
+
+ if (_.isFunction(options.error)) {
+ options.error(self, msg);
+ } else {
+ OC.dialogs.alert(msg, t('core', 'Error removing share'));
+ }
});
},
password: 'test'
});
});
+ it('forwards error message on add', function() {
+ var errorStub = sinon.stub();
+ model.set({
+ linkShare: {
+ isLinkShare: false
+ }
+ }, {
+ });
+
+ model.saveLinkShare({
+ password: 'test'
+ }, {
+ error: errorStub
+ });
+
+ addShareStub.yieldTo('error', 'Some error message');
+
+ expect(errorStub.calledOnce).toEqual(true);
+ expect(errorStub.lastCall.args[0]).toEqual('Some error message');
+ });
+ it('forwards error message on update', function() {
+ var errorStub = sinon.stub();
+ model.set({
+ linkShare: {
+ isLinkShare: true,
+ id: '123'
+ }
+ }, {
+ });
+
+ model.saveLinkShare({
+ password: 'test'
+ }, {
+ error: errorStub
+ });
+
+ updateShareStub.yieldTo('error', 'Some error message');
+
+ expect(errorStub.calledOnce).toEqual(true);
+ expect(errorStub.lastCall.args[0]).toEqual('Some error message');
+ });
});
describe('creating shares', function() {
it('sends POST method to endpoint with passed values', function() {
shareWith: 'group1'
});
});
+ it('calls error handler with error message', function() {
+ var errorStub = sinon.stub();
+ model.addShare({
+ shareType: OC.Share.SHARE_TYPE_GROUP,
+ shareWith: 'group1'
+ }, {
+ error: errorStub
+ });
+
+ expect(fakeServer.requests.length).toEqual(1);
+ fakeServer.requests[0].respond(
+ 400,
+ { 'Content-Type': 'application/json' },
+ JSON.stringify({
+ ocs: {
+ meta: {
+ message: 'Some error message'
+ }
+ }
+ })
+ );
+
+ expect(errorStub.calledOnce).toEqual(true);
+ expect(errorStub.lastCall.args[1]).toEqual('Some error message');
+ });
});
describe('updating shares', function() {
it('sends PUT method to endpoint with passed values', function() {
permissions: '' + (OC.PERMISSION_READ | OC.PERMISSION_SHARE)
});
});
+ it('calls error handler with error message', function() {
+ var errorStub = sinon.stub();
+ model.updateShare(123, {
+ permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
+ }, {
+ error: errorStub
+ });
+
+ expect(fakeServer.requests.length).toEqual(1);
+ fakeServer.requests[0].respond(
+ 400,
+ { 'Content-Type': 'application/json' },
+ JSON.stringify({
+ ocs: {
+ meta: {
+ message: 'Some error message'
+ }
+ }
+ })
+ );
+
+ expect(errorStub.calledOnce).toEqual(true);
+ expect(errorStub.lastCall.args[1]).toEqual('Some error message');
+ });
});
describe('removing shares', function() {
it('sends DELETE method to endpoint with share id', function() {
'shares/123?format=json'
);
});
+ it('calls error handler with error message', function() {
+ var errorStub = sinon.stub();
+ model.removeShare(123, {
+ error: errorStub
+ });
+
+ expect(fakeServer.requests.length).toEqual(1);
+ fakeServer.requests[0].respond(
+ 400,
+ { 'Content-Type': 'application/json' },
+ JSON.stringify({
+ ocs: {
+ meta: {
+ message: 'Some error message'
+ }
+ }
+ })
+ );
+
+ expect(errorStub.calledOnce).toEqual(true);
+ expect(errorStub.lastCall.args[1]).toEqual('Some error message');
+ });
});
});