Skip to content

Commit fb9927d

Browse files
committed
Refactor oauth1 flow tests +
Add request error tests for step3
1 parent 45d0971 commit fb9927d

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

test/flow/oauth1.js

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,16 @@ var oauth1 = require('../../lib/flow/oauth1')
1010

1111
describe('oauth1', function () {
1212
function url (path) {
13-
var c = config.server
14-
return c.protocol + '://' + c.host + path
15-
}
16-
17-
var grant, app, server
18-
var config = {
19-
server: {protocol: 'http', host: 'localhost:5000', callback: '/'},
20-
twitter: {}
13+
return 'http://localhost:5000' + path
2114
}
2215

2316
describe('success', function () {
17+
var grant, server
18+
2419
before(function (done) {
20+
var config = {twitter: {}}
2521
grant = new Grant(config)
26-
app = express().use(grant)
27-
28-
grant.config.twitter.request_url = url('/request_url')
29-
grant.config.twitter.authorize_url = '/authorize_url'
30-
grant.config.twitter.access_url = url('/access_url')
22+
var app = express().use(grant)
3123

3224
app.post('/request_url', function (req, res) {
3325
res.end(qs.stringify({some: 'data'}))
@@ -39,18 +31,21 @@ describe('oauth1', function () {
3931
})
4032

4133
it('step1', function (done) {
34+
grant.config.twitter.request_url = url('/request_url')
4235
oauth1.step1(grant.config.twitter, function (err, data) {
4336
t.deepEqual(data, {some: 'data'})
4437
done()
4538
})
4639
})
4740

4841
it('step2', function () {
42+
grant.config.twitter.authorize_url = '/authorize_url'
4943
var url = oauth1.step2(grant.config.twitter, {oauth_token: 'token'})
5044
t.equal(url, '/authorize_url?oauth_token=token')
5145
})
5246

5347
it('step3', function (done) {
48+
grant.config.twitter.access_url = url('/access_url')
5449
oauth1.step3(grant.config.twitter, {}, {oauth_token: 'token'}, function (err, url) {
5550
t.equal(url, 'raw%5Bsome%5D=data')
5651
done()
@@ -63,22 +58,25 @@ describe('oauth1', function () {
6358
})
6459

6560
describe('error', function () {
61+
var grant, server
62+
6663
before(function (done) {
64+
var config = {twitter: {}}
6765
grant = new Grant(config)
68-
app = express().use(grant)
69-
70-
grant.config.twitter.request_url = url('/request_err')
71-
grant.config.twitter.authorize_url = '/authorize_url'
72-
grant.config.twitter.access_url = url('/access_url')
66+
var app = express().use(grant)
7367

7468
app.post('/request_url', function (req, res) {
7569
res.status(500).end(JSON.stringify({error: 'invalid'}))
7670
})
71+
app.post('/access_url', function (req, res) {
72+
res.status(500).end(JSON.stringify({error: 'invalid'}))
73+
})
7774

7875
server = app.listen(5000, done)
7976
})
8077

8178
it('step1 - network error', function (done) {
79+
grant.config.twitter.request_url = url('/request_err')
8280
oauth1.step1(grant.config.twitter, function (err, body) {
8381
t.deepEqual(qs.parse(err), {error: {'Cannot POST /request_err\n': ''}})
8482
done()
@@ -116,6 +114,20 @@ describe('oauth1', function () {
116114
done()
117115
})
118116
})
117+
it('step3 - network error', function (done) {
118+
grant.config.twitter.access_url = url('/access_err')
119+
oauth1.step3(grant.config.twitter, {}, {oauth_token: 'token'}, function (err, body) {
120+
t.deepEqual(qs.parse(err), {error: {'Cannot POST /access_err\n': ''}})
121+
done()
122+
})
123+
})
124+
it('step3 - error response', function (done) {
125+
grant.config.twitter.access_url = url('/access_url')
126+
oauth1.step3(grant.config.twitter, {}, {oauth_token: 'token'}, function (err, body) {
127+
t.deepEqual(qs.parse(err), {error: {error: 'invalid'}})
128+
done()
129+
})
130+
})
119131

120132
after(function (done) {
121133
server.close(done)
@@ -124,16 +136,18 @@ describe('oauth1', function () {
124136

125137
describe('custom', function () {
126138
describe('step1', function () {
139+
var grant, server
140+
127141
before(function (done) {
128-
util._extend(config, {
129-
copy: {}, etsy: {}, flickr: {}, freshbooks: {}, goodreads: {},
130-
intuit: {}, linkedin: {}, ravelry: {}, trello: {}, tripit: {}
131-
})
142+
var config = {copy: {}, etsy: {}, freshbooks: {}, linkedin: {}}
132143
grant = new Grant(config)
133-
app = express().use(grant)
144+
var app = express().use(grant)
134145

135146
app.post('/request_url', function (req, res) {
136-
res.end(qs.stringify({oauth: req.headers.authorization, scope: req.query.scope}))
147+
res.end(qs.stringify({
148+
oauth: req.headers.authorization,
149+
scope: req.query.scope
150+
}))
137151
})
138152
server = app.listen(5000, done)
139153
})
@@ -192,7 +206,10 @@ describe('oauth1', function () {
192206
})
193207

194208
describe('step2', function () {
209+
var grant
210+
195211
before(function () {
212+
var config = {flickr: {}, freshbooks: {}, ravelry: {}, trello: {}, tripit: {}}
196213
grant = new Grant(config)
197214
})
198215

@@ -235,6 +252,7 @@ describe('oauth1', function () {
235252

236253
describe('oauth_callback', function () {
237254
it('tripit', function () {
255+
grant.config.tripit.redirect_uri = url('/connect/tripit/callback')
238256
var uri = oauth1.step2(grant.config.tripit, {oauth_token: 'token'})
239257
var query = qs.parse(uri.split('?')[1])
240258
t.deepEqual(query,
@@ -252,9 +270,12 @@ describe('oauth1', function () {
252270
})
253271

254272
describe('step3', function () {
273+
var grant, server
274+
255275
before(function (done) {
276+
var config = {freshbooks: {}, goodreads: {}, intuit: {}, tripit: {}}
256277
grant = new Grant(config)
257-
app = express().use(grant)
278+
var app = express().use(grant)
258279

259280
app.post('/access_url', function (req, res) {
260281
res.end(qs.stringify({oauth: req.headers.authorization}))

0 commit comments

Comments
 (0)