token-access-page.spec.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import TokenAccessPage from '@/features/token-access/components/token-access-root'
  2. import { SplitTestProvider } from '@/shared/context/split-test-context'
  3. import { location } from '@/shared/components/location'
  4. describe('<TokenAccessPage/>', function () {
  5. // this is a URL for a read-only token, but the process is the same for read-write tokens
  6. const url = '/read/123/grant'
  7. beforeEach(function () {
  8. cy.window().then(win => {
  9. win.metaAttributesCache.set('ol-postUrl', url)
  10. win.metaAttributesCache.set('ol-user', { email: 'test@example.com' })
  11. win.metaAttributesCache.set('ol-splitTestVariants', {
  12. 'sharing-updates': 'enabled',
  13. })
  14. })
  15. })
  16. it('handles a successful token access request', function () {
  17. cy.intercept(
  18. { method: 'post', url, times: 1 },
  19. {
  20. body: {
  21. requireAccept: { projectName: 'Test Project' },
  22. },
  23. }
  24. ).as('grantRequest')
  25. cy.mount(
  26. <SplitTestProvider>
  27. <TokenAccessPage />
  28. </SplitTestProvider>
  29. )
  30. cy.wait('@grantRequest').then(interception => {
  31. expect(interception.request.body.confirmedByUser).to.be.false
  32. })
  33. cy.findByRole('heading', {
  34. name: /you’re joining Test Project as test@example.com/i,
  35. })
  36. cy.findByText(
  37. /your name and email address will be visible to project editors/i
  38. )
  39. cy.intercept(
  40. { method: 'post', url, times: 1 },
  41. {
  42. body: {
  43. redirect: '/project/123',
  44. },
  45. }
  46. ).as('confirmedGrantRequest')
  47. cy.stub(location, 'replace').as('replaceLocation')
  48. cy.findByRole('button', { name: /join project/i }).click()
  49. cy.wait('@confirmedGrantRequest').then(interception => {
  50. expect(interception.request.body.confirmedByUser).to.be.true
  51. })
  52. cy.get('@replaceLocation').should(
  53. 'have.been.calledOnceWith',
  54. '/project/123'
  55. )
  56. })
  57. it('handles a project not found response', function () {
  58. cy.intercept({ method: 'post', url, times: 1 }, { statusCode: 404 }).as(
  59. 'grantRequest'
  60. )
  61. cy.mount(
  62. <SplitTestProvider>
  63. <TokenAccessPage />
  64. </SplitTestProvider>
  65. )
  66. cy.wait('@grantRequest')
  67. cy.findByRole('heading', { name: /sorry, this project isn’t available/i })
  68. cy.findByText(/the link may be broken or you may not have access rights/i)
  69. cy.findByRole('button', { name: 'Join Project' }).should('not.exist')
  70. cy.contains(
  71. new RegExp(
  72. 'you are currently logged in as test@example.com. ' +
  73. 'you might need to log in with a different email address',
  74. 'i'
  75. )
  76. )
  77. })
  78. it('handles a redirect response', function () {
  79. cy.intercept(
  80. { method: 'post', url, times: 1 },
  81. {
  82. body: {
  83. redirect: '/restricted',
  84. },
  85. }
  86. ).as('grantRequest')
  87. cy.stub(location, 'replace').as('replaceLocation')
  88. cy.mount(
  89. <SplitTestProvider>
  90. <TokenAccessPage />
  91. </SplitTestProvider>
  92. )
  93. cy.wait('@grantRequest')
  94. cy.get('@replaceLocation').should('have.been.calledOnceWith', '/restricted')
  95. })
  96. it('handles a v1 "must login" response', function () {
  97. cy.intercept(
  98. { method: 'post', url, times: 1 },
  99. {
  100. body: {
  101. v1Import: { status: 'mustLogin' },
  102. },
  103. }
  104. ).as('grantRequest')
  105. cy.stub(location, 'replace').as('replaceLocation')
  106. cy.mount(
  107. <SplitTestProvider>
  108. <TokenAccessPage />
  109. </SplitTestProvider>
  110. )
  111. cy.wait('@grantRequest')
  112. cy.get('h1').should('have.text', 'Please log in')
  113. cy.findByRole('link', { name: 'Log in to access project' })
  114. .should('have.attr', 'href')
  115. .and('match', /^\/login\?redir=/)
  116. })
  117. it('handles a v1 "cannot import" response', function () {
  118. cy.intercept(
  119. { method: 'post', url, times: 1 },
  120. {
  121. body: {
  122. v1Import: { status: 'cannotImport' },
  123. },
  124. }
  125. ).as('grantRequest')
  126. cy.stub(location, 'replace').as('replaceLocation')
  127. cy.mount(
  128. <SplitTestProvider>
  129. <TokenAccessPage />
  130. </SplitTestProvider>
  131. )
  132. cy.wait('@grantRequest')
  133. cy.get('h1').should('have.text', 'Overleaf v1 Project')
  134. cy.get('h2').should('have.text', 'Cannot Access Overleaf v1 Project')
  135. })
  136. it('handles a v1 "can download zip" response', function () {
  137. cy.intercept(
  138. { method: 'post', url, times: 1 },
  139. {
  140. body: {
  141. v1Import: {
  142. status: 'canDownloadZip',
  143. projectId: '123',
  144. name: 'Test Project',
  145. },
  146. },
  147. }
  148. ).as('grantRequest')
  149. cy.stub(location, 'replace').as('replaceLocation')
  150. cy.mount(
  151. <SplitTestProvider>
  152. <TokenAccessPage />
  153. </SplitTestProvider>
  154. )
  155. cy.wait('@grantRequest')
  156. cy.get('h1').should('have.text', 'Overleaf v1 Project')
  157. cy.findByRole('link', { name: 'Download project zip file' }).should(
  158. 'have.attr',
  159. 'href',
  160. '/overleaf/project/123/download/zip'
  161. )
  162. })
  163. })