2020-02-17 02:38:06 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const chai = require('chai');
|
|
|
|
const should = chai.should();
|
|
|
|
const {
|
|
|
|
expect
|
|
|
|
} = chai;
|
|
|
|
const modulePath = "../../../app/js/ContactManager.js";
|
|
|
|
const SandboxedModule = require('sandboxed-module');
|
|
|
|
const {
|
|
|
|
ObjectId
|
|
|
|
} = require("mongojs");
|
|
|
|
const tk = require("timekeeper");
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
describe("ContactManager", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
tk.freeze(Date.now());
|
|
|
|
this.ContactManager = SandboxedModule.require(modulePath, { requires: {
|
2015-10-06 11:41:35 -04:00
|
|
|
"./mongojs": {
|
2020-02-17 02:38:06 -05:00
|
|
|
db: (this.db = {contacts: {}}),
|
|
|
|
ObjectId
|
2017-03-17 05:31:12 -04:00
|
|
|
},
|
|
|
|
'logger-sharelatex': {log: sinon.stub()},
|
|
|
|
'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
|
2020-02-17 02:38:06 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.user_id = ObjectId().toString();
|
|
|
|
this.contact_id = ObjectId().toString();
|
|
|
|
return this.callback = sinon.stub();
|
|
|
|
});
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
afterEach(() => tk.reset());
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
describe("touchContact", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.db.contacts.update = sinon.stub().callsArg(3);
|
|
|
|
});
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
describe("with a valid user_id", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.ContactManager.touchContact(this.user_id, (this.contact_id = "mock_contact"), this.callback);
|
|
|
|
});
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
it("should increment the contact count and timestamp", function() {
|
|
|
|
return this.db.contacts.update
|
2015-10-06 11:41:35 -04:00
|
|
|
.calledWith({
|
2020-02-17 02:38:06 -05:00
|
|
|
user_id: sinon.match(o => o.toString() === this.user_id.toString())
|
2015-10-06 11:41:35 -04:00
|
|
|
}, {
|
2020-02-17 02:38:06 -05:00
|
|
|
$inc: {
|
2015-10-06 11:41:35 -04:00
|
|
|
"contacts.mock_contact.n": 1
|
2020-02-17 02:38:06 -05:00
|
|
|
},
|
|
|
|
$set: {
|
2015-10-06 11:41:35 -04:00
|
|
|
"contacts.mock_contact.ts": new Date()
|
2020-02-17 02:38:06 -05:00
|
|
|
}
|
2015-10-06 11:41:35 -04:00
|
|
|
}, {
|
|
|
|
upsert: true
|
|
|
|
})
|
2020-02-17 02:38:06 -05:00
|
|
|
.should.equal(true);
|
|
|
|
});
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
return it("should call the callback", function() {
|
|
|
|
return this.callback.called.should.equal(true);
|
|
|
|
});
|
|
|
|
});
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
return describe("with an invalid user id", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.ContactManager.touchContact("not-valid-object-id", this.contact_id, this.callback);
|
|
|
|
});
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
return it("should call the callback with an error", function() {
|
|
|
|
return this.callback.calledWith(new Error()).should.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
return describe("getContacts", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.user = {
|
2015-10-06 12:22:11 -04:00
|
|
|
contacts: ["mock", "contacts"]
|
2020-02-17 02:38:06 -05:00
|
|
|
};
|
|
|
|
return this.db.contacts.findOne = sinon.stub().callsArgWith(1, null, this.user);
|
|
|
|
});
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
describe("with a valid user_id", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.ContactManager.getContacts(this.user_id, this.callback);
|
|
|
|
});
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
it("should find the user's contacts", function() {
|
|
|
|
return this.db.contacts.findOne
|
2015-10-06 12:22:11 -04:00
|
|
|
.calledWith({
|
2020-02-17 02:38:06 -05:00
|
|
|
user_id: sinon.match(o => o.toString() === this.user_id.toString())
|
2015-10-06 12:22:11 -04:00
|
|
|
})
|
2020-02-17 02:38:06 -05:00
|
|
|
.should.equal(true);
|
|
|
|
});
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
return it("should call the callback with the contacts", function() {
|
|
|
|
return this.callback.calledWith(null, this.user.contacts).should.equal(true);
|
|
|
|
});
|
|
|
|
});
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
return describe("with an invalid user id", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.ContactManager.getContacts("not-valid-object-id", this.callback);
|
|
|
|
});
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:06 -05:00
|
|
|
return it("should call the callback with an error", function() {
|
|
|
|
return this.callback.calledWith(new Error()).should.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-10-06 12:22:11 -04:00
|
|
|
|