SharingTests.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { expect } from 'chai'
  2. import UserHelper from './helpers/User.mjs'
  3. const User = UserHelper.promises
  4. describe('Sharing', function () {
  5. beforeEach(async function () {
  6. this.ownerSession = new User()
  7. this.collaboratorSession = new User()
  8. this.strangerSession = new User()
  9. this.reviewerSession = new User()
  10. await this.ownerSession.login()
  11. await this.collaboratorSession.login()
  12. await this.strangerSession.login()
  13. await this.reviewerSession.login()
  14. this.owner = await this.ownerSession.get()
  15. this.collaborator = await this.collaboratorSession.get()
  16. this.stranger = await this.strangerSession.get()
  17. this.reviewer = await this.reviewerSession.get()
  18. this.projectId = await this.ownerSession.createProject('Test project')
  19. })
  20. describe('with read-only collaborator', function () {
  21. beforeEach(async function () {
  22. await this.ownerSession.addUserToProject(
  23. this.projectId,
  24. this.collaborator,
  25. 'readOnly'
  26. )
  27. })
  28. it('sets the privilege level to read-write', async function () {
  29. await this.ownerSession.setCollaboratorInfo(
  30. this.projectId,
  31. this.collaborator._id,
  32. { privilegeLevel: 'readAndWrite' }
  33. )
  34. const project = await this.ownerSession.getProject(this.projectId)
  35. expect(project.collaberator_refs).to.deep.equal([this.collaborator._id])
  36. expect(project.readOnly_refs).to.deep.equal([])
  37. expect(project.reviewer_refs).to.deep.equal([])
  38. })
  39. it('sets the privilege level to review', async function () {
  40. await this.ownerSession.setCollaboratorInfo(
  41. this.projectId,
  42. this.collaborator._id,
  43. { privilegeLevel: 'review' }
  44. )
  45. const project = await this.ownerSession.getProject(this.projectId)
  46. expect(project.reviewer_refs).to.deep.equal([this.collaborator._id])
  47. expect(project.collaberator_refs).to.deep.equal([])
  48. expect(project.readOnly_refs).to.deep.equal([])
  49. })
  50. it('treats setting the privilege to read-only as a noop', async function () {
  51. await this.ownerSession.setCollaboratorInfo(
  52. this.projectId,
  53. this.collaborator._id,
  54. { privilegeLevel: 'readOnly' }
  55. )
  56. const project = await this.ownerSession.getProject(this.projectId)
  57. expect(project.collaberator_refs).to.deep.equal([])
  58. expect(project.reviewer_refs).to.deep.equal([])
  59. expect(project.readOnly_refs).to.deep.equal([this.collaborator._id])
  60. })
  61. it('prevents non-owners to set the privilege level', async function () {
  62. await expect(
  63. this.collaboratorSession.setCollaboratorInfo(
  64. this.projectId,
  65. this.collaborator._id,
  66. { privilegeLevel: 'readAndWrite' }
  67. )
  68. ).to.be.rejectedWith(/failed: status=403 /)
  69. })
  70. it('validates the privilege level', async function () {
  71. await expect(
  72. this.collaboratorSession.setCollaboratorInfo(
  73. this.projectId,
  74. this.collaborator._id,
  75. { privilegeLevel: 'superpowers' }
  76. )
  77. ).to.be.rejectedWith(/failed: status=400 /)
  78. })
  79. it('returns 404 if the user is not already a collaborator', async function () {
  80. await expect(
  81. this.ownerSession.setCollaboratorInfo(
  82. this.projectId,
  83. this.stranger._id,
  84. { privilegeLevel: 'readOnly' }
  85. )
  86. ).to.be.rejectedWith(/failed: status=404 /)
  87. })
  88. })
  89. describe('with read-write collaborator', function () {
  90. beforeEach(async function () {
  91. await this.ownerSession.addUserToProject(
  92. this.projectId,
  93. this.collaborator,
  94. 'readAndWrite'
  95. )
  96. })
  97. it('sets the privilege level to read-only', async function () {
  98. await this.ownerSession.setCollaboratorInfo(
  99. this.projectId,
  100. this.collaborator._id,
  101. { privilegeLevel: 'readOnly' }
  102. )
  103. const project = await this.ownerSession.getProject(this.projectId)
  104. expect(project.collaberator_refs).to.deep.equal([])
  105. expect(project.reviewer_refs).to.deep.equal([])
  106. expect(project.readOnly_refs).to.deep.equal([this.collaborator._id])
  107. })
  108. })
  109. describe('with reviewer collaborator', function () {
  110. beforeEach(async function () {
  111. await this.ownerSession.addUserToProject(
  112. this.projectId,
  113. this.reviewer,
  114. 'review'
  115. )
  116. })
  117. it('prevents non-owners to set the privilege level', async function () {
  118. await expect(
  119. this.collaboratorSession.setCollaboratorInfo(
  120. this.projectId,
  121. this.reviewer._id,
  122. { privilegeLevel: 'review' }
  123. )
  124. ).to.be.rejectedWith(/failed: status=403 /)
  125. })
  126. it('sets the privilege level to read-only', async function () {
  127. await this.ownerSession.setCollaboratorInfo(
  128. this.projectId,
  129. this.reviewer._id,
  130. { privilegeLevel: 'readOnly' }
  131. )
  132. const project = await this.ownerSession.getProject(this.projectId)
  133. expect(project.collaberator_refs).to.deep.equal([])
  134. expect(project.reviewer_refs).to.deep.equal([])
  135. expect(project.readOnly_refs).to.deep.equal([this.reviewer._id])
  136. })
  137. })
  138. })