2013-05-24 08:02:41 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
should = require('should'),
|
2013-05-24 08:02:41 +04:00
|
|
|
helpers = require('./helpers'),
|
|
|
|
UserProvider = require('../../shared/models/dataProvider.bookshelf.users');
|
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
describe('Bookshelf UsersProvider', function () {
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
var users;
|
|
|
|
|
|
|
|
beforeEach(function (done) {
|
|
|
|
helpers.resetData().then(function () {
|
|
|
|
users = new UserProvider();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can browse', function (done) {
|
2013-05-26 23:25:17 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
|
|
|
|
|
|
|
done();
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can read', function (done) {
|
|
|
|
var firstUser;
|
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
|
|
|
|
|
|
|
firstUser = results.models[0];
|
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.read({email_address: firstUser.attributes.email_address}).then(function (found) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(found);
|
|
|
|
|
|
|
|
found.attributes.username.should.equal(firstUser.attributes.username);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
|
|
|
done();
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-24 08:02:41 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-24 08:02:41 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can edit', function (done) {
|
|
|
|
var firstUser;
|
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
firstUser = results.models[0];
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.edit({id: firstUser.id, url: "some.newurl.com"}).then(function (edited) {
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
should.exist(edited);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
edited.attributes.url.should.equal('some.newurl.com');
|
2013-05-24 08:02:41 +04:00
|
|
|
|
|
|
|
done();
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-24 08:02:41 +04:00
|
|
|
});
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can add', function (done) {
|
|
|
|
var userData = {
|
|
|
|
password: 'testpass1',
|
|
|
|
email_address: "test@test1.com"
|
|
|
|
};
|
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.add(userData).then(function (createdUser) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(createdUser);
|
|
|
|
|
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, "password was hashed");
|
|
|
|
createdUser.attributes.email_address.should.eql(userData.email_address, "email address corred");
|
|
|
|
|
|
|
|
done();
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can delete', function (done) {
|
|
|
|
var firstUserId,
|
|
|
|
ids,
|
|
|
|
hasDeletedId;
|
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
|
|
|
|
|
|
|
firstUserId = results.models[0].id;
|
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.destroy(firstUserId).then(function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-26 23:25:17 +04:00
|
|
|
users.browse().then(function (newResults) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
if (newResults.length < 1) {
|
|
|
|
// Bug out if we only had one user and deleted it.
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
ids = _.pluck(newResults.models, "id");
|
|
|
|
|
|
|
|
hasDeletedId = _.any(ids, function (id) {
|
|
|
|
return id === firstUserId;
|
|
|
|
});
|
|
|
|
|
|
|
|
hasDeletedId.should.equal(false);
|
|
|
|
|
|
|
|
done();
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
2013-05-26 23:25:17 +04:00
|
|
|
}, function (error) {
|
|
|
|
throw error;
|
2013-05-24 08:02:41 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}());
|