2022-12-16 05:42:39 -05:00
|
|
|
import sinon from 'sinon'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import esmock from 'esmock'
|
|
|
|
import { ObjectId } from 'mongodb'
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('ContactManager', function () {
|
2022-12-16 05:42:39 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.clock = sinon.useFakeTimers(new Date())
|
|
|
|
|
|
|
|
this.db = { contacts: {} }
|
|
|
|
|
|
|
|
this.ContactManager = await esmock('../../../app/js/ContactManager', {
|
|
|
|
'../../../app/js/mongodb': {
|
|
|
|
db: this.db,
|
|
|
|
ObjectId,
|
2021-07-13 07:04:47 -04:00
|
|
|
},
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
2022-12-16 05:42:39 -05:00
|
|
|
|
2023-10-23 04:21:16 -04:00
|
|
|
this.user_id = new ObjectId().toString()
|
|
|
|
this.contact_id = new ObjectId().toString()
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
afterEach(function () {
|
2022-12-16 05:42:39 -05:00
|
|
|
this.clock.restore()
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('touchContact', function () {
|
|
|
|
beforeEach(function () {
|
2022-12-16 05:42:39 -05:00
|
|
|
this.db.contacts.updateOne = sinon.stub().resolves()
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('with a valid user_id', function () {
|
2022-12-16 05:42:39 -05:00
|
|
|
it('should increment the contact count and timestamp', async function () {
|
|
|
|
await expect(
|
|
|
|
this.ContactManager.touchContact(this.user_id, 'mock_contact')
|
|
|
|
).not.to.be.rejected
|
2020-02-17 02:38:13 -05:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
expect(this.db.contacts.updateOne).to.be.calledWith(
|
|
|
|
{
|
|
|
|
user_id: sinon.match(o => o.toString() === this.user_id),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$inc: {
|
|
|
|
'contacts.mock_contact.n': 1,
|
2020-02-17 02:38:13 -05:00
|
|
|
},
|
2022-12-16 05:42:39 -05:00
|
|
|
$set: {
|
|
|
|
'contacts.mock_contact.ts': new Date(),
|
2020-02-17 02:38:13 -05:00
|
|
|
},
|
2022-12-16 05:42:39 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
upsert: true,
|
|
|
|
}
|
|
|
|
)
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
describe('with an invalid user id', function () {
|
|
|
|
it('should be rejected', async function () {
|
|
|
|
await expect(
|
|
|
|
this.ContactManager.touchContact(
|
|
|
|
'not-valid-object-id',
|
|
|
|
this.contact_id
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(
|
2023-10-23 04:21:16 -04:00
|
|
|
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
|
2020-02-17 02:38:13 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
describe('getContacts', function () {
|
2020-06-04 03:49:46 -04:00
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.user = {
|
2021-07-13 07:04:47 -04:00
|
|
|
contacts: ['mock', 'contacts'],
|
2020-02-17 02:38:13 -05:00
|
|
|
}
|
2022-12-16 05:42:39 -05:00
|
|
|
this.db.contacts.findOne = sinon.stub().resolves(this.user)
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('with a valid user_id', function () {
|
2022-12-16 05:42:39 -05:00
|
|
|
it("should find the user's contacts", async function () {
|
|
|
|
await expect(
|
|
|
|
this.ContactManager.getContacts(this.user_id)
|
|
|
|
).to.eventually.deep.equal(this.user.contacts)
|
2020-02-17 02:38:13 -05:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
expect(this.db.contacts.findOne).to.be.calledWith({
|
|
|
|
user_id: sinon.match(o => o.toString() === this.user_id),
|
|
|
|
})
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
describe('with an invalid user id', function () {
|
|
|
|
it('should be rejected', async function () {
|
|
|
|
await expect(this.ContactManager.getContacts('not-valid-object-id')).to
|
|
|
|
.be.rejected
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|