ProjectOwnershipTransferTests.mjs 4.0 KB

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