apiTest.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. 'Content-Type': 'application/json',
  27. 'X-Api-Key': 'invalid'
  28. }
  29. }, function(error, response, body) {
  30. if (!error && response.statusCode === 401) {
  31. done();
  32. } else {
  33. done(error || response.statusCode);
  34. }
  35. });
  36. });
  37. it('should fail without an URL when asynchronous', function(done) {
  38. this.timeout(15000);
  39. request({
  40. method: 'POST',
  41. url: serverUrl + '/api/runs',
  42. body: {
  43. url: '',
  44. waitForResponse: true
  45. },
  46. json: true,
  47. headers: {
  48. 'Content-Type': 'application/json',
  49. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  50. }
  51. }, function(error, response, body) {
  52. if (!error && response.statusCode === 400) {
  53. done();
  54. } else {
  55. done(error || response.statusCode);
  56. }
  57. });
  58. });
  59. it('should fail without an URL when synchronous', function(done) {
  60. this.timeout(15000);
  61. request({
  62. method: 'POST',
  63. url: serverUrl + '/api/runs',
  64. body: {
  65. url: '',
  66. waitForResponse: true
  67. },
  68. json: true,
  69. headers: {
  70. 'Content-Type': 'application/json',
  71. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  72. }
  73. }, function(error, response, body) {
  74. if (!error && response.statusCode === 400) {
  75. done();
  76. } else {
  77. done(error || response.statusCode);
  78. }
  79. });
  80. });
  81. it('should launch a synchronous run', function(done) {
  82. this.timeout(15000);
  83. request({
  84. method: 'POST',
  85. url: serverUrl + '/api/runs',
  86. body: {
  87. url: wwwUrl + '/simple-page.html',
  88. waitForResponse: true,
  89. screenshot: true,
  90. device: 'tablet',
  91. //waitForSelector: '*',
  92. cookie: 'foo=bar;domain=google.com',
  93. authUser: 'joe',
  94. authPass: 'secret'
  95. },
  96. json: true,
  97. headers: {
  98. 'Content-Type': 'application/json',
  99. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  100. }
  101. }, function(error, response, body) {
  102. if (!error && response.statusCode === 302) {
  103. response.headers.should.have.a.property('location').that.is.a('string');
  104. syncRunResultUrl = response.headers.location;
  105. done();
  106. } else {
  107. done(error || response.statusCode);
  108. }
  109. });
  110. });
  111. it('should return the rules only', function(done) {
  112. this.timeout(15000);
  113. request({
  114. method: 'POST',
  115. url: serverUrl + '/api/runs',
  116. body: {
  117. url: wwwUrl + '/simple-page.html',
  118. waitForResponse: true,
  119. partialResult: 'rules'
  120. },
  121. json: true,
  122. headers: {
  123. 'Content-Type': 'application/json',
  124. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  125. }
  126. }, function(error, response, body) {
  127. if (!error && response.statusCode === 302) {
  128. response.headers.should.have.a.property('location').that.is.a('string');
  129. response.headers.location.should.contain('/rules');
  130. done();
  131. } else {
  132. done(error || response.statusCode);
  133. }
  134. });
  135. });
  136. it('should retrieve the results for the synchronous run', function(done) {
  137. this.timeout(15000);
  138. request({
  139. method: 'GET',
  140. url: serverUrl + syncRunResultUrl,
  141. json: true,
  142. }, function(error, response, body) {
  143. if (!error && response.statusCode === 200) {
  144. body.should.have.a.property('runId').that.is.a('string');
  145. body.should.have.a.property('params').that.is.an('object');
  146. body.should.have.a.property('scoreProfiles').that.is.an('object');
  147. body.should.have.a.property('rules').that.is.an('object');
  148. body.should.have.a.property('toolsResults').that.is.an('object');
  149. // Check if settings are correctly sent and retrieved
  150. body.params.options.should.have.a.property('device').that.equals('tablet');
  151. //body.params.options.should.have.a.property('waitForSelector').that.equals('*');
  152. body.params.options.should.have.a.property('cookie').that.equals('foo=bar;domain=google.com');
  153. body.params.options.should.have.a.property('authUser').that.equals('joe');
  154. body.params.options.should.have.a.property('authPass').that.equals('secret');
  155. // Check if the screenshot temporary file was correctly removed
  156. body.params.options.should.not.have.a.property('screenshot');
  157. // Check if the screenshot buffer was correctly removed
  158. body.should.not.have.a.property('screenshotBuffer');
  159. // Check if the screenshot url is here
  160. body.should.have.a.property('screenshotUrl');
  161. body.screenshotUrl.should.equal('api/results/' + body.runId + '/screenshot.jpg');
  162. screenshotUrl = body.screenshotUrl;
  163. done();
  164. } else {
  165. done(error || response.statusCode);
  166. }
  167. });
  168. });
  169. it('should launch a run without waiting for the response', function(done) {
  170. this.timeout(5000);
  171. request({
  172. method: 'POST',
  173. url: serverUrl + '/api/runs',
  174. body: {
  175. url: wwwUrl + '/simple-page.html',
  176. waitForResponse: false,
  177. jsTimeline: true
  178. },
  179. json: true,
  180. headers: {
  181. 'Content-Type': 'application/json',
  182. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  183. }
  184. }, function(error, response, body) {
  185. if (!error && response.statusCode === 200) {
  186. asyncRunId = body.runId;
  187. asyncRunId.should.be.a('string');
  188. done();
  189. } else {
  190. done(error || response.statusCode);
  191. }
  192. });
  193. });
  194. it('should respond run status: running', function(done) {
  195. this.timeout(5000);
  196. request({
  197. method: 'GET',
  198. url: serverUrl + '/api/runs/' + asyncRunId,
  199. json: true,
  200. headers: {
  201. 'Content-Type': 'application/json',
  202. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  203. }
  204. }, function(error, response, body) {
  205. if (!error && response.statusCode === 200) {
  206. body.runId.should.equal(asyncRunId);
  207. body.status.should.deep.equal({
  208. statusCode: 'running'
  209. });
  210. done();
  211. } else {
  212. done(error || response.statusCode);
  213. }
  214. });
  215. });
  216. it('should accept up to 10 anonymous runs to the API', function(done) {
  217. this.timeout(5000);
  218. function launchRun() {
  219. var deferred = Q.defer();
  220. request({
  221. method: 'POST',
  222. url: serverUrl + '/api/runs',
  223. body: {
  224. url: wwwUrl + '/simple-page.html',
  225. waitForResponse: false
  226. },
  227. json: true
  228. }, function(error, response, body) {
  229. lastRunId = body.runId;
  230. if (error) {
  231. deferred.reject(error);
  232. } else {
  233. deferred.resolve(response, body);
  234. }
  235. });
  236. return deferred.promise;
  237. }
  238. launchRun()
  239. .then(launchRun)
  240. .then(launchRun)
  241. .then(launchRun)
  242. .then(launchRun)
  243. .then(function(response, body) {
  244. // Here should still be ok
  245. response.statusCode.should.equal(200);
  246. launchRun()
  247. .then(launchRun)
  248. .then(launchRun)
  249. .then(launchRun)
  250. .then(launchRun)
  251. .then(launchRun)
  252. .then(function(response, body) {
  253. // It should fail now
  254. response.statusCode.should.equal(429);
  255. done();
  256. })
  257. .fail(function(error) {
  258. done(error);
  259. });
  260. }).fail(function(error) {
  261. done(error);
  262. });
  263. });
  264. it('should respond 404 to unknown runId', function(done) {
  265. this.timeout(5000);
  266. request({
  267. method: 'GET',
  268. url: serverUrl + '/api/runs/unknown',
  269. json: true
  270. }, function(error, response, body) {
  271. if (!error && response.statusCode === 404) {
  272. done();
  273. } else {
  274. done(error || response.statusCode);
  275. }
  276. });
  277. });
  278. it('should respond 404 to unknown result', function(done) {
  279. this.timeout(5000);
  280. request({
  281. method: 'GET',
  282. url: serverUrl + '/api/results/unknown',
  283. json: true
  284. }, function(error, response, body) {
  285. if (!error && response.statusCode === 404) {
  286. done();
  287. } else {
  288. done(error || response.statusCode);
  289. }
  290. });
  291. });
  292. it('should respond status complete to the first run', function(done) {
  293. this.timeout(12000);
  294. function checkStatus() {
  295. request({
  296. method: 'GET',
  297. url: serverUrl + '/api/runs/' + asyncRunId,
  298. json: true
  299. }, function(error, response, body) {
  300. if (!error && response.statusCode === 200) {
  301. body.runId.should.equal(asyncRunId);
  302. if (body.status.statusCode === 'running') {
  303. setTimeout(checkStatus, 250);
  304. } else if (body.status.statusCode === 'complete') {
  305. done();
  306. } else {
  307. done(body.status.statusCode);
  308. }
  309. } else {
  310. done(error || response.statusCode);
  311. }
  312. });
  313. }
  314. checkStatus();
  315. });
  316. it('should find the result of the async run', function(done) {
  317. this.timeout(5000);
  318. request({
  319. method: 'GET',
  320. url: serverUrl + '/api/results/' + asyncRunId,
  321. json: true,
  322. }, function(error, response, body) {
  323. if (!error && response.statusCode === 200) {
  324. body.should.have.a.property('runId').that.equals(asyncRunId);
  325. body.should.have.a.property('params').that.is.an('object');
  326. body.params.url.should.equal(wwwUrl + '/simple-page.html');
  327. body.should.have.a.property('scoreProfiles').that.is.an('object');
  328. body.scoreProfiles.should.have.a.property('generic').that.is.an('object');
  329. body.scoreProfiles.generic.should.have.a.property('globalScore').that.is.a('number');
  330. body.scoreProfiles.generic.should.have.a.property('categories').that.is.an('object');
  331. body.should.have.a.property('rules').that.is.an('object');
  332. body.should.have.a.property('toolsResults').that.is.an('object');
  333. body.toolsResults.should.have.a.property('phantomas').that.is.an('object');
  334. done();
  335. } else {
  336. done(error || response.statusCode);
  337. }
  338. });
  339. });
  340. it('should return the generic score object', function(done) {
  341. this.timeout(5000);
  342. request({
  343. method: 'GET',
  344. url: serverUrl + '/api/results/' + asyncRunId + '/generalScores',
  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 return the generic score object also', function(done) {
  357. this.timeout(5000);
  358. request({
  359. method: 'GET',
  360. url: serverUrl + '/api/results/' + asyncRunId + '/generalScores/generic',
  361. json: true,
  362. }, function(error, response, body) {
  363. if (!error && response.statusCode === 200) {
  364. body.should.have.a.property('globalScore').that.is.a('number');
  365. body.should.have.a.property('categories').that.is.an('object');
  366. done();
  367. } else {
  368. done(error || response.statusCode);
  369. }
  370. });
  371. });
  372. it('should not find an unknown score object', function(done) {
  373. this.timeout(5000);
  374. request({
  375. method: 'GET',
  376. url: serverUrl + '/api/results/' + asyncRunId + '/generalScores/unknown',
  377. json: true,
  378. }, function(error, response, body) {
  379. if (!error && response.statusCode === 404) {
  380. done();
  381. } else {
  382. done(error || response.statusCode);
  383. }
  384. });
  385. });
  386. it('should return the rules', function(done) {
  387. this.timeout(5000);
  388. request({
  389. method: 'GET',
  390. url: serverUrl + '/api/results/' + asyncRunId + '/rules',
  391. json: true,
  392. }, function(error, response, body) {
  393. if (!error && response.statusCode === 200) {
  394. var firstRule = body[Object.keys(body)[0]];
  395. firstRule.should.have.a.property('policy').that.is.an('object');
  396. firstRule.should.have.a.property('value').that.is.a('number');
  397. firstRule.should.have.a.property('bad').that.is.a('boolean');
  398. firstRule.should.have.a.property('abnormal').that.is.a('boolean');
  399. firstRule.should.have.a.property('score').that.is.a('number');
  400. firstRule.should.have.a.property('abnormalityScore').that.is.a('number');
  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 return the entire object and exclude toolsResults', function(done) {
  424. this.timeout(5000);
  425. request({
  426. method: 'GET',
  427. url: serverUrl + '/api/results/' + asyncRunId + '?exclude=toolsResults',
  428. json: true,
  429. }, function(error, response, body) {
  430. if (!error && response.statusCode === 200) {
  431. body.should.have.a.property('runId').that.equals(asyncRunId);
  432. body.should.have.a.property('params').that.is.an('object');
  433. body.should.have.a.property('scoreProfiles').that.is.an('object');
  434. body.should.have.a.property('rules').that.is.an('object');
  435. body.should.not.have.a.property('toolsResults').that.is.an('object');
  436. done();
  437. } else {
  438. done(error || response.statusCode);
  439. }
  440. });
  441. });
  442. it('should return the entire object and exclude params and toolsResults', function(done) {
  443. this.timeout(5000);
  444. request({
  445. method: 'GET',
  446. url: serverUrl + '/api/results/' + asyncRunId + '?exclude=toolsResults,params',
  447. json: true,
  448. }, function(error, response, body) {
  449. if (!error && response.statusCode === 200) {
  450. body.should.have.a.property('runId').that.equals(asyncRunId);
  451. body.should.have.a.property('scoreProfiles').that.is.an('object');
  452. body.should.have.a.property('rules').that.is.an('object');
  453. body.should.not.have.a.property('params').that.is.an('object');
  454. body.should.not.have.a.property('toolsResults').that.is.an('object');
  455. done();
  456. } else {
  457. done(error || response.statusCode);
  458. }
  459. });
  460. });
  461. it('should return the entire object and don\'t exclude anything', function(done) {
  462. this.timeout(5000);
  463. request({
  464. method: 'GET',
  465. url: serverUrl + '/api/results/' + asyncRunId + '?exclude=',
  466. json: true,
  467. }, function(error, response, body) {
  468. if (!error && response.statusCode === 200) {
  469. body.should.have.a.property('runId').that.equals(asyncRunId);
  470. body.should.have.a.property('scoreProfiles').that.is.an('object');
  471. body.should.have.a.property('rules').that.is.an('object');
  472. body.should.have.a.property('params').that.is.an('object');
  473. body.should.have.a.property('toolsResults').that.is.an('object');
  474. done();
  475. } else {
  476. done(error || response.statusCode);
  477. }
  478. });
  479. });
  480. it('should return the entire object and don\'t exclude anything', function(done) {
  481. this.timeout(5000);
  482. request({
  483. method: 'GET',
  484. url: serverUrl + '/api/results/' + asyncRunId + '?exclude=null',
  485. json: true,
  486. }, function(error, response, body) {
  487. if (!error && response.statusCode === 200) {
  488. body.should.have.a.property('runId').that.equals(asyncRunId);
  489. body.should.have.a.property('scoreProfiles').that.is.an('object');
  490. body.should.have.a.property('rules').that.is.an('object');
  491. body.should.have.a.property('params').that.is.an('object');
  492. body.should.have.a.property('toolsResults').that.is.an('object');
  493. done();
  494. } else {
  495. done(error || response.statusCode);
  496. }
  497. });
  498. });
  499. it('should retrieve the screenshot', function(done) {
  500. this.timeout(5000);
  501. request({
  502. method: 'GET',
  503. url: serverUrl + '/' + screenshotUrl
  504. }, function(error, response, body) {
  505. if (!error && response.statusCode === 200) {
  506. response.headers['content-type'].should.equal('image/jpeg');
  507. done();
  508. } else {
  509. done(error || response.statusCode);
  510. }
  511. });
  512. });
  513. it('should fail on a unexistant screenshot', function(done) {
  514. this.timeout(5000);
  515. request({
  516. method: 'GET',
  517. url: serverUrl + '/api/results/000000/screenshot.jpg'
  518. }, function(error, response, body) {
  519. if (!error && response.statusCode === 404) {
  520. done();
  521. } else {
  522. done(error || response.statusCode);
  523. }
  524. });
  525. });
  526. it('should refuse a query on a blocked Url', function(done) {
  527. this.timeout(5000);
  528. request({
  529. method: 'POST',
  530. url: serverUrl + '/api/runs',
  531. body: {
  532. url: 'http://www.test.com/something.html',
  533. waitForResponse: false
  534. },
  535. json: true,
  536. headers: {
  537. 'Content-Type': 'application/json',
  538. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  539. }
  540. }, function(error, response, body) {
  541. if (!error && response.statusCode === 403) {
  542. done();
  543. } else {
  544. done(error || response.statusCode);
  545. }
  546. });
  547. });
  548. });