1
1
var _describe = { } ;
2
2
var _it = { } ;
3
3
var _beforeEach = { } ;
4
- var helpers = module . exports = {
4
+ var helpers = exports = module . exports = {
5
5
describe : _describe ,
6
6
it : _it ,
7
7
beforeEach : _beforeEach
@@ -79,7 +79,7 @@ _beforeEach.withArgs = function() {
79
79
}
80
80
81
81
_beforeEach . givenModel = function ( modelName , attrs , optionalHandler ) {
82
- var modelKey = modelName ;
82
+ var modelKey = modelName
83
83
84
84
if ( typeof attrs === 'funciton' ) {
85
85
optionalHandler = attrs ;
@@ -94,6 +94,8 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
94
94
95
95
var model = loopback . getModel ( modelName ) ;
96
96
97
+ assert ( model , 'cannot get model of name ' + modelName ) ;
98
+
97
99
beforeEach ( function ( done ) {
98
100
var test = this ;
99
101
@@ -132,6 +134,29 @@ _beforeEach.givenUser = function(attrs, optionalHandler) {
132
134
_beforeEach . givenModel ( 'user' , attrs , optionalHandler ) ;
133
135
}
134
136
137
+ _beforeEach . givenLoggedInUser = function ( credentials , optionalHandler ) {
138
+ _beforeEach . givenUser ( credentials , function ( done ) {
139
+ var test = this ;
140
+ this . user . constructor . login ( credentials , function ( err , token ) {
141
+ if ( err ) {
142
+ done ( err ) ;
143
+ } else {
144
+ test . loggedInAccessToken = token ;
145
+ done ( ) ;
146
+ }
147
+ } ) ;
148
+ } ) ;
149
+
150
+ afterEach ( function ( done ) {
151
+ var test = this ;
152
+ this . loggedInAccessToken . destroy ( function ( err ) {
153
+ if ( err ) return done ( err ) ;
154
+ test . loggedInAccessToken = undefined ;
155
+ done ( ) ;
156
+ } ) ;
157
+ } ) ;
158
+ }
159
+
135
160
_beforeEach . givenAnUnauthenticatedToken = function ( attrs , optionalHandler ) {
136
161
_beforeEach . givenModel ( 'accessToken' , attrs , optionalHandler ) ;
137
162
}
@@ -158,14 +183,12 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
158
183
if ( methodForVerb === 'delete' ) methodForVerb = 'del' ;
159
184
160
185
this . http = this . request [ methodForVerb ] ( this . url ) ;
161
-
162
- // request settings
186
+ this . url = undefined ;
163
187
this . http . set ( 'Accept' , 'application/json' ) ;
164
- this . http . set ( 'authorization' , null ) ;
165
- if ( this . accessToken ) {
166
- this . http . set ( 'authorization' , this . accessToken . id ) ;
188
+ if ( this . loggedInAccessToken ) {
189
+ this . http . set ( 'authorization' , this . loggedInAccessToken . id ) ;
167
190
}
168
-
191
+ this . req = this . http . req ;
169
192
var test = this ;
170
193
this . http . end ( function ( err ) {
171
194
test . req = test . http . req ;
@@ -179,49 +202,31 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
179
202
} ) ;
180
203
}
181
204
205
+ _describe . whenLoggedInAsUser = function ( credentials , cb ) {
206
+ describe ( 'when logged in as user' , function ( ) {
207
+ _beforeEach . givenLoggedInUser ( credentials ) ;
208
+ cb ( ) ;
209
+ } ) ;
210
+ }
211
+
182
212
_describe . whenCalledByUser = function ( credentials , verb , url , cb ) {
183
- _describe . whenLoggedInAsUser ( credentials , function ( ) {
213
+ describe ( 'when called by logged in user' , function ( ) {
214
+ _beforeEach . givenLoggedInUser ( credentials ) ;
184
215
_describe . whenCalledRemotely ( verb , url , cb ) ;
185
216
} ) ;
186
217
}
187
218
188
219
_describe . whenCalledAnonymously = function ( verb , url , cb ) {
189
- _describe . whenCalledRemotely ( verb , url , function ( ) {
220
+ describe ( 'when called anonymously' , function ( ) {
190
221
_beforeEach . givenAnAnonymousToken ( ) ;
191
- cb ( ) ;
222
+ _describe . whenCalledRemotely ( verb , url , cb ) ;
192
223
} ) ;
193
224
}
194
225
195
226
_describe . whenCalledUnauthenticated = function ( verb , url , cb ) {
196
- _describe . whenCalledRemotely ( verb , url , function ( ) {
197
- _beforeEach . givenAnUnauthenticatedToken ( ) ;
198
- cb ( ) ;
199
- } ) ;
200
- }
201
-
202
- _describe . whenLoggedInAsUser = function ( credentials , cb ) {
203
- describe ( 'when logged in user' , function ( ) {
204
- _beforeEach . givenUser ( credentials , function ( done ) {
205
- var test = this ;
206
- this . user . constructor . login ( credentials , function ( err , token ) {
207
- if ( err ) {
208
- done ( err ) ;
209
- } else {
210
- test . accessToken = token ;
211
- done ( ) ;
212
- }
213
- } ) ;
214
- } ) ;
215
-
216
- afterEach ( function ( done ) {
217
- this . accessToken . destroy ( done ) ;
218
- } ) ;
219
-
220
- afterEach ( function ( ) {
221
- this . accessToken = undefined ;
222
- } ) ;
223
-
224
- cb ( ) ;
227
+ describe ( 'when called with unauthenticated token' , function ( ) {
228
+ _beforeEach . givenAnAnonymousToken ( ) ;
229
+ _describe . whenCalledRemotely ( verb , url , cb ) ;
225
230
} ) ;
226
231
}
227
232
@@ -241,6 +246,13 @@ _it.shouldBeDenied = function() {
241
246
} ) ;
242
247
}
243
248
249
+ _it . shouldNotBeFound = function ( ) {
250
+ it ( 'should not be found' , function ( ) {
251
+ assert ( this . res ) ;
252
+ assert . equal ( this . res . statusCode , 404 ) ;
253
+ } ) ;
254
+ }
255
+
244
256
_it . shouldBeAllowedWhenCalledAnonymously =
245
257
function ( verb , url ) {
246
258
_describe . whenCalledAnonymously ( verb , url , function ( ) {
@@ -282,3 +294,5 @@ function(credentials, verb, url) {
282
294
_it . shouldBeDenied ( ) ;
283
295
} ) ;
284
296
}
297
+
298
+ exports . TestDataBuilder = require ( './lib/test-data-builder' ) ;
0 commit comments