ares-ide
Version:
A browser-based code editor and UI designer for Enyo 2 projects
126 lines (102 loc) • 4.6 kB
text/coffeescript
describe 'Dropbox.Oauth', ->
beforeEach ->
= new Dropbox.Oauth
key: 'dpf43f3p2l4k3l03',
secret: 'kd94hf93k423kf44'
.setToken 'nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00'
# The example in OAuth 1.0a Appendix A.
=
method: 'GET',
url: 'http://photos.example.net/photos'
params:
file: 'vacation.jpg',
size: 'original'
= sinon.stub Date, 'now'
.returns 1191242096999
afterEach ->
.restore()
describe '#boilerplateParams', ->
it 'issues unique nonces', ->
nonces = {}
for i in [1..100]
nonce = .boilerplateParams({}).oauth_nonce
expect(nonces).not.to.have.property nonce
nonces[nonce] = true
it 'fills all the arguments', ->
params = .boilerplateParams(.params)
properties = ['oauth_consumer_key', 'oauth_nonce',
'oauth_signature_method', 'oauth_timestamp',
'oauth_version']
for property in properties
expect(params).to.have.property property
describe '#signature', ->
beforeEach ->
= sinon.stub Dropbox.Oauth, 'nonce'
.returns 'kllo9940pd9333jh'
afterEach ->
.restore()
it 'works for the OAuth 1.0a example', ->
.boilerplateParams(.params)
expect(.signature(.method, .url, .params)).
to.equal 'tR3+Ty81lMeYAr/Fid0kMTYa/WM='
it 'works with an encoded key', ->
= new Dropbox.Oauth
key: Dropbox.Util.encodeKey(.key, .secret),
token: .token, tokenSecret: .tokenSecret
.boilerplateParams(.params)
expect(.signature(.method, .url, .params)).
to.equal 'tR3+Ty81lMeYAr/Fid0kMTYa/WM='
describe '#addAuthParams', ->
beforeEach ->
= sinon.stub Dropbox.Oauth, 'nonce'
.returns 'kllo9940pd9333jh'
afterEach ->
.restore()
it 'matches the OAuth 1.0a example', ->
goldenParams =
file: 'vacation.jpg'
oauth_consumer_key: 'dpf43f3p2l4k3l03'
oauth_nonce: 'kllo9940pd9333jh'
oauth_signature: 'tR3+Ty81lMeYAr/Fid0kMTYa/WM='
oauth_signature_method: 'HMAC-SHA1'
oauth_timestamp: '1191242096'
oauth_token: 'nnch734d00sl2jdk'
oauth_version: '1.0'
size: 'original'
.addAuthParams .method, .url, .params
expect(Dropbox.Xhr.urlEncode(.params)).to.
eql Dropbox.Xhr.urlEncode(goldenParams)
it "doesn't leave any OAuth-related value in params", ->
.authHeader(.method, .url, .params)
expect(Dropbox.Xhr.urlEncode(.params)).to.
equal "file=vacation.jpg&size=original"
describe '#authHeader', ->
beforeEach ->
= sinon.stub Dropbox.Oauth, 'nonce'
.returns 'kllo9940pd9333jh'
afterEach ->
.restore()
it 'matches the OAuth 1.0a example', ->
goldenHeader = 'OAuth oauth_consumer_key="dpf43f3p2l4k3l03",oauth_nonce="kllo9940pd9333jh",oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1191242096",oauth_token="nnch734d00sl2jdk",oauth_version="1.0"'
header = .authHeader .method, .url, .params
expect(header).to.equal goldenHeader
it "doesn't leave any OAuth-related value in params", ->
.authHeader(.method, .url, .params)
expect(Dropbox.Xhr.urlEncode(.params)).to.
equal "file=vacation.jpg&size=original"
describe '#appHash', ->
it 'is a non-trivial string', ->
expect(.appHash()).to.be.a 'string'
expect(.appHash().length).to.be.greaterThan 4
it 'is consistent', ->
oauth = new Dropbox.Oauth key: .key, secret: .secret
expect(oauth.appHash()).to.equal .appHash()
it 'depends on the app key', ->
oauth = new Dropbox.Oauth key: .key + '0', secret: .secret
expect(oauth.appHash()).not.to.equal .appHash()
expect(oauth.appHash()).to.be.a 'string'
expect(oauth.appHash().length).to.be.greaterThan 4
describe '#constructor', ->
it 'raises an Error if initialized without an API key / secret', ->
expect(-> new Dropbox.Oauth(token: '123', tokenSecret: '456')).to.
throw(Error, /no api key/i)