HttpControllerTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* eslint-disable
  2. mocha/no-pending-tests,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon');
  14. const chai = require('chai');
  15. const should = chai.should();
  16. const {
  17. expect
  18. } = chai;
  19. const modulePath = "../../../app/js/HttpController.js";
  20. const SandboxedModule = require('sandboxed-module');
  21. describe("HttpController", function() {
  22. beforeEach(function() {
  23. this.HttpController = SandboxedModule.require(modulePath, { requires: {
  24. "./ContactManager": (this.ContactManager = {}),
  25. "logger-sharelatex": (this.logger = { log: sinon.stub() })
  26. }
  27. });
  28. this.user_id = "mock-user-id";
  29. this.contact_id = "mock-contact-id";
  30. this.req = {};
  31. this.res = {};
  32. this.res.status = sinon.stub().returns(this.res);
  33. this.res.end = sinon.stub();
  34. this.res.send = sinon.stub();
  35. return this.next = sinon.stub();
  36. });
  37. describe("addContact", function() {
  38. beforeEach(function() {
  39. this.req.params =
  40. {user_id: this.user_id};
  41. return this.ContactManager.touchContact = sinon.stub().callsArg(2);
  42. });
  43. describe("with a valid user_id and contact_id", function() {
  44. beforeEach(function() {
  45. this.req.body =
  46. {contact_id: this.contact_id};
  47. return this.HttpController.addContact(this.req, this.res, this.next);
  48. });
  49. it("should update the contact in the user's contact list", function() {
  50. return this.ContactManager.touchContact
  51. .calledWith(this.user_id, this.contact_id)
  52. .should.equal(true);
  53. });
  54. it("should update the user in the contact's contact list", function() {
  55. return this.ContactManager.touchContact
  56. .calledWith(this.contact_id, this.user_id)
  57. .should.equal(true);
  58. });
  59. return it("should send back a 204 status", function() {
  60. this.res.status.calledWith(204).should.equal(true);
  61. return this.res.end.called.should.equal(true);
  62. });
  63. });
  64. return describe("with an invalid contact id", function() {
  65. beforeEach(function() {
  66. this.req.body =
  67. {contact_id: ""};
  68. return this.HttpController.addContact(this.req, this.res, this.next);
  69. });
  70. return it("should return 400, Bad Request", function() {
  71. this.res.status.calledWith(400).should.equal(true);
  72. return this.res.send.calledWith("contact_id should be a non-blank string").should.equal(true);
  73. });
  74. });
  75. });
  76. return describe("getContacts", function() {
  77. beforeEach(function() {
  78. this.req.params =
  79. {user_id: this.user_id};
  80. const now = Date.now();
  81. this.contacts = {
  82. "user-id-1": { n: 2, ts: new Date(now) },
  83. "user-id-2": { n: 4, ts: new Date(now) },
  84. "user-id-3": { n: 2, ts: new Date(now - 1000) }
  85. };
  86. return this.ContactManager.getContacts = sinon.stub().callsArgWith(1, null, this.contacts);
  87. });
  88. describe("normally", function() {
  89. beforeEach(function() {
  90. return this.HttpController.getContacts(this.req, this.res, this.next);
  91. });
  92. it("should look up the contacts in mongo", function() {
  93. return this.ContactManager.getContacts
  94. .calledWith(this.user_id)
  95. .should.equal(true);
  96. });
  97. return it("should return a sorted list of contacts by count and timestamp", function() {
  98. return this.res.send
  99. .calledWith({
  100. contact_ids: [
  101. "user-id-2",
  102. "user-id-1",
  103. "user-id-3"
  104. ]
  105. })
  106. .should.equal(true);
  107. });
  108. });
  109. describe("with more contacts than the limit", function() {
  110. beforeEach(function() {
  111. this.req.query =
  112. {limit: 2};
  113. return this.HttpController.getContacts(this.req, this.res, this.next);
  114. });
  115. return it("should return the most commonly used contacts up to the limit", function() {
  116. return this.res.send
  117. .calledWith({
  118. contact_ids: [
  119. "user-id-2",
  120. "user-id-1"
  121. ]
  122. })
  123. .should.equal(true);
  124. });
  125. });
  126. describe("without a contact list", function() {
  127. beforeEach(function() {
  128. this.ContactManager.getContacts = sinon.stub().callsArgWith(1, null, null);
  129. return this.HttpController.getContacts(this.req, this.res, this.next);
  130. });
  131. return it("should return an empty list", function() {
  132. return this.res.send
  133. .calledWith({
  134. contact_ids: []
  135. })
  136. .should.equal(true);
  137. });
  138. });
  139. return describe("with a holding account", function() { return it("should not return holding accounts"); });
  140. });
  141. });