Merge pull request #1843 from xtuple/4_6_x
[xtuple] / test / extensions / crm / characteristics.js
1 /*jshint trailing:true, white:true, indent:2, strict:true, curly:true,
2   immed:true, eqeqeq:true, forin:true, latedef:true,
3   newcap:true, noarg:true, undef:true, expr: true */
4 /*global XT:true, XM:true, XV:true, describe:true, it:true,
5   before:true, beforeEach: true, afterEach: true, module:true,
6   require:true, enyo:true, console:true, setTimeout:true,
7   clearInterval:true, setInterval:true */
8
9 (function () {
10   "use strict";
11
12   var _ = require("underscore"),
13     zombieAuth = require("../../lib/zombie_auth"),
14     smoke = require("../../lib/smoke"),
15     common = require("../../lib/common"),
16     modelData = require("../../lib/model_data"),
17     assert = require("chai").assert;
18
19   describe('Characteristics widgets', function () {
20
21     before(function (done) {
22       this.timeout(30 * 1000);
23       zombieAuth.loadApp(done);
24     });
25
26     describe(' ', function () {
27       it('should be possible to add one', function (done) {
28         this.timeout(30 * 1000);
29         smoke.navigateToNewWorkspace(XT.app, "XV.ContactList", function (workspaceContainer) {
30
31           var workspace = workspaceContainer.$.workspace,
32             model = workspace.value,
33             charWidget,
34             charModel,
35             contactCharModel;
36
37           assert.equal(model.recordType, "XM.Contact");
38           smoke.setWorkspaceAttributes(workspace, modelData.contact);
39
40           //
41           // Add a characteristic
42           //
43           assert.equal(model.get("characteristics").length, 0);
44           charWidget = workspace.$.contactCharacteristicsWidget;
45           charWidget.newItem();
46           // XXX it'd be better to do this through enyo
47           charModel = _.find(XM.characteristics.models, function (m) {
48             // support masterref and postbooks
49             return m.id === 'CONTACT-BIRTHDAY' || m.id === 'Birthday';
50           });
51           assert.isObject(charModel);
52           contactCharModel = charWidget.value.models[0];
53           contactCharModel.set({
54             characteristic: charModel,
55             value: "Tuesday"
56           });
57
58           //
59           // On the second save: exit
60           //
61           var modelResaved = function (m, status) {
62             if (status === XM.Model.READY_CLEAN) {
63               model.off("statusChange", modelResaved);
64               workspaceContainer.close();
65
66               setTimeout(function () {
67                 assert(XT.app.$.postbooks.getActive().kind, "XV.Navigator");
68                 done();
69               }, 5000);
70
71             }
72           };
73
74           //
75           // On the first save: try deleting the characteristic and resaving
76           //
77           var modelSaved = function (m, status) {
78             var charItem, picker, deleteItem;
79
80             if (status === XM.Model.READY_CLEAN) {
81               model.off("statusChange", modelSaved);
82               assert.equal(model.get("characteristics").length, 1);
83
84               charItem = charWidget.$.repeater.children[0].$.characteristicItem;
85               assert.isObject(charItem);
86
87               picker = charItem.$.characteristicPicker.$.picker;
88               deleteItem = _.find(picker.$, function (item) {
89                 return item.value === null;
90               });
91               assert.isFunction(deleteItem.tap);
92               deleteItem.tap();
93               assert.equal(model.get("characteristics").models[0].getStatusString(),
94                 "DESTROYED_DIRTY");
95
96               model.on("statusChange", modelResaved);
97               workspaceContainer.apply();
98             }
99           };
100
101           model.on("statusChange", modelSaved);
102           workspaceContainer.apply();
103         });
104       });
105
106
107     });
108     /**
109     * Test INCDT-21540 fix
110     */
111     describe.skip('INCDT-21540: Characteristics appear editable when they shouldn\'t be',
112         function () {
113       var workspaceContainer, workspace, model, id, moduleContainer, originalPrivileges;
114
115       beforeEach(function () {
116         originalPrivileges = _.clone(XT.session.privileges.attributes);
117       });
118
119       it('test Characteristics widgets for enable/disable capability', function (done) {
120         this.timeout(30 * 1000);
121
122         smoke.navigateToExistingWorkspace(XT.app, "XV.IncidentList",
123             function (_workspaceContainer) {
124           workspaceContainer = _workspaceContainer;
125           moduleContainer = XT.app.$.postbooks;
126
127
128           workspace = workspaceContainer.$.workspace;
129           id = workspace.getValue().id;
130           model = workspace.getValue();
131
132           _.each(
133             _(workspaceContainer.$.workspace.$).filter(function (component) {
134               return component.name.indexOf("Characteristic") !== -1;
135             }),
136             function (characteristic) {
137               assert.isDefined(characteristic.getDisabled);
138               assert.isFalse(characteristic.getDisabled());
139             }
140           );
141           done();
142         });
143       });
144       it('test with insufficent permissions: fields should be disabled', function (done) {
145         this.timeout(30 * 1000);
146
147         // revoke permissions
148         _.each(XT.session.privileges.attributes, function (value, privilege) {
149           if (/Maintain/.test(privilege) && /Incident/.test(privilege)) {
150             XT.session.privileges.attributes[privilege] = false;
151           }
152         });
153
154         smoke.navigateToExistingWorkspace(XT.app, "XV.IncidentList",
155             function (_workspaceContainer) {
156           workspaceContainer = _workspaceContainer;
157           moduleContainer = XT.app.$.postbooks;
158
159           workspace = workspaceContainer.$.workspace;
160           id = workspace.getValue().id;
161           model = workspace.getValue();
162
163           var interval = setInterval(function () {
164             // XXX this busy-guard should be folded into smoke
165             if (!workspaceContainer.hasNode()) { return; }
166
167             clearInterval(interval);
168             _.each(
169               _(workspaceContainer.$.workspace.$).filter(function (component) {
170                 return component.name.indexOf("Characteristic") !== -1;
171               }),
172               function (characteristic) {
173                 assert.isDefined(characteristic.getDisabled);
174                 assert.isTrue(
175                   characteristic.getDisabled(),
176                   "CharacteristicsWidget should be disabled."
177                 );
178
179                 // Verify that the "New" button is disabled.
180                 assert.isTrue(
181                   characteristic.$.newButton.getDisabled(),
182                   "Characteristic 'New' button should be disabled.");
183
184                 // Verify that each CharacteristicItem is disabled.
185                 _.each(
186                   characteristic.$.repeater.controls,
187                   function (item) {
188                     assert.isTrue(
189                       item.$.characteristicItem.getDisabled(),
190                       item.$.characteristicItem.id + " should be disabled."
191                     );
192                   }
193                 );
194               }
195             );
196             done();
197           }, 100);
198         });
199
200       });
201       afterEach(function (done) {
202         this.timeout(30 * 1000);
203
204         // restore permissions
205         _.extend(XT.session.privileges.attributes, originalPrivileges);
206
207         // maybe one of the tests already released the lock
208         if (!model.hasLockKey()) {
209           done();
210           return;
211         }
212         model.on("lockChange", function () {
213           model.off("lockChange");
214           assert.isFalse(model.hasLockKey());
215
216           setTimeout(function () {
217             done();
218           }, 5000);
219         });
220         workspaceContainer.close();
221       });
222     });
223   });
224
225 }());