apiTest.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. var should = require('chai').should();
  2. var request = require('request');
  3. var Q = require('q');
  4. var config = {
  5. "authorizedKeys": {
  6. "1234567890": "contact@gaelmetais.com"
  7. }
  8. };
  9. var serverUrl = 'http://localhost:8387';
  10. var wwwUrl = 'http://localhost:8388';
  11. describe('api', function() {
  12. var syncRunResultUrl;
  13. var asyncRunId;
  14. var screenshotUrl;
  15. it('should refuse a query with an invalid key', function(done) {
  16. this.timeout(5000);
  17. request({
  18. method: 'POST',
  19. url: serverUrl + '/api/runs',
  20. body: {
  21. url: wwwUrl + '/simple-page.html',
  22. waitForResponse: false
  23. },
  24. json: true,
  25. headers: {
  26. 'X-Api-Key': 'invalid'
  27. }
  28. }, function(error, response, body) {
  29. if (!error && response.statusCode === 401) {
  30. done();
  31. } else {
  32. done(error || response.statusCode);
  33. }
  34. });
  35. });
  36. it('should fail without an URL when asynchronous', function(done) {
  37. this.timeout(15000);
  38. request({
  39. method: 'POST',
  40. url: serverUrl + '/api/runs',
  41. body: {
  42. url: ''
  43. },
  44. json: true,
  45. headers: {
  46. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  47. }
  48. }, function(error, response, body) {
  49. if (!error && response.statusCode === 400) {
  50. done();
  51. } else {
  52. done(error || response.statusCode);
  53. }
  54. });
  55. });
  56. it('should fail without an URL when synchronous', function(done) {
  57. this.timeout(15000);
  58. request({
  59. method: 'POST',
  60. url: serverUrl + '/api/runs',
  61. body: {
  62. url: '',
  63. waitForResponse: true
  64. },
  65. json: true,
  66. headers: {
  67. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  68. }
  69. }, function(error, response, body) {
  70. if (!error && response.statusCode === 400) {
  71. done();
  72. } else {
  73. done(error || response.statusCode);
  74. }
  75. });
  76. });
  77. it('should launch a synchronous run', function(done) {
  78. this.timeout(15000);
  79. request({
  80. method: 'POST',
  81. url: serverUrl + '/api/runs',
  82. body: {
  83. url: wwwUrl + '/simple-page.html',
  84. waitForResponse: true,
  85. screenshot: true
  86. },
  87. json: true,
  88. headers: {
  89. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  90. }
  91. }, function(error, response, body) {
  92. if (!error && response.statusCode === 302) {
  93. response.headers.should.have.a.property('location').that.is.a('string');
  94. syncRunResultUrl = response.headers.location;
  95. done();
  96. } else {
  97. done(error || response.statusCode);
  98. }
  99. });
  100. });
  101. it('should return the rules only', function(done) {
  102. this.timeout(15000);
  103. request({
  104. method: 'POST',
  105. url: serverUrl + '/api/runs',
  106. body: {
  107. url: wwwUrl + '/simple-page.html',
  108. waitForResponse: true,
  109. partialResult: 'rules'
  110. },
  111. json: true,
  112. headers: {
  113. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  114. }
  115. }, function(error, response, body) {
  116. if (!error && response.statusCode === 302) {
  117. response.headers.should.have.a.property('location').that.is.a('string');
  118. response.headers.location.should.contain('/rules');
  119. done();
  120. } else {
  121. done(error || response.statusCode);
  122. }
  123. });
  124. });
  125. it('should retrieve the results for the synchronous run', function(done) {
  126. this.timeout(15000);
  127. request({
  128. method: 'GET',
  129. url: serverUrl + syncRunResultUrl,
  130. json: true,
  131. }, function(error, response, body) {
  132. if (!error && response.statusCode === 200) {
  133. body.should.have.a.property('runId').that.is.a('string');
  134. body.should.have.a.property('params').that.is.an('object');
  135. body.should.have.a.property('scoreProfiles').that.is.an('object');
  136. body.should.have.a.property('rules').that.is.an('object');
  137. body.should.have.a.property('toolsResults').that.is.an('object');
  138. body.should.have.a.property('javascriptExecutionTree').that.is.an('object');
  139. // Check if the screenshot temporary file was correctly removed
  140. body.params.options.should.not.have.a.property('screenshot');
  141. // Check if the screenshot buffer was correctly removed
  142. body.should.not.have.a.property('screenshotBuffer');
  143. // Check if the screenshot url is here
  144. body.should.have.a.property('screenshotUrl');
  145. body.screenshotUrl.should.equal('/api/results/' + body.runId + '/screenshot.jpg');
  146. screenshotUrl = body.screenshotUrl;
  147. done();
  148. } else {
  149. done(error || response.statusCode);
  150. }
  151. });
  152. });
  153. it('should launch a run without waiting for the response', function(done) {
  154. this.timeout(5000);
  155. request({
  156. method: 'POST',
  157. url: serverUrl + '/api/runs',
  158. body: {
  159. url: wwwUrl + '/simple-page.html',
  160. waitForResponse: false
  161. },
  162. json: true,
  163. headers: {
  164. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  165. }
  166. }, function(error, response, body) {
  167. if (!error && response.statusCode === 200) {
  168. asyncRunId = body.runId;
  169. asyncRunId.should.be.a('string');
  170. done();
  171. } else {
  172. done(error || response.statusCode);
  173. }
  174. });
  175. });
  176. it('should respond run status: running', function(done) {
  177. this.timeout(5000);
  178. request({
  179. method: 'GET',
  180. url: serverUrl + '/api/runs/' + asyncRunId,
  181. json: true,
  182. headers: {
  183. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  184. }
  185. }, function(error, response, body) {
  186. if (!error && response.statusCode === 200) {
  187. body.runId.should.equal(asyncRunId);
  188. body.status.should.deep.equal({
  189. statusCode: 'running'
  190. });
  191. done();
  192. } else {
  193. done(error || response.statusCode);
  194. }
  195. });
  196. });
  197. it('should accept up to 10 anonymous runs to the API', function(done) {
  198. this.timeout(5000);
  199. function launchRun() {
  200. var deferred = Q.defer();
  201. request({
  202. method: 'POST',
  203. url: serverUrl + '/api/runs',
  204. body: {
  205. url: wwwUrl + '/simple-page.html',
  206. waitForResponse: false
  207. },
  208. json: true
  209. }, function(error, response, body) {
  210. lastRunId = body.runId;
  211. if (error) {
  212. deferred.reject(error);
  213. } else {
  214. deferred.resolve(response, body);
  215. }
  216. });
  217. return deferred.promise;
  218. }
  219. launchRun()
  220. .then(launchRun)
  221. .then(launchRun)
  222. .then(launchRun)
  223. .then(launchRun)
  224. .then(function(response, body) {
  225. // Here should still be ok
  226. response.statusCode.should.equal(200);
  227. launchRun()
  228. .then(launchRun)
  229. .then(launchRun)
  230. .then(launchRun)
  231. .then(launchRun)
  232. .then(launchRun)
  233. .then(function(response, body) {
  234. // It should fail now
  235. response.statusCode.should.equal(429);
  236. done();
  237. })
  238. .fail(function(error) {
  239. done(error);
  240. });
  241. }).fail(function(error) {
  242. done(error);
  243. });
  244. });
  245. it('should respond 404 to unknown runId', function(done) {
  246. this.timeout(5000);
  247. request({
  248. method: 'GET',
  249. url: serverUrl + '/api/runs/unknown',
  250. json: true
  251. }, function(error, response, body) {
  252. if (!error && response.statusCode === 404) {
  253. done();
  254. } else {
  255. done(error || response.statusCode);
  256. }
  257. });
  258. });
  259. it('should respond 404 to unknown result', function(done) {
  260. this.timeout(5000);
  261. request({
  262. method: 'GET',
  263. url: serverUrl + '/api/results/unknown',
  264. json: true
  265. }, function(error, response, body) {
  266. if (!error && response.statusCode === 404) {
  267. done();
  268. } else {
  269. done(error || response.statusCode);
  270. }
  271. });
  272. });
  273. it('should respond status complete to the first run', function(done) {
  274. this.timeout(12000);
  275. function checkStatus() {
  276. request({
  277. method: 'GET',
  278. url: serverUrl + '/api/runs/' + asyncRunId,
  279. json: true
  280. }, function(error, response, body) {
  281. if (!error && response.statusCode === 200) {
  282. body.runId.should.equal(asyncRunId);
  283. if (body.status.statusCode === 'running') {
  284. setTimeout(checkStatus, 250);
  285. } else if (body.status.statusCode === 'complete') {
  286. done();
  287. } else {
  288. done(body.status.statusCode);
  289. }
  290. } else {
  291. done(error || response.statusCode);
  292. }
  293. });
  294. }
  295. checkStatus();
  296. });
  297. it('should find the result of the async run', function(done) {
  298. this.timeout(5000);
  299. request({
  300. method: 'GET',
  301. url: serverUrl + '/api/results/' + asyncRunId,
  302. json: true,
  303. }, function(error, response, body) {
  304. if (!error && response.statusCode === 200) {
  305. body.should.have.a.property('runId').that.equals(asyncRunId);
  306. body.should.have.a.property('params').that.is.an('object');
  307. body.params.url.should.equal(wwwUrl + '/simple-page.html');
  308. body.should.have.a.property('scoreProfiles').that.is.an('object');
  309. body.scoreProfiles.should.have.a.property('generic').that.is.an('object');
  310. body.scoreProfiles.generic.should.have.a.property('globalScore').that.is.a('number');
  311. body.scoreProfiles.generic.should.have.a.property('categories').that.is.an('object');
  312. body.should.have.a.property('rules').that.is.an('object');
  313. body.should.have.a.property('toolsResults').that.is.an('object');
  314. body.toolsResults.should.have.a.property('phantomas').that.is.an('object');
  315. body.should.have.a.property('javascriptExecutionTree').that.is.an('object');
  316. body.javascriptExecutionTree.should.have.a.property('data').that.is.an('object');
  317. body.javascriptExecutionTree.data.should.have.a.property('type').that.equals('main');
  318. done();
  319. } else {
  320. done(error || response.statusCode);
  321. }
  322. });
  323. });
  324. it('should return the generic score object', function(done) {
  325. this.timeout(5000);
  326. request({
  327. method: 'GET',
  328. url: serverUrl + '/api/results/' + asyncRunId + '/generalScores',
  329. json: true,
  330. }, function(error, response, body) {
  331. if (!error && response.statusCode === 200) {
  332. body.should.have.a.property('globalScore').that.is.a('number');
  333. body.should.have.a.property('categories').that.is.an('object');
  334. done();
  335. } else {
  336. done(error || response.statusCode);
  337. }
  338. });
  339. });
  340. it('should return the generic score object also', function(done) {
  341. this.timeout(5000);
  342. request({
  343. method: 'GET',
  344. url: serverUrl + '/api/results/' + asyncRunId + '/generalScores/generic',
  345. json: true,
  346. }, function(error, response, body) {
  347. if (!error && response.statusCode === 200) {
  348. body.should.have.a.property('globalScore').that.is.a('number');
  349. body.should.have.a.property('categories').that.is.an('object');
  350. done();
  351. } else {
  352. done(error || response.statusCode);
  353. }
  354. });
  355. });
  356. it('should not find an unknown score object', function(done) {
  357. this.timeout(5000);
  358. request({
  359. method: 'GET',
  360. url: serverUrl + '/api/results/' + asyncRunId + '/generalScores/unknown',
  361. json: true,
  362. }, function(error, response, body) {
  363. if (!error && response.statusCode === 404) {
  364. done();
  365. } else {
  366. done(error || response.statusCode);
  367. }
  368. });
  369. });
  370. it('should return the rules', function(done) {
  371. this.timeout(5000);
  372. request({
  373. method: 'GET',
  374. url: serverUrl + '/api/results/' + asyncRunId + '/rules',
  375. json: true,
  376. }, function(error, response, body) {
  377. if (!error && response.statusCode === 200) {
  378. var firstRule = body[Object.keys(body)[0]];
  379. firstRule.should.have.a.property('policy').that.is.an('object');
  380. firstRule.should.have.a.property('value').that.is.a('number');
  381. firstRule.should.have.a.property('bad').that.is.a('boolean');
  382. firstRule.should.have.a.property('abnormal').that.is.a('boolean');
  383. firstRule.should.have.a.property('score').that.is.a('number');
  384. firstRule.should.have.a.property('abnormalityScore').that.is.a('number');
  385. done();
  386. } else {
  387. done(error || response.statusCode);
  388. }
  389. });
  390. });
  391. it('should return the javascript execution tree', function(done) {
  392. this.timeout(5000);
  393. request({
  394. method: 'GET',
  395. url: serverUrl + '/api/results/' + asyncRunId + '/javascriptExecutionTree',
  396. json: true,
  397. }, function(error, response, body) {
  398. if (!error && response.statusCode === 200) {
  399. body.should.have.a.property('data').that.is.an('object');
  400. body.data.should.have.a.property('type').that.equals('main');
  401. done();
  402. } else {
  403. done(error || response.statusCode);
  404. }
  405. });
  406. });
  407. it('should return the phantomas results', function(done) {
  408. this.timeout(5000);
  409. request({
  410. method: 'GET',
  411. url: serverUrl + '/api/results/' + asyncRunId + '/toolsResults/phantomas',
  412. json: true,
  413. }, function(error, response, body) {
  414. if (!error && response.statusCode === 200) {
  415. body.should.have.a.property('metrics').that.is.an('object');
  416. body.should.have.a.property('offenders').that.is.an('object');
  417. done();
  418. } else {
  419. done(error || response.statusCode);
  420. }
  421. });
  422. });
  423. it('should retrieve the screenshot', function(done) {
  424. this.timeout(5000);
  425. request({
  426. method: 'GET',
  427. url: serverUrl + screenshotUrl
  428. }, function(error, response, body) {
  429. if (!error && response.statusCode === 200) {
  430. response.headers['content-type'].should.equal('image/jpeg');
  431. done();
  432. } else {
  433. done(error || response.statusCode);
  434. }
  435. });
  436. });
  437. it('should fail on a unexistant screenshot', function(done) {
  438. this.timeout(5000);
  439. request({
  440. method: 'GET',
  441. url: serverUrl + '/api/results/000000/screenshot.jpg'
  442. }, function(error, response, body) {
  443. if (!error && response.statusCode === 404) {
  444. done();
  445. } else {
  446. done(error || response.statusCode);
  447. }
  448. });
  449. });
  450. });