Skip to content

Commit c8ec494

Browse files
committed
21 Adapations to createUser method + tests
1 parent 43c71f5 commit c8ec494

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/security/Security.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,25 @@ Security.prototype.createUser = function (id, content, options, cb) {
509509
options = null;
510510
}
511511

512-
if (options) {
513-
action = options.replaceIfExist ? 'createOrReplaceUser' : 'createUser';
514-
}
512+
if (options && options.hasOwnProperty('replaceIfExist')) {
513+
self.fetchUser(id, function (fetchError, fetchResult) {
514+
if (fetchResult instanceof User) {
515+
if (options.replaceIfExist !== true) {
516+
return cb(new Error('Security.createUser: User was found and shouldn\'t be replaced'));
517+
}
518+
action = 'replaceUser';
519+
}
515520

516-
self.kuzzle.query(this.buildQueryArgs(action), data, null, cb && function (err, res) {
517-
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
518-
});
521+
self.kuzzle.query(self.buildQueryArgs(action), data, null, cb && function (err, res) {
522+
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
523+
});
524+
});
525+
}
526+
else {
527+
self.kuzzle.query(self.buildQueryArgs(action), data, null, cb && function (err, res) {
528+
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
529+
});
530+
}
519531
};
520532

521533
/**

test/security/kuzzleSecurity/userMethods.test.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var
22
should = require('should'),
3+
sinon = require('sinon'),
34
Kuzzle = require('../../../src/Kuzzle'),
45
User = require('../../../src/security/User');
56

@@ -223,12 +224,52 @@ describe('Security user methods', function () {
223224
done();
224225
});
225226

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];
228231
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');
230263

231264
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+
232273
should(err).be.null();
233274
should(res).be.instanceof(User);
234275
done();

0 commit comments

Comments
 (0)