|
@@ -2,65 +2,64 @@ var chai = require('chai')
|
|
|
, expect = chai.expect
|
|
, expect = chai.expect
|
|
|
, sinon = require('sinon')
|
|
, sinon = require('sinon')
|
|
|
, sinonChai = require('sinon-chai')
|
|
, sinonChai = require('sinon-chai')
|
|
|
- , proxyquire = require('proxyquire')
|
|
|
|
|
|
|
+ , Promise = require('bluebird')
|
|
|
|
|
+ , utils = require('../utils.js')
|
|
|
;
|
|
;
|
|
|
chai.use(sinonChai);
|
|
chai.use(sinonChai);
|
|
|
|
|
|
|
|
describe('Page', function () {
|
|
describe('Page', function () {
|
|
|
- var conn
|
|
|
|
|
- , crowi = new (require(ROOT_DIR + '/lib/crowi'))(ROOT_DIR, process.env)
|
|
|
|
|
- , Page = proxyquire(MODEL_DIR + '/page.js', {mongoose: mongoose})(crowi)
|
|
|
|
|
- , User = proxyquire(MODEL_DIR + '/user.js', {mongoose: mongoose})(crowi)
|
|
|
|
|
- ;
|
|
|
|
|
-
|
|
|
|
|
- if (!mongoUri) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ var Page = utils.models.Page,
|
|
|
|
|
+ User = utils.models.User,
|
|
|
|
|
+ conn = utils.mongoose.connection;
|
|
|
|
|
|
|
|
before(function (done) {
|
|
before(function (done) {
|
|
|
- conn = mongoose.createConnection(mongoUri, function(err) {
|
|
|
|
|
- if (err) {
|
|
|
|
|
- done();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ Promise.resolve().then(function() {
|
|
|
|
|
+ var userFixture = [
|
|
|
|
|
+ {name: 'Anon 0', username: 'anonymous0', email: 'anonymous0@example.com'},
|
|
|
|
|
+ {name: 'Anon 1', username: 'anonymous1', email: 'anonymous1@example.com'}
|
|
|
|
|
+ ];
|
|
|
|
|
|
|
|
- Page = conn.model('Page');
|
|
|
|
|
- User = conn.model('User');
|
|
|
|
|
|
|
+ return testDBUtil.generateFixture(conn, 'User', userFixture);
|
|
|
|
|
+ }).then(function(testUsers) {
|
|
|
|
|
+ var testUser0 = testUsers[0];
|
|
|
|
|
|
|
|
var fixture = [
|
|
var fixture = [
|
|
|
{
|
|
{
|
|
|
path: '/user/anonymous/memo',
|
|
path: '/user/anonymous/memo',
|
|
|
grant: Page.GRANT_RESTRICTED,
|
|
grant: Page.GRANT_RESTRICTED,
|
|
|
- grantedUsers: []
|
|
|
|
|
|
|
+ grantedUsers: [testUser0],
|
|
|
|
|
+ creator: testUser0
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
path: '/grant/public',
|
|
path: '/grant/public',
|
|
|
- grant: Page.GRANT_PUBLIC
|
|
|
|
|
|
|
+ grant: Page.GRANT_PUBLIC,
|
|
|
|
|
+ grantedUsers: [testUser0],
|
|
|
|
|
+ creator: testUser0
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
path: '/grant/restricted',
|
|
path: '/grant/restricted',
|
|
|
- grant: Page.GRANT_RESTRICTED
|
|
|
|
|
|
|
+ grant: Page.GRANT_RESTRICTED,
|
|
|
|
|
+ grantedUsers: [testUser0],
|
|
|
|
|
+ creator: testUser0
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
path: '/grant/specified',
|
|
path: '/grant/specified',
|
|
|
- grant: Page.GRANT_SPECIFIED
|
|
|
|
|
|
|
+ grant: Page.GRANT_SPECIFIED,
|
|
|
|
|
+ grantedUsers: [testUser0],
|
|
|
|
|
+ creator: testUser0
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
path: '/grant/owner',
|
|
path: '/grant/owner',
|
|
|
- grant: Page.GRANT_OWNER
|
|
|
|
|
|
|
+ grant: Page.GRANT_OWNER,
|
|
|
|
|
+ grantedUsers: [testUser0],
|
|
|
|
|
+ creator: testUser0
|
|
|
}
|
|
}
|
|
|
];
|
|
];
|
|
|
- var userFixture = [
|
|
|
|
|
- {userId: 'anonymous', email: 'anonymous@gmail.com'}
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
- testDBUtil.generateFixture(conn, 'Page', fixture, function() {});
|
|
|
|
|
- testDBUtil.generateFixture(conn, 'User', userFixture, done);
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
|
|
|
|
|
- after(function (done) {
|
|
|
|
|
- return testDBUtil.cleanUpDb(conn, 'Page', function(err, doc) {
|
|
|
|
|
- return conn.close(done);
|
|
|
|
|
|
|
+ testDBUtil.generateFixture(conn, 'Page', fixture)
|
|
|
|
|
+ .then(function(pages) {
|
|
|
|
|
+ done();
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -88,23 +87,45 @@ describe('Page', function () {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ describe('.isCreator', function() {
|
|
|
|
|
+ context('with creator', function() {
|
|
|
|
|
+ it('should return true', function(done) {
|
|
|
|
|
+ User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
|
|
|
|
|
+ if (err) { done(err); }
|
|
|
|
|
+
|
|
|
|
|
+ Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
|
|
|
|
|
+ expect(page.isCreator(user)).to.be.equal(true);
|
|
|
|
|
+ done();
|
|
|
|
|
+ })
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ context('with non-creator', function() {
|
|
|
|
|
+ it('should return false', function(done) {
|
|
|
|
|
+ User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
|
|
|
|
|
+ if (err) { done(err); }
|
|
|
|
|
+
|
|
|
|
|
+ Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
|
|
|
|
|
+ expect(page.isCreator(user)).to.be.equal(false);
|
|
|
|
|
+ done();
|
|
|
|
|
+ })
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
describe('.isGrantedFor', function() {
|
|
describe('.isGrantedFor', function() {
|
|
|
context('with a granted user', function() {
|
|
context('with a granted user', function() {
|
|
|
it('should return true', function(done) {
|
|
it('should return true', function(done) {
|
|
|
- User.find({userId: 'anonymous'}, function(err, user) {
|
|
|
|
|
|
|
+ User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
|
|
|
if (err) { done(err); }
|
|
if (err) { done(err); }
|
|
|
|
|
|
|
|
Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
|
|
Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
|
|
|
if (err) { done(err); }
|
|
if (err) { done(err); }
|
|
|
|
|
|
|
|
- page.grantedUsers.push(user.id);
|
|
|
|
|
-
|
|
|
|
|
- page.save(function(err, newPage) {
|
|
|
|
|
- if (err) { done(err); }
|
|
|
|
|
-
|
|
|
|
|
- expect(newPage.isGrantedFor(user)).to.be.equal(true);
|
|
|
|
|
- done();
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ expect(page.isGrantedFor(user)).to.be.equal(true);
|
|
|
|
|
+ done();
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
@@ -112,10 +133,10 @@ describe('Page', function () {
|
|
|
|
|
|
|
|
context('with a public page', function() {
|
|
context('with a public page', function() {
|
|
|
it('should return true', function(done) {
|
|
it('should return true', function(done) {
|
|
|
- User.find({userId: 'anonymous'}, function(err, user) {
|
|
|
|
|
|
|
+ User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
|
|
|
if (err) { done(err); }
|
|
if (err) { done(err); }
|
|
|
|
|
|
|
|
- Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
|
|
|
|
|
|
|
+ Page.findOne({path: '/grant/public'}, function(err, page) {
|
|
|
if (err) { done(err); }
|
|
if (err) { done(err); }
|
|
|
|
|
|
|
|
expect(page.isGrantedFor(user)).to.be.equal(true);
|
|
expect(page.isGrantedFor(user)).to.be.equal(true);
|
|
@@ -127,7 +148,7 @@ describe('Page', function () {
|
|
|
|
|
|
|
|
context('with a restricted page and an user who has no grant', function() {
|
|
context('with a restricted page and an user who has no grant', function() {
|
|
|
it('should return false', function(done) {
|
|
it('should return false', function(done) {
|
|
|
- User.find({userId: 'anonymous'}, function(err, user) {
|
|
|
|
|
|
|
+ User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
|
|
|
if (err) { done(err); }
|
|
if (err) { done(err); }
|
|
|
|
|
|
|
|
Page.findOne({path: '/grant/restricted'}, function(err, page) {
|
|
Page.findOne({path: '/grant/restricted'}, function(err, page) {
|