|
1 | 1 | var |
2 | 2 | should = require('should'), |
| 3 | + sinon = require('sinon'), |
3 | 4 | Kuzzle = require('../../../src/Kuzzle'), |
4 | 5 | User = require('../../../src/security/User'); |
5 | 6 |
|
@@ -223,12 +224,52 @@ describe('Security user methods', function () { |
223 | 224 | done(); |
224 | 225 | }); |
225 | 226 |
|
226 | | - it('should construct a createOrReplaceUser action if option replaceIfExist is set to true', function (done) { |
227 | | - expectedQuery.body = result.result._source; |
| 227 | + it('should construct a replaceUser action if option replaceIfExist is set to true', function (done) { |
| 228 | + var spy; |
| 229 | + |
| 230 | + expectedQuery.body = [{}, result.result._source]; |
228 | 231 | expectedQuery._id = result.result._id; |
229 | | - expectedQuery.action = 'createOrReplaceUser'; |
| 232 | + |
| 233 | + kuzzle.query = function (args, query, options, cb) { |
| 234 | + should(args.controller).be.exactly(expectedQuery.controller); |
| 235 | + |
| 236 | + if (expectedQuery.options) { |
| 237 | + should(options).match(expectedQuery.options); |
| 238 | + } |
| 239 | + |
| 240 | + if (expectedQuery.body) { |
| 241 | + if (!query.body) { |
| 242 | + query.body = {}; |
| 243 | + } |
| 244 | + |
| 245 | + should(expectedQuery.body).containEql(query.body); |
| 246 | + } else { |
| 247 | + should(query.body).be.undefined(); |
| 248 | + } |
| 249 | + |
| 250 | + if (expectedQuery._id) { |
| 251 | + should(query._id).be.exactly(expectedQuery._id); |
| 252 | + } |
| 253 | + |
| 254 | + if (cb) { |
| 255 | + if (error) { |
| 256 | + return cb(error); |
| 257 | + } |
| 258 | + |
| 259 | + cb(error, result); |
| 260 | + } |
| 261 | + }; |
| 262 | + spy = sinon.spy(kuzzle, 'query'); |
230 | 263 |
|
231 | 264 | should(kuzzle.security.createUser(result.result._id, result.result._source, {replaceIfExist: true}, function (err, res) { |
| 265 | + should(spy).be.calledTwice(); |
| 266 | + |
| 267 | + should(spy.getCall(0).args[0].action).be.exactly('getUser'); |
| 268 | + should(spy.getCall(0).args[1]).deepEqual({_id: 'foobar', body: {}}); |
| 269 | + |
| 270 | + should(spy.getCall(1).args[0].action).be.exactly('replaceUser'); |
| 271 | + should(spy.getCall(1).args[1]).deepEqual({_id: 'foobar', body: {profileIds: ['myProfile']}}); |
| 272 | + |
232 | 273 | should(err).be.null(); |
233 | 274 | should(res).be.instanceof(User); |
234 | 275 | done(); |
|
0 commit comments