Skip to content

Commit a3b1295

Browse files
authored
Merge pull request #48 from Simperium/add/create-users
Add Support for Creating Users
2 parents dc804e2 + b5a8d3a commit a3b1295

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simperium",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "A simperium client for node.js",
55
"main": "./lib/simperium/index.js",
66
"repository": {

src/simperium/auth.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ export default function Auth( appId, appSecret ) {
1414
inherits( Auth, EventEmitter );
1515

1616
Auth.prototype.authorize = function( username, password ) {
17-
var body = JSON.stringify( {username: username, password: password } ),
17+
var body = JSON.stringify( { username: username, password: password } ),
1818
promise = this.request( 'authorize/', body );
1919

2020
return promise;
2121
}
2222

23-
// TODO: username and password to create a user
24-
Auth.prototype.create = function() {
23+
Auth.prototype.create = function( username, password ) {
24+
var body = JSON.stringify( { username: username, password: password } ),
25+
promise = this.request( 'create/', body );
2526

27+
return promise;
2628
}
2729

2830
Auth.prototype.getUrlOptions = function( path ) {

test/simperium/auth_test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,33 @@ describe( 'Auth', () => {
6161
done()
6262
} )
6363
} )
64+
65+
it( 'should create an account with valid credentials', ( done ) => {
66+
stub( ( data, handler ) => {
67+
const { username, password } = JSON.parse( data )
68+
const response = new EventEmitter()
69+
equal( username, 'username' )
70+
equal( password, 'password' )
71+
72+
handler( response )
73+
response.emit( 'data', '{\"access_token\": \"secret-token\"}' )
74+
response.emit( 'end' );
75+
} )
76+
77+
auth.create( 'username', 'password' )
78+
.then( ( user ) => {
79+
equal( user.access_token, 'secret-token' )
80+
done()
81+
} )
82+
} )
83+
84+
it( 'should fail to create an account with invalid credentials', ( done ) => {
85+
stubResponse( 'this is not json' )
86+
87+
auth.create( 'username', 'bad-password' )
88+
.catch( ( e ) => {
89+
equal( e.message, 'this is not json' )
90+
done()
91+
} )
92+
} )
6493
} )

0 commit comments

Comments
 (0)