diff options
author | Garren Smith <garren.smith@gmail.com> | 2013-09-25 16:35:49 +0200 |
---|---|---|
committer | Garren Smith <garren.smith@gmail.com> | 2013-09-25 17:41:36 +0200 |
commit | 1acae2594a084088fbcf685912e87fec8d77fe08 (patch) | |
tree | de48a098d3fe514d4a01a0697286e5055c03385e | |
parent | 481cd4f99711035b89b09aa532b0c44fe913a976 (diff) | |
download | couchdb-1acae2594a084088fbcf685912e87fec8d77fe08.tar.gz |
more tests and error notifications
-rw-r--r-- | src/fauxton/app/addons/permissions/resources.js | 17 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/tests/viewsSpec.js | 82 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/views.js | 30 |
3 files changed, 114 insertions, 15 deletions
diff --git a/src/fauxton/app/addons/permissions/resources.js b/src/fauxton/app/addons/permissions/resources.js index 5829971c6..66eaffd7b 100644 --- a/src/fauxton/app/addons/permissions/resources.js +++ b/src/fauxton/app/addons/permissions/resources.js @@ -28,9 +28,8 @@ function (app, FauxtonAPI ) { names: [], roles: [] } - }, - + isNew: function () { return false; }, @@ -46,9 +45,19 @@ function (app, FauxtonAPI ) { addItem: function (value, type, section) { var sectionValues = this.get(section); - if (!sectionValues || !sectionValues[type]) { return; } + if (!sectionValues || !sectionValues[type]) { + return { + error: true, + msg: 'Section ' + section + 'does not exist' + }; + } - if (sectionValues[type].indexOf(value) > -1) { return; } + if (sectionValues[type].indexOf(value) > -1) { + return { + error: true, + msg: 'Role/Name has already been added' + }; + } sectionValues[type].push(value); return this.set(section, sectionValues); diff --git a/src/fauxton/app/addons/permissions/tests/viewsSpec.js b/src/fauxton/app/addons/permissions/tests/viewsSpec.js index 73afec24f..e5330c066 100644 --- a/src/fauxton/app/addons/permissions/tests/viewsSpec.js +++ b/src/fauxton/app/addons/permissions/tests/viewsSpec.js @@ -18,6 +18,49 @@ define([ var assert = testUtils.assert, ViewSandbox = testUtils.ViewSandbox; + describe('Permission View', function () { + + beforeEach(function () { + security = new Models.Security({'admins': { + 'names': ['_user'], + 'roles': [] + } + }, {database: 'fakedb'}); + + section = new Views.Permissions({ + database: 'fakedb', + model: security + }); + + viewSandbox = new ViewSandbox(); + viewSandbox.renderView(section); + }); + + afterEach(function () { + viewSandbox.remove(); + }); + + describe('itemRemoved', function () { + + it('Should set model', function () { + var saveMock = sinon.spy(security, 'set'); + Views.events.trigger('itemRemoved'); + + assert.ok(saveMock.calledOnce); + var args = saveMock.args; + assert.deepEqual(args[0][0], {"admins":{"names":["_user"],"roles":[]},"members":{"names":[],"roles":[]}}); + }); + + it('Should save model', function () { + var saveMock = sinon.spy(security, 'save'); + Views.events.trigger('itemRemoved'); + + assert.ok(saveMock.calledOnce); + }); + }); + + }); + describe('PermissionsSection', function () { var section, security; @@ -29,7 +72,8 @@ define([ }, {database: 'fakedb'}); section = new Views.PermissionSection({ - section: 'admins' + section: 'admins', + model: security }); viewSandbox = new ViewSandbox(); @@ -40,6 +84,42 @@ define([ viewSandbox.remove(); }); + describe('#discardRemovedViews', function () { + it('Should not filter out active views', function () { + section.discardRemovedViews(); + + assert.equal(section.nameViews.length, 1); + + }); + + it('Should filter out removed views', function () { + section.nameViews[0].removed = true; + section.discardRemovedViews(); + + assert.equal(section.nameViews.length, 0); + + }); + + }); + + describe('#getItemFromView', function () { + + it('Should return item list', function () { + var items = section.getItemFromView(section.nameViews); + + assert.deepEqual(items, ['_user']); + }); + + }); + + describe('#addItems', function () { + + it('Should add item to model', function () { + //todo add a test here + + }); + + }); }); diff --git a/src/fauxton/app/addons/permissions/views.js b/src/fauxton/app/addons/permissions/views.js index 3c5d3c2b1..4c2987b76 100644 --- a/src/fauxton/app/addons/permissions/views.js +++ b/src/fauxton/app/addons/permissions/views.js @@ -27,13 +27,7 @@ function (app, FauxtonAPI, Permissions ) { this.listenTo(Permissions.events, 'itemRemoved', this.itemRemoved); }, - events: { - "submit .permission-item-form": "addItem", - 'click .close': "removeItem" - }, - itemRemoved: function (event) { - console.log('item remove'); this.model.set({ admins: this.adminsView.items(), members: this.membersView.items() @@ -43,8 +37,14 @@ function (app, FauxtonAPI, Permissions ) { FauxtonAPI.addNotification({ msg: 'Database permissions has been updated.' }); + }, function (xhr) { + FauxtonAPI.addNotification({ + msg: 'Could not update permissions - reason: ' + xhr.responseText, + type: 'error' + }); }); }, + beforeRender: function () { this.adminsView = this.insertView('#sections', new Permissions.PermissionSection({ model: this.model, @@ -59,12 +59,9 @@ function (app, FauxtonAPI, Permissions ) { })); }, - serialize: function () { - console.log('s', this.model.toJSON()); return { databaseName: this.database.id, - security: this.model.toJSON() }; } }); @@ -135,12 +132,25 @@ function (app, FauxtonAPI, Permissions ) { type = $item.data('type'), that = this; - this.model.addItem(value, type, section); + var resp = this.model.addItem(value, type, section); + + if (resp && resp.error) { + return FauxtonAPI.addNotification({ + msg: resp.msg, + type: 'error' + }); + } + this.model.save().then(function () { that.render(); FauxtonAPI.addNotification({ msg: 'Database permissions has been updated.' }); + }, function (xhr) { + FauxtonAPI.addNotification({ + msg: 'Could not update permissions - reason: ' + xhr.responseText, + type: 'error' + }); }); }, |