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