HttpControllerTests.js 4.3 KB

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