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