11import Auth from '../../src/simperium/auth'
22import https from 'https'
3- import assert from 'assert'
3+ import { equal , deepEqual } from 'assert'
44import { 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
3221describe ( '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