V1SusbcriptionManagerTests.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const modulePath = path.join(
  4. __dirname,
  5. '../../../../app/src/Features/Subscription/V1SubscriptionManager'
  6. )
  7. const sinon = require('sinon')
  8. const { expect } = require('chai')
  9. describe('V1SubscriptionManager', function () {
  10. beforeEach(function () {
  11. this.V1SubscriptionManager = SandboxedModule.require(modulePath, {
  12. requires: {
  13. '../User/UserGetter': (this.UserGetter = {}),
  14. '@overleaf/settings': (this.Settings = {
  15. apis: {
  16. v1: {
  17. host: (this.host = 'http://overleaf.example.com'),
  18. url: 'v1.url',
  19. },
  20. },
  21. v1GrandfatheredFeaturesUidCutoff: 10,
  22. v1GrandfatheredFeatures: {
  23. github: true,
  24. mendeley: true,
  25. },
  26. }),
  27. requestretry: (this.request = sinon.stub()),
  28. },
  29. })
  30. this.userId = 'abcd'
  31. this.v1UserId = 42
  32. this.user = {
  33. _id: this.userId,
  34. email: 'user@example.com',
  35. overleaf: {
  36. id: this.v1UserId,
  37. },
  38. }
  39. })
  40. describe('getGrandfatheredFeaturesForV1User', function () {
  41. describe('when the user ID is greater than the cutoff', function () {
  42. it('should return an empty feature set', function (done) {
  43. expect(
  44. this.V1SubscriptionManager.getGrandfatheredFeaturesForV1User(100)
  45. ).to.eql({})
  46. done()
  47. })
  48. })
  49. describe('when the user ID is less than the cutoff', function () {
  50. it('should return a feature set with grandfathered properties for github and mendeley', function (done) {
  51. expect(
  52. this.V1SubscriptionManager.getGrandfatheredFeaturesForV1User(1)
  53. ).to.eql({
  54. github: true,
  55. mendeley: true,
  56. })
  57. done()
  58. })
  59. })
  60. })
  61. describe('_v1Request', function () {
  62. beforeEach(function () {
  63. this.UserGetter.getUser = sinon.stub().yields(null, this.user)
  64. })
  65. describe('when v1IdForUser produces an error', function () {
  66. beforeEach(function () {
  67. this.V1SubscriptionManager.v1IdForUser = sinon
  68. .stub()
  69. .yields(new Error('woops'))
  70. this.call = cb => {
  71. this.V1SubscriptionManager._v1Request(
  72. this.user_id,
  73. {
  74. url() {
  75. return '/foo'
  76. },
  77. },
  78. cb
  79. )
  80. }
  81. })
  82. it('should not call request', function (done) {
  83. this.call(() => {
  84. expect(this.request.callCount).to.equal(0)
  85. done()
  86. })
  87. })
  88. it('should produce an error', function (done) {
  89. this.call((err, planCode) => {
  90. expect(err).to.exist
  91. done()
  92. })
  93. })
  94. })
  95. describe('when v1IdForUser does not find a user', function () {
  96. beforeEach(function () {
  97. this.V1SubscriptionManager.v1IdForUser = sinon.stub().yields(null, null)
  98. this.call = cb => {
  99. this.V1SubscriptionManager._v1Request(
  100. this.user_id,
  101. {
  102. url() {
  103. return '/foo'
  104. },
  105. },
  106. cb
  107. )
  108. }
  109. })
  110. it('should not call request', function (done) {
  111. this.call((err, planCode) => {
  112. if (err) return done(err)
  113. expect(this.request.callCount).to.equal(0)
  114. done()
  115. })
  116. })
  117. it('should not error', function (done) {
  118. this.call(err => {
  119. expect(err).to.not.exist
  120. done()
  121. })
  122. })
  123. })
  124. describe('when the request to v1 fails', function () {
  125. beforeEach(function () {
  126. this.request.yields(new Error('woops'))
  127. this.call = cb => {
  128. this.V1SubscriptionManager._v1Request(
  129. this.user_id,
  130. {
  131. url() {
  132. return '/foo'
  133. },
  134. },
  135. cb
  136. )
  137. }
  138. })
  139. it('should produce an error', function (done) {
  140. this.call(err => {
  141. expect(err).to.exist
  142. done()
  143. })
  144. })
  145. })
  146. describe('when the call succeeds', function () {
  147. beforeEach(function () {
  148. this.V1SubscriptionManager.v1IdForUser = sinon
  149. .stub()
  150. .yields(null, this.v1UserId)
  151. this.request.yields(null, { statusCode: 200 }, '{}')
  152. this.call = cb => {
  153. this.V1SubscriptionManager._v1Request(
  154. this.user_id,
  155. {
  156. method: 'GET',
  157. url() {
  158. return '/foo'
  159. },
  160. },
  161. cb
  162. )
  163. }
  164. })
  165. it('should not produce an error', function (done) {
  166. this.call((err, body, v1Id) => {
  167. expect(err).not.to.exist
  168. done()
  169. })
  170. })
  171. it('should have supplied retry options to request', function (done) {
  172. this.call((err, body, v1Id) => {
  173. if (err) return done(err)
  174. const requestOptions = this.request.lastCall.args[0]
  175. expect(requestOptions.url).to.equal('/foo')
  176. expect(requestOptions.maxAttempts).to.exist
  177. expect(requestOptions.maxAttempts > 0).to.be.true
  178. expect(requestOptions.retryDelay).to.exist
  179. expect(requestOptions.retryDelay > 0).to.be.true
  180. done()
  181. })
  182. })
  183. it('should return the v1 user id', function (done) {
  184. this.call((err, body, v1Id) => {
  185. if (err) return done(err)
  186. expect(v1Id).to.equal(this.v1UserId)
  187. done()
  188. })
  189. })
  190. it('should return the http response body', function (done) {
  191. this.call((err, body, v1Id) => {
  192. if (err) return done(err)
  193. expect(body).to.equal('{}')
  194. done()
  195. })
  196. })
  197. })
  198. describe('when the call returns an http error status code', function () {
  199. beforeEach(function () {
  200. this.V1SubscriptionManager.v1IdForUser = sinon
  201. .stub()
  202. .yields(null, this.v1UserId)
  203. this.request.yields(null, { statusCode: 500 }, '{}')
  204. this.call = cb => {
  205. this.V1SubscriptionManager._v1Request(
  206. this.user_id,
  207. {
  208. url() {
  209. return '/foo'
  210. },
  211. },
  212. cb
  213. )
  214. }
  215. })
  216. it('should produce an error', function (done) {
  217. this.call((err, body, v1Id) => {
  218. expect(err).to.exist
  219. done()
  220. })
  221. })
  222. })
  223. describe('when the call returns an http not-found status code', function () {
  224. beforeEach(function () {
  225. this.V1SubscriptionManager.v1IdForUser = sinon
  226. .stub()
  227. .yields(null, this.v1UserId)
  228. this.request.yields(null, { statusCode: 404 }, '{}')
  229. this.call = cb => {
  230. this.V1SubscriptionManager._v1Request(
  231. this.user_id,
  232. {
  233. url() {
  234. return '/foo'
  235. },
  236. },
  237. cb
  238. )
  239. }
  240. })
  241. it('should produce an not-found error', function (done) {
  242. this.call((err, body, v1Id) => {
  243. expect(err).to.exist
  244. expect(err.name).to.equal('NotFoundError')
  245. done()
  246. })
  247. })
  248. })
  249. })
  250. describe('v1IdForUser', function () {
  251. beforeEach(function () {
  252. this.UserGetter.getUser = sinon.stub().yields(null, this.user)
  253. })
  254. describe('when getUser produces an error', function () {
  255. beforeEach(function () {
  256. this.UserGetter.getUser = sinon.stub().yields(new Error('woops'))
  257. this.call = cb => {
  258. this.V1SubscriptionManager.v1IdForUser(this.user_id, cb)
  259. }
  260. })
  261. it('should produce an error', function (done) {
  262. this.call(err => {
  263. expect(err).to.exist
  264. done()
  265. })
  266. })
  267. })
  268. describe('when getUser does not find a user', function () {
  269. beforeEach(function () {
  270. this.UserGetter.getUser = sinon.stub().yields(null, null)
  271. this.call = cb => {
  272. this.V1SubscriptionManager.v1IdForUser(this.user_id, cb)
  273. }
  274. })
  275. it('should not error', function (done) {
  276. this.call((err, userId) => {
  277. expect(err).to.not.exist
  278. done()
  279. })
  280. })
  281. })
  282. describe('when it works', function () {
  283. beforeEach(function () {
  284. this.call = cb => {
  285. this.V1SubscriptionManager.v1IdForUser(this.user_id, cb)
  286. }
  287. })
  288. it('should not error', function (done) {
  289. this.call((err, userId) => {
  290. expect(err).to.not.exist
  291. done()
  292. })
  293. })
  294. it('should return the v1 user id', function (done) {
  295. this.call((err, userId) => {
  296. if (err) return done(err)
  297. expect(userId).to.eql(42)
  298. done()
  299. })
  300. })
  301. })
  302. })
  303. })