Просмотр исходного кода

Merge pull request #1068 from weseek/imprv/abolish-old-config-api-jest

Imprv/abolish old config api jest
Yuki Takei 6 лет назад
Родитель
Сommit
d0ac4482c5
5 измененных файлов с 127 добавлено и 144 удалено
  1. 17 17
      src/server/crowi/index.js
  2. 1 1
      src/server/models/index.js
  3. 4 12
      src/test/models/config.test.js
  4. 103 113
      src/test/models/page.test.js
  5. 2 1
      src/test/setup.js

+ 17 - 17
src/server/crowi/index.js

@@ -118,7 +118,7 @@ Crowi.prototype.initForTest = async function() {
     // this.setUpSlacklNotification(),
   ]);
 
-  // await Promise.all([
+  await Promise.all([
   //   this.scanRuntimeVersions(),
   //   this.setupPassport(),
   //   this.setupSearcher(),
@@ -127,10 +127,10 @@ Crowi.prototype.initForTest = async function() {
   //   this.setupCsrf(),
   //   this.setUpGlobalNotification(),
   //   this.setUpFileUpload(),
-  //   this.setUpAcl(),
+    this.setUpAcl(),
   //   this.setUpCustomize(),
   //   this.setUpRestQiitaAPI(),
-  // ]);
+  ]);
 };
 
 Crowi.prototype.isPageId = function(pageId) {
@@ -241,7 +241,7 @@ Crowi.prototype.getIo = function() {
   return this.io;
 };
 
-Crowi.prototype.scanRuntimeVersions = function() {
+Crowi.prototype.scanRuntimeVersions = async function() {
   const self = this;
 
   const check = require('check-node-version');
@@ -276,7 +276,7 @@ Crowi.prototype.getRestQiitaAPIService = function() {
   return this.restQiitaAPIService;
 };
 
-Crowi.prototype.setupPassport = function() {
+Crowi.prototype.setupPassport = async function() {
   debug('Passport is enabled');
 
   // initialize service
@@ -301,7 +301,7 @@ Crowi.prototype.setupPassport = function() {
   return Promise.resolve();
 };
 
-Crowi.prototype.setupSearcher = function() {
+Crowi.prototype.setupSearcher = async function() {
   const self = this;
   const searcherUri = this.env.ELASTICSEARCH_URI
     || this.env.BONSAI_URL
@@ -320,7 +320,7 @@ Crowi.prototype.setupSearcher = function() {
   }));
 };
 
-Crowi.prototype.setupMailer = function() {
+Crowi.prototype.setupMailer = async function() {
   const self = this;
   return new Promise(((resolve, reject) => {
     self.mailer = require('../util/mailer')(self);
@@ -328,7 +328,7 @@ Crowi.prototype.setupMailer = function() {
   }));
 };
 
-Crowi.prototype.setupSlack = function() {
+Crowi.prototype.setupSlack = async function() {
   const self = this;
 
   return new Promise(((resolve, reject) => {
@@ -340,7 +340,7 @@ Crowi.prototype.setupSlack = function() {
   }));
 };
 
-Crowi.prototype.setupCsrf = function() {
+Crowi.prototype.setupCsrf = async function() {
   const Tokens = require('csrf');
   this.tokens = new Tokens();
 
@@ -444,7 +444,7 @@ Crowi.prototype.require = function(modulePath) {
 /**
  * setup GlobalNotificationService
  */
-Crowi.prototype.setUpGlobalNotification = function() {
+Crowi.prototype.setUpGlobalNotification = async function() {
   const GlobalNotificationService = require('../service/global-notification');
   if (this.globalNotificationService == null) {
     this.globalNotificationService = new GlobalNotificationService(this);
@@ -454,7 +454,7 @@ Crowi.prototype.setUpGlobalNotification = function() {
 /**
  * setup SlackNotificationService
  */
-Crowi.prototype.setUpSlacklNotification = function() {
+Crowi.prototype.setUpSlacklNotification = async function() {
   const SlackNotificationService = require('../service/slack-notification');
   if (this.slackNotificationService == null) {
     this.slackNotificationService = new SlackNotificationService(this.configManager);
@@ -464,7 +464,7 @@ Crowi.prototype.setUpSlacklNotification = function() {
 /**
  * setup XssService
  */
-Crowi.prototype.setUpXss = function() {
+Crowi.prototype.setUpXss = async function() {
   const XssService = require('../service/xss');
   if (this.xssService == null) {
     this.xssService = new XssService(this.configManager);
@@ -474,7 +474,7 @@ Crowi.prototype.setUpXss = function() {
 /**
  * setup AclService
  */
-Crowi.prototype.setUpAcl = function() {
+Crowi.prototype.setUpAcl = async function() {
   const AclService = require('../service/acl');
   if (this.aclService == null) {
     this.aclService = new AclService(this.configManager);
@@ -484,7 +484,7 @@ Crowi.prototype.setUpAcl = function() {
 /**
  * setup CustomizeService
  */
-Crowi.prototype.setUpCustomize = function() {
+Crowi.prototype.setUpCustomize = async function() {
   const CustomizeService = require('../service/customize');
   if (this.customizeService == null) {
     this.customizeService = new CustomizeService(this.configManager, this.appService, this.xssService);
@@ -496,7 +496,7 @@ Crowi.prototype.setUpCustomize = function() {
 /**
  * setup AppService
  */
-Crowi.prototype.setUpApp = function() {
+Crowi.prototype.setUpApp = async function() {
   const AppService = require('../service/app');
   if (this.appService == null) {
     this.appService = new AppService(this.configManager);
@@ -506,7 +506,7 @@ Crowi.prototype.setUpApp = function() {
 /**
  * setup FileUploadService
  */
-Crowi.prototype.setUpFileUpload = function() {
+Crowi.prototype.setUpFileUpload = async function() {
   if (this.fileUploadService == null) {
     this.fileUploadService = require('../service/file-uploader')(this);
   }
@@ -515,7 +515,7 @@ Crowi.prototype.setUpFileUpload = function() {
 /**
  * setup RestQiitaAPIService
  */
-Crowi.prototype.setUpRestQiitaAPI = function() {
+Crowi.prototype.setUpRestQiitaAPI = async function() {
   const RestQiitaAPIService = require('../service/rest-qiita-API');
   if (this.restQiitaAPIService == null) {
     this.restQiitaAPIService = new RestQiitaAPIService(this);

+ 1 - 1
src/server/models/index.js

@@ -1,5 +1,5 @@
 module.exports = {
-  Config: require('./Config'),
+  Config: require('./config'),
   Page: require('./page'),
   PageTagRelation: require('./page-tag-relation'),
   User: require('./user'),

+ 4 - 12
src/test/models/config.test.js

@@ -1,27 +1,19 @@
-const mongoose = require('mongoose');
-
 const { getInstance } = require('../setup-crowi');
 
 describe('Config model test', () => {
   // eslint-disable-next-line no-unused-vars
   let crowi;
-  let Config;
 
   beforeAll(async(done) => {
     crowi = await getInstance();
     done();
   });
 
-  beforeEach(async(done) => {
-    Config = mongoose.model('Config');
-    done();
-  });
-
   describe('.CONSTANTS', () => {
-    test('Config has constants', async() => {
-      expect(Config.SECURITY_REGISTRATION_MODE_OPEN).toBe('Open');
-      expect(Config.SECURITY_REGISTRATION_MODE_RESTRICTED).toBe('Resricted');
-      expect(Config.SECURITY_REGISTRATION_MODE_CLOSED).toBe('Closed');
+    test('AclService has constants', async() => {
+      expect(crowi.aclService.labels.SECURITY_REGISTRATION_MODE_OPEN).toBe('Open');
+      expect(crowi.aclService.labels.SECURITY_REGISTRATION_MODE_RESTRICTED).toBe('Resricted');
+      expect(crowi.aclService.labels.SECURITY_REGISTRATION_MODE_CLOSED).toBe('Closed');
     });
   });
 

+ 103 - 113
src/test/models/page.test.js

@@ -2,6 +2,11 @@ const mongoose = require('mongoose');
 
 const { getInstance } = require('../setup-crowi');
 
+let testUser0;
+let testUser1;
+let testUser2;
+let testGroup0;
+
 describe('Page', () => {
   // eslint-disable-next-line no-unused-vars
   let crowi;
@@ -12,10 +17,7 @@ describe('Page', () => {
 
   beforeAll(async(done) => {
     crowi = await getInstance();
-    done();
-  });
 
-  beforeEach(async(done) => {
     User = mongoose.model('User');
     UserGroup = mongoose.model('UserGroup');
     UserGroupRelation = mongoose.model('UserGroupRelation');
@@ -40,10 +42,11 @@ describe('Page', () => {
       { name: 'TestGroup1' },
     ]);
 
-    const testUser0 = await User.findOne({ username: 'anonymous0' });
-    const testUser1 = await User.findOne({ username: 'anonymous1' });
+    testUser0 = await User.findOne({ username: 'anonymous0' });
+    testUser1 = await User.findOne({ username: 'anonymous1' });
+    testUser2 = await User.findOne({ username: 'anonymous2' });
 
-    const testGroup0 = await UserGroup.findOne({ name: 'TestGroup0' });
+    testGroup0 = await UserGroup.findOne({ name: 'TestGroup0' });
 
     await UserGroupRelation.insertMany([
       {
@@ -264,112 +267,99 @@ describe('Page', () => {
     });
   });
 
-  // describe('.findPage', () => {
-  //   describe('findByIdAndViewer', () => {
-  //     test('should find page (public)', async() => {
-  //       const pageToFind = createdPages[1];
-  //       const grantedUser = createdUsers[0];
-
-  //       const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
-  //       expect(page).to.be.not.null;
-  //       expect(page.path).to.equal(pageToFind.path);
-  //     });
-
-  //     test('should find page (anyone knows link)', async() => {
-  //       const pageToFind = createdPages[2];
-  //       const grantedUser = createdUsers[1];
-
-  //       const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
-  //       expect(page).to.be.not.null;
-  //       expect(page.path).to.equal(pageToFind.path);
-  //     });
-
-  //     test('should find page (just me)', async() => {
-  //       const pageToFind = createdPages[4];
-  //       const grantedUser = createdUsers[0];
-
-  //       const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
-  //       expect(page).to.be.not.null;
-  //       expect(page.path).to.equal(pageToFind.path);
-  //     });
-
-  //     test('should not be found by grant (just me)', async() => {
-  //       const pageToFind = createdPages[4];
-  //       const grantedUser = createdUsers[1];
-
-  //       const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
-  //       expect(page).toBeNull();
-  //     });
-  //   });
-
-  //   describe('findByIdAndViewer granted userGroup', () => {
-  //     test('should find page', async() => {
-  //       const pageToFind = createdPages[6];
-  //       const grantedUser = createdUsers[0];
-
-  //       const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
-  //       expect(page).to.be.not.null;
-  //       expect(page.path).to.equal(pageToFind.path);
-  //     });
-
-  //     test('should not be found by grant', async() => {
-  //       const pageToFind = createdPages[6];
-  //       const grantedUser = createdUsers[2];
-
-  //       const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
-  //       expect(page).toBeNull();
-  //     });
-  //   });
-  // });
-
-  // describe('findListWithDescendants', () => {
-  //   test('should return only /page/', async() => {
-  //     const user = createdUsers[0];
-
-  //     const result = await Page.findListWithDescendants('/page/', user, { isRegExpEscapedFromPath: true });
-
-  //     // assert totalCount
-  //     expect(result.totalCount).to.equal(1);
-  //     // assert paths
-  //     const pagePaths = result.pages.map((page) => { return page.path });
-  //     expect(pagePaths).to.include.members(['/page/for/extended']);
-  //   });
-  //   test('should return only /page1/', async() => {
-  //     const user = createdUsers[0];
-
-  //     const result = await Page.findListWithDescendants('/page1/', user, { isRegExpEscapedFromPath: true });
-
-  //     // assert totalCount
-  //     expect(result.totalCount).to.equal(2);
-  //     // assert paths
-  //     const pagePaths = result.pages.map((page) => { return page.path });
-  //     expect(pagePaths).to.include.members(['/page1', '/page1/child1']);
-  //   });
-  // });
-
-  // describe('findListByStartWith', () => {
-  //   test('should return pages which starts with /page', async() => {
-  //     const user = createdUsers[0];
-
-  //     const result = await Page.findListByStartWith('/page', user, {});
-
-  //     // assert totalCount
-  //     expect(result.totalCount).to.equal(4);
-  //     // assert paths
-  //     const pagePaths = result.pages.map((page) => { return page.path });
-  //     expect(pagePaths).to.include.members(['/page/for/extended', '/page1', '/page1/child1', '/page2']);
-  //   });
-  //   test('should process with regexp', async() => {
-  //     const user = createdUsers[0];
-
-  //     const result = await Page.findListByStartWith('/page\\d{1}/', user, {});
-
-  //     // assert totalCount
-  //     expect(result.totalCount).to.equal(3);
-  //     // assert paths
-  //     const pagePaths = result.pages.map((page) => { return page.path });
-  //     expect(pagePaths).to.include.members(['/page1', '/page1/child1', '/page2']);
-  //   });
-  // });
+  describe('.findPage', () => {
+    describe('findByIdAndViewer', () => {
+      test('should find page (public)', async() => {
+        const expectedPage = await Page.findOne({ path: '/grant/public' });
+        const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
+        expect(page).not.toBeNull();
+        expect(page.path).toEqual(expectedPage.path);
+      });
+
+      test('should find page (anyone knows link)', async() => {
+        const expectedPage = await Page.findOne({ path: '/grant/restricted' });
+        const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
+        expect(page).not.toBeNull();
+        expect(page.path).toEqual(expectedPage.path);
+      });
+
+      test('should find page (just me)', async() => {
+        const expectedPage = await Page.findOne({ path: '/grant/owner' });
+        const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
+        expect(page).not.toBeNull();
+        expect(page.path).toEqual(expectedPage.path);
+      });
 
+      test('should not be found by grant (just me)', async() => {
+        const expectedPage = await Page.findOne({ path: '/grant/owner' });
+        const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
+        expect(page).toBeNull();
+      });
+    });
+
+    describe('findByIdAndViewer granted userGroup', () => {
+      test('should find page', async() => {
+        const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
+        const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
+        expect(page).not.toBeNull();
+        expect(page.path).toEqual(expectedPage.path);
+      });
+
+      test('should not be found by grant', async() => {
+        const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
+        const page = await Page.findByIdAndViewer(expectedPage.id, testUser2);
+        expect(page).toBeNull();
+      });
+    });
+  });
+
+  describe('findListWithDescendants', () => {
+    test('should return only /page/', async() => {
+      const result = await Page.findListWithDescendants('/page/', testUser0, { isRegExpEscapedFromPath: true });
+
+      // assert totalCount
+      expect(result.totalCount).toEqual(1);
+      // assert paths
+      const pagePaths = result.pages.map((page) => { return page.path });
+      expect(pagePaths).toContainEqual('/page/for/extended');
+    });
+
+    test('should return only /page1/', async() => {
+      const result = await Page.findListWithDescendants('/page1/', testUser0, { isRegExpEscapedFromPath: true });
+
+      // assert totalCount
+      expect(result.totalCount).toEqual(2);
+      // assert paths
+      const pagePaths = result.pages.map((page) => { return page.path });
+      expect(pagePaths).toContainEqual('/page1');
+      expect(pagePaths).toContainEqual('/page1/child1');
+    });
+  });
+
+  describe('findListByStartWith', () => {
+    test('should return pages which starts with /page', async() => {
+      const result = await Page.findListByStartWith('/page', testUser0, {});
+
+      // assert totalCount
+      expect(result.totalCount).toEqual(4);
+      // assert paths
+      const pagePaths = result.pages.map((page) => { return page.path });
+      expect(pagePaths).toContainEqual('/page/for/extended');
+      expect(pagePaths).toContainEqual('/page1');
+      expect(pagePaths).toContainEqual('/page1/child1');
+      expect(pagePaths).toContainEqual('/page2');
+    });
+
+    test('should process with regexp', async() => {
+      const result = await Page.findListByStartWith('/page\\d{1}/', testUser0, {});
+
+      // assert totalCount
+      expect(result.totalCount).toEqual(3);
+      // assert paths
+      const pagePaths = result.pages.map((page) => { return page.path });
+      expect(pagePaths).toContainEqual('/page1');
+      expect(pagePaths).toContainEqual('/page1/child1');
+      expect(pagePaths).toContainEqual('/page2');
+    });
+  });
 });

+ 2 - 1
src/test/setup.js

@@ -1,10 +1,11 @@
-
 const mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || 'mongodb://localhost/growi_test';
 
 const mongoose = require('mongoose');
 
 mongoose.Promise = global.Promise;
 
+jest.setTimeout(15000); // default 5000
+
 beforeAll(async(done) => {
   await mongoose.connect(mongoUri, { useNewUrlParser: true });
   await mongoose.connection.dropDatabase();