DeletionTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. const User = require('./helpers/User')
  2. const request = require('./helpers/request')
  3. const async = require('async')
  4. const { expect } = require('chai')
  5. const settings = require('settings-sharelatex')
  6. const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb')
  7. const { Subscription } = require('../../../app/src/models/Subscription')
  8. const SubscriptionViewModelBuilder = require('../../../app/src/Features/Subscription/SubscriptionViewModelBuilder')
  9. const MockDocstoreApiClass = require('./mocks/MockDocstoreApi')
  10. const MockFilestoreApiClass = require('./mocks/MockFilestoreApi')
  11. let MockDocstoreApi, MockFilestoreApi
  12. before(function() {
  13. MockDocstoreApi = MockDocstoreApiClass.instance()
  14. MockFilestoreApi = MockFilestoreApiClass.instance()
  15. })
  16. describe('Deleting a user', function() {
  17. beforeEach(function(done) {
  18. this.user = new User()
  19. async.series(
  20. [
  21. this.user.ensureUserExists.bind(this.user),
  22. this.user.login.bind(this.user)
  23. ],
  24. done
  25. )
  26. })
  27. it('Should remove the user from active users', function(done) {
  28. this.user.get((error, user) => {
  29. expect(error).not.to.exist
  30. expect(user).to.exist
  31. this.user.deleteUser(error => {
  32. expect(error).not.to.exist
  33. this.user.get((error, user) => {
  34. expect(error).not.to.exist
  35. expect(user).not.to.exist
  36. done()
  37. })
  38. })
  39. })
  40. })
  41. it('Should create a soft-deleted user', function(done) {
  42. this.user.get((error, user) => {
  43. expect(error).not.to.exist
  44. this.user.deleteUser(error => {
  45. expect(error).not.to.exist
  46. db.deletedUsers.findOne(
  47. { 'user._id': user._id },
  48. (error, deletedUser) => {
  49. expect(error).not.to.exist
  50. expect(deletedUser).to.exist
  51. // it should set the 'deleterData' correctly
  52. expect(deletedUser.deleterData.deleterId.toString()).to.equal(
  53. user._id.toString()
  54. )
  55. expect(deletedUser.deleterData.deletedUserId.toString()).to.equal(
  56. user._id.toString()
  57. )
  58. expect(deletedUser.deleterData.deletedUserReferralId).to.equal(
  59. user.referal_id
  60. )
  61. // it should set the 'user' correctly
  62. expect(deletedUser.user._id.toString()).to.equal(
  63. user._id.toString()
  64. )
  65. expect(deletedUser.user.email).to.equal(user.email)
  66. done()
  67. }
  68. )
  69. })
  70. })
  71. })
  72. it('Should fail if the user has a subscription', function(done) {
  73. Subscription.create(
  74. {
  75. admin_id: this.user._id,
  76. manager_ids: [this.user._id],
  77. planCode: 'collaborator'
  78. },
  79. error => {
  80. expect(error).not.to.exist
  81. SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
  82. this.user,
  83. error => {
  84. expect(error).not.to.exist
  85. this.user.deleteUser(error => {
  86. expect(error).to.exist
  87. done()
  88. })
  89. }
  90. )
  91. }
  92. )
  93. })
  94. it("Should delete the user's projects", function(done) {
  95. this.user.createProject('wombat', (error, projectId) => {
  96. expect(error).not.to.exist
  97. this.user.getProject(projectId, (error, project) => {
  98. expect(error).not.to.exist
  99. expect(project).to.exist
  100. this.user.deleteUser(error => {
  101. expect(error).not.to.exist
  102. this.user.getProject(projectId, (error, project) => {
  103. expect(error).not.to.exist
  104. expect(project).not.to.exist
  105. done()
  106. })
  107. })
  108. })
  109. })
  110. })
  111. describe('when scrubbing the user', function() {
  112. beforeEach(function(done) {
  113. this.user.get((error, user) => {
  114. if (error) {
  115. throw error
  116. }
  117. this.userId = user._id
  118. this.user.deleteUser(done)
  119. })
  120. })
  121. it('Should remove the user data from mongo', function(done) {
  122. db.deletedUsers.findOne(
  123. { 'deleterData.deletedUserId': this.userId },
  124. (error, deletedUser) => {
  125. expect(error).not.to.exist
  126. expect(deletedUser).to.exist
  127. expect(deletedUser.deleterData.deleterIpAddress).to.exist
  128. expect(deletedUser.user).to.exist
  129. request.post(
  130. `/internal/users/${this.userId}/expire`,
  131. {
  132. auth: {
  133. user: settings.apis.web.user,
  134. pass: settings.apis.web.pass,
  135. sendImmediately: true
  136. }
  137. },
  138. (error, res) => {
  139. expect(error).not.to.exist
  140. expect(res.statusCode).to.equal(204)
  141. db.deletedUsers.findOne(
  142. { 'deleterData.deletedUserId': this.userId },
  143. (error, deletedUser) => {
  144. expect(error).not.to.exist
  145. expect(deletedUser).to.exist
  146. expect(deletedUser.deleterData.deleterIpAddress).not.to.exist
  147. expect(deletedUser.user).not.to.exist
  148. done()
  149. }
  150. )
  151. }
  152. )
  153. }
  154. )
  155. })
  156. })
  157. })
  158. describe('Deleting a project', function() {
  159. beforeEach(function(done) {
  160. this.user = new User()
  161. this.projectName = 'wombat'
  162. this.user.ensureUserExists(() => {
  163. this.user.login(() => {
  164. this.user.createProject(this.projectName, (_e, projectId) => {
  165. this.projectId = projectId
  166. done()
  167. })
  168. })
  169. })
  170. })
  171. it('Should remove the project from active projects', function(done) {
  172. this.user.getProject(this.projectId, (error, project) => {
  173. expect(error).not.to.exist
  174. expect(project).to.exist
  175. this.user.deleteProject(this.projectId, error => {
  176. expect(error).not.to.exist
  177. this.user.getProject(this.projectId, (error, project) => {
  178. expect(error).not.to.exist
  179. expect(project).not.to.exist
  180. done()
  181. })
  182. })
  183. })
  184. })
  185. it('Should create a soft-deleted project', function(done) {
  186. this.user.getProject(this.projectId, (error, project) => {
  187. expect(error).not.to.exist
  188. this.user.get((error, user) => {
  189. expect(error).not.to.exist
  190. this.user.deleteProject(this.projectId, error => {
  191. expect(error).not.to.exist
  192. db.deletedProjects.findOne(
  193. { 'deleterData.deletedProjectId': project._id },
  194. (error, deletedProject) => {
  195. expect(error).not.to.exist
  196. expect(deletedProject).to.exist
  197. // it should set the 'deleterData' correctly
  198. expect(deletedProject.deleterData.deleterId.toString()).to.equal(
  199. user._id.toString()
  200. )
  201. expect(
  202. deletedProject.deleterData.deletedProjectId.toString()
  203. ).to.equal(project._id.toString())
  204. expect(
  205. deletedProject.deleterData.deletedProjectOwnerId.toString()
  206. ).to.equal(user._id.toString())
  207. // it should set the 'user' correctly
  208. expect(deletedProject.project._id.toString()).to.equal(
  209. project._id.toString()
  210. )
  211. expect(deletedProject.project.name).to.equal(this.projectName)
  212. done()
  213. }
  214. )
  215. })
  216. })
  217. })
  218. })
  219. describe('When the project has docs', function() {
  220. beforeEach(function(done) {
  221. this.user.getProject(this.projectId, (error, project) => {
  222. if (error) {
  223. throw error
  224. }
  225. this.user.createDocInProject(
  226. this.projectId,
  227. project.rootFolder[0]._id,
  228. 'potato',
  229. (error, docId) => {
  230. if (error) {
  231. throw error
  232. }
  233. this.docId = docId
  234. done()
  235. }
  236. )
  237. MockFilestoreApi.files[this.projectId.toString()] = {
  238. dummyFile: 'wombat'
  239. }
  240. })
  241. })
  242. describe('When the deleted project is expired', function() {
  243. beforeEach(function(done) {
  244. this.user.deleteProject(this.projectId, error => {
  245. if (error) {
  246. throw error
  247. }
  248. done()
  249. })
  250. })
  251. it('Should destroy the docs', function(done) {
  252. expect(
  253. MockDocstoreApi.docs[this.projectId.toString()][this.docId.toString()]
  254. ).to.exist
  255. request.post(
  256. `/internal/project/${this.projectId}/expire-deleted-project`,
  257. {
  258. auth: {
  259. user: settings.apis.web.user,
  260. pass: settings.apis.web.pass,
  261. sendImmediately: true
  262. }
  263. },
  264. (error, res) => {
  265. expect(error).not.to.exist
  266. expect(res.statusCode).to.equal(200)
  267. expect(MockDocstoreApi.docs[this.projectId.toString()]).not.to.exist
  268. done()
  269. }
  270. )
  271. })
  272. it('Should destroy the files', function(done) {
  273. expect(MockFilestoreApi.files[this.projectId.toString()]).to.exist
  274. request.post(
  275. `/internal/project/${this.projectId}/expire-deleted-project`,
  276. {
  277. auth: {
  278. user: settings.apis.web.user,
  279. pass: settings.apis.web.pass,
  280. sendImmediately: true
  281. }
  282. },
  283. (error, res) => {
  284. expect(error).not.to.exist
  285. expect(res.statusCode).to.equal(200)
  286. expect(MockFilestoreApi.files[this.projectId.toString()]).not.to
  287. .exist
  288. done()
  289. }
  290. )
  291. })
  292. it('Should remove the project data from mongo', function(done) {
  293. db.deletedProjects.findOne(
  294. { 'deleterData.deletedProjectId': ObjectId(this.projectId) },
  295. (error, deletedProject) => {
  296. expect(error).not.to.exist
  297. expect(deletedProject).to.exist
  298. expect(deletedProject.project).to.exist
  299. expect(deletedProject.deleterData.deleterIpAddress).to.exist
  300. expect(deletedProject.deleterData.deletedAt).to.exist
  301. request.post(
  302. `/internal/project/${this.projectId}/expire-deleted-project`,
  303. {
  304. auth: {
  305. user: settings.apis.web.user,
  306. pass: settings.apis.web.pass,
  307. sendImmediately: true
  308. }
  309. },
  310. (error, res) => {
  311. expect(error).not.to.exist
  312. expect(res.statusCode).to.equal(200)
  313. db.deletedProjects.findOne(
  314. { 'deleterData.deletedProjectId': ObjectId(this.projectId) },
  315. (error, deletedProject) => {
  316. expect(error).not.to.exist
  317. expect(deletedProject).to.exist
  318. expect(deletedProject.project).not.to.exist
  319. expect(deletedProject.deleterData.deleterIpAddress).not.to
  320. .exist
  321. expect(deletedProject.deleterData.deletedAt).to.exist
  322. done()
  323. }
  324. )
  325. }
  326. )
  327. }
  328. )
  329. })
  330. })
  331. })
  332. })