token-access-page.spec.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.get('h3').should('have.text', 'Join Project')
  68. cy.get('h4').should('have.text', 'Project not found')
  69. cy.findByRole('button', { name: 'Join Project' }).should('not.exist')
  70. })
  71. it('handles a redirect response', function () {
  72. cy.intercept(
  73. { method: 'post', url, times: 1 },
  74. {
  75. body: {
  76. redirect: '/restricted',
  77. },
  78. }
  79. ).as('grantRequest')
  80. cy.stub(location, 'replace').as('replaceLocation')
  81. cy.mount(
  82. <SplitTestProvider>
  83. <TokenAccessPage />
  84. </SplitTestProvider>
  85. )
  86. cy.wait('@grantRequest')
  87. cy.get('@replaceLocation').should('have.been.calledOnceWith', '/restricted')
  88. })
  89. it('handles a v1 "must login" response', function () {
  90. cy.intercept(
  91. { method: 'post', url, times: 1 },
  92. {
  93. body: {
  94. v1Import: { status: 'mustLogin' },
  95. },
  96. }
  97. ).as('grantRequest')
  98. cy.stub(location, 'replace').as('replaceLocation')
  99. cy.mount(
  100. <SplitTestProvider>
  101. <TokenAccessPage />
  102. </SplitTestProvider>
  103. )
  104. cy.wait('@grantRequest')
  105. cy.get('h1').should('have.text', 'Please log in')
  106. cy.findByRole('link', { name: 'Log in to access project' })
  107. .should('have.attr', 'href')
  108. .and('match', /^\/login\?redir=/)
  109. })
  110. it('handles a v1 "cannot import" response', function () {
  111. cy.intercept(
  112. { method: 'post', url, times: 1 },
  113. {
  114. body: {
  115. v1Import: { status: 'cannotImport' },
  116. },
  117. }
  118. ).as('grantRequest')
  119. cy.stub(location, 'replace').as('replaceLocation')
  120. cy.mount(
  121. <SplitTestProvider>
  122. <TokenAccessPage />
  123. </SplitTestProvider>
  124. )
  125. cy.wait('@grantRequest')
  126. cy.get('h1').should('have.text', 'Overleaf v1 Project')
  127. cy.get('h2').should('have.text', 'Cannot Access Overleaf v1 Project')
  128. })
  129. it('handles a v1 "can download zip" response', function () {
  130. cy.intercept(
  131. { method: 'post', url, times: 1 },
  132. {
  133. body: {
  134. v1Import: {
  135. status: 'canDownloadZip',
  136. projectId: '123',
  137. name: 'Test Project',
  138. },
  139. },
  140. }
  141. ).as('grantRequest')
  142. cy.stub(location, 'replace').as('replaceLocation')
  143. cy.mount(
  144. <SplitTestProvider>
  145. <TokenAccessPage />
  146. </SplitTestProvider>
  147. )
  148. cy.wait('@grantRequest')
  149. cy.get('h1').should('have.text', 'Overleaf v1 Project')
  150. cy.findByRole('link', { name: 'Download project zip file' }).should(
  151. 'have.attr',
  152. 'href',
  153. '/overleaf/project/123/download/zip'
  154. )
  155. })
  156. })