companion.js 4.6 KB

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