1+ 'use strict'
2+
3+ var should = require ( 'should' ) ,
4+ urljoin = require ( 'url-join' ) ,
5+ config = require ( '../../helpers' ) . config ,
6+ UsergridClient = require ( '../../lib/client' ) ,
7+ UsergridEntity = require ( '../../lib/entity' ) ,
8+ UsergridQuery = require ( '../../lib/query' ) ,
9+ UsergridAuth = require ( '../../lib/auth' ) ,
10+ UsergridAppAuth = require ( '../../lib/appAuth' ) ,
11+ _ = require ( 'lodash' )
12+
13+ _ . mixin ( require ( 'lodash-uuid' ) )
14+
15+ var _uuid ,
16+ _slow = 500 ,
17+ _timeout = 4000
18+
19+ describe ( 'authenticateApp()' , function ( ) {
20+
21+ this . slow ( _slow )
22+ this . timeout ( _timeout )
23+
24+ var response , token , client = new UsergridClient ( )
25+ before ( function ( done ) {
26+ client . setAppAuth ( config . clientId , config . clientSecret )
27+ client . authenticateApp ( function ( err , r , t ) {
28+ response = r
29+ token = t
30+ done ( )
31+ } )
32+ } )
33+
34+ it ( 'should fail when called without a clientId and clientSecret' , function ( ) {
35+ should ( function ( ) {
36+ var client = new UsergridClient ( )
37+ client . setAppAuth ( undefined , undefined , 0 )
38+ client . authenticateApp ( )
39+ } ) . throw ( )
40+ } )
41+
42+ it ( 'should authenticate by passing clientId and clientSecret in an object' , function ( done ) {
43+ var isolatedClient = new UsergridClient ( )
44+ isolatedClient . authenticateApp ( config , function ( err , reponse , token ) {
45+ isolatedClient . appAuth . should . have . property ( 'token' ) . equal ( token )
46+ done ( )
47+ } )
48+ } )
49+
50+ it ( 'should not set client.appAuth when authenticating with a bad clientId and clientSecret in an object' , function ( done ) {
51+ var failClient = new UsergridClient ( )
52+ failClient . authenticateApp ( {
53+ clientId : 'BADCLIENTID' ,
54+ clientSecret : 'BADCLIENTSECRET'
55+ } , function ( e , r , token ) {
56+ e . should . containDeep ( {
57+ name : 'invalid_grant' ,
58+ description : 'invalid username or password'
59+ } )
60+ should ( token ) . be . undefined ( )
61+ should ( failClient . appAuth ) . be . undefined ( )
62+ done ( )
63+ } )
64+ } )
65+
66+ it ( 'should not set client.appAuth when authenticating with a bad UsergridAppAuth instance (using an object)' , function ( done ) {
67+ var failClient = new UsergridClient ( )
68+ failClient . authenticateApp ( new UsergridAppAuth ( {
69+ clientId : 'BADCLIENTID' ,
70+ clientSecret : 'BADCLIENTSECRET'
71+ } ) , function ( e , r , token ) {
72+ e . should . containDeep ( {
73+ name : 'invalid_grant' ,
74+ description : 'invalid username or password'
75+ } )
76+ should ( token ) . be . undefined ( )
77+ should ( failClient . appAuth ) . be . undefined ( )
78+ done ( )
79+ } )
80+ } )
81+
82+
83+ it ( 'should not set client.appAuth when authenticating with a bad UsergridAppAuth instance (using arguments)' , function ( done ) {
84+ var failClient = new UsergridClient ( )
85+ failClient . authenticateApp ( new UsergridAppAuth ( 'BADCLIENTID' , 'BADCLIENTSECRET' ) , function ( e , r , token ) {
86+ e . should . containDeep ( {
87+ name : 'invalid_grant' ,
88+ description : 'invalid username or password'
89+ } )
90+ should ( token ) . be . undefined ( )
91+ should ( failClient . appAuth ) . be . undefined ( )
92+ done ( )
93+ } )
94+ } )
95+
96+ it ( 'should return a 200 ok' , function ( ) {
97+ response . statusCode . should . equal ( 200 )
98+ } )
99+
100+ it ( 'should have a valid token' , function ( ) {
101+ token . should . be . a . String ( )
102+ token . length . should . be . greaterThan ( 10 )
103+ } )
104+
105+ it ( 'client.appAuth.token should be set to the token returned from Usergrid' , function ( ) {
106+ client . appAuth . should . have . property ( 'token' ) . equal ( token )
107+ } )
108+
109+ it ( 'client.appAuth.isValid should be true' , function ( ) {
110+ client . appAuth . should . have . property ( 'isValid' ) . which . is . true ( )
111+ } )
112+
113+ it ( 'client.appAuth.expiry should be set to a future date' , function ( ) {
114+ client . appAuth . should . have . property ( 'expiry' ) . greaterThan ( Date . now ( ) )
115+ } )
116+ } )
117+
118+ describe ( 'authenticateUser()' , function ( ) {
119+
120+ this . slow ( _slow )
121+ this . timeout ( _timeout )
122+
123+ var response , token , client = new UsergridClient ( )
124+ before ( function ( done ) {
125+ client . authenticateUser ( {
126+ username : config . test . username ,
127+ password : config . test . password
128+ } , function ( err , r , t ) {
129+ response = r
130+ token = t
131+ done ( )
132+ } )
133+ } )
134+
135+ it ( 'should fail when called without a email (or username) and password' , function ( ) {
136+ should ( function ( ) {
137+ var client = new UsergridClient ( )
138+ client . authenticateUser ( { } )
139+ } ) . throw ( )
140+ } )
141+
142+ it ( 'should return a 200 ok' , function ( ) {
143+ response . statusCode . should . equal ( 200 )
144+ } )
145+
146+ it ( 'should have a valid token' , function ( ) {
147+ token . should . be . a . String ( )
148+ token . length . should . be . greaterThan ( 10 )
149+ } )
150+
151+ it ( 'client.currentUser.auth.token should be set to the token returned from Usergrid' , function ( ) {
152+ client . currentUser . auth . should . have . property ( 'token' ) . equal ( token )
153+ } )
154+
155+ it ( 'client.currentUser.auth.isValid should be true' , function ( ) {
156+ client . currentUser . auth . should . have . property ( 'isValid' ) . which . is . true ( )
157+ } )
158+
159+ it ( 'client.currentUser.auth.expiry should be set to a future date' , function ( ) {
160+ client . currentUser . auth . should . have . property ( 'expiry' ) . greaterThan ( Date . now ( ) )
161+ } )
162+
163+ it ( 'client.currentUser should have a username' , function ( ) {
164+ client . currentUser . should . have . property ( 'username' )
165+ } )
166+
167+ it ( 'client.currentUser should have an email' , function ( ) {
168+ client . currentUser . should . have . property ( 'email' )
169+ } )
170+
171+ it ( 'client.currentUser and client.currentUser.auth should not store password' , function ( ) {
172+ client . currentUser . should . not . have . property ( 'password' )
173+ client . currentUser . auth . should . not . have . property ( 'password' )
174+ } )
175+ } )
176+
177+ describe ( 'appAuth, setAppAuth()' , function ( ) {
178+ it ( 'should initialize by passing a list of arguments' , function ( ) {
179+ var client = new UsergridClient ( )
180+ client . setAppAuth ( config . clientId , config . clientSecret , config . tokenTtl )
181+ client . appAuth . should . be . instanceof ( UsergridAppAuth )
182+ } )
183+
184+ it ( 'should be a subclass of UsergridAuth' , function ( ) {
185+ var client = new UsergridClient ( )
186+ client . setAppAuth ( config . clientId , config . clientSecret , config . tokenTtl )
187+ client . appAuth . should . be . instanceof ( UsergridAuth )
188+ } )
189+
190+ it ( 'should initialize by passing an object' , function ( ) {
191+ var client = new UsergridClient ( )
192+ client . setAppAuth ( {
193+ clientId : config . clientId ,
194+ clientSecret : config . clientSecret ,
195+ tokenTtl : config . tokenTtl
196+ } )
197+ client . appAuth . should . be . instanceof ( UsergridAppAuth )
198+ } )
199+
200+ it ( 'should initialize by passing an instance of UsergridAppAuth' , function ( ) {
201+ var client = new UsergridClient ( )
202+ client . setAppAuth ( new UsergridAppAuth ( config . clientId , config . clientSecret , config . tokenTtl ) )
203+ client . appAuth . should . be . instanceof ( UsergridAppAuth )
204+ } )
205+
206+ it ( 'should initialize by setting to an instance of UsergridAppAuth' , function ( ) {
207+ var client = new UsergridClient ( )
208+ client . appAuth = new UsergridAppAuth ( config . clientId , config . clientSecret , config . tokenTtl )
209+ client . appAuth . should . be . instanceof ( UsergridAppAuth )
210+ } )
211+ } )
0 commit comments