providers.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 AUTH_PROVIDERS = {
  23. drive: 'googledrive',
  24. googlephotos: 'googlephotos',
  25. onedrive: 'microsoft',
  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 providerFixture = fixtures.providers[providerName]?.expects ?? {}
  49. return request(authServer)
  50. .get(`/${providerName}/list/${providerFixture.listPath || ''}`)
  51. .set('uppy-auth-token', token)
  52. .expect(200)
  53. .then((res) => {
  54. expect(res.header['i-am']).toBe('http://localhost:3020')
  55. return {
  56. username: res.body.username,
  57. items: res.body.items,
  58. providerFixture,
  59. }
  60. })
  61. }
  62. function expect1({ username, items, providerFixture }) {
  63. expect(username).toBe(defaults.USERNAME)
  64. const item = items[0]
  65. expect(item.isFolder).toBe(false)
  66. expect(item.name).toBe(providerFixture.itemName || defaults.ITEM_NAME)
  67. expect(item.mimeType).toBe(providerFixture.itemMimeType || defaults.MIME_TYPE)
  68. expect(item.id).toBe(providerFixture.itemId || defaults.ITEM_ID)
  69. expect(item.size).toBe(thisOrThat(providerFixture.itemSize, defaults.FILE_SIZE))
  70. expect(item.requestPath).toBe(providerFixture.itemRequestPath || defaults.ITEM_ID)
  71. expect(item.icon).toBe(providerFixture.itemIcon || defaults.THUMBNAIL_URL)
  72. }
  73. test('dropbox', async () => {
  74. nock('https://api.dropboxapi.com').post('/2/users/get_current_account').reply(200, {
  75. name: {
  76. given_name: 'Franz',
  77. surname: 'Ferdinand',
  78. familiar_name: 'Franz',
  79. display_name: 'Franz Ferdinand (Personal)',
  80. abbreviated_name: 'FF',
  81. },
  82. email: defaults.USERNAME,
  83. email_verified: true,
  84. disabled: false,
  85. locale: 'en',
  86. referral_link: 'https://db.tt/ZITNuhtI',
  87. is_paired: true,
  88. })
  89. nock('https://api.dropboxapi.com').post('/2/files/list_folder').reply(200, {
  90. entries: [
  91. {
  92. '.tag': 'file',
  93. name: defaults.ITEM_NAME,
  94. id: defaults.ITEM_ID,
  95. client_modified: '2015-05-12T15:50:38Z',
  96. server_modified: '2015-05-12T15:50:38Z',
  97. rev: 'a1c10ce0dd78',
  98. size: defaults.FILE_SIZE,
  99. path_lower: '/homework/math/prime_numbers.txt',
  100. path_display: '/Homework/math/Prime_Numbers.txt',
  101. is_downloadable: true,
  102. has_explicit_shared_members: false,
  103. content_hash: 'e3b0c44298fc1c149afbf41e4649b934ca49',
  104. file_lock_info: {
  105. is_lockholder: true,
  106. lockholder_name: 'Imaginary User',
  107. created: '2015-05-12T15:50:38Z',
  108. },
  109. },
  110. ],
  111. cursor: 'ZtkX9_EHj3x7PMkVuFIhwKYXEpwpLwyxp9vMKomUhllil9q7eWiAu',
  112. has_more: false,
  113. })
  114. const { username, items, providerFixture } = await runTest('dropbox')
  115. expect1({ username, items, providerFixture })
  116. })
  117. test('box', async () => {
  118. nock('https://api.box.com').get('/2.0/users/me').reply(200, {
  119. login: defaults.USERNAME,
  120. })
  121. nock('https://api.box.com').get('/2.0/folders/0/items?fields=id%2Cmodified_at%2Cname%2Cpermissions%2Csize%2Ctype&limit=1000').reply(200, {
  122. entries: [
  123. {
  124. type: 'file',
  125. name: defaults.ITEM_NAME,
  126. id: defaults.ITEM_ID,
  127. modified_at: '2015-05-12T15:50:38Z',
  128. size: defaults.FILE_SIZE,
  129. },
  130. ],
  131. })
  132. const { username, items, providerFixture } = await runTest('box')
  133. expect1({ username, items, providerFixture })
  134. })
  135. test('drive', async () => {
  136. nock('https://www.googleapis.com').get('/drive/v3/drives?fields=*&pageToken=&pageSize=100').reply(200, {
  137. kind: 'drive#driveList', drives: [],
  138. })
  139. 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, {
  140. kind: 'drive#fileList',
  141. nextPageToken: defaults.NEXT_PAGE_TOKEN,
  142. files: [
  143. {
  144. kind: 'drive#file',
  145. id: defaults.ITEM_ID,
  146. name: defaults.ITEM_NAME,
  147. mimeType: defaults.MIME_TYPE,
  148. iconLink: 'https://drive-thirdparty.googleusercontent.com/16/type/video/mp4',
  149. thumbnailLink: defaults.THUMBNAIL_URL,
  150. modifiedTime: '2016-07-10T20:00:08.096Z',
  151. ownedByMe: true,
  152. permissions: [{ role: 'owner', emailAddress: defaults.USERNAME }],
  153. size: '758051',
  154. },
  155. ],
  156. })
  157. nock('https://www.googleapis.com').get((uri) => uri.includes('about')).reply(200, { user: { emailAddress: 'john.doe@transloadit.com' } })
  158. const { username, items, providerFixture } = await runTest('drive')
  159. // Drive has a virtual "shared-with-me" folder as the first item
  160. const [item0, ...rest] = items
  161. expect(item0.isFolder).toBe(true)
  162. expect(item0.name).toBe('Shared with me')
  163. expect(item0.mimeType).toBe('application/vnd.google-apps.folder')
  164. expect(item0.id).toBe('shared-with-me')
  165. expect(item0.requestPath).toBe('shared-with-me')
  166. expect(item0.icon).toBe('folder')
  167. expect1({ username, items: rest, providerFixture })
  168. })
  169. test('googlephotos', async () => {
  170. nock('https://photoslibrary.googleapis.com').get('/v1/albums?pageSize=50').reply(200, {
  171. albums: [
  172. {
  173. coverPhotoBaseUrl: 'https://test',
  174. title: 'album',
  175. id: '1',
  176. }
  177. ]
  178. })
  179. nock('https://photoslibrary.googleapis.com').get('/v1/sharedAlbums?pageSize=50').reply(200, {
  180. sharedAlbums: [
  181. {
  182. coverPhotoBaseUrl: 'https://test2',
  183. title: 'shared album',
  184. id: '2',
  185. }
  186. ]
  187. })
  188. nock('https://www.googleapis.com').get('/oauth2/v1/userinfo').reply(200, {
  189. email: defaults.USERNAME,
  190. })
  191. const { items } = await runTest('googlephotos')
  192. expect(items[0].isFolder).toBe(true)
  193. expect(items[0].name).toBe('album')
  194. expect(items[0].id).toBe('1')
  195. expect(items[0].requestPath).toBe('1')
  196. expect(items[0].icon).toBe('https://drive-thirdparty.googleusercontent.com/32/type/application/vnd.google-apps.folder')
  197. expect(items[0].thumbnail).toBe('https://test=w300-h300-c')
  198. expect(items[1].isFolder).toBe(true)
  199. expect(items[1].name).toBe('shared album')
  200. expect(items[1].id).toBe('2')
  201. expect(items[1].requestPath).toBe('2')
  202. expect(items[1].icon).toBe('https://drive-thirdparty.googleusercontent.com/32/type/application/vnd.google-apps.folder')
  203. expect(items[1].thumbnail).toBe('https://test2=w300-h300-c')
  204. })
  205. test('facebook', async () => {
  206. nock('https://graph.facebook.com').get('/me?fields=email').reply(200, {
  207. name: 'Fiona Fox',
  208. birthday: '01/01/1985',
  209. email: defaults.USERNAME,
  210. })
  211. nock('https://graph.facebook.com').get('/ALBUM-ID/photos?fields=icon%2Cimages%2Cname%2Cwidth%2Cheight%2Ccreated_time').reply(200, {
  212. data: [
  213. {
  214. images: [
  215. {
  216. height: 1365,
  217. source: defaults.THUMBNAIL_URL,
  218. width: 2048,
  219. },
  220. ],
  221. width: 720,
  222. height: 479,
  223. created_time: '2015-07-17T17:26:50+0000',
  224. id: defaults.ITEM_ID,
  225. },
  226. ],
  227. paging: {},
  228. })
  229. const { username, items, providerFixture } = await runTest('facebook')
  230. expect1({ username, items, providerFixture })
  231. })
  232. test('instagram', async () => {
  233. nock('https://graph.instagram.com').get('/me?fields=username').reply(200, {
  234. id: '17841405793187218',
  235. username: defaults.USERNAME,
  236. })
  237. 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, {
  238. data: [
  239. {
  240. id: defaults.ITEM_ID,
  241. media_type: 'IMAGE',
  242. timestamp: '2017-08-31T18:10:00+0000',
  243. media_url: defaults.THUMBNAIL_URL,
  244. },
  245. ],
  246. })
  247. const { username, items, providerFixture } = await runTest('instagram')
  248. expect1({ username, items, providerFixture })
  249. })
  250. test('onedrive', async () => {
  251. nock('https://graph.microsoft.com').get('/v1.0/me').reply(200, {
  252. userPrincipalName: defaults.USERNAME,
  253. mail: defaults.USERNAME,
  254. })
  255. nock('https://graph.microsoft.com').get('/v1.0/me/drive/root/children?%24expand=thumbnails&%24top=999').reply(200, {
  256. value: [
  257. {
  258. createdDateTime: '2020-01-31T15:40:26.197Z',
  259. id: defaults.ITEM_ID,
  260. lastModifiedDateTime: '2020-01-31T15:40:38.723Z',
  261. name: defaults.ITEM_NAME,
  262. size: defaults.FILE_SIZE,
  263. parentReference: {
  264. driveId: 'DUMMY-DRIVE-ID',
  265. driveType: 'personal',
  266. path: '/drive/root:',
  267. },
  268. file: {
  269. mimeType: defaults.MIME_TYPE,
  270. },
  271. thumbnails: [{
  272. id: '0',
  273. large: {
  274. height: 452,
  275. url: defaults.THUMBNAIL_URL,
  276. width: 800,
  277. },
  278. medium: {
  279. height: 100,
  280. url: defaults.THUMBNAIL_URL,
  281. width: 176,
  282. },
  283. small: {
  284. height: 54,
  285. url: defaults.THUMBNAIL_URL,
  286. width: 96,
  287. },
  288. }],
  289. },
  290. ],
  291. })
  292. const { username, items, providerFixture } = await runTest('onedrive')
  293. expect1({ username, items, providerFixture })
  294. })
  295. test('zoom', async () => {
  296. nock('https://zoom.us').get('/v2/users/me').reply(200, {
  297. id: 'DUMMY-USER-ID',
  298. first_name: 'John',
  299. last_name: 'Doe',
  300. email: 'john.doe@transloadit.com',
  301. timezone: '',
  302. dept: '',
  303. created_at: '2020-07-21T09:13:30Z',
  304. last_login_time: '2020-10-12T07:55:02Z',
  305. group_ids: [],
  306. im_group_ids: [],
  307. account_id: 'DUMMY-ACCOUNT-ID',
  308. language: 'en-US',
  309. })
  310. nockZoomRecordings()
  311. const { username, items, providerFixture } = await runTest('zoom')
  312. expect1({ username, items, providerFixture })
  313. })
  314. })
  315. describe('provider file gets downloaded from', () => {
  316. async function runTest (providerName) {
  317. const providerFixture = fixtures.providers[providerName]?.expects ?? {}
  318. const res = await request(authServer)
  319. .post(`/${providerName}/get/${providerFixture.itemRequestPath || defaults.ITEM_ID}`)
  320. .set('uppy-auth-token', token)
  321. .set('Content-Type', 'application/json')
  322. .send({
  323. endpoint: 'http://tusd.tusdemo.net/files',
  324. protocol: 'tus',
  325. })
  326. .expect(200)
  327. expect(res.body.token).toBeTruthy()
  328. }
  329. test('dropbox', async () => {
  330. nock('https://api.dropboxapi.com').post('/2/files/get_metadata').reply(200, { size: defaults.FILE_SIZE })
  331. nock('https://content.dropboxapi.com').post('/2/files/download').reply(200, {})
  332. await runTest('dropbox')
  333. })
  334. test('box', async () => {
  335. nock('https://api.box.com').get(`/2.0/files/${defaults.ITEM_ID}`).reply(200, { size: defaults.FILE_SIZE })
  336. nock('https://api.box.com').get(`/2.0/files/${defaults.ITEM_ID}/content`).reply(200, { size: defaults.FILE_SIZE })
  337. await runTest('box')
  338. })
  339. test('drive', async () => {
  340. nockGoogleDownloadFile()
  341. await runTest('drive')
  342. })
  343. test('googlephotos', async () => {
  344. nock('https://photoslibrary.googleapis.com').get(`/v1/mediaItems/${defaults.ITEM_ID}`).reply(200, {
  345. baseUrl: 'https://lh3.googleusercontent.com/test',
  346. })
  347. nock('https://lh3.googleusercontent.com').get(`/test=d`).reply(200, ' ', { 'content-length': 1 })
  348. await runTest('googlephotos')
  349. })
  350. test('facebook', async () => {
  351. // times(2) because of size request
  352. nock('https://graph.facebook.com').get(`/${defaults.ITEM_ID}?fields=images`).times(2).reply(200, {
  353. images: [
  354. {
  355. height: 1365,
  356. source: defaults.THUMBNAIL_URL,
  357. width: 2048,
  358. },
  359. ],
  360. id: defaults.ITEM_ID,
  361. })
  362. await runTest('facebook')
  363. })
  364. test('instagram', async () => {
  365. // times(2) because of size request
  366. nock('https://graph.instagram.com').get(`/${defaults.ITEM_ID}?fields=media_url`).times(2).reply(200, {
  367. id: defaults.ITEM_ID,
  368. media_type: 'IMAGE',
  369. media_url: defaults.THUMBNAIL_URL,
  370. timestamp: '2017-08-31T18:10:00+0000',
  371. })
  372. await runTest('instagram')
  373. })
  374. test('onedrive', async () => {
  375. nock('https://graph.microsoft.com').get(`/v1.0/drives/DUMMY-DRIVE-ID/items/${defaults.ITEM_ID}`).reply(200, {
  376. size: defaults.FILE_SIZE,
  377. })
  378. nock('https://graph.microsoft.com').get(`/v1.0/drives/DUMMY-DRIVE-ID/items/${defaults.ITEM_ID}/content`).reply(200, {})
  379. await runTest('onedrive')
  380. })
  381. test('zoom', async () => {
  382. // times(2) because of size request
  383. nockZoomRecordings({ times: 2 })
  384. nock('https://us02web.zoom.us').get('/rec/download/DUMMY-DOWNLOAD-PATH?access_token=token%20value').reply(200, {})
  385. await runTest('zoom')
  386. })
  387. })
  388. describe('connect to provider', () => {
  389. test.each(providerNames)('connect to %s via grant.js endpoint', (providerName) => {
  390. const authProvider = AUTH_PROVIDERS[providerName] || providerName
  391. if (authProvider.authProvider == null) return
  392. request(authServer)
  393. .get(`/${providerName}/connect?foo=bar`)
  394. .set('uppy-auth-token', token)
  395. .expect(302)
  396. .expect('Location', `http://localhost:3020/connect/${authProvider}?state=${OAUTH_STATE}`)
  397. })
  398. })
  399. describe('logout of provider', () => {
  400. async function runTest (providerName) {
  401. const res = await request(authServer)
  402. .get(`/${providerName}/logout/`)
  403. .set('uppy-auth-token', token)
  404. .expect(200)
  405. // only some providers can actually be revoked
  406. const expectRevoked = ['box', 'dropbox', 'drive', 'googlephotos', 'facebook', 'zoom'].includes(providerName)
  407. expect(res.body).toMatchObject({
  408. ok: true,
  409. revoked: expectRevoked,
  410. })
  411. }
  412. test('dropbox', async () => {
  413. nock('https://api.dropboxapi.com').post('/2/auth/token/revoke').reply(200, {})
  414. await runTest('dropbox')
  415. })
  416. test('box', async () => {
  417. nock('https://api.box.com').post('/oauth2/revoke').reply(200, {})
  418. await runTest('box')
  419. })
  420. test('dropbox', async () => {
  421. nock('https://api.dropboxapi.com').post('/2/auth/token/revoke').reply(200, {})
  422. await runTest('dropbox')
  423. })
  424. test('drive', async () => {
  425. nock('https://accounts.google.com').post('/o/oauth2/revoke?token=token+value').reply(200, {})
  426. await runTest('drive')
  427. })
  428. test('googlephotos', async () => {
  429. nock('https://accounts.google.com').post('/o/oauth2/revoke?token=token+value').reply(200, {})
  430. await runTest('googlephotos')
  431. })
  432. test('facebook', async () => {
  433. nock('https://graph.facebook.com').delete('/me/permissions').reply(200, {})
  434. await runTest('facebook')
  435. })
  436. test('instagram', async () => {
  437. await runTest('instagram')
  438. })
  439. test('onedrive', async () => {
  440. await runTest('onedrive')
  441. })
  442. test('zoom', async () => {
  443. nockZoomRevoke({ key: localZoomKey, secret: localZoomSecret })
  444. await runTest('zoom')
  445. })
  446. })