Procházet zdrojové kódy

refactor and add tests

Yuki Takei před 8 měsíci
rodič
revize
65a2d9734e

+ 95 - 0
apps/app/src/server/service/growi-info/growi-info.integ.ts

@@ -113,5 +113,100 @@ describe('GrowiInfoService', () => {
       });
     });
 
+    test('Should get correct GROWI info with specific options - attachment only', async() => {
+      // act
+      const growiInfo = await growiInfoService.getGrowiInfo({ includeAttachment: true });
+
+      // assert
+      assert(growiInfo != null);
+      expect(growiInfo.additionalInfo).toEqual({
+        attachmentType: 'aws',
+        activeExternalAccountTypes: ['saml', 'github'],
+      });
+    });
+
+    test('Should get correct GROWI info with specific options - user count only', async() => {
+      // act
+      const growiInfo = await growiInfoService.getGrowiInfo({ includeUserCount: true });
+
+      // assert
+      assert(growiInfo != null);
+      expect(growiInfo.additionalInfo).toEqual({
+        attachmentType: 'aws',
+        activeExternalAccountTypes: ['saml', 'github'],
+        currentUsersCount: 1, // Only one user from the legacy test
+        currentActiveUsersCount: 1,
+      });
+    });
+
+    test('Should get correct GROWI info with specific options - installed info only', async() => {
+      // act
+      const growiInfo = await growiInfoService.getGrowiInfo({ includeInstalledInfo: true });
+
+      // assert
+      assert(growiInfo != null);
+      expect(growiInfo.additionalInfo).toEqual({
+        attachmentType: 'aws',
+        activeExternalAccountTypes: ['saml', 'github'],
+        installedAt: new Date('2000-01-01'),
+        installedAtByOldestUser: new Date('2000-01-01'),
+      });
+    });
+
+    test('Should get correct GROWI info with combined options', async() => {
+      // act
+      const growiInfo = await growiInfoService.getGrowiInfo({
+        includeAttachment: true,
+        includeUserCount: true,
+      });
+
+      // assert
+      assert(growiInfo != null);
+      expect(growiInfo.additionalInfo).toEqual({
+        attachmentType: 'aws',
+        activeExternalAccountTypes: ['saml', 'github'],
+        currentUsersCount: 1,
+        currentActiveUsersCount: 1,
+      });
+    });
+
+    test('Should get correct GROWI info with all options', async() => {
+      // act
+      const growiInfo = await growiInfoService.getGrowiInfo({
+        includeAttachment: true,
+        includeInstalledInfo: true,
+        includeUserCount: true,
+      });
+
+      // assert
+      assert(growiInfo != null);
+      expect(growiInfo.additionalInfo).toEqual({
+        attachmentType: 'aws',
+        activeExternalAccountTypes: ['saml', 'github'],
+        installedAt: new Date('2000-01-01'),
+        installedAtByOldestUser: new Date('2000-01-01'),
+        currentUsersCount: 1,
+        currentActiveUsersCount: 1,
+      });
+    });
+
+    test('Should get correct GROWI info with empty options', async() => {
+      // act
+      const growiInfo = await growiInfoService.getGrowiInfo({});
+
+      // assert
+      assert(growiInfo != null);
+      expect(growiInfo.additionalInfo).toBeUndefined();
+      expect(growiInfo).toEqual({
+        version: appVersion,
+        appSiteUrl: 'http://growi.test.jp',
+        serviceInstanceId: '',
+        type: 'on-premise',
+        wikiType: 'closed',
+        deploymentType: 'growi-docker-compose',
+        osInfo: growiInfo.osInfo, // Keep the osInfo as it's dynamic
+      });
+    });
+
   });
 });

+ 29 - 16
apps/app/src/server/service/growi-info/growi-info.ts

@@ -63,8 +63,8 @@ export class GrowiInfoService {
   getGrowiInfo(includeAdditionalInfo: true): Promise<IGrowiInfo<IGrowiAdditionalInfoResult<typeof FULL_ADDITIONAL_INFO_OPTIONS>>>;
 
   async getGrowiInfo<T extends GrowiInfoOptions>(
-      optionsOrLegacyFlag?: T | boolean,
-  ): Promise<IGrowiInfo<IGrowiAdditionalInfoResult<T>>> {
+      optionsOrLegacyFlag?: T | true,
+  ): Promise<IGrowiInfo<IGrowiAdditionalInfoResult<T>> | IGrowiInfo<undefined> | IGrowiInfo<IGrowiAdditionalInfoResult<typeof FULL_ADDITIONAL_INFO_OPTIONS>>> {
 
     const appSiteUrl = this.getSiteUrl();
 
@@ -117,18 +117,31 @@ export class GrowiInfoService {
   private async getAdditionalInfoByOptions<T extends GrowiInfoOptions>(options: T): Promise<IGrowiAdditionalInfoResult<T>> {
     const User = mongoose.model<IUser, Model<IUser>>('User');
 
-    const result: Record<string, unknown> = {};
+    // Check if any option is enabled to determine if we should return additional info
+    const hasAnyOption = options.includeAttachment || options.includeInstalledInfo || options.includeUserCount;
 
-    // Always include attachment info if any option is enabled
-    if (options.includeAttachment || options.includeInstalledInfo || options.includeUserCount) {
-      const activeExternalAccountTypes: IExternalAuthProviderType[] = Object.values(IExternalAuthProviderType).filter((type) => {
-        return configManager.getConfig(`security:passport-${type}:isEnabled`);
-      });
-
-      result.attachmentType = configManager.getConfig('app:fileUploadType');
-      result.activeExternalAccountTypes = activeExternalAccountTypes;
+    if (!hasAnyOption) {
+      return undefined as IGrowiAdditionalInfoResult<T>;
     }
 
+    // Include attachment info (required for all additional info)
+    const activeExternalAccountTypes: IExternalAuthProviderType[] = Object.values(IExternalAuthProviderType).filter((type) => {
+      return configManager.getConfig(`security:passport-${type}:isEnabled`);
+    });
+
+    // Build result incrementally with proper typing
+    const partialResult: Partial<{
+      attachmentType: unknown;
+      activeExternalAccountTypes: IExternalAuthProviderType[];
+      installedAt: Date | null;
+      installedAtByOldestUser: Date | null;
+      currentUsersCount: number;
+      currentActiveUsersCount: number;
+    }> = {
+      attachmentType: configManager.getConfig('app:fileUploadType'),
+      activeExternalAccountTypes,
+    };
+
     if (options.includeInstalledInfo) {
       // Get the oldest user who probably installed this GROWI.
       const user = await User.findOne({ createdAt: { $ne: null } }).sort({ createdAt: 1 });
@@ -138,19 +151,19 @@ export class GrowiInfoService {
       const oldestConfig = await Config.findOne().sort({ createdAt: 1 });
       const installedAt = installedAtByOldestUser ?? appInstalledConfig?.createdAt ?? oldestConfig?.createdAt ?? null;
 
-      result.installedAt = installedAt;
-      result.installedAtByOldestUser = installedAtByOldestUser;
+      partialResult.installedAt = installedAt;
+      partialResult.installedAtByOldestUser = installedAtByOldestUser;
     }
 
     if (options.includeUserCount) {
       const currentUsersCount = await User.countDocuments();
       const currentActiveUsersCount = await (User as unknown as { countActiveUsers: () => Promise<number> }).countActiveUsers();
 
-      result.currentUsersCount = currentUsersCount;
-      result.currentActiveUsersCount = currentActiveUsersCount;
+      partialResult.currentUsersCount = currentUsersCount;
+      partialResult.currentActiveUsersCount = currentActiveUsersCount;
     }
 
-    return (Object.keys(result).length > 0 ? result : undefined) as IGrowiAdditionalInfoResult<T>;
+    return partialResult as IGrowiAdditionalInfoResult<T>;
   }
 
 }