ProjectOwnershipTransferTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { expect } = require('chai')
  2. const User = require('./helpers/User').promises
  3. describe('Project ownership transfer', function () {
  4. beforeEach(async function () {
  5. this.ownerSession = new User()
  6. this.collaboratorSession = new User()
  7. this.strangerSession = new User()
  8. this.adminSession = new User()
  9. this.invitedAdminSession = new User()
  10. await this.invitedAdminSession.ensureUserExists()
  11. await this.invitedAdminSession.ensureAdmin()
  12. await this.invitedAdminSession.login()
  13. await this.adminSession.ensureUserExists()
  14. await this.adminSession.ensureAdmin()
  15. await this.ownerSession.login()
  16. await this.collaboratorSession.login()
  17. await this.strangerSession.login()
  18. await this.adminSession.login()
  19. this.owner = await this.ownerSession.get()
  20. this.collaborator = await this.collaboratorSession.get()
  21. this.stranger = await this.strangerSession.get()
  22. this.admin = await this.adminSession.get()
  23. this.invitedAdmin = await this.invitedAdminSession.get()
  24. this.projectId = await this.ownerSession.createProject('Test project')
  25. await this.ownerSession.addUserToProject(
  26. this.projectId,
  27. this.invitedAdmin,
  28. 'readAndWrite'
  29. )
  30. await this.ownerSession.addUserToProject(
  31. this.projectId,
  32. this.collaborator,
  33. 'readAndWrite'
  34. )
  35. })
  36. describe('happy path', function () {
  37. beforeEach(async function () {
  38. await this.ownerSession.transferProjectOwnership(
  39. this.projectId,
  40. this.collaborator._id
  41. )
  42. })
  43. it('changes the project owner', async function () {
  44. const project = await this.collaboratorSession.getProject(this.projectId)
  45. expect(project.owner_ref.toString()).to.equal(
  46. this.collaborator._id.toString()
  47. )
  48. })
  49. it('adds the previous owner as a read/write collaborator', async function () {
  50. const project = await this.collaboratorSession.getProject(this.projectId)
  51. expect(project.collaberator_refs.map(x => x.toString())).to.have.members([
  52. this.owner._id.toString(),
  53. this.invitedAdmin._id.toString(),
  54. ])
  55. })
  56. it('lets the new owner open the project', async function () {
  57. await this.collaboratorSession.openProject(this.projectId)
  58. })
  59. it('lets the previous owner open the project', async function () {
  60. await this.ownerSession.openProject(this.projectId)
  61. })
  62. })
  63. describe('ownership change as admin', function () {
  64. it('lets the invited admin transfer ownership', async function () {
  65. await this.invitedAdminSession.transferProjectOwnership(
  66. this.projectId,
  67. this.collaborator._id
  68. )
  69. const project = await this.invitedAdminSession.getProject(this.projectId)
  70. expect(project.owner_ref.toString()).to.equal(
  71. this.collaborator._id.toString()
  72. )
  73. })
  74. it('lets the non-invited admin transfer ownership', async function () {
  75. await this.adminSession.transferProjectOwnership(
  76. this.projectId,
  77. this.collaborator._id
  78. )
  79. const project = await this.adminSession.getProject(this.projectId)
  80. expect(project.owner_ref.toString()).to.equal(
  81. this.collaborator._id.toString()
  82. )
  83. })
  84. })
  85. describe('validation', function () {
  86. it('lets only the project owner transfer ownership', async function () {
  87. await expect(
  88. this.collaboratorSession.transferProjectOwnership(
  89. this.projectId,
  90. this.collaborator._id
  91. )
  92. ).to.be.rejectedWith(/failed: status=403 /)
  93. })
  94. it('prevents transfers to a non-collaborator', async function () {
  95. await expect(
  96. this.ownerSession.transferProjectOwnership(
  97. this.projectId,
  98. this.stranger._id
  99. )
  100. ).to.be.rejectedWith(/failed: status=403 /)
  101. })
  102. it('allows an admin to transfer to any project to a non-collaborator', async function () {
  103. await expect(
  104. this.adminSession.transferProjectOwnership(
  105. this.projectId,
  106. this.stranger._id
  107. )
  108. ).to.be.fulfilled
  109. })
  110. })
  111. })