@@ -79,15 +79,23 @@ _beforeEach.withArgs = function() {
79
79
}
80
80
81
81
_beforeEach . givenModel = function ( modelName , attrs , optionalHandler ) {
82
+ var modelKey = modelName
83
+
82
84
if ( typeof attrs === 'funciton' ) {
83
85
optionalHandler = attrs ;
84
86
attrs = undefined ;
85
87
}
86
88
89
+ if ( typeof optionalHandler === 'string' ) {
90
+ modelKey = optionalHandler ;
91
+ }
92
+
87
93
attrs = attrs || { } ;
88
94
89
95
var model = loopback . getModel ( modelName ) ;
90
96
97
+ assert ( model , 'cannot get model of name ' + modelName ) ;
98
+
91
99
beforeEach ( function ( done ) {
92
100
var test = this ;
93
101
@@ -105,7 +113,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
105
113
if ( err ) {
106
114
done ( err ) ;
107
115
} else {
108
- test [ modelName ] = result ;
116
+ test [ modelKey ] = result ;
109
117
done ( ) ;
110
118
}
111
119
} ) ;
@@ -116,14 +124,37 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
116
124
}
117
125
118
126
afterEach ( function ( done ) {
119
- this [ modelName ] . destroy ( done ) ;
127
+ this [ modelKey ] . destroy ( done ) ;
120
128
} ) ;
121
129
}
122
130
123
131
_beforeEach . givenUser = function ( attrs , optionalHandler ) {
124
132
_beforeEach . givenModel ( 'user' , attrs , optionalHandler ) ;
125
133
}
126
134
135
+ _beforeEach . givenLoggedInUser = function ( credentials , optionalHandler ) {
136
+ _beforeEach . givenUser ( credentials , function ( done ) {
137
+ var test = this ;
138
+ this . user . constructor . login ( credentials , function ( err , token ) {
139
+ if ( err ) {
140
+ done ( err ) ;
141
+ } else {
142
+ test . loggedInAccessToken = token ;
143
+ done ( ) ;
144
+ }
145
+ } ) ;
146
+ } ) ;
147
+
148
+ afterEach ( function ( done ) {
149
+ var test = this ;
150
+ this . loggedInAccessToken . destroy ( function ( err ) {
151
+ if ( err ) return done ( err ) ;
152
+ test . loggedInAccessToken = undefined ;
153
+ done ( ) ;
154
+ } ) ;
155
+ } ) ;
156
+ }
157
+
127
158
_beforeEach . givenAnUnauthenticatedToken = function ( attrs , optionalHandler ) {
128
159
_beforeEach . givenModel ( 'accessToken' , attrs , optionalHandler ) ;
129
160
}
@@ -144,12 +175,16 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
144
175
}
145
176
this . remotely = true ;
146
177
this . verb = verb . toUpperCase ( ) ;
147
- this . url = url ;
178
+ this . url = this . url || url ;
148
179
var methodForVerb = verb . toLowerCase ( ) ;
149
180
if ( methodForVerb === 'delete' ) methodForVerb = 'del' ;
150
181
151
- this . http = this . request [ methodForVerb ] ( url ) ;
182
+ this . http = this . request [ methodForVerb ] ( this . url ) ;
183
+ this . url = undefined ;
152
184
this . http . set ( 'Accept' , 'application/json' ) ;
185
+ if ( this . loggedInAccessToken ) {
186
+ this . http . set ( 'authorization' , this . loggedInAccessToken . id ) ;
187
+ }
153
188
this . req = this . http . req ;
154
189
var test = this ;
155
190
this . http . end ( function ( err ) {
@@ -162,45 +197,31 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
162
197
} ) ;
163
198
}
164
199
200
+ _describe . whenLoggedInAsUser = function ( credentials , cb ) {
201
+ describe ( 'when logged in as user' , function ( ) {
202
+ _beforeEach . givenLoggedInUser ( credentials ) ;
203
+ cb ( ) ;
204
+ } ) ;
205
+ }
206
+
165
207
_describe . whenCalledByUser = function ( credentials , verb , url , cb ) {
166
- _describe . whenLoggedInAsUser ( credentials , function ( ) {
208
+ describe ( 'when called by logged in user' , function ( ) {
209
+ _beforeEach . givenLoggedInUser ( credentials ) ;
167
210
_describe . whenCalledRemotely ( verb , url , cb ) ;
168
211
} ) ;
169
212
}
170
213
171
214
_describe . whenCalledAnonymously = function ( verb , url , cb ) {
172
- _describe . whenCalledRemotely ( verb , url , function ( ) {
215
+ describe ( 'when called anonymously' , function ( ) {
173
216
_beforeEach . givenAnAnonymousToken ( ) ;
174
- cb ( ) ;
217
+ _describe . whenCalledRemotely ( verb , url , cb ) ;
175
218
} ) ;
176
219
}
177
220
178
221
_describe . whenCalledUnauthenticated = function ( verb , url , cb ) {
179
- _describe . whenCalledRemotely ( verb , url , function ( ) {
180
- _beforeEach . givenAnUnauthenticatedToken ( ) ;
181
- cb ( ) ;
182
- } ) ;
183
- }
184
-
185
- _describe . whenLoggedInAsUser = function ( credentials , cb ) {
186
- describe ( 'when logged in user' , function ( ) {
187
- _beforeEach . givenUser ( credentials , function ( done ) {
188
- var test = this ;
189
- this . remotely = true ;
190
- this . user . constructor . login ( credentials , function ( err , token ) {
191
- if ( err ) {
192
- done ( err ) ;
193
- } else {
194
- test . accessToken = token ;
195
- test . req . set ( 'authorization' , token . id ) ;
196
- done ( ) ;
197
- }
198
- } ) ;
199
- } ) ;
200
-
201
- afterEach ( function ( done ) {
202
- this . accessToken . destroy ( done ) ;
203
- } ) ;
222
+ describe ( 'when called with unauthenticated token' , function ( ) {
223
+ _beforeEach . givenAnAnonymousToken ( ) ;
224
+ _describe . whenCalledRemotely ( verb , url , cb ) ;
204
225
} ) ;
205
226
}
206
227
@@ -219,6 +240,13 @@ _it.shouldBeDenied = function() {
219
240
} ) ;
220
241
}
221
242
243
+ _it . shouldNotBeFound = function ( ) {
244
+ it ( 'should not be found' , function ( ) {
245
+ assert ( this . res ) ;
246
+ assert . equal ( this . res . statusCode , 404 ) ;
247
+ } ) ;
248
+ }
249
+
222
250
_it . shouldBeAllowedWhenCalledAnonymously =
223
251
function ( verb , url ) {
224
252
_describe . whenCalledAnonymously ( verb , url , function ( ) {
0 commit comments