123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /* global jest:false, test:false, expect:false, describe:false */
- jest.mock('tus-js-client')
- jest.mock('purest')
- jest.mock('../../src/server/helpers/oauth-state', () => {
- return {
- generateState: () => 'some-cool-nice-encrytpion',
- addToState: () => 'some-cool-nice-encrytpion',
- getFromState: (state) => {
- return state === 'state-with-invalid-instance-url' ? 'http://localhost:3452' : 'http://localhost:3020'
- }
- }
- })
- const request = require('supertest')
- const tokenService = require('../../src/server/helpers/jwt')
- const { authServer, noAuthServer } = require('../mockserver')
- const authData = {
- dropbox: 'token value',
- drive: 'token value'
- }
- const token = tokenService.generateToken(authData, process.env.COMPANION_SECRET || process.env.UPPYSERVER_SECRET)
- const OAUTH_STATE = 'some-cool-nice-encrytpion'
- describe('set i-am header', () => {
- test('set i-am header in response', () => {
- return request(authServer)
- .get('/dropbox/list/')
- .set('uppy-auth-token', token)
- .expect(200)
- .then((res) => expect(res.header['i-am']).toBe('http://localhost:3020'))
- })
- })
- describe('list provider files', () => {
- test('list files for dropbox', () => {
- return request(authServer)
- .get('/dropbox/list/')
- .set('uppy-auth-token', token)
- .expect(200)
- .then((res) => expect(res.body.username).toBe('foo@bar.com'))
- })
- test('list files for google drive', () => {
- return request(authServer)
- .get('/drive/list/')
- .set('uppy-auth-token', token)
- .expect(200)
- .then((res) => expect(res.body.username).toBe('ife@bala.com'))
- })
- })
- describe('download provdier file', () => {
- test('specified file gets downloaded from provider', () => {
- return request(authServer)
- .post('/drive/get/README.md')
- .set('uppy-auth-token', token)
- .set('Content-Type', 'application/json')
- .send({
- endpoint: 'http://master.tus.com/files',
- protocol: 'tus'
- })
- .expect(200)
- .then((res) => expect(res.body.token).toBeTruthy())
- })
- })
- describe('test authentication', () => {
- test('authentication callback redirects to send-token url', () => {
- return request(authServer)
- .get('/drive/callback')
- .expect(302)
- .expect((res) => {
- expect(res.header['location']).toContain('http://localhost:3020/drive/send-token?uppyAuthToken=')
- })
- })
- test('the token gets sent via cookie and html', () => {
- return request(authServer)
- .get(`/drive/send-token?uppyAuthToken=${token}`)
- .expect(200)
- .expect((res) => {
- const authToken = res.header['set-cookie'][0].split(';')[0].split('uppyAuthToken=')[1]
- expect(authToken).toEqual(token)
- // see mock ../../src/server/helpers/oauth-state above for http://localhost:3020
- const body = `
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <script>
- window.opener.postMessage({token: "${token}"}, "http://localhost:3020")
- window.close()
- </script>
- </head>
- <body></body>
- </html>`
- expect(res.text).toBe(body)
- })
- })
- test('check for authenticated provider', () => {
- request(authServer)
- .get('/drive/authorized/')
- .set('uppy-auth-token', token)
- .expect(200)
- .then((res) => expect(res.body.authenticated).toBe(true))
- request(noAuthServer)
- .get('/drive/authorized/')
- .expect(200)
- .then((res) => expect(res.body.authenticated).toBe(false))
- })
- test('logout provider', () => {
- return request(authServer)
- .get('/drive/logout/')
- .set('uppy-auth-token', token)
- .expect(200)
- .then((res) => expect(res.body.ok).toBe(true))
- })
- })
- describe('connect to provider', () => {
- test('connect to dropbox via grant.js endpoint', () => {
- return request(authServer)
- .get('/dropbox/connect?foo=bar')
- .set('uppy-auth-token', token)
- .expect(302)
- .expect('Location', `http://localhost:3020/connect/dropbox?state=${OAUTH_STATE}`)
- })
- test('connect to drive via grant.js endpoint', () => {
- return request(authServer)
- .get('/drive/connect?foo=bar')
- .set('uppy-auth-token', token)
- .expect(302)
- .expect('Location', `http://localhost:3020/connect/google?state=${OAUTH_STATE}`)
- })
- })
- describe('handle oauth redirect', () => {
- test('redirect to a valid uppy instance', () => {
- return request(authServer)
- .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(authServer)
- .get(`/dropbox/redirect?state=${state}`)
- .set('uppy-auth-token', token)
- .expect(400)
- })
- })
|