apiTest.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var should = require('chai').should();
  2. var request = require('request');
  3. var jwt = require('jwt-simple');
  4. var config = {
  5. "authorizedKeys": {
  6. "1234567890": "test@test.com"
  7. },
  8. "tokenSalt": "test-salt",
  9. "authorizedApplications": ["wooot"]
  10. };
  11. var apiUrl = 'http://localhost:8387/api';
  12. var wwwUrl = 'http://localhost:8388';
  13. describe('api', function() {
  14. var runId;
  15. it('should not accept a query if there is no key in headers', function(done) {
  16. this.timeout(5000);
  17. request({
  18. method: 'POST',
  19. url: apiUrl + '/runs',
  20. body: {
  21. url: wwwUrl + '/simple-page.html',
  22. waitForResponse: false
  23. },
  24. json: true
  25. }, function(error, response, body) {
  26. if (!error && response.statusCode === 401) {
  27. done();
  28. } else {
  29. done(error || response.statusCode);
  30. }
  31. });
  32. });
  33. it('should refuse a query with an invalid key', function(done) {
  34. this.timeout(5000);
  35. request({
  36. method: 'POST',
  37. url: apiUrl + '/runs',
  38. body: {
  39. url: wwwUrl + '/simple-page.html',
  40. waitForResponse: false
  41. },
  42. json: true,
  43. headers: {
  44. 'X-Api-Key': 'invalid'
  45. }
  46. }, function(error, response, body) {
  47. if (!error && response.statusCode === 401) {
  48. done();
  49. } else {
  50. done(error || response.statusCode);
  51. }
  52. });
  53. });
  54. it('should accept a query with a valid key', function(done) {
  55. this.timeout(5000);
  56. request({
  57. method: 'POST',
  58. url: apiUrl + '/runs',
  59. body: {
  60. url: wwwUrl + '/simple-page.html',
  61. waitForResponse: false
  62. },
  63. json: true,
  64. headers: {
  65. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  66. }
  67. }, function(error, response, body) {
  68. if (!error && response.statusCode === 200) {
  69. runId = body.runId;
  70. runId.should.be.a('string');
  71. done();
  72. } else {
  73. done(error || response.statusCode);
  74. }
  75. });
  76. });
  77. it('should refuse an expired token', function(done) {
  78. this.timeout(5000);
  79. request({
  80. method: 'POST',
  81. url: apiUrl + '/runs',
  82. body: {
  83. url: wwwUrl + '/simple-page.html',
  84. waitForResponse: false
  85. },
  86. json: true,
  87. headers: {
  88. 'X-Api-Token': jwt.encode({
  89. application: config.authorizedApplications[0],
  90. expire: Date.now() - 60000
  91. }, config.tokenSalt)
  92. }
  93. }, function(error, response, body) {
  94. if (!error && response.statusCode === 401) {
  95. done();
  96. } else {
  97. done(error || response.statusCode);
  98. }
  99. });
  100. });
  101. it('should refuse a token from an unknown app', function(done) {
  102. this.timeout(5000);
  103. request({
  104. method: 'POST',
  105. url: apiUrl + '/runs',
  106. body: {
  107. url: wwwUrl + '/simple-page.html',
  108. waitForResponse: false
  109. },
  110. json: true,
  111. headers: {
  112. 'X-Api-Token': jwt.encode({
  113. application: 'unknown-app',
  114. expire: Date.now() + 60000
  115. }, config.tokenSalt)
  116. }
  117. }, function(error, response, body) {
  118. if (!error && response.statusCode === 401) {
  119. done();
  120. } else {
  121. done(error || response.statusCode);
  122. }
  123. });
  124. });
  125. it('should accept a good token', function(done) {
  126. this.timeout(5000);
  127. request({
  128. method: 'POST',
  129. url: apiUrl + '/runs',
  130. body: {
  131. url: wwwUrl + '/simple-page.html',
  132. waitForResponse: false
  133. },
  134. json: true,
  135. headers: {
  136. 'X-Api-Token': jwt.encode({
  137. application: config.authorizedApplications[0],
  138. expire: Date.now() + 60000
  139. }, config.tokenSalt)
  140. }
  141. }, function(error, response, body) {
  142. if (!error && response.statusCode === 200) {
  143. runId = body.runId;
  144. runId.should.be.a('string');
  145. done();
  146. } else {
  147. done(error || response.statusCode);
  148. }
  149. });
  150. });
  151. });