-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniele Bernardi
committed
Mar 12, 2020
1 parent
8f60d4b
commit e5f2e5b
Showing
9 changed files
with
111 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const nock = require('nock'); | ||
const bearerToken = require('../bearer-token'); | ||
const assert = require('assert'); | ||
|
||
const tokenValue = 'access_token_from_api'; | ||
|
||
(async () => { | ||
const scope = nock('https://api.twitter.com') | ||
.post('/oauth2/token') | ||
.reply(200, {token_type: 'bearer', access_token: 'access_token_from_api'}); | ||
|
||
let token = null; | ||
await assert.doesNotReject(async () => { | ||
token = await bearerToken({ | ||
consumer_key: 'test_consumer_key', | ||
consumer_secret: 'test_consumer_secret', | ||
}); | ||
}); | ||
|
||
assert.equal(token, tokenValue); | ||
scope.done(); | ||
})(); | ||
|
||
(async () => { | ||
const scope = nock('https://api.twitter.com') | ||
.post('/oauth2/token') | ||
.reply(503, { | ||
errors: [{ | ||
message: 'test error', | ||
code: 1337, | ||
}], | ||
}); | ||
|
||
await assert.rejects(async () => { | ||
const token = await bearerToken({ | ||
consumer_key: 'test_consumer_key', | ||
consumer_secret: 'test_consumer_secret', | ||
}); | ||
}, { | ||
name: 'BearerTokenError', | ||
}); | ||
scope.done(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const assert = require('assert'); | ||
const oauthInstance = require('../oauth'); | ||
const URL = require('url').URL; | ||
|
||
const oAuthConfig = { | ||
oauth: { | ||
consumer_key: 'consumer_key', | ||
consymer_secret: 'consumer_secret', | ||
token: 'test_user_token', | ||
token_secret: 'test_user_token_secret', | ||
}, | ||
}; | ||
|
||
const signatures = { | ||
baseUrl: 'OAuth oauth_consumer_key="consumer_key", oauth_nonce="GXjhffMbAMz2qblDzzgYbP4ZkfPp7RGmhry5Upatw", oauth_signature="RGpMnOI1WxKDZtgORbi32uc8P%2BY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1583967563", oauth_token="test_user_token", oauth_version="1.0"', | ||
urlWithParams: 'OAuth oauth_consumer_key="consumer_key", oauth_nonce="xaCqEY8Ed9vnfFvZJsu8AjSF1", oauth_signature="LYT0mRAq62MXsnxsTOppEtViTUs%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1583967750", oauth_token="test_user_token", oauth_version="1.0"', | ||
}; | ||
|
||
const baseUrl = new URL('https://api.twitter.com/1.1/account/verify_credentials.json') | ||
oauthInstance.setNonceFn(() => 'GXjhffMbAMz2qblDzzgYbP4ZkfPp7RGmhry5Upatw'); | ||
oauthInstance.setTimestampFn(() => '1583967563'); | ||
assert.throws(() => { | ||
// non-object body throws TypeError | ||
oauthInstance.oauth(baseUrl, 'GET', '', oAuthConfig); | ||
}, { | ||
name: 'TypeError', | ||
}); | ||
assert.equal(oauthInstance.oauth(baseUrl, 'GET', {}, oAuthConfig), signatures.baseUrl); | ||
|
||
const urlWithParams = new URL('https://api.twitter.com/1.1/account/verify_credentials.json') | ||
urlWithParams.searchParams.append('param_test', '1'); | ||
urlWithParams.searchParams.append('example', '1'); | ||
oauthInstance.setNonceFn(() => 'xaCqEY8Ed9vnfFvZJsu8AjSF1'); | ||
oauthInstance.setTimestampFn(() => '1583967750'); | ||
assert.equal(oauthInstance.oauth(urlWithParams, 'GET', {}, oAuthConfig), signatures.urlWithParams); |