companion.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* global jest:false, test:false, expect:false, describe:false */
  2. const mockOauthState = require('../mockoauthstate')()
  3. jest.mock('tus-js-client')
  4. jest.mock('purest')
  5. jest.mock('../../src/server/helpers/oauth-state', () => ({
  6. ...jest.requireActual('../../src/server/helpers/oauth-state'),
  7. ...mockOauthState,
  8. }))
  9. const request = require('supertest')
  10. const tokenService = require('../../src/server/helpers/jwt')
  11. const { getServer } = require('../mockserver')
  12. const authServer = getServer()
  13. const authData = {
  14. dropbox: 'token value',
  15. box: 'token value',
  16. drive: 'token value',
  17. }
  18. const token = tokenService.generateEncryptedToken(authData, process.env.COMPANION_SECRET)
  19. const OAUTH_STATE = 'some-cool-nice-encrytpion'
  20. describe('validate upload data', () => {
  21. test('invalid upload protocol gets rejected', () => {
  22. return request(authServer)
  23. .post('/drive/get/DUMMY-FILE-ID')
  24. .set('uppy-auth-token', token)
  25. .set('Content-Type', 'application/json')
  26. .send({
  27. endpoint: 'http://url.myendpoint.com/files',
  28. protocol: 'tusInvalid',
  29. })
  30. .expect(400)
  31. .then((res) => expect(res.body.message).toBe('unsupported protocol specified'))
  32. })
  33. test('invalid upload fieldname gets rejected', () => {
  34. return request(authServer)
  35. .post('/drive/get/DUMMY-FILE-ID')
  36. .set('uppy-auth-token', token)
  37. .set('Content-Type', 'application/json')
  38. .send({
  39. endpoint: 'http://url.myendpoint.com/files',
  40. protocol: 'tus',
  41. fieldname: 390,
  42. })
  43. .expect(400)
  44. .then((res) => expect(res.body.message).toBe('fieldname must be a string'))
  45. })
  46. test('invalid upload metadata gets rejected', () => {
  47. return request(authServer)
  48. .post('/drive/get/DUMMY-FILE-ID')
  49. .set('uppy-auth-token', token)
  50. .set('Content-Type', 'application/json')
  51. .send({
  52. endpoint: 'http://url.myendpoint.com/files',
  53. protocol: 'tus',
  54. metadata: 'I am a string instead of object',
  55. })
  56. .expect(400)
  57. .then((res) => expect(res.body.message).toBe('metadata must be an object'))
  58. })
  59. test('invalid upload headers get rejected', () => {
  60. return request(authServer)
  61. .post('/drive/get/DUMMY-FILE-ID')
  62. .set('uppy-auth-token', token)
  63. .set('Content-Type', 'application/json')
  64. .send({
  65. endpoint: 'http://url.myendpoint.com/files',
  66. protocol: 'tus',
  67. headers: 'I am a string instead of object',
  68. })
  69. .expect(400)
  70. .then((res) => expect(res.body.message).toBe('headers must be an object'))
  71. })
  72. test('invalid upload HTTP Method gets rejected', () => {
  73. return request(authServer)
  74. .post('/drive/get/DUMMY-FILE-ID')
  75. .set('uppy-auth-token', token)
  76. .set('Content-Type', 'application/json')
  77. .send({
  78. endpoint: 'http://url.myendpoint.com/files',
  79. protocol: 'tus',
  80. httpMethod: 'DELETE',
  81. })
  82. .expect(400)
  83. .then((res) => expect(res.body.message).toBe('unsupported HTTP METHOD specified'))
  84. })
  85. test('valid upload data is allowed - tus', () => {
  86. return request(authServer)
  87. .post('/drive/get/DUMMY-FILE-ID')
  88. .set('uppy-auth-token', token)
  89. .set('Content-Type', 'application/json')
  90. .send({
  91. endpoint: 'http://url.myendpoint.com/files',
  92. protocol: 'tus',
  93. httpMethod: 'POST',
  94. headers: {
  95. customheader: 'header value',
  96. },
  97. metadata: {
  98. mymetadata: 'matadata value',
  99. },
  100. fieldname: 'uploadField',
  101. })
  102. .expect(200)
  103. })
  104. test('valid upload data is allowed - s3-multipart', () => {
  105. return request(authServer)
  106. .post('/drive/get/DUMMY-FILE-ID')
  107. .set('uppy-auth-token', token)
  108. .set('Content-Type', 'application/json')
  109. .send({
  110. endpoint: 'http://url.myendpoint.com/files',
  111. protocol: 's3-multipart',
  112. httpMethod: 'PUT',
  113. headers: {
  114. customheader: 'header value',
  115. },
  116. metadata: {
  117. mymetadata: 'matadata value',
  118. },
  119. fieldname: 'uploadField',
  120. })
  121. .expect(200)
  122. })
  123. })
  124. describe('handle main oauth redirect', () => {
  125. const serverWithMainOauth = getServer({
  126. COMPANION_OAUTH_DOMAIN: 'localhost:3040',
  127. })
  128. test('redirect to a valid uppy instance', () => {
  129. return request(serverWithMainOauth)
  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(serverWithMainOauth)
  138. .get(`/dropbox/redirect?state=${state}`)
  139. .set('uppy-auth-token', token)
  140. .expect(400)
  141. })
  142. })