companion.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 } = require('../mockserver')
  16. const authData = {
  17. dropbox: 'token value',
  18. drive: 'token value'
  19. }
  20. const token = tokenService.generateToken(authData, process.env.COMPANION_SECRET || 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.username).toBe('foo@bar.com'))
  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.username).toBe('ife@bala.com'))
  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. test('download should fail if protocol is not specified', () => {
  61. return request(authServer)
  62. .post('/drive/get/README.md')
  63. .set('uppy-auth-token', token)
  64. .set('Content-Type', 'application/json')
  65. .send({
  66. endpoint: 'http://master.tus.com/files'
  67. })
  68. .expect(400)
  69. })
  70. })
  71. describe('test authentication', () => {
  72. test('authentication callback redirects to send-token url', () => {
  73. return request(authServer)
  74. .get('/drive/callback')
  75. .expect(302)
  76. .expect((res) => {
  77. expect(res.header['location']).toContain('http://localhost:3020/drive/send-token?uppyAuthToken=')
  78. })
  79. })
  80. test('the token gets sent via cookie and html', () => {
  81. return request(authServer)
  82. .get(`/drive/send-token?uppyAuthToken=${token}`)
  83. .expect(200)
  84. .expect((res) => {
  85. const authToken = res.header['set-cookie'][0].split(';')[0].split('uppyAuthToken--google=')[1]
  86. expect(authToken).toEqual(token)
  87. // see mock ../../src/server/helpers/oauth-state above for http://localhost:3020
  88. const body = `
  89. <!DOCTYPE html>
  90. <html>
  91. <head>
  92. <meta charset="utf-8" />
  93. <script>
  94. window.opener.postMessage(JSON.stringify({token: "${token}"}), "http://localhost:3020")
  95. window.close()
  96. </script>
  97. </head>
  98. <body></body>
  99. </html>`
  100. expect(res.text).toBe(body)
  101. })
  102. })
  103. test('logout provider', () => {
  104. return request(authServer)
  105. .get('/drive/logout/')
  106. .set('uppy-auth-token', token)
  107. .expect(200)
  108. .then((res) => expect(res.body.ok).toBe(true))
  109. })
  110. })
  111. describe('connect to provider', () => {
  112. test('connect to dropbox via grant.js endpoint', () => {
  113. return request(authServer)
  114. .get('/dropbox/connect?foo=bar')
  115. .set('uppy-auth-token', token)
  116. .expect(302)
  117. .expect('Location', `http://localhost:3020/connect/dropbox?state=${OAUTH_STATE}`)
  118. })
  119. test('connect to drive via grant.js endpoint', () => {
  120. return request(authServer)
  121. .get('/drive/connect?foo=bar')
  122. .set('uppy-auth-token', token)
  123. .expect(302)
  124. .expect('Location', `http://localhost:3020/connect/google?state=${OAUTH_STATE}`)
  125. })
  126. })
  127. describe('handle oauth redirect', () => {
  128. test('redirect to a valid uppy instance', () => {
  129. return request(authServer)
  130. .get(`/dropbox/redirect?state=${OAUTH_STATE}`)
  131. .set('uppy-auth-token', token)
  132. .expect(302)
  133. .expect('Location', `http://localhost:3020/connect/dropbox/callback?state=${OAUTH_STATE}`)
  134. })
  135. test('do not redirect to invalid uppy instances', () => {
  136. const state = 'state-with-invalid-instance-url' // see mock ../../src/server/helpers/oauth-state above
  137. return request(authServer)
  138. .get(`/dropbox/redirect?state=${state}`)
  139. .set('uppy-auth-token', token)
  140. .expect(400)
  141. })
  142. })