Skip to content

Commit 6c401c0

Browse files
committed
Merge pull request #24 from Simperium/fix/try-parse-auth
Handle failed authentication attempts
2 parents 8298244 + c866c02 commit 6c401c0

4 files changed

Lines changed: 40 additions & 42 deletions

File tree

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"eol-last": 1,
2424
"indent": [ 1, "tab", { "SwitchCase": 1 } ],
2525
"key-spacing": 1,
26+
"keyword-spacing": [ 1 ],
2627
// Most common is "Emitter", should be improved
2728
"new-cap": 1,
2829
"no-cond-assign": 2,
@@ -58,7 +59,6 @@
5859
"quote-props": [ 1, "as-needed" ],
5960
"quotes": [ 1, "single", "avoid-escape" ],
6061
"semi-spacing": 1,
61-
"space-after-keywords": [ 1, "always" ],
6262
"space-before-blocks": [ 1, "always" ],
6363
"space-before-function-paren": [ 1, "never" ],
6464
// Our array literal index exception violates this rule

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
language: node_js
22
sudo: false
33
before_install:
4-
- npm install -g eslint babel-eslint
4+
- npm install -g eslint@2.2 babel-eslint
55
script:
66
- eslint .
77
- npm test
88
node_js:
9-
- "0.12"
109
- "4.0"
1110
- "5.0"

src/simperium/auth.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ Auth.prototype.request = function( endpoint, body ) {
4040
} );
4141

4242
res.on( 'end', () => {
43-
var user = User.fromJSON( responseData );
43+
var user;
44+
45+
try {
46+
user = User.fromJSON( responseData );
47+
} catch ( e ) {
48+
return reject( new Error( responseData ) );
49+
}
4450
this.emit( 'authorize', user );
4551
resolve( user );
4652
} );

test/simperium/auth_test.js

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,44 @@
11
import Auth from '../../src/simperium/auth'
22
import https from 'https'
3-
import assert from 'assert'
3+
import { equal, deepEqual } from 'assert'
44
import { EventEmitter } from 'events'
55

6-
if ( !Object.assign ) {
7-
// Very naive polyfill for Object.assign for testing purposes
8-
Object.assign = function() {
9-
return [].slice.apply( arguments ).reduce( ( out, from ) => {
10-
var x;
11-
for ( x in from ) {
12-
out[x] = from[x];
13-
}
14-
return out;
15-
}, {} )
6+
const stub = ( respond ) => {
7+
https.request = ( options, handler ) => {
8+
const req = new EventEmitter()
9+
req.end = ( body ) => respond( body, handler )
10+
return req
1611
}
1712
}
1813

19-
const requests = new EventEmitter();
20-
21-
https.request = function( options, handler ) {
22-
requests.emit( 'opened', options, handler );
23-
const req = new EventEmitter();
24-
25-
req.end = function( msg ) {
26-
requests.emit( 'body', msg, handler );
27-
}
28-
29-
return req;
30-
}
14+
const stubResponse = ( data ) => stub( ( body, handler ) => {
15+
const response = new EventEmitter()
16+
handler( response )
17+
response.emit( 'data', data )
18+
response.emit( 'end' )
19+
} )
3120

3221
describe( 'Auth', () => {
33-
var auth;
22+
var auth
3423

3524
beforeEach( () => {
3625
auth = new Auth( 'token', 'secret' );
3726
} )
3827

3928
it( 'getUrlOptions', () => {
4029
const { hostname, headers, pathname, method } = auth.getUrlOptions( 'path' )
41-
assert.equal( method, 'POST' )
42-
assert.equal( hostname, 'auth.simperium.com' )
43-
assert.equal( pathname, '/1/token/path' )
44-
assert.deepEqual( headers, { 'X-Simperium-API-Key': 'secret' } )
30+
equal( method, 'POST' )
31+
equal( hostname, 'auth.simperium.com' )
32+
equal( pathname, '/1/token/path' )
33+
deepEqual( headers, { 'X-Simperium-API-Key': 'secret' } )
4534
} )
4635

4736
it( 'should request auth token', ( done ) => {
48-
requests.on( 'opened', ( options ) => {
49-
const { pathname } = options;
50-
assert.equal( pathname, '/1/token/authorize/' )
51-
} )
52-
53-
requests.on( 'body', ( data, handler ) => {
37+
stub( ( data, handler ) => {
5438
const { username, password } = JSON.parse( data )
5539
const response = new EventEmitter()
56-
assert.equal( username, 'username' )
57-
assert.equal( password, 'password' )
40+
equal( username, 'username' )
41+
equal( password, 'password' )
5842

5943
handler( response )
6044
response.emit( 'data', '{\"access_token\": \"secret-token\"}' )
@@ -63,9 +47,18 @@ describe( 'Auth', () => {
6347

6448
auth.authorize( 'username', 'password' )
6549
.then( ( user ) => {
66-
assert.equal( user.access_token, 'secret-token' )
50+
equal( user.access_token, 'secret-token' )
51+
done()
52+
} )
53+
} )
54+
55+
it( 'should fail to auth with invalid credentials', ( done ) => {
56+
stubResponse( 'this is not json' )
57+
58+
auth.authorize( 'username', 'bad-password' )
59+
.catch( ( e ) => {
60+
equal( e.message, 'this is not json' )
6761
done()
6862
} )
69-
.catch( done )
7063
} )
7164
} )

0 commit comments

Comments
 (0)