|
@@ -1,158 +1,114 @@
|
|
|
-/* eslint-disable
|
|
|
|
|
- max-len,
|
|
|
|
|
- no-return-assign,
|
|
|
|
|
-*/
|
|
|
|
|
-// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
|
-// Fix any style issues and re-enable lint.
|
|
|
|
|
-/*
|
|
|
|
|
- * decaffeinate suggestions:
|
|
|
|
|
- * DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
|
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
|
- */
|
|
|
|
|
|
|
+const { expect } = require('chai')
|
|
|
const sinon = require('sinon')
|
|
const sinon = require('sinon')
|
|
|
const modulePath = '../../../../app/src/Features/Contacts/ContactManager'
|
|
const modulePath = '../../../../app/src/Features/Contacts/ContactManager'
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe('ContactManager', function () {
|
|
describe('ContactManager', function () {
|
|
|
beforeEach(function () {
|
|
beforeEach(function () {
|
|
|
|
|
+ this.user_id = 'user-id-123'
|
|
|
|
|
+ this.contact_id = 'contact-id-123'
|
|
|
|
|
+ this.contact_ids = ['mock', 'contact_ids']
|
|
|
|
|
+ this.qs = new URLSearchParams({ limit: 42 })
|
|
|
|
|
+ this.fetch = sinon.stub()
|
|
|
this.ContactManager = SandboxedModule.require(modulePath, {
|
|
this.ContactManager = SandboxedModule.require(modulePath, {
|
|
|
requires: {
|
|
requires: {
|
|
|
- request: (this.request = sinon.stub()),
|
|
|
|
|
|
|
+ 'node-fetch': this.fetch,
|
|
|
'@overleaf/settings': (this.settings = {
|
|
'@overleaf/settings': (this.settings = {
|
|
|
apis: {
|
|
apis: {
|
|
|
contacts: {
|
|
contacts: {
|
|
|
- url: 'contacts.sharelatex.com',
|
|
|
|
|
|
|
+ url: 'http://contacts.sharelatex.com',
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
}),
|
|
}),
|
|
|
},
|
|
},
|
|
|
})
|
|
})
|
|
|
-
|
|
|
|
|
- this.user_id = 'user-id-123'
|
|
|
|
|
- this.contact_id = 'contact-id-123'
|
|
|
|
|
- return (this.callback = sinon.stub())
|
|
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('getContacts', function () {
|
|
describe('getContacts', function () {
|
|
|
describe('with a successful response code', function () {
|
|
describe('with a successful response code', function () {
|
|
|
- beforeEach(function () {
|
|
|
|
|
- this.request.get = sinon
|
|
|
|
|
- .stub()
|
|
|
|
|
- .callsArgWith(
|
|
|
|
|
- 1,
|
|
|
|
|
- null,
|
|
|
|
|
- { statusCode: 204 },
|
|
|
|
|
- { contact_ids: (this.contact_ids = ['mock', 'contact_ids']) }
|
|
|
|
|
- )
|
|
|
|
|
- return this.ContactManager.getContactIds(
|
|
|
|
|
|
|
+ beforeEach(async function () {
|
|
|
|
|
+ this.response = {
|
|
|
|
|
+ ok: true,
|
|
|
|
|
+ json: sinon.stub().resolves({ contact_ids: this.contact_ids }),
|
|
|
|
|
+ }
|
|
|
|
|
+ this.fetch.resolves(this.response)
|
|
|
|
|
+
|
|
|
|
|
+ this.result = await this.ContactManager.promises.getContactIds(
|
|
|
this.user_id,
|
|
this.user_id,
|
|
|
- { limit: 42 },
|
|
|
|
|
- this.callback
|
|
|
|
|
|
|
+ { limit: 42 }
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should get the contacts from the contacts api', function () {
|
|
it('should get the contacts from the contacts api', function () {
|
|
|
- return this.request.get
|
|
|
|
|
- .calledWith({
|
|
|
|
|
- url: `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts`,
|
|
|
|
|
- qs: { limit: 42 },
|
|
|
|
|
- json: true,
|
|
|
|
|
- jar: false,
|
|
|
|
|
|
|
+ this.fetch.should.have.been.calledWithMatch(
|
|
|
|
|
+ `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts?${this.qs}`,
|
|
|
|
|
+ sinon.match({
|
|
|
|
|
+ method: 'GET',
|
|
|
|
|
+ headers: { Accept: 'application/json' },
|
|
|
})
|
|
})
|
|
|
- .should.equal(true)
|
|
|
|
|
|
|
+ )
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should call the callback with the contatcs', function () {
|
|
|
|
|
- return this.callback
|
|
|
|
|
- .calledWith(null, this.contact_ids)
|
|
|
|
|
- .should.equal(true)
|
|
|
|
|
|
|
+ it('should return the contacts', function () {
|
|
|
|
|
+ this.result.should.equal(this.contact_ids)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('with a failed response code', function () {
|
|
describe('with a failed response code', function () {
|
|
|
- beforeEach(function () {
|
|
|
|
|
- this.request.get = sinon
|
|
|
|
|
- .stub()
|
|
|
|
|
- .callsArgWith(1, null, { statusCode: 500 }, null)
|
|
|
|
|
- return this.ContactManager.getContactIds(
|
|
|
|
|
- this.user_id,
|
|
|
|
|
- { limit: 42 },
|
|
|
|
|
- this.callback
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ beforeEach(async function () {
|
|
|
|
|
+ this.response = {
|
|
|
|
|
+ ok: false,
|
|
|
|
|
+ statusCode: 500,
|
|
|
|
|
+ json: sinon.stub().resolves({ contact_ids: this.contact_ids }),
|
|
|
|
|
+ }
|
|
|
|
|
+ this.fetch.resolves(this.response)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should call the callback with an error', function () {
|
|
|
|
|
- return this.callback
|
|
|
|
|
- .calledWith(
|
|
|
|
|
- sinon.match
|
|
|
|
|
- .instanceOf(Error)
|
|
|
|
|
- .and(
|
|
|
|
|
- sinon.match.has(
|
|
|
|
|
- 'message',
|
|
|
|
|
- 'contacts api responded with non-success code: 500'
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
- .should.equal(true)
|
|
|
|
|
|
|
+ it('should reject the promise', async function () {
|
|
|
|
|
+ await expect(
|
|
|
|
|
+ this.ContactManager.promises.getContactIds(this.user_id, {
|
|
|
|
|
+ limit: 42,
|
|
|
|
|
+ })
|
|
|
|
|
+ ).to.be.rejectedWith(
|
|
|
|
|
+ 'contacts api responded with non-success code: 500'
|
|
|
|
|
+ )
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('addContact', function () {
|
|
describe('addContact', function () {
|
|
|
describe('with a successful response code', function () {
|
|
describe('with a successful response code', function () {
|
|
|
- beforeEach(function () {
|
|
|
|
|
- this.request.post = sinon
|
|
|
|
|
- .stub()
|
|
|
|
|
- .callsArgWith(1, null, { statusCode: 200 }, null)
|
|
|
|
|
- return this.ContactManager.addContact(
|
|
|
|
|
|
|
+ beforeEach(async function () {
|
|
|
|
|
+ this.response = {
|
|
|
|
|
+ ok: true,
|
|
|
|
|
+ json: sinon.stub().resolves({ contact_ids: this.contact_ids }),
|
|
|
|
|
+ }
|
|
|
|
|
+ this.fetch.resolves(this.response)
|
|
|
|
|
+
|
|
|
|
|
+ this.result = await this.ContactManager.promises.addContact(
|
|
|
this.user_id,
|
|
this.user_id,
|
|
|
- this.contact_id,
|
|
|
|
|
- this.callback
|
|
|
|
|
|
|
+ this.contact_id
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should add the contacts for the user in the contacts api', function () {
|
|
it('should add the contacts for the user in the contacts api', function () {
|
|
|
- return this.request.post
|
|
|
|
|
- .calledWith({
|
|
|
|
|
- url: `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts`,
|
|
|
|
|
- json: {
|
|
|
|
|
- contact_id: this.contact_id,
|
|
|
|
|
|
|
+ this.fetch.should.have.been.calledWithMatch(
|
|
|
|
|
+ `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts`,
|
|
|
|
|
+ sinon.match({
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
|
|
+ Accept: 'application/json',
|
|
|
},
|
|
},
|
|
|
- jar: false,
|
|
|
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
|
+ contact_id: this.contact_id,
|
|
|
|
|
+ }),
|
|
|
})
|
|
})
|
|
|
- .should.equal(true)
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- it('should call the callback', function () {
|
|
|
|
|
- return this.callback.called.should.equal(true)
|
|
|
|
|
- })
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- describe('with a failed response code', function () {
|
|
|
|
|
- beforeEach(function () {
|
|
|
|
|
- this.request.post = sinon
|
|
|
|
|
- .stub()
|
|
|
|
|
- .callsArgWith(1, null, { statusCode: 500 }, null)
|
|
|
|
|
- return this.ContactManager.addContact(
|
|
|
|
|
- this.user_id,
|
|
|
|
|
- this.contact_id,
|
|
|
|
|
- this.callback
|
|
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should call the callback with an error', function () {
|
|
|
|
|
- return this.callback
|
|
|
|
|
- .calledWith(
|
|
|
|
|
- sinon.match
|
|
|
|
|
- .instanceOf(Error)
|
|
|
|
|
- .and(
|
|
|
|
|
- sinon.match.has(
|
|
|
|
|
- 'message',
|
|
|
|
|
- 'contacts api responded with non-success code: 500'
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
- .should.equal(true)
|
|
|
|
|
|
|
+ it('should call the callback', function () {
|
|
|
|
|
+ this.result.should.equal(this.contact_ids)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|