token-access-page.spec.tsx 4.1 KB

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