token-access-page.spec.tsx 4.1 KB

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