providers.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. const request = require('supertest')
  2. const nock = require('nock')
  3. const mockOauthState = require('../mockoauthstate')
  4. jest.mock('tus-js-client')
  5. jest.mock('../../src/server/helpers/request', () => {
  6. return {
  7. getURLMeta: () => Promise.resolve({ size: 758051 }),
  8. }
  9. })
  10. jest.mock('../../src/server/helpers/oauth-state', () => mockOauthState())
  11. const fixtures = require('../fixtures')
  12. const { nockGoogleDownloadFile } = require('../fixtures/drive')
  13. const { nockZoomRecordings, nockZoomRevoke, expects: { localZoomKey, localZoomSecret } } = require('../fixtures/zoom')
  14. const defaults = require('../fixtures/constants')
  15. const tokenService = require('../../src/server/helpers/jwt')
  16. const { getServer } = require('../mockserver')
  17. // todo don't share server between tests. rewrite to not use env variables
  18. const authServer = getServer({ COMPANION_CLIENT_SOCKET_CONNECT_TIMEOUT: '0' })
  19. const OAUTH_STATE = 'some-cool-nice-encrytpion'
  20. const providers = require('../../src/server/provider').getDefaultProviders()
  21. const providerNames = Object.keys(providers)
  22. const oauthProviders = Object.fromEntries(
  23. Object.entries(providers).flatMap(([name, provider]) => (
  24. provider.oauthProvider != null ? [[name, provider.oauthProvider]] : []
  25. ))
  26. )
  27. const authData = {}
  28. providerNames.forEach((provider) => {
  29. authData[provider] = { accessToken: 'token value' }
  30. })
  31. const token = tokenService.generateEncryptedAuthToken(authData, process.env.COMPANION_SECRET)
  32. const thisOrThat = (value1, value2) => {
  33. if (value1 !== undefined) {
  34. return value1
  35. }
  36. return value2
  37. }
  38. beforeAll(() => {
  39. const url = new URL(defaults.THUMBNAIL_URL)
  40. nock(url.origin).get(url.pathname).reply(200, () => '').persist()
  41. })
  42. afterAll(() => {
  43. nock.cleanAll()
  44. nock.restore()
  45. })
  46. describe('list provider files', () => {
  47. async function runTest (providerName) {
  48. const providerFixtures = fixtures.providers[providerName].expects
  49. return request(authServer)
  50. .get(`/${providerName}/list/${providerFixtures.listPath || ''}`)
  51. .set('uppy-auth-token', token)
  52. .expect(200)
  53. .then((res) => {
  54. expect(res.header['i-am']).toBe('http://localhost:3020')
  55. expect(res.body.username).toBe(defaults.USERNAME)
  56. const items = [...res.body.items]
  57. // Drive has a virtual "shared-with-me" folder as the first item
  58. if (providerName === 'drive') {
  59. const item0 = items.shift()
  60. expect(item0.isFolder).toBe(true)
  61. expect(item0.name).toBe('Shared with me')
  62. expect(item0.mimeType).toBe('application/vnd.google-apps.folder')
  63. expect(item0.id).toBe('shared-with-me')
  64. expect(item0.requestPath).toBe('shared-with-me')
  65. expect(item0.icon).toBe('folder')
  66. }
  67. const item = items[0]
  68. expect(item.isFolder).toBe(false)
  69. expect(item.name).toBe(providerFixtures.itemName || defaults.ITEM_NAME)
  70. expect(item.mimeType).toBe(providerFixtures.itemMimeType || defaults.MIME_TYPE)
  71. expect(item.id).toBe(providerFixtures.itemId || defaults.ITEM_ID)
  72. expect(item.size).toBe(thisOrThat(providerFixtures.itemSize, defaults.FILE_SIZE))
  73. expect(item.requestPath).toBe(providerFixtures.itemRequestPath || defaults.ITEM_ID)
  74. expect(item.icon).toBe(providerFixtures.itemIcon || defaults.THUMBNAIL_URL)
  75. })
  76. }
  77. test('dropbox', async () => {
  78. nock('https://api.dropboxapi.com').post('/2/users/get_current_account').reply(200, {
  79. name: {
  80. given_name: 'Franz',
  81. surname: 'Ferdinand',
  82. familiar_name: 'Franz',
  83. display_name: 'Franz Ferdinand (Personal)',
  84. abbreviated_name: 'FF',
  85. },
  86. email: defaults.USERNAME,
  87. email_verified: true,
  88. disabled: false,
  89. locale: 'en',
  90. referral_link: 'https://db.tt/ZITNuhtI',
  91. is_paired: true,
  92. })
  93. nock('https://api.dropboxapi.com').post('/2/files/list_folder').reply(200, {
  94. entries: [
  95. {
  96. '.tag': 'file',
  97. name: defaults.ITEM_NAME,
  98. id: defaults.ITEM_ID,
  99. client_modified: '2015-05-12T15:50:38Z',
  100. server_modified: '2015-05-12T15:50:38Z',
  101. rev: 'a1c10ce0dd78',
  102. size: defaults.FILE_SIZE,
  103. path_lower: '/homework/math/prime_numbers.txt',
  104. path_display: '/Homework/math/Prime_Numbers.txt',
  105. is_downloadable: true,
  106. has_explicit_shared_members: false,
  107. content_hash: 'e3b0c44298fc1c149afbf41e4649b934ca49',
  108. file_lock_info: {
  109. is_lockholder: true,
  110. lockholder_name: 'Imaginary User',
  111. created: '2015-05-12T15:50:38Z',
  112. },
  113. },
  114. ],
  115. cursor: 'ZtkX9_EHj3x7PMkVuFIhwKYXEpwpLwyxp9vMKomUhllil9q7eWiAu',
  116. has_more: false,
  117. })
  118. await runTest('dropbox')
  119. })
  120. test('box', async () => {
  121. nock('https://api.box.com').get('/2.0/users/me').reply(200, {
  122. login: defaults.USERNAME,
  123. })
  124. nock('https://api.box.com').get('/2.0/folders/0/items?fields=id%2Cmodified_at%2Cname%2Cpermissions%2Csize%2Ctype&limit=1000').reply(200, {
  125. entries: [
  126. {
  127. type: 'file',
  128. name: defaults.ITEM_NAME,
  129. id: defaults.ITEM_ID,
  130. modified_at: '2015-05-12T15:50:38Z',
  131. size: defaults.FILE_SIZE,
  132. },
  133. ],
  134. })
  135. await runTest('box')
  136. })
  137. test('drive', async () => {
  138. nock('https://www.googleapis.com').get('/drive/v3/drives?fields=*&pageToken=&pageSize=100').reply(200, {
  139. kind: 'drive#driveList', drives: [],
  140. })
  141. nock('https://www.googleapis.com').get('/drive/v3/files?fields=kind%2CnextPageToken%2CincompleteSearch%2Cfiles%28kind%2Cid%2CimageMediaMetadata%2Cname%2CmimeType%2CownedByMe%2Csize%2CmodifiedTime%2CiconLink%2CthumbnailLink%2CteamDriveId%2CvideoMediaMetadata%2CexportLinks%2CshortcutDetails%28targetId%2CtargetMimeType%29%29&q=%28%27root%27+in+parents%29+and+trashed%3Dfalse&pageSize=1000&orderBy=folder%2Cname&includeItemsFromAllDrives=true&supportsAllDrives=true').reply(200, {
  142. kind: 'drive#fileList',
  143. nextPageToken: defaults.NEXT_PAGE_TOKEN,
  144. files: [
  145. {
  146. kind: 'drive#file',
  147. id: defaults.ITEM_ID,
  148. name: defaults.ITEM_NAME,
  149. mimeType: defaults.MIME_TYPE,
  150. iconLink: 'https://drive-thirdparty.googleusercontent.com/16/type/video/mp4',
  151. thumbnailLink: defaults.THUMBNAIL_URL,
  152. modifiedTime: '2016-07-10T20:00:08.096Z',
  153. ownedByMe: true,
  154. permissions: [{ role: 'owner', emailAddress: defaults.USERNAME }],
  155. size: '758051',
  156. },
  157. ],
  158. })
  159. nock('https://www.googleapis.com').get((uri) => uri.includes('about')).reply(200, { user: { emailAddress: 'john.doe@transloadit.com' } })
  160. await runTest('drive')
  161. })
  162. test('facebook', async () => {
  163. nock('https://graph.facebook.com').get('/me?fields=email').reply(200, {
  164. name: 'Fiona Fox',
  165. birthday: '01/01/1985',
  166. email: defaults.USERNAME,
  167. })
  168. nock('https://graph.facebook.com').get('/ALBUM-ID/photos?fields=icon%2Cimages%2Cname%2Cwidth%2Cheight%2Ccreated_time').reply(200, {
  169. data: [
  170. {
  171. images: [
  172. {
  173. height: 1365,
  174. source: defaults.THUMBNAIL_URL,
  175. width: 2048,
  176. },
  177. ],
  178. width: 720,
  179. height: 479,
  180. created_time: '2015-07-17T17:26:50+0000',
  181. id: defaults.ITEM_ID,
  182. },
  183. ],
  184. paging: {},
  185. })
  186. await runTest('facebook')
  187. })
  188. test('instagram', async () => {
  189. nock('https://graph.instagram.com').get('/me?fields=username').reply(200, {
  190. id: '17841405793187218',
  191. username: defaults.USERNAME,
  192. })
  193. nock('https://graph.instagram.com').get('/me/media?fields=id%2Cmedia_type%2Cthumbnail_url%2Cmedia_url%2Ctimestamp%2Cchildren%7Bmedia_type%2Cmedia_url%2Cthumbnail_url%2Ctimestamp%7D').reply(200, {
  194. data: [
  195. {
  196. id: defaults.ITEM_ID,
  197. media_type: 'IMAGE',
  198. timestamp: '2017-08-31T18:10:00+0000',
  199. media_url: defaults.THUMBNAIL_URL,
  200. },
  201. ],
  202. })
  203. await runTest('instagram')
  204. })
  205. test('onedrive', async () => {
  206. nock('https://graph.microsoft.com').get('/v1.0/me').reply(200, {
  207. userPrincipalName: defaults.USERNAME,
  208. mail: defaults.USERNAME,
  209. })
  210. nock('https://graph.microsoft.com').get('/v1.0/me/drive/root/children?%24expand=thumbnails&%24top=999').reply(200, {
  211. value: [
  212. {
  213. createdDateTime: '2020-01-31T15:40:26.197Z',
  214. id: defaults.ITEM_ID,
  215. lastModifiedDateTime: '2020-01-31T15:40:38.723Z',
  216. name: defaults.ITEM_NAME,
  217. size: defaults.FILE_SIZE,
  218. parentReference: {
  219. driveId: 'DUMMY-DRIVE-ID',
  220. driveType: 'personal',
  221. path: '/drive/root:',
  222. },
  223. file: {
  224. mimeType: defaults.MIME_TYPE,
  225. },
  226. thumbnails: [{
  227. id: '0',
  228. large: {
  229. height: 452,
  230. url: defaults.THUMBNAIL_URL,
  231. width: 800,
  232. },
  233. medium: {
  234. height: 100,
  235. url: defaults.THUMBNAIL_URL,
  236. width: 176,
  237. },
  238. small: {
  239. height: 54,
  240. url: defaults.THUMBNAIL_URL,
  241. width: 96,
  242. },
  243. }],
  244. },
  245. ],
  246. })
  247. await runTest('onedrive')
  248. })
  249. test('zoom', async () => {
  250. nock('https://zoom.us').get('/v2/users/me').reply(200, {
  251. id: 'DUMMY-USER-ID',
  252. first_name: 'John',
  253. last_name: 'Doe',
  254. email: 'john.doe@transloadit.com',
  255. timezone: '',
  256. dept: '',
  257. created_at: '2020-07-21T09:13:30Z',
  258. last_login_time: '2020-10-12T07:55:02Z',
  259. group_ids: [],
  260. im_group_ids: [],
  261. account_id: 'DUMMY-ACCOUNT-ID',
  262. language: 'en-US',
  263. })
  264. nockZoomRecordings()
  265. await runTest('zoom')
  266. })
  267. })
  268. describe('provider file gets downloaded from', () => {
  269. async function runTest (providerName) {
  270. const providerFixtures = fixtures.providers[providerName].expects
  271. const res = await request(authServer)
  272. .post(`/${providerName}/get/${providerFixtures.itemRequestPath || defaults.ITEM_ID}`)
  273. .set('uppy-auth-token', token)
  274. .set('Content-Type', 'application/json')
  275. .send({
  276. endpoint: 'http://tusd.tusdemo.net/files',
  277. protocol: 'tus',
  278. })
  279. .expect(200)
  280. expect(res.body.token).toBeTruthy()
  281. }
  282. test('dropbox', async () => {
  283. nock('https://api.dropboxapi.com').post('/2/files/get_metadata').reply(200, { size: defaults.FILE_SIZE })
  284. nock('https://content.dropboxapi.com').post('/2/files/download').reply(200, {})
  285. await runTest('dropbox')
  286. })
  287. test('box', async () => {
  288. nock('https://api.box.com').get(`/2.0/files/${defaults.ITEM_ID}`).reply(200, { size: defaults.FILE_SIZE })
  289. nock('https://api.box.com').get(`/2.0/files/${defaults.ITEM_ID}/content`).reply(200, { size: defaults.FILE_SIZE })
  290. await runTest('box')
  291. })
  292. test('drive', async () => {
  293. // times(2) because of size request
  294. nockGoogleDownloadFile({ times: 2 })
  295. await runTest('drive')
  296. })
  297. test('facebook', async () => {
  298. // times(2) because of size request
  299. nock('https://graph.facebook.com').get(`/${defaults.ITEM_ID}?fields=images`).times(2).reply(200, {
  300. images: [
  301. {
  302. height: 1365,
  303. source: defaults.THUMBNAIL_URL,
  304. width: 2048,
  305. },
  306. ],
  307. id: defaults.ITEM_ID,
  308. })
  309. await runTest('facebook')
  310. })
  311. test('instagram', async () => {
  312. // times(2) because of size request
  313. nock('https://graph.instagram.com').get(`/${defaults.ITEM_ID}?fields=media_url`).times(2).reply(200, {
  314. id: defaults.ITEM_ID,
  315. media_type: 'IMAGE',
  316. media_url: defaults.THUMBNAIL_URL,
  317. timestamp: '2017-08-31T18:10:00+0000',
  318. })
  319. await runTest('instagram')
  320. })
  321. test('onedrive', async () => {
  322. nock('https://graph.microsoft.com').get(`/v1.0/drives/DUMMY-DRIVE-ID/items/${defaults.ITEM_ID}`).reply(200, {
  323. size: defaults.FILE_SIZE,
  324. })
  325. nock('https://graph.microsoft.com').get(`/v1.0/drives/DUMMY-DRIVE-ID/items/${defaults.ITEM_ID}/content`).reply(200, {})
  326. await runTest('onedrive')
  327. })
  328. test('zoom', async () => {
  329. // times(2) because of size request
  330. nockZoomRecordings({ times: 2 })
  331. nock('https://us02web.zoom.us').get('/rec/download/DUMMY-DOWNLOAD-PATH?access_token=token%20value').reply(200, {})
  332. await runTest('zoom')
  333. })
  334. })
  335. describe('connect to provider', () => {
  336. test.each(providerNames)('connect to %s via grant.js endpoint', async (providerName) => {
  337. const oauthProvider = oauthProviders[providerName]
  338. if (oauthProvider == null) return
  339. await request(authServer)
  340. .get(`/${providerName}/connect?foo=bar`)
  341. .set('uppy-auth-token', token)
  342. .expect(302)
  343. .expect('Location', `http://localhost:3020/connect/${oauthProvider}?state=${OAUTH_STATE}`)
  344. })
  345. })
  346. describe('logout of provider', () => {
  347. async function runTest (providerName) {
  348. const res = await request(authServer)
  349. .get(`/${providerName}/logout/`)
  350. .set('uppy-auth-token', token)
  351. .expect(200)
  352. // only some providers can actually be revoked
  353. const expectRevoked = ['box', 'dropbox', 'drive', 'facebook', 'zoom'].includes(providerName)
  354. expect(res.body).toMatchObject({
  355. ok: true,
  356. revoked: expectRevoked,
  357. })
  358. }
  359. test('dropbox', async () => {
  360. nock('https://api.dropboxapi.com').post('/2/auth/token/revoke').reply(200, {})
  361. await runTest('dropbox')
  362. })
  363. test('box', async () => {
  364. nock('https://api.box.com').post('/oauth2/revoke').reply(200, {})
  365. await runTest('box')
  366. })
  367. test('dropbox', async () => {
  368. nock('https://api.dropboxapi.com').post('/2/auth/token/revoke').reply(200, {})
  369. await runTest('dropbox')
  370. })
  371. test('drive', async () => {
  372. nock('https://accounts.google.com').post('/o/oauth2/revoke?token=token+value').reply(200, {})
  373. await runTest('drive')
  374. })
  375. test('facebook', async () => {
  376. nock('https://graph.facebook.com').delete('/me/permissions').reply(200, {})
  377. await runTest('facebook')
  378. })
  379. test('instagram', async () => {
  380. await runTest('instagram')
  381. })
  382. test('onedrive', async () => {
  383. await runTest('onedrive')
  384. })
  385. test('zoom', async () => {
  386. nockZoomRevoke({ key: localZoomKey, secret: localZoomSecret })
  387. await runTest('zoom')
  388. })
  389. })