|
|
@@ -9,6 +9,7 @@ describe "HttpController", ->
|
|
|
beforeEach ->
|
|
|
@HttpController = SandboxedModule.require modulePath, requires:
|
|
|
"./ContactManager": @ContactManager = {}
|
|
|
+ "./WebApiManager": @WebApiManager = {}
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub() }
|
|
|
@user_id = "mock-user-id"
|
|
|
@contact_id = "mock-contact-id"
|
|
|
@@ -55,3 +56,80 @@ describe "HttpController", ->
|
|
|
it "should return 400, Bad Request", ->
|
|
|
@res.status.calledWith(400).should.equal true
|
|
|
@res.send.calledWith("contact_id should be a non-blank string").should.equal true
|
|
|
+
|
|
|
+ describe "getContacts", ->
|
|
|
+ beforeEach ->
|
|
|
+ @req.params =
|
|
|
+ user_id: @user_id
|
|
|
+ now = Date.now()
|
|
|
+ @contacts = {
|
|
|
+ "user-id-1": { n: 2, ts: new Date(now) }
|
|
|
+ "user-id-2": { n: 4, ts: new Date(now) }
|
|
|
+ "user-id-3": { n: 2, ts: new Date(now - 1000) }
|
|
|
+ }
|
|
|
+ @user_details = {
|
|
|
+ "user-id-1": { _id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example", extra: "foo" }
|
|
|
+ "user-id-2": { _id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example", extra: "foo" }
|
|
|
+ "user-id-3": { _id: "user-id-3", email: "sam@example.com", first_name: "Sam", last_name: "Example", extra: "foo" }
|
|
|
+ }
|
|
|
+ @ContactManager.getContacts = sinon.stub().callsArgWith(1, null, @contacts)
|
|
|
+ @WebApiManager.getUserDetails = (user_id, callback = (error, user) ->) =>
|
|
|
+ callback null, @user_details[user_id]
|
|
|
+ sinon.spy @WebApiManager, "getUserDetails"
|
|
|
+
|
|
|
+ describe "normally", ->
|
|
|
+ beforeEach ->
|
|
|
+ @HttpController.getContacts @req, @res, @next
|
|
|
+
|
|
|
+ it "should look up the contacts in mongo", ->
|
|
|
+ @ContactManager.getContacts
|
|
|
+ .calledWith(@user_id)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should look up each contact in web for their details", ->
|
|
|
+ for user_id, data of @contacts
|
|
|
+ @WebApiManager.getUserDetails
|
|
|
+ .calledWith(user_id)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should return a sorted list of contacts by count and timestamp", ->
|
|
|
+ @res.send
|
|
|
+ .calledWith({
|
|
|
+ contacts: [
|
|
|
+ { id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example" }
|
|
|
+ { id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example" }
|
|
|
+ { id: "user-id-3", email: "sam@example.com", first_name: "Sam", last_name: "Example" }
|
|
|
+ ]
|
|
|
+ })
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ describe "with more contacts than the limit", ->
|
|
|
+ beforeEach ->
|
|
|
+ @req.query =
|
|
|
+ limit: 2
|
|
|
+ @HttpController.getContacts @req, @res, @next
|
|
|
+
|
|
|
+ it "should return the most commonly used contacts up to the limit", ->
|
|
|
+ @res.send
|
|
|
+ .calledWith({
|
|
|
+ contacts: [
|
|
|
+ { id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example" }
|
|
|
+ { id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example" }
|
|
|
+ ]
|
|
|
+ })
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ describe "without a contact list", ->
|
|
|
+ beforeEach ->
|
|
|
+ @ContactManager.getContacts = sinon.stub().callsArgWith(1, null, null)
|
|
|
+ @HttpController.getContacts @req, @res, @next
|
|
|
+
|
|
|
+ it "should return an empty list", ->
|
|
|
+ @res.send
|
|
|
+ .calledWith({
|
|
|
+ contacts: []
|
|
|
+ })
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ describe "with a holding account", ->
|
|
|
+ it "should not return holding accounts"
|