Skip to content

Commit 38cc0ab

Browse files
aiskleeyeh
authored andcommitted
feat: add AV.User#getRoles() (#363)
1 parent 39016ca commit 38cc0ab

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

src/user.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,16 @@ module.exports = function(AV) {
589589
return this._sessionToken;
590590
},
591591

592+
/**
593+
* Get this user's Roles.
594+
* @param {Object} options A Backbone-style options object.
595+
* @return {AV.Promise} A promise that is fulfilled with the roles when
596+
* the query is complete.
597+
*/
598+
getRoles(options) {
599+
return AV.Relation.reverseQuery("_Role", "users", this).find(options);
600+
},
601+
592602
}, /** @lends AV.User */ {
593603
// Class Variables
594604

test/query.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -294,30 +294,20 @@ describe('Queries', function () {
294294
});
295295

296296
describe('destroyAll', function () {
297-
it('should be deleted', function (done) {
297+
it('should be deleted', function () {
298298
// save some objects
299299
var promises = [];
300300
for (var i = 0; i < 10; i++) {
301301
var test = new AV.Object('deletedAll');
302302
test.set('number', i);
303303
promises.unshift(test.save());
304304
}
305-
// AV.Promise.when(promises).then(function() {done();});
306-
AV.Promise.when(promises).then(function () {
307-
var query = new AV.Query('deletedAll');
308-
query.limit(1000).find().then(function (results) {
309-
// expect(results.length).to.be(10);
310-
query.destroyAll().then(function () {
311-
query.find().then(function (results) {
312-
expect(results.length).to.be(0);
313-
done();
314-
});
315-
}, function (err) {
316-
done(err);
317-
});
318-
}, function (err) {
319-
done(err);
320-
});
305+
return AV.Promise.when(promises).then(function () {
306+
return new AV.Query('deletedAll').limit(300).destroyAll();
307+
}).then(function() {
308+
return new AV.Query('deletedAll').count();
309+
}).then(function(count) {
310+
expect(count).to.be(0);
321311
});
322312
});
323313
});

test/user.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ describe("User", function() {
179179
});
180180
});
181181

182+
describe('User#getRoles', function() {
183+
it('Should get the current user\'s role', function() {
184+
var user = AV.User.current();
185+
return new AV.Query('_Role').equalTo('name', 'testRole').first()
186+
.then(function(role) {
187+
return role.destroy();
188+
}).catch(function() {
189+
// already destroyed
190+
}).then(function() {
191+
var role = new AV.Role("testRole");
192+
role.getUsers().add(user);
193+
return role.save()
194+
}).then(function() {
195+
return user.getRoles()
196+
}).then(function(roles) {
197+
expect(roles.length).to.be(1);
198+
expect(roles[0].getName()).to.be('testRole');
199+
});
200+
});
201+
});
182202

183203
describe("Associations", function() {
184204
it("return post relation to user", function(done) {

0 commit comments

Comments
 (0)