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

Merge pull request #1466 from weseek/fix/1443-parsing-monguri-for-migration

Fix/1443 parsing monguri for migration
Yuki Takei 6 лет назад
Родитель
Сommit
12d8d3666b
2 измененных файлов с 42 добавлено и 1 удалено
  1. 5 1
      config/migrate.js
  2. 37 0
      src/test/config/migrate.test.js

+ 5 - 1
config/migrate.js

@@ -16,8 +16,12 @@ const mongoUri = getMongoUri();
 // parse url
 const url = new URL(mongoUri);
 
+const authStr = (url.username.length > 0 && url.password.length > 0)
+  ? `${url.username}:${url.password}@`
+  : '';
+
 const mongodb = {
-  url: `${url.protocol}//${url.host}`,
+  url: `${url.protocol}//${authStr}${url.host}${url.search}`,
   databaseName: url.pathname.substring(1), // omit heading slash
   options: {
     useNewUrlParser: true, // removes a deprecation warning when connecting

+ 37 - 0
src/test/config/migrate.test.js

@@ -0,0 +1,37 @@
+describe('config/migrate.js', () => {
+
+  beforeEach(async(done) => {
+    jest.resetModules();
+    done();
+  });
+
+  /* eslint-disable indent */
+  describe.each`
+    MONGO_URI                                         | expectedUrl                                     | expectedDbName
+    ${'mongodb://example.com/growi'}                  | ${'mongodb://example.com'}                      | ${'growi'}
+    ${'mongodb://user:pass@example.com/growi'}        | ${'mongodb://user:pass@example.com'}            | ${'growi'}
+    ${'mongodb://example.com/growi?replicaSet=mySet'} | ${'mongodb://example.com?replicaSet=mySet'}     | ${'growi'}
+  `('returns', ({ MONGO_URI, expectedUrl, expectedDbName }) => {
+    test(`when 'MONGO_URI' is '${MONGO_URI}`, () => {
+
+      // mock for mongoose-utils
+      jest.doMock('@commons/util/mongoose-utils', () => {
+        return {
+          getMongoUri: () => {
+            return MONGO_URI;
+          },
+        };
+      });
+
+      const { mongoUri, mongodb } = require('@root/config/migrate');
+
+      jest.dontMock('@commons/util/mongoose-utils');
+
+      expect(mongoUri).toBe(MONGO_URI);
+      expect(mongodb.url).toBe(expectedUrl);
+      expect(mongodb.databaseName).toBe(expectedDbName);
+    });
+  });
+  /* eslint-enable indent */
+
+});