Yuki Takei 6 лет назад
Родитель
Сommit
e8f7467e77
2 измененных файлов с 82 добавлено и 12 удалено
  1. 12 12
      src/server/crowi/index.js
  2. 70 0
      src/test/service/passport.test.js

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

@@ -130,19 +130,19 @@ Crowi.prototype.initForTest = async function() {
   ]);
 
   await Promise.all([
-  //   this.scanRuntimeVersions(),
-  //   this.setupPassport(),
-  //   this.setupSearcher(),
-  //   this.setupMailer(),
-  //   this.setupSlack(),
-  //   this.setupCsrf(),
-  //   this.setUpFileUpload(),
+    // this.scanRuntimeVersions(),
+    this.setupPassport(),
+    // this.setupSearcher(),
+    // this.setupMailer(),
+    // this.setupSlack(),
+    // this.setupCsrf(),
+    // this.setUpFileUpload(),
     this.setUpAcl(),
-  //   this.setUpCustomize(),
-  //   this.setUpRestQiitaAPI(),
-  //   this.setupUserGroup(),
-  //   this.setupExport(),
-  //   this.setupImport(),
+    // this.setUpCustomize(),
+    // this.setUpRestQiitaAPI(),
+    // this.setupUserGroup(),
+    // this.setupExport(),
+    // this.setupImport(),
   ]);
 
   // globalNotification depends on slack and mailer

+ 70 - 0
src/test/service/passport.test.js

@@ -0,0 +1,70 @@
+const { getInstance } = require('../setup-crowi');
+
+describe('PassportService test', () => {
+  let crowi;
+
+  beforeEach(async(done) => {
+    crowi = await getInstance();
+    done();
+  });
+
+
+  describe('verifySAMLResponseByABLCRule()', () => {
+
+    let getConfigSpy;
+    let extractAttributesFromSAMLResponseSpy;
+
+    beforeEach(async(done) => {
+      // prepare spy for ConfigManager.getConfig
+      getConfigSpy = jest.spyOn(crowi.configManager, 'getConfig');
+      // prepare spy for extractAttributesFromSAMLResponse method
+      extractAttributesFromSAMLResponseSpy = jest.spyOn(crowi.passportService, 'extractAttributesFromSAMLResponse');
+      done();
+    });
+
+    /* eslint-disable indent */
+    describe.each`
+      conditionId | departments   | positions     | ruleStr                                                                       | expected
+      ${1}        | ${['A']}      | ${[]}         | ${'Department = A | Department = B & Position = Leader'}                      | ${true}
+      ${2}        | ${['B']}      | ${['Leader']} | ${'Department = A | Department = B & Position = Leader'}                      | ${true}
+      ${3}        | ${['A', 'C']} | ${['Leader']} | ${'Department = A | Department = B & Position = Leader'}                      | ${true}
+      ${4}        | ${['B', 'C']} | ${['Leader']} | ${'Department = A | Department = B & Position = Leader'}                      | ${true}
+      ${5}        | ${[]}         | ${[]}         | ${'Department = A | Department = B & Position = Leader'}                      | ${false}
+      ${6}        | ${['C']}      | ${['Leader']} | ${'Department = A | Department = B & Position = Leader'}                      | ${false}
+      ${7}        | ${['A']}      | ${['Leader']} | ${'Department = A & Position = Leader | Department = B & Position = Leader'}  | ${true}
+      ${8}        | ${['B']}      | ${['Leader']} | ${'Department = A & Position = Leader | Department = B & Position = Leader'}  | ${true}
+      ${9}        | ${['C']}      | ${['Leader']} | ${'Department = A & Position = Leader | Department = B & Position = Leader'}  | ${false}
+      ${9}        | ${['A', 'B']} | ${['']}       | ${'Department = A & Position = Leader | Department = B & Position = Leader'}  | ${false}
+    `('to be $expected under rule="$ruleStr"', ({
+      conditionId, departments, positions, ruleStr, expected,
+    }) => {
+      test(`when condition=${conditionId}`, async() => {
+        const responseMock = {};
+
+        // setup mock implementation
+        getConfigSpy.mockImplementation((ns, key) => {
+          if (ns === 'crowi' && key === 'security:passport-saml:ABLCRule') {
+            return ruleStr;
+          }
+          throw new Error('Unexpected behavior.');
+        });
+        extractAttributesFromSAMLResponseSpy.mockImplementation((response) => {
+          if (response !== responseMock) {
+            throw new Error('Unexpected args.');
+          }
+          return {
+            Department: departments,
+            Position: positions,
+          };
+        });
+
+        const result = crowi.passportService.verifySAMLResponseByABLCRule(responseMock);
+
+        expect(result).toBe(expected);
+      });
+    });
+
+  });
+
+
+});