token-access-page.spec.tsx 4.1 KB

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