token-access-page.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import TokenAccessPage from '@/features/token-access/components/token-access-root'
  2. import { location } from '@/shared/components/location'
  3. describe('<TokenAccessPage/>', function () {
  4. // this is a URL for a read-only token, but the process is the same for read-write tokens
  5. const url = '/read/123/grant'
  6. beforeEach(function () {
  7. cy.window().then(win => {
  8. win.metaAttributesCache.set('ol-postUrl', url)
  9. win.metaAttributesCache.set('ol-user', { email: 'test@example.com' })
  10. })
  11. })
  12. it('handles a successful token access request', function () {
  13. cy.intercept(
  14. { method: 'post', url, times: 1 },
  15. {
  16. body: {
  17. requireAccept: { projectName: 'Test Project' },
  18. },
  19. }
  20. ).as('grantRequest')
  21. cy.mount(<TokenAccessPage />)
  22. cy.wait('@grantRequest').then(interception => {
  23. expect(interception.request.body.confirmedByUser).to.be.false
  24. })
  25. cy.get('.link-sharing-invite-header').should(
  26. 'have.text',
  27. ['You’re joining', 'Test Project', 'as test@example.com'].join('')
  28. )
  29. cy.intercept(
  30. { method: 'post', url, times: 1 },
  31. {
  32. body: {
  33. redirect: '/project/123',
  34. },
  35. }
  36. ).as('confirmedGrantRequest')
  37. cy.stub(location, 'replace').as('replaceLocation')
  38. cy.findByRole('button', { name: 'OK, join project' }).click()
  39. cy.wait('@confirmedGrantRequest').then(interception => {
  40. expect(interception.request.body.confirmedByUser).to.be.true
  41. })
  42. cy.get('@replaceLocation').should(
  43. 'have.been.calledOnceWith',
  44. '/project/123'
  45. )
  46. })
  47. it('handles a project not found response', function () {
  48. cy.intercept({ method: 'post', url, times: 1 }, { statusCode: 404 }).as(
  49. 'grantRequest'
  50. )
  51. cy.mount(<TokenAccessPage />)
  52. cy.wait('@grantRequest')
  53. cy.get('h3').should('have.text', 'Join Project')
  54. cy.get('h4').should('have.text', 'Project not found')
  55. cy.findByRole('button', { name: 'Join Project' }).should('not.exist')
  56. })
  57. it('handles a redirect response', function () {
  58. cy.intercept(
  59. { method: 'post', url, times: 1 },
  60. {
  61. body: {
  62. redirect: '/restricted',
  63. },
  64. }
  65. ).as('grantRequest')
  66. cy.stub(location, 'replace').as('replaceLocation')
  67. cy.mount(<TokenAccessPage />)
  68. cy.wait('@grantRequest')
  69. cy.get('@replaceLocation').should('have.been.calledOnceWith', '/restricted')
  70. })
  71. it('handles a v1 "must login" response', function () {
  72. cy.intercept(
  73. { method: 'post', url, times: 1 },
  74. {
  75. body: {
  76. v1Import: { status: 'mustLogin' },
  77. },
  78. }
  79. ).as('grantRequest')
  80. cy.stub(location, 'replace').as('replaceLocation')
  81. cy.mount(<TokenAccessPage />)
  82. cy.wait('@grantRequest')
  83. cy.get('h1').should('have.text', 'Please log in')
  84. cy.findByRole('link', { name: 'Log in to access project' })
  85. .should('have.attr', 'href')
  86. .and('match', /^\/login\?redir=/)
  87. })
  88. it('handles a v1 "cannot import" response', function () {
  89. cy.intercept(
  90. { method: 'post', url, times: 1 },
  91. {
  92. body: {
  93. v1Import: { status: 'cannotImport' },
  94. },
  95. }
  96. ).as('grantRequest')
  97. cy.stub(location, 'replace').as('replaceLocation')
  98. cy.mount(<TokenAccessPage />)
  99. cy.wait('@grantRequest')
  100. cy.get('h1').should('have.text', 'Overleaf v1 Project')
  101. cy.get('h2').should('have.text', 'Cannot Access Overleaf v1 Project')
  102. })
  103. it('handles a v1 "can download zip" response', function () {
  104. cy.intercept(
  105. { method: 'post', url, times: 1 },
  106. {
  107. body: {
  108. v1Import: {
  109. status: 'canDownloadZip',
  110. projectId: '123',
  111. name: 'Test Project',
  112. },
  113. },
  114. }
  115. ).as('grantRequest')
  116. cy.stub(location, 'replace').as('replaceLocation')
  117. cy.mount(<TokenAccessPage />)
  118. cy.wait('@grantRequest')
  119. cy.get('h1').should('have.text', 'Overleaf v1 Project')
  120. cy.findByRole('link', { name: 'Download project zip file' }).should(
  121. 'have.attr',
  122. 'href',
  123. '/overleaf/project/123/download/zip'
  124. )
  125. })
  126. })