Issue #24544:display posted date rather than create date
[xtuple] / test / lib / zombie_auth.js
1 /*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
2 regexp:true, undef:true, strict:true, trailing:true, white:true */
3 /*global XT:true, XM:true, XV:true, XZ:true, enyo:true, XG:true */
4
5 // global objects
6 enyo = {};
7 XT = {};
8 XG = {};
9 XM = {};
10 XV = {};
11 XZ = {}; // xTuple Zombie. Used to help zombie within the context of these tests.
12
13 var assert = require('assert'),
14   zombie = require('zombie'),
15   URL = require('url'),
16   _ = require('underscore');
17
18
19 /**
20 Simplest possible usage:
21
22   var zombieTest = require('./zombie_auth');
23   zombieTest.testLoad();
24 */
25 (function () {
26   "use strict";
27
28   var secondsToWait = 40;
29
30   /**
31     Loads up the xTuple environment and makes the global variables globally available.
32
33     The first three options are optional, but if omitted then the login data should be
34     available in the /test/lib/loginData.js file.
35
36     There is one important limitation to this code at the moment: the client-side
37     app must be built. (Going in through debug.html won't work).
38
39     @param {Object} options
40     @param {String} options.username
41     @param {String} options.password
42     @param {String} options.host
43     @param {Boolean} options.verbose
44     @param {Function} options.callback. This function will be called with the zombie browser
45       as a parameter if the app loads up successfully.
46
47     Supported signatures are:
48     loadApp(callback);
49     loadApp({username: "admin", password: "somenew", callback: callback});
50     loadApp({callback: callback, verbose: true});
51   */
52   var loadApp = exports.loadApp = function (options) {
53     options = options || {};
54
55     var username = options.username,
56       password = options.password,
57       database = options.database,
58       host = options.host,
59       callback = options.callback,
60       verboseMode = options.verbose,
61       loginData;
62
63     //
64     // Handle multiple signatures
65     //
66     if (typeof arguments[0] === 'function') {
67       // if the sole parameter is the callback, then we get the auth data from a file
68       callback = arguments[0];
69     }
70
71     if (!username || !password) {
72       try {
73         loginData = require(options.loginDataPath || './login_data');
74       } catch (err) {
75         console.log("Make sure you put your login credentials in the /test/lib/login_data.js file");
76         process.exit(1);
77       }
78
79       username = loginData.data.username;
80       password = loginData.data.pwd;
81       database = loginData.data.org;
82       host = loginData.data.webaddress;
83     }
84     host = host || "https://localhost:443";
85
86     if (options.refreshLogin) {
87       enyo = {};
88       XT = {};
89       XM = {};
90       XV = {};
91       XZ = {};
92     }
93
94     // when we run all our tests we only want to have to log in for the first one
95     if (XT.app) {
96       if (verboseMode) {
97         console.log("Using pre-existing zombie session");
98       }
99       callback();
100       return;
101     }
102
103     var parse = URL.parse;
104     URL.parse = function (url) {
105       if (_.isObject(url) && _.isString(url.href)) {
106         return parse(url.href);
107       }
108       else {
109         return parse(url);
110       }
111     };
112
113     zombie.visit(host, {debug: verboseMode}, function (e, browser) {
114       if (e) {
115         //console.log("Zombie visit error: ", e);
116       }
117       //
118       // This is the login screen
119       //
120       browser
121         .fill('id', username)
122         .fill('password', password)
123         .select('database', database)
124         .pressButton('submit', function () {
125
126           // Note: make sure the app is built
127           // XXX this limitation should be fixed, to allow testing off of debug.html
128           // it's possible that Zombie 2.0 will get this right.
129
130           //
131           // Plan to give up after a set time
132           //
133           var timeout = setTimeout(function () {
134               console.log("App did not fully load");
135               process.exit(1);
136             }, secondsToWait * 1000);
137
138           //
139           // Check frequently to see if the app is loaded, and move forward when it is
140           //
141           var interval = setInterval(function () {
142
143             if (browser.window.XT && browser.window.XT.app && browser.window.XT.app.state === 6) {
144
145               // add the global objects to our global namespace
146               enyo = browser.window.enyo;
147               XG = browser.window.XG;
148               XM = browser.window.XM;
149               XT = browser.window.XT;
150               XV = browser.window.XV;
151               XZ.browser = browser;
152               XZ.host = host;
153               XZ.database = database;
154
155               XT.log = function (message, obj) {
156                 if (message && message.toLowerCase().indexOf("error") === 0) {
157                   // errors from the datasource should cause the test to fail
158                   assert.fail(message + " " + JSON.stringify(obj));
159                 }
160                 // log if verbose mode or if the log is a warning
161                 if (verboseMode || (message && message.code)) {
162                   console.log(JSON.stringify(arguments));
163                 }
164               };
165
166               /*
167               var oldNotify = XT.app.$.postbooks.notify;
168               XT.app.$.postbooks.notify = function (notifySender, notifyObj) {
169                 if (notifyObj && notifyObj.type === XM.Model.CRITICAL) {
170                   assert.fail(JSON.stringify(notifyObj));
171                 } else {
172                   oldNotify(notifySender, notifyObj);
173                 }
174               };
175               */
176               // WIP. Not yet working. Probably need to move it up to earlier app start status.
177               /*
178               var oldLoc = XT.String.loc;
179               XT.String.loc = function (str) {
180                 var localized = XT.localizeString(str);
181                 if (localized === str) {
182                   assert.fail(str + " has no translation");
183                 } else {
184                   oldLoc(str);
185                 }
186               };
187               */
188
189               // these are really annoying
190               browser.window.Backbone.Relational.showWarnings = false;
191
192               // clear out both is interval and the I'm-giving-up timeout
193               // we really want neither to be run again.
194               clearInterval(interval);
195               clearTimeout(timeout);
196
197               // give control back to whoever called us
198               callback();
199             }
200           }, 100); // 100 = check to see if the app is loaded every 0.1 seconds
201         });
202     });
203   };
204
205   /**
206     More of a proof-of-concept than anything else.
207    */
208   var testLoad = exports.testLoad = function () {
209     console.log("Testing loadup of app.");
210
211     loadApp(function () {
212       console.log("App loaded successfully.");
213       process.exit(0);
214     });
215   };
216
217 }());