Browse Source

Add User test and refactor test utils

Sotaro KARASAWA 10 years ago
parent
commit
e22ba2ae68
7 changed files with 170 additions and 162 deletions
  1. 4 0
      lib/models/user.js
  2. 5 28
      test/bootstrap.js
  3. 7 11
      test/crowi/crowi.test.js
  4. 18 38
      test/models/config.test.js
  5. 49 85
      test/models/page.test.js
  6. 34 0
      test/models/user.test.js
  7. 53 0
      test/utils.js

+ 4 - 0
lib/models/user.js

@@ -35,6 +35,10 @@ module.exports = function(crowi) {
     var Config = crowi.model('Config'),
       config = crowi.getConfig();
 
+    if (!config.crowi) {
+      return STATUS_ACTIVE; // is this ok?
+    }
+
     // status decided depends on registrationMode
     switch (config.crowi['security:registrationMode']) {
       case Config.SECURITY_REGISTRATION_MODE_OPEN:

+ 5 - 28
test/bootstrap.js

@@ -2,19 +2,17 @@
 
 var express = require('express')
   , async = require('async')
-  , mongoose= require('mongoose')
   , ROOT_DIR = __dirname + '/..'
   , MODEL_DIR = __dirname + '/../lib/models'
   , Promise = require('bluebird')
-  , mongoUri
   , testDBUtil
   ;
 
-mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || null;
-
-
 testDBUtil = {
   generateFixture: function (conn, model, fixture) {
+    if (conn.readyState == 0) {
+      return Promise.reject();
+    }
     var m = conn.model(model);
 
     return new Promise(function(resolve, reject) {
@@ -26,9 +24,9 @@ testDBUtil = {
           Object.keys(entity).forEach(function(k) {
             newDoc[k] = entity[k];
           });
-
           return new Promise(function(r, rj) {
             newDoc.save(function(err, data) {
+
               createdModels.push(data);
               return r();
             });
@@ -38,31 +36,10 @@ testDBUtil = {
         resolve(createdModels);
       });
     });
-  },
-  cleanUpDb: function (conn, models) {
-    return new Promise(function(resolve, reject) {
-      if (Array.isArray(models)) {
-        models.reduce(function(promise, model) {
-          return promise.then(function() {
-            var m = conn.model(model);
-            return new Promise(function(r, rj) {
-              m.remove({}, r);
-            });
-          });
-        }, Promise.resolve()).then(function() {
-          resolve();
-        });
-      } else {
-        var m = conn.model(models);
-        m.remove({}, resolve);
-      }
-    });
-  },
+  }
 };
 
 global.express = express;
-global.mongoose = mongoose;
-global.mongoUri = mongoUri;
 global.ROOT_DIR = ROOT_DIR;
 global.MODEL_DIR = MODEL_DIR;
 global.testDBUtil = testDBUtil;

+ 7 - 11
test/crowi/crowi.test.js

@@ -43,17 +43,13 @@ describe('Test for Crowi application context', function () {
       // set
       var p = crowi.setupDatabase()
       expect(p).to.instanceof(Promise);
-      if (mongoUri) {
-        p.then(function() {
-          expect(mongoose.connection.readyState).to.equals(1);
-          done();
-        });
-      } else {
-        p.catch(function() {
-          expect(mongoose.connection.readyState).to.equals(1);
-          done();
-        });
-      }
+      p.then(function() {
+        expect(mongoose.connection.readyState).to.equals(1);
+        done();
+      }).catch(function() {
+        expect(mongoose.connection.readyState).to.equals(1);
+        done();
+      });
     });
   });
 });

+ 18 - 38
test/models/config.test.js

@@ -2,51 +2,31 @@ var chai = require('chai')
   , expect = chai.expect
   , sinon = require('sinon')
   , sinonChai = require('sinon-chai')
-  , proxyquire = require('proxyquire')
   , Promise = require('bluebird')
+  , utils = require('../utils.js')
   ;
 chai.use(sinonChai);
 
 describe('Config model test', function () {
-  var conn
-    , crowi = new (require(ROOT_DIR + '/lib/crowi'))(ROOT_DIR, process.env)
-    , Config = proxyquire(MODEL_DIR + '/config.js', {mongoose: mongoose})(crowi)
-    ;
+  var Page = utils.models.Page,
+    Config = utils.models.Config,
+    User   = utils.models.User,
+    conn = utils.mongoose.connection;
 
   before(function (done) {
-    if (mongoUri) {
-      // 基本的に mongoUri がセットされてたら、そのURIにはつながる前提
-      conn = mongoose.createConnection(mongoUri, function(err) {
-        if (err) {
-          done(); // ここで skip したいなあ
-        }
-
-        Config = conn.model('Config');
-        var fixture = [
-          {ns: 'crowi', key: 'test:test', value: JSON.stringify('crowi test value')},
-          {ns: 'crowi', key: 'test:test2', value: JSON.stringify(11111)},
-          {ns: 'crowi', key: 'test:test3', value: JSON.stringify([1, 2, 3, 4, 5])},
-          {ns: 'plugin', key: 'other:config', value: JSON.stringify('this is data')},
-        ];
-
-        testDBUtil.generateFixture(conn, 'Config', fixture)
-        .then(function(configs) {
-          done();
-        });
-      });
-    }
-  });
-
-  beforeEach(function () {
-  });
-
-  after(function (done) {
-    if (mongoUri) {
-      testDBUtil.cleanUpDb(conn, 'Config')
-        .then(function() {
-          return conn.close(done);
-        });
-    }
+    var fixture = [
+      {ns: 'crowi', key: 'test:test', value: JSON.stringify('crowi test value')},
+      {ns: 'crowi', key: 'test:test2', value: JSON.stringify(11111)},
+      {ns: 'crowi', key: 'test:test3', value: JSON.stringify([1, 2, 3, 4, 5])},
+      {ns: 'plugin', key: 'other:config', value: JSON.stringify('this is data')},
+    ];
+
+    testDBUtil.generateFixture(conn, 'Config', fixture)
+    .then(function(configs) {
+      done();
+    }).catch(function() {
+      done(new Error('Skip this test.'));
+    });
   });
 
   describe('.CONSTANTS', function () {

+ 49 - 85
test/models/page.test.js

@@ -2,103 +2,67 @@ var chai = require('chai')
   , expect = chai.expect
   , sinon = require('sinon')
   , sinonChai = require('sinon-chai')
-  , proxyquire = require('proxyquire')
   , Promise = require('bluebird')
+  , utils = require('../utils.js')
   ;
 chai.use(sinonChai);
 
 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) {
-    conn = mongoose.createConnection(mongoUri, function(err) {
-      if (err) {
+    Promise.resolve().then(function() {
+      var userFixture = [
+        {name: 'Anon 0', username: 'anonymous0', email: 'anonymous0@example.com'},
+        {name: 'Anon 1', username: 'anonymous1', email: 'anonymous1@example.com'}
+      ];
+
+      return testDBUtil.generateFixture(conn, 'User', userFixture);
+    }).then(function(testUsers) {
+      var testUser0 = testUsers[0];
+
+      var fixture = [
+        {
+          path: '/user/anonymous/memo',
+          grant: Page.GRANT_RESTRICTED,
+          grantedUsers: [testUser0],
+          creator: testUser0
+        },
+        {
+          path: '/grant/public',
+          grant: Page.GRANT_PUBLIC,
+          grantedUsers: [testUser0],
+          creator: testUser0
+        },
+        {
+          path: '/grant/restricted',
+          grant: Page.GRANT_RESTRICTED,
+          grantedUsers: [testUser0],
+          creator: testUser0
+        },
+        {
+          path: '/grant/specified',
+          grant: Page.GRANT_SPECIFIED,
+          grantedUsers: [testUser0],
+          creator: testUser0
+        },
+        {
+          path: '/grant/owner',
+          grant: Page.GRANT_OWNER,
+          grantedUsers: [testUser0],
+          creator: testUser0
+        }
+      ];
+
+      testDBUtil.generateFixture(conn, 'Page', fixture)
+      .then(function(pages) {
         done();
-      }
-
-      var Page = conn.model('Page'),
-        User = conn.model('User');
-
-      new Promise(function(resolve, reject) {
-        testDBUtil.cleanUpDb(conn, 'Page')
-          .then(function() {
-            return testDBUtil.cleanUpDb(conn, 'User')
-          })
-          .then(resolve);
-      }).then(function() {
-        var userFixture = [
-          {name: 'Anon 0', username: 'anonymous0', email: 'anonymous0@example.com'},
-          {name: 'Anon 1', username: 'anonymous1', email: 'anonymous1@example.com'}
-        ];
-
-        return new Promise(function(resolve, reject) {
-          testDBUtil.generateFixture(conn, 'User', userFixture)
-          .then(function(users) {
-            resolve(users);
-          });
-        });
-      }).then(function(testUsers) {
-        var testUser0 = testUsers[0];
-
-        var fixture = [
-          {
-            path: '/user/anonymous/memo',
-            grant: Page.GRANT_RESTRICTED,
-            grantedUsers: [testUser0],
-            creator: testUser0
-          },
-          {
-            path: '/grant/public',
-            grant: Page.GRANT_PUBLIC,
-            grantedUsers: [testUser0],
-            creator: testUser0
-          },
-          {
-            path: '/grant/restricted',
-            grant: Page.GRANT_RESTRICTED,
-            grantedUsers: [testUser0],
-            creator: testUser0
-          },
-          {
-            path: '/grant/specified',
-            grant: Page.GRANT_SPECIFIED,
-            grantedUsers: [testUser0],
-            creator: testUser0
-          },
-          {
-            path: '/grant/owner',
-            grant: Page.GRANT_OWNER,
-            grantedUsers: [testUser0],
-            creator: testUser0
-          }
-        ];
-
-        testDBUtil.generateFixture(conn, 'Page', fixture)
-        .then(function(pages) {
-          done();
-        });
       });
-
-
     });
   });
 
-  after(function (done) {
-    testDBUtil.cleanUpDb(conn, 'Page')
-      .then(function() {
-        return testDBUtil.cleanUpDb(conn, 'User')
-      })
-      .then(done);
-  });
-
   describe('.isPublic', function () {
     context('with a public page', function() {
       it('should return true', function(done) {

+ 34 - 0
test/models/user.test.js

@@ -0,0 +1,34 @@
+var chai = require('chai')
+  , expect = chai.expect
+  , sinon = require('sinon')
+  , sinonChai = require('sinon-chai')
+  , Promise = require('bluebird')
+  , utils = require('../utils.js')
+  ;
+chai.use(sinonChai);
+
+describe('User', function () {
+  var Page = utils.models.Page,
+    User   = utils.models.User,
+    conn   = utils.mongoose.connection;
+
+  describe('Create and Find.', function () {
+    context('The user', function() {
+      it('should created', function(done) {
+        User.createUserByEmailAndPassword('Aoi Miyazaki', 'aoi', 'aoi@example.com', 'hogefuga11', function (err, userData) {
+          expect(err).to.be.null;
+          expect(userData).to.instanceof(User);
+          done();
+        });
+      });
+
+      it('should be found by findUserByUsername', function(done) {
+        User.findUserByUsername('aoi', function (err, userData) {
+          expect(err).to.be.null;
+          expect(userData).to.instanceof(User);
+          done();
+        });
+      });
+    });
+  });
+});

+ 53 - 0
test/utils.js

@@ -0,0 +1,53 @@
+'use strict';
+
+var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || null
+  , mongoose= require('mongoose')
+  , models = {}
+  , crowi = new (require(ROOT_DIR + '/lib/crowi'))(ROOT_DIR, process.env)
+  ;
+
+
+before('Create database connection and clean up', function (done) {
+  if (!mongoUri) {
+    return done();
+  }
+
+  mongoose.connect(mongoUri);
+
+  function clearDB() {
+    for (var i in mongoose.connection.collections) {
+      mongoose.connection.collections[i].remove(function() {});
+    }
+    return done();
+  }
+
+  if (mongoose.connection.readyState === 0) {
+    mongoose.connect(mongoUri, function (err) {
+      if (err) {
+        throw err;
+      }
+      return clearDB();
+    });
+  } else {
+    return clearDB();
+  }
+});
+
+after('Close database connection', function (done) {
+  if (!mongoUri) {
+    return done();
+  }
+
+  mongoose.disconnect();
+  return done();
+});
+
+
+models.Page   = require(MODEL_DIR + '/page.js')(crowi);
+models.User   = require(MODEL_DIR + '/user.js')(crowi);
+models.Config = require(MODEL_DIR + '/config.js')(crowi);
+
+module.exports = {
+  models: models,
+  mongoose: mongoose,
+}