companion.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* global jest:false, test:false, expect:false, describe:false */
  2. jest.mock('tus-js-client')
  3. jest.mock('purest')
  4. jest.mock('../../src/server/helpers/oauth-state', () => {
  5. return {
  6. generateState: () => 'some-cool-nice-encrytpion',
  7. addToState: () => 'some-cool-nice-encrytpion',
  8. getFromState: (state) => {
  9. return state === 'state-with-invalid-instance-url' ? 'http://localhost:3452' : 'http://localhost:3020'
  10. }
  11. }
  12. })
  13. const request = require('supertest')
  14. const tokenService = require('../../src/server/helpers/jwt')
  15. const { authServer, noAuthServer } = require('../mockserver')
  16. const authData = {
  17. dropbox: 'token value',
  18. drive: 'token value'
  19. }
  20. const token = tokenService.generateToken(authData, process.env.UPPYSERVER_SECRET)
  21. const OAUTH_STATE = 'some-cool-nice-encrytpion'
  22. describe('set i-am header', () => {
  23. test('set i-am header in response', () => {
  24. return request(authServer)
  25. .get('/dropbox/list/')
  26. .set('uppy-auth-token', token)
  27. .expect(200)
  28. .then((res) => expect(res.header['i-am']).toBe('http://localhost:3020'))
  29. })
  30. })
  31. describe('list provider files', () => {
  32. test('list files for dropbox', () => {
  33. return request(authServer)
  34. .get('/dropbox/list/')
  35. .set('uppy-auth-token', token)
  36. .expect(200)
  37. .then((res) => expect(res.body.hash).toBe('0a9f95a989dd4b1851f0103c31e304ce'))
  38. })
  39. test('list files for google drive', () => {
  40. return request(authServer)
  41. .get('/drive/list/')
  42. .set('uppy-auth-token', token)
  43. .expect(200)
  44. .then((res) => expect(res.body.etag).toBe('"bcIyJ9A3gXa8oTYmz6nzAjQd-lY/eQc3WbZHkXpcItNyGKDuKXM_bNY"'))
  45. })
  46. })
  47. describe('download provdier file', () => {
  48. test('specified file gets downloaded from provider', () => {
  49. return request(authServer)
  50. .post('/drive/get/README.md')
  51. .set('uppy-auth-token', token)
  52. .set('Content-Type', 'application/json')
  53. .send({
  54. endpoint: 'http://master.tus.com/files',
  55. protocol: 'tus'
  56. })
  57. .expect(200)
  58. .then((res) => expect(res.body.token).toBeTruthy())
  59. })
  60. })
  61. describe('test authentication', () => {
  62. test('authentication callback redirects to specified url', () => {
  63. return request(authServer)
  64. .get('/drive/callback')
  65. .set('uppy-auth-token', token)
  66. .expect(200)
  67. .expect((res) => {
  68. const authToken = res.header['set-cookie'][0].split(';')[0].split('uppyAuthToken=')[1]
  69. // see mock ../../src/server/helpers/oauth-state above for http://localhost:3020
  70. const body = `
  71. <!DOCTYPE html>
  72. <html>
  73. <head>
  74. <meta charset="utf-8" />
  75. <script>
  76. window.opener.postMessage({token: "${authToken}"}, "http://localhost:3020")
  77. window.close()
  78. </script>
  79. </head>
  80. <body></body>
  81. </html>`
  82. expect(res.text).toBe(body)
  83. })
  84. })
  85. test('check for authenticated provider', () => {
  86. request(authServer)
  87. .get('/drive/authorized/')
  88. .set('uppy-auth-token', token)
  89. .expect(200)
  90. .then((res) => expect(res.body.authenticated).toBe(true))
  91. request(noAuthServer)
  92. .get('/drive/authorized/')
  93. .expect(200)
  94. .then((res) => expect(res.body.authenticated).toBe(false))
  95. })
  96. test('logout provider', () => {
  97. return request(authServer)
  98. .get('/drive/logout/')
  99. .set('uppy-auth-token', token)
  100. .expect(200)
  101. .then((res) => expect(res.body.ok).toBe(true))
  102. })
  103. })
  104. describe('connect to provider', () => {
  105. test('connect to dropbox via grant.js endpoint', () => {
  106. return request(authServer)
  107. .get('/dropbox/connect?foo=bar')
  108. .set('uppy-auth-token', token)
  109. .expect(302)
  110. .expect('Location', `http://localhost:3020/connect/dropbox?state=${OAUTH_STATE}`)
  111. })
  112. test('connect to drive via grant.js endpoint', () => {
  113. return request(authServer)
  114. .get('/drive/connect?foo=bar')
  115. .set('uppy-auth-token', token)
  116. .expect(302)
  117. .expect('Location', `http://localhost:3020/connect/google?state=${OAUTH_STATE}`)
  118. })
  119. })
  120. describe('handle oauth redirect', () => {
  121. test('redirect to a valid uppy instance', () => {
  122. return request(authServer)
  123. .get(`/dropbox/redirect?state=${OAUTH_STATE}`)
  124. .set('uppy-auth-token', token)
  125. .expect(302)
  126. .expect('Location', `http://localhost:3020/connect/dropbox/callback?state=${OAUTH_STATE}`)
  127. })
  128. test('do not redirect to invalid uppy instances', () => {
  129. const state = 'state-with-invalid-instance-url' // see mock ../../src/server/helpers/oauth-state above
  130. return request(authServer)
  131. .get(`/dropbox/redirect?state=${state}`)
  132. .set('uppy-auth-token', token)
  133. .expect(400)
  134. })
  135. })