WebApiManagerTests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon');
  13. const chai = require('chai');
  14. const should = chai.should();
  15. const { expect } = chai;
  16. const modulePath = "../../../../app/js/WebApiManager.js";
  17. const SandboxedModule = require('sandboxed-module');
  18. describe("WebApiManager", function() {
  19. beforeEach(function() {
  20. this.WebApiManager = SandboxedModule.require(modulePath, { requires: {
  21. "requestretry": (this.request = {}),
  22. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub() }),
  23. 'settings-sharelatex': (this.settings = {
  24. apis: {
  25. web: {
  26. url: "http://example.com",
  27. user: "sharelatex",
  28. pass: "password"
  29. }
  30. }
  31. })
  32. }
  33. }
  34. );
  35. this.callback = sinon.stub();
  36. this.user_id = "mock-user-id";
  37. this.project_id = "mock-project-id";
  38. this.user_info = {
  39. email: "leo@sharelatex.com",
  40. id: this.user_id,
  41. first_name: "Leo",
  42. last_nane: "Lion",
  43. extra_param: "blah"
  44. };
  45. return this.project =
  46. {features: "mock-features"};
  47. });
  48. describe("getUserInfo", function() {
  49. describe("successfully", function() {
  50. beforeEach(function() {
  51. this.body = JSON.stringify(this.user_info);
  52. this.request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, this.body);
  53. return this.WebApiManager.getUserInfo(this.user_id, this.callback);
  54. });
  55. it('should get the user from the web api', function() {
  56. return this.request.get
  57. .calledWithMatch({
  58. url: `${this.settings.apis.web.url}/user/${this.user_id}/personal_info`,
  59. auth: {
  60. user: this.settings.apis.web.user,
  61. pass: this.settings.apis.web.pass,
  62. sendImmediately: true
  63. }
  64. })
  65. .should.equal(true);
  66. });
  67. return it("should call the callback with only the email, id and names", function() {
  68. return this.callback.calledWith(null, {
  69. id: this.user_id,
  70. email: this.user_info.email,
  71. first_name: this.user_info.first_name,
  72. last_name: this.user_info.last_name
  73. }).should.equal(true);
  74. });
  75. });
  76. describe("when the web API returns an error", function() {
  77. beforeEach(function() {
  78. this.request.get = sinon.stub().callsArgWith(1, (this.error = new Error("something went wrong")), null, null);
  79. return this.WebApiManager.getUserInfo(this.user_id, this.callback);
  80. });
  81. return it("should return an error to the callback", function() {
  82. return this.callback.calledWith(this.error).should.equal(true);
  83. });
  84. });
  85. describe("when the web returns a failure error code", function() {
  86. beforeEach(function() {
  87. this.request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500, attempts: 42}, "");
  88. return this.WebApiManager.getUserInfo(this.user_id, this.callback);
  89. });
  90. return it("should return the callback with an error", function() {
  91. return this.callback
  92. .calledWith(sinon.match.has('message', "web returned a non-success status code: 500 (attempts: 42)"))
  93. .should.equal(true);
  94. });
  95. });
  96. return describe("when the user cannot be found", function() {
  97. beforeEach(function() {
  98. this.request.get = sinon.stub().callsArgWith(1, null, {statusCode: 404}, "nothing");
  99. return this.WebApiManager.getUserInfo(this.user_id, this.callback);
  100. });
  101. return it("should return a null value", function() {
  102. return this.callback
  103. .calledWith(null, null)
  104. .should.equal(true);
  105. });
  106. });
  107. });
  108. return describe("getProjectDetails", function() {
  109. describe("successfully", function() {
  110. beforeEach(function() {
  111. this.body = JSON.stringify(this.project);
  112. this.request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, this.body);
  113. return this.WebApiManager.getProjectDetails(this.project_id, this.callback);
  114. });
  115. it('should get the project from the web api', function() {
  116. return this.request.get
  117. .calledWithMatch({
  118. url: `${this.settings.apis.web.url}/project/${this.project_id}/details`,
  119. auth: {
  120. user: this.settings.apis.web.user,
  121. pass: this.settings.apis.web.pass,
  122. sendImmediately: true
  123. }
  124. })
  125. .should.equal(true);
  126. });
  127. return it("should call the callback with the project", function() {
  128. return this.callback.calledWith(null, this.project).should.equal(true);
  129. });
  130. });
  131. describe("when the web API returns an error", function() {
  132. beforeEach(function() {
  133. this.request.get = sinon.stub().callsArgWith(1, (this.error = new Error("something went wrong")), null, null);
  134. return this.WebApiManager.getProjectDetails(this.project_id, this.callback);
  135. });
  136. return it("should return an error to the callback", function() {
  137. return this.callback.calledWith(this.error).should.equal(true);
  138. });
  139. });
  140. return describe("when the web returns a failure error code", function() {
  141. beforeEach(function() {
  142. this.request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500, attempts: 42 }, "");
  143. return this.WebApiManager.getProjectDetails(this.project_id, this.callback);
  144. });
  145. return it("should return the callback with an error", function() {
  146. return this.callback
  147. .calledWith(sinon.match.has('message', "web returned a non-success status code: 500 (attempts: 42)"))
  148. .should.equal(true);
  149. });
  150. });
  151. });
  152. });