@@ -79,11 +79,17 @@ _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 ) ;
@@ -103,9 +109,11 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
103
109
104
110
model . create ( attrs , function ( err , result ) {
105
111
if ( err ) {
112
+ console . error ( err . message ) ;
113
+ if ( err . details ) console . error ( err . details ) ;
106
114
done ( err ) ;
107
115
} else {
108
- test [ modelName ] = result ;
116
+ test [ modelKey ] = result ;
109
117
done ( ) ;
110
118
}
111
119
} ) ;
@@ -116,7 +124,7 @@ _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
@@ -137,23 +145,32 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
137
145
if ( typeof url === 'function' ) {
138
146
urlStr = '/<dynamic>' ;
139
147
}
148
+
140
149
describe ( verb . toUpperCase ( ) + ' ' + urlStr , function ( ) {
141
150
beforeEach ( function ( cb ) {
142
151
if ( typeof url === 'function' ) {
143
152
url = url . call ( this ) ;
144
153
}
145
154
this . remotely = true ;
146
155
this . verb = verb . toUpperCase ( ) ;
147
- this . url = url ;
156
+ this . url = this . url || url ;
148
157
var methodForVerb = verb . toLowerCase ( ) ;
149
158
if ( methodForVerb === 'delete' ) methodForVerb = 'del' ;
150
159
151
- this . http = this . request [ methodForVerb ] ( url ) ;
160
+ this . http = this . request [ methodForVerb ] ( this . url ) ;
161
+
162
+ // request settings
152
163
this . http . set ( 'Accept' , 'application/json' ) ;
153
- this . req = this . http . req ;
164
+ this . http . set ( 'authorization' , null ) ;
165
+ if ( this . accessToken ) {
166
+ this . http . set ( 'authorization' , this . accessToken . id ) ;
167
+ }
168
+
154
169
var test = this ;
155
170
this . http . end ( function ( err ) {
171
+ test . req = test . http . req ;
156
172
test . res = test . http . res ;
173
+ test . url = undefined ;
157
174
cb ( ) ;
158
175
} ) ;
159
176
} ) ;
@@ -186,13 +203,11 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
186
203
describe ( 'when logged in user' , function ( ) {
187
204
_beforeEach . givenUser ( credentials , function ( done ) {
188
205
var test = this ;
189
- this . remotely = true ;
190
206
this . user . constructor . login ( credentials , function ( err , token ) {
191
207
if ( err ) {
192
208
done ( err ) ;
193
209
} else {
194
210
test . accessToken = token ;
195
- test . req . set ( 'authorization' , token . id ) ;
196
211
done ( ) ;
197
212
}
198
213
} ) ;
@@ -201,6 +216,12 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
201
216
afterEach ( function ( done ) {
202
217
this . accessToken . destroy ( done ) ;
203
218
} ) ;
219
+
220
+ afterEach ( function ( ) {
221
+ this . accessToken = undefined ;
222
+ } ) ;
223
+
224
+ cb ( ) ;
204
225
} ) ;
205
226
}
206
227
@@ -215,7 +236,8 @@ _it.shouldBeAllowed = function() {
215
236
_it . shouldBeDenied = function ( ) {
216
237
it ( 'should not be allowed' , function ( ) {
217
238
assert ( this . res ) ;
218
- assert . equal ( this . res . statusCode , 401 ) ;
239
+ var status = this . res . statusCode ;
240
+ assert ( status === 401 || status === 404 ) ;
219
241
} ) ;
220
242
}
221
243
0 commit comments