companion.js 4.9 KB

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