ares-ide
Version:
A browser-based code editor and UI designer for Enyo 2 projects
456 lines (393 loc) • 20 kB
text/coffeescript
describe 'Dropbox.Drivers.BrowserBase', ->
beforeEach ->
= module? and module?.exports? and require?
= chrome? and (chrome.extension or chrome.app)
= new Dropbox.Client testKeys
describe 'with rememberUser: false', ->
beforeEach (done) ->
return done() if or
= new Dropbox.Drivers.BrowserBase
.setStorageKey
.forgetCredentials done
afterEach (done) ->
return done() if or
.forgetCredentials done
describe '#loadCredentials', ->
it 'produces the credentials passed to storeCredentials', (done) ->
return done() if or
goldCredentials = .credentials()
.storeCredentials goldCredentials, =>
.loadCredentials (credentials) ->
expect(credentials).to.deep.equal goldCredentials
done()
it 'produces null after forgetCredentials was called', (done) ->
return done() if or
.storeCredentials .credentials(), =>
.forgetCredentials =>
.loadCredentials (credentials) ->
expect(credentials).to.equal null
done()
it 'produces null if a different scope is provided', (done) ->
return done() if or
.setStorageKey
.storeCredentials .credentials(), =>
.forgetCredentials =>
.loadCredentials (credentials) ->
expect(credentials).to.equal null
done()
describe '#locationToken', ->
beforeEach ->
= sinon.stub Dropbox.Drivers.BrowserBase, 'currentLocation'
afterEach ->
.restore()
it 'returns null if the location does not contain the token', ->
.returns 'http://test/file?_dropboxjs_scope=default&' +
'another_token=ab%20cd&oauth_tok=en'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal null
it 'returns null if the rejected location does not contain the token', ->
.returns 'http://test/file?_dropboxjs_scope=default&' +
'another_token=ab%20cd&dboauth_tok=en¬_approved=true'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal null
it 'returns null if the location fragment does not contain the token', ->
.returns 'http://test/file#?_dropboxjs_scope=default& ' +
'another_token=ab%20cd&oauth_tok=en'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal null
it 'returns null if the rejected location fragment misses the token', ->
.returns 'http://test/file#?_dropboxjs_scope=default& ' +
'another_token=ab%20cd&dboauth_tok=en¬_approved=true'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal null
it "extracts a token successfully with default scope", ->
.returns 'http://test/file?_dropboxjs_scope=default&' +
'oauth_token=ab%20cd&other_param=true'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal 'ab cd'
it "extracts a rejected token successfully with default scope", ->
.returns 'http://test/file?_dropboxjs_scope=default&' +
'dboauth_token=ab%20cd&other_param=true¬_approved=true'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal 'ab cd'
it "extracts a token successfully with set scope", ->
.returns 'http://test/file?_dropboxjs_scope=not%20default&' +
'oauth_token=ab%20cd'
driver = new Dropbox.Drivers.BrowserBase scope: 'not default'
expect(driver.locationToken()).to.equal 'ab cd'
it "extracts a rejected token successfully with set scope", ->
.returns 'http://test/file?_dropboxjs_scope=not%20default&' +
'dboauth_token=ab%20cd¬_approved=true'
driver = new Dropbox.Drivers.BrowserBase scope: 'not default'
expect(driver.locationToken()).to.equal 'ab cd'
it "extracts a token from a fragment with default scope", ->
.returns 'http://test/file#?_dropboxjs_scope=default&' +
'oauth_token=ab%20cd&other_param=true'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal 'ab cd'
it "extracts a rejected token from a fragment with default scope", ->
.returns 'http://test/file#?_dropboxjs_scope=default&' +
'dboauth_token=ab%20cd&other_param=true¬_approved=true'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal 'ab cd'
it "extracts a token from a fragment with set scope", ->
.returns 'http://test/file#?_dropboxjs_scope=not%20default&' +
'oauth_token=ab%20cd'
driver = new Dropbox.Drivers.BrowserBase scope: 'not default'
expect(driver.locationToken()).to.equal 'ab cd'
it "extracts a rejected token from a fragment with set scope", ->
.returns 'http://test/file#?_dropboxjs_scope=not%20default&' +
'dboauth_token=ab%20cd¬_approved=true'
driver = new Dropbox.Drivers.BrowserBase scope: 'not default'
expect(driver.locationToken()).to.equal 'ab cd'
it "returns null if the location scope doesn't match", ->
.returns 'http://test/file?_dropboxjs_scope=defaultx&oauth_token=ab'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal null
it "returns null if the location fragment scope doesn't match", ->
.returns 'http://test/file#?_dropboxjs_scope=defaultx&oauth_token=a'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.locationToken()).to.equal null
describe '#computeUrl', ->
it 'adds a query string to a static URL', ->
baseUrl = 'http://test/file'
driver = new Dropbox.Drivers.BrowserBase useQuery: true
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file?_dropboxjs_scope=default&dboauth_token=', ''])
it 'adds a fragment to a static URL', ->
baseUrl = 'http://test/file'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file#?_dropboxjs_scope=default&dboauth_token=', ''])
it 'adds a query param to a URL with a query string', ->
baseUrl = 'http://test/file?a=true'
driver = new Dropbox.Drivers.BrowserBase useQuery: true
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file?a=true&_dropboxjs_scope=default&dboauth_token=',
''])
it 'adds a fragment to a URL with a query string', ->
baseUrl = 'http://test/file?a=true'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file?a=true#?_dropboxjs_scope=default&dboauth_token=',
''])
it 'adds a query string to a static URL with a fragment', ->
baseUrl = 'http://test/file#fragment'
driver = new Dropbox.Drivers.BrowserBase useQuery: true
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file?_dropboxjs_scope=default&dboauth_token=',
'#fragment'])
it 'replaces the fragment in a static URL with a fragment', ->
baseUrl = 'http://test/file#fragment'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file#?_dropboxjs_scope=default&dboauth_token=', ''])
it 'adds a query param to a URL with a query string and fragment', ->
baseUrl = 'http://test/file?a=true#frag'
driver = new Dropbox.Drivers.BrowserBase useQuery: true
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file?a=true&_dropboxjs_scope=default&dboauth_token=',
'#frag'])
it 'replaces the fragment in a URL with a query string and fragment', ->
baseUrl = 'http://test/file?a=true#frag'
driver = new Dropbox.Drivers.BrowserBase
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file?a=true#?_dropboxjs_scope=default&dboauth_token=',
''])
it 'obeys the scope option', ->
baseUrl = 'http://test/file'
driver = new Dropbox.Drivers.BrowserBase(
scope: 'not default', useQuery: true)
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file?_dropboxjs_scope=not%20default&dboauth_token=',
''])
it 'obeys the scope option when adding a fragment', ->
baseUrl = 'http://test/file'
driver = new Dropbox.Drivers.BrowserBase scope: 'not default'
expect(driver.computeUrl(baseUrl)).to.deep.equal(
['http://test/file#?_dropboxjs_scope=not%20default&dboauth_token=',
''])
describe 'Dropbox.Drivers.Redirect', ->
describe '#url', ->
beforeEach ->
= sinon.stub Dropbox.Drivers.BrowserBase, 'currentLocation'
afterEach ->
.restore()
it 'adds a query param to a URL with a query string', ->
.returns 'http://test/file?a=true'
driver = new Dropbox.Drivers.Redirect useQuery: true
expect(driver.url('oauth token')).to.
equal 'http://test/file?a=true&_dropboxjs_scope=default&' +
'dboauth_token=oauth%20token'
it 'adds a fragment to a URL with a query string', ->
.returns 'http://test/file?a=true'
driver = new Dropbox.Drivers.Redirect
expect(driver.url('oauth token')).to.
equal 'http://test/file?a=true#?_dropboxjs_scope=default' +
'&dboauth_token=oauth%20token'
it 'obeys the scope option', ->
.returns 'http://test/file'
driver = new Dropbox.Drivers.Redirect(
scope: 'not default', useQuery: true)
expect(driver.url('oauth token')).to.
equal 'http://test/file?_dropboxjs_scope=not%20default&' +
'dboauth_token=oauth%20token'
it 'obeys the scope option when adding a fragment', ->
.returns 'http://test/file'
driver = new Dropbox.Drivers.Redirect scope: 'not default'
expect(driver.url('oauth token')).to.
equal 'http://test/file#?_dropboxjs_scope=not%20default&' +
'dboauth_token=oauth%20token'
describe '#loadCredentials', ->
beforeEach ->
= module? and module.exports? and require?
= chrome? and (chrome.extension or chrome.app?.runtime)
return if or
= new Dropbox.Client testKeys
= new Dropbox.Drivers.Redirect scope: 'some_scope'
.setStorageKey
it 'produces the credentials passed to storeCredentials', (done) ->
return done() if or
goldCredentials = .credentials()
.storeCredentials goldCredentials, =>
= new Dropbox.Drivers.Redirect scope: 'some_scope'
.setStorageKey
.loadCredentials (credentials) ->
expect(credentials).to.deep.equal goldCredentials
done()
it 'produces null after forgetCredentials was called', (done) ->
return done() if or
.storeCredentials .credentials(), =>
.forgetCredentials =>
= new Dropbox.Drivers.Redirect scope: 'some_scope'
.setStorageKey
.loadCredentials (credentials) ->
expect(credentials).to.equal null
done()
it 'produces null if a different scope is provided', (done) ->
return done() if or
.setStorageKey
.storeCredentials .credentials(), =>
= new Dropbox.Drivers.Redirect scope: 'other_scope'
.setStorageKey
.loadCredentials (credentials) ->
expect(credentials).to.equal null
done()
describe 'integration', ->
beforeEach ->
= module? and module.exports? and require?
= chrome? and (chrome.extension or chrome.app?.runtime)
= cordova?
it 'should work', (done) ->
return done() if or or
30 * 1000 # Time-consuming because the user must click.
listenerCalled = false
listener = (event) ->
return if listenerCalled is true
listenerCalled = true
data = event.data or event
expect(data).to.match(/^\[.*\]$/)
[error, credentials] = JSON.parse data
expect(error).to.equal null
expect(credentials).to.have.property 'uid'
expect(credentials.uid).to.be.a 'string'
window.removeEventListener 'message', listener
Dropbox.Drivers.Popup.onMessage.removeListener listener
done()
window.addEventListener 'message', listener
Dropbox.Drivers.Popup.onMessage.addListener listener
(new Dropbox.Drivers.Popup()).openWindow(
'/test/html/redirect_driver_test.html')
describe 'Dropbox.Drivers.Popup', ->
describe '#url', ->
beforeEach ->
= sinon.stub Dropbox.Drivers.BrowserBase, 'currentLocation'
.returns 'http://test:123/a/path/file.htmx'
afterEach ->
.restore()
it 'reflects the current page when there are no options', ->
driver = new Dropbox.Drivers.Popup
expect(driver.url('oauth token')).to.equal(
'http://test:123/a/path/file.htmx#?_dropboxjs_scope=default&' +
'dboauth_token=oauth%20token')
it 'replaces the current file correctly', ->
driver = new Dropbox.Drivers.Popup receiverFile: 'another.file'
expect(driver.url('oauth token')).to.equal(
'http://test:123/a/path/another.file#?_dropboxjs_scope=default&' +
'dboauth_token=oauth%20token')
it 'replaces the current file without a fragment correctly', ->
driver = new Dropbox.Drivers.Popup
receiverFile: 'another.file', useQuery: true
expect(driver.url('oauth token')).to.equal(
'http://test:123/a/path/another.file?_dropboxjs_scope=default&' +
'dboauth_token=oauth%20token')
it 'replaces an entire URL without a fragment correctly', ->
driver = new Dropbox.Drivers.Popup
receiverUrl: 'https://something.com/filez'
expect(driver.url('oauth token')).to.equal(
'https://something.com/filez#?_dropboxjs_scope=default&' +
'dboauth_token=oauth%20token')
it 'replaces an entire URL with a fragment correctly', ->
driver = new Dropbox.Drivers.Popup
receiverUrl: 'https://something.com/filez#frag'
expect(driver.url('oauth token')).to.equal(
'https://something.com/filez#?_dropboxjs_scope=default&' +
'dboauth_token=oauth%20token')
it 'replaces an entire URL without a fragment and useQuery correctly', ->
driver = new Dropbox.Drivers.Popup
receiverUrl: 'https://something.com/filez', useQuery: true
expect(driver.url('oauth token')).to.equal(
'https://something.com/filez?_dropboxjs_scope=default&' +
'dboauth_token=oauth%20token')
describe '#loadCredentials', ->
beforeEach ->
= module? and module.exports? and require?
= chrome? and (chrome.extension or chrome.app?.runtime)
return if or
= new Dropbox.Client testKeys
= new Dropbox.Drivers.Popup scope: 'some_scope'
.setStorageKey
it 'produces the credentials passed to storeCredentials', (done) ->
return done() if or
goldCredentials = .credentials()
.storeCredentials goldCredentials, =>
= new Dropbox.Drivers.Popup scope: 'some_scope'
.setStorageKey
.loadCredentials (credentials) ->
expect(credentials).to.deep.equal goldCredentials
done()
it 'produces null after forgetCredentials was called', (done) ->
return done() if or
.storeCredentials .credentials(), =>
.forgetCredentials =>
= new Dropbox.Drivers.Popup scope: 'some_scope'
.setStorageKey
.loadCredentials (credentials) ->
expect(credentials).to.equal null
done()
it 'produces null if a different scope is provided', (done) ->
return done() if or
.setStorageKey
.storeCredentials .credentials(), =>
= new Dropbox.Drivers.Popup scope: 'other_scope'
.setStorageKey
.loadCredentials (credentials) ->
expect(credentials).to.equal null
done()
describe 'integration', ->
beforeEach ->
= module? and module.exports? and require?
= chrome? and (chrome.extension or chrome.app?.runtime)
= cordova?
it 'should work with a query string', (done) ->
return done() if or or
45 * 1000 # Time-consuming because the user must click.
client = new Dropbox.Client testKeys
client.reset()
authDriver = new Dropbox.Drivers.Popup
receiverFile: 'oauth_receiver.html', useQuery: true,
scope: 'popup-integration', rememberUser: false
client.authDriver authDriver
client.authenticate (error, client) =>
expect(error).to.equal null
expect(client.authState).to.equal Dropbox.Client.DONE
# Verify that we can do API calls.
client.getUserInfo (error, userInfo) ->
expect(error).to.equal null
expect(userInfo).to.be.instanceOf Dropbox.UserInfo
# Follow-up authenticate() should restart the process.
client.reset()
client.authenticate interactive: false, (error, client) ->
expect(error).to.equal null
expect(client.authState).to.equal Dropbox.Client.RESET
expect(client.isAuthenticated()).to.equal false
done()
it 'should work with a URL fragment and rememberUser: true', (done) ->
return done() if or or
45 * 1000 # Time-consuming because the user must click.
client = new Dropbox.Client testKeys
client.reset()
authDriver = new Dropbox.Drivers.Popup
receiverFile: 'oauth_receiver.html', useQuery: false,
scope: 'popup-integration', rememberUser: true
client.authDriver authDriver
authDriver.setStorageKey client
authDriver.forgetCredentials ->
client.authenticate (error, client) ->
expect(error).to.equal null
expect(client.authState).to.equal Dropbox.Client.DONE
# Verify that we can do API calls.
client.getUserInfo (error, userInfo) ->
expect(error).to.equal null
expect(userInfo).to.be.instanceOf Dropbox.UserInfo
# Follow-up authenticate() should use stored credentials.
client.reset()
client.authenticate interactive: false, (error, client) ->
expect(error).to.equal null
expect(client.authState).to.equal Dropbox.Client.DONE
expect(client.isAuthenticated()).to.equal true
# Verify that we can do API calls.
client.getUserInfo (error, userInfo) ->
expect(error).to.equal null
expect(userInfo).to.be.instanceOf Dropbox.UserInfo
done()