Yuki Takei 5 месяцев назад
Родитель
Сommit
fbeccc4bab
1 измененных файлов с 240 добавлено и 28 удалено
  1. 240 28
      apps/app/src/server/routes/apiv3/app-settings/file-upload-setting.integ.ts

+ 240 - 28
apps/app/src/server/routes/apiv3/app-settings/file-upload-setting.integ.ts

@@ -88,38 +88,250 @@ describe('file-upload-setting route', () => {
     expect(crowiMock.setUpFileUpload).toHaveBeenCalledWith(true);
   });
 
-  it('should preserve existing s3SecretAccessKey when not included in request', async() => {
-    // Arrange: Set up existing secret in DB
-    const existingSecret = 'existing-secret-key-12345';
-    await configManager.updateConfigs({
-      'app:fileUploadType': 'aws',
-      'aws:s3SecretAccessKey': toNonBlankString(existingSecret),
-      'aws:s3Region': toNonBlankString('us-west-2'),
-      'aws:s3Bucket': toNonBlankString('existing-bucket'),
+  describe('AWS settings', () => {
+    const setupAwsSecret = async(secret: string) => {
+      await configManager.updateConfigs({
+        'app:fileUploadType': 'aws',
+        'aws:s3SecretAccessKey': toNonBlankString(secret),
+        'aws:s3Region': toNonBlankString('us-west-2'),
+        'aws:s3Bucket': toNonBlankString('existing-bucket'),
+      });
+      await configManager.loadConfigs();
+    };
+
+    it('should preserve existing s3SecretAccessKey when not included in request', async() => {
+      const existingSecret = 'existing-secret-key-12345';
+      await setupAwsSecret(existingSecret);
+
+      expect(configManager.getConfig('aws:s3SecretAccessKey')).toBe(existingSecret);
+
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'aws',
+          s3Region: 'us-east-1',
+          s3Bucket: 'test-bucket',
+          s3ReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('aws:s3SecretAccessKey')).toBe(existingSecret);
+      expect(response.body.responseParams.fileUploadType).toBe('aws');
     });
-    await configManager.loadConfigs(); // Reload configs after update
 
-    // Verify the secret was set
-    const secretBeforeUpdate = configManager.getConfig('aws:s3SecretAccessKey');
-    expect(secretBeforeUpdate).toBe(existingSecret);
+    it('should update s3SecretAccessKey when new value is provided in request', async() => {
+      const existingSecret = 'existing-secret-key-12345';
+      await setupAwsSecret(existingSecret);
 
-    // Act: Update AWS settings without including s3SecretAccessKey
-    const response = await request(app)
-      .put('/')
-      .send({
-        fileUploadType: 'aws',
-        s3Region: 'us-east-1',
-        s3Bucket: 'test-bucket',
-        s3ReferenceFileWithRelayMode: false,
-      })
-      .expect(200);
+      expect(configManager.getConfig('aws:s3SecretAccessKey')).toBe(existingSecret);
 
-    // Reload configs to get latest values
-    await configManager.loadConfigs();
+      const newSecret = 'new-secret-key-67890';
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'aws',
+          s3Region: 'us-east-1',
+          s3Bucket: 'test-bucket',
+          s3SecretAccessKey: newSecret,
+          s3ReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('aws:s3SecretAccessKey')).toBe(newSecret);
+      expect(response.body.responseParams.fileUploadType).toBe('aws');
+    });
+
+    it('should remove s3SecretAccessKey when empty string is provided in request', async() => {
+      const existingSecret = 'existing-secret-key-12345';
+      await setupAwsSecret(existingSecret);
+
+      expect(configManager.getConfig('aws:s3SecretAccessKey')).toBe(existingSecret);
+
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'aws',
+          s3Region: 'us-east-1',
+          s3Bucket: 'test-bucket',
+          s3SecretAccessKey: '',
+          s3ReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('aws:s3SecretAccessKey')).toBeUndefined();
+      expect(response.body.responseParams.fileUploadType).toBe('aws');
+    });
+  });
+
+  describe('GCS settings', () => {
+    const setupGcsSecret = async(apiKeyPath: string) => {
+      await configManager.updateConfigs({
+        'app:fileUploadType': 'gcs',
+        'gcs:apiKeyJsonPath': toNonBlankString(apiKeyPath),
+        'gcs:bucket': toNonBlankString('existing-bucket'),
+      });
+      await configManager.loadConfigs();
+    };
+
+    it('should preserve existing gcsApiKeyJsonPath when not included in request', async() => {
+      const existingApiKeyPath = '/path/to/existing-api-key.json';
+      await setupGcsSecret(existingApiKeyPath);
+
+      expect(configManager.getConfig('gcs:apiKeyJsonPath')).toBe(existingApiKeyPath);
+
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'gcs',
+          gcsBucket: 'test-bucket',
+          gcsReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('gcs:apiKeyJsonPath')).toBe(existingApiKeyPath);
+      expect(response.body.responseParams.fileUploadType).toBe('gcs');
+    });
+
+    it('should update gcsApiKeyJsonPath when new value is provided in request', async() => {
+      const existingApiKeyPath = '/path/to/existing-api-key.json';
+      await setupGcsSecret(existingApiKeyPath);
+
+      expect(configManager.getConfig('gcs:apiKeyJsonPath')).toBe(existingApiKeyPath);
 
-    // Assert: Secret should still exist in DB
-    const secretAfterUpdate = configManager.getConfig('aws:s3SecretAccessKey');
-    expect(secretAfterUpdate).toBe(existingSecret);
-    expect(response.body.responseParams.fileUploadType).toBe('aws');
+      const newApiKeyPath = '/path/to/new-api-key.json';
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'gcs',
+          gcsBucket: 'test-bucket',
+          gcsApiKeyJsonPath: newApiKeyPath,
+          gcsReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('gcs:apiKeyJsonPath')).toBe(newApiKeyPath);
+      expect(response.body.responseParams.fileUploadType).toBe('gcs');
+    });
+
+    it('should remove gcsApiKeyJsonPath when empty string is provided in request', async() => {
+      const existingApiKeyPath = '/path/to/existing-api-key.json';
+      await setupGcsSecret(existingApiKeyPath);
+
+      expect(configManager.getConfig('gcs:apiKeyJsonPath')).toBe(existingApiKeyPath);
+
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'gcs',
+          gcsBucket: 'test-bucket',
+          gcsApiKeyJsonPath: '',
+          gcsReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('gcs:apiKeyJsonPath')).toBeUndefined();
+      expect(response.body.responseParams.fileUploadType).toBe('gcs');
+    });
+  });
+
+  describe('Azure settings', () => {
+    const setupAzureSecret = async(secret: string) => {
+      await configManager.updateConfigs({
+        'app:fileUploadType': 'azure',
+        'azure:clientSecret': toNonBlankString(secret),
+        'azure:tenantId': toNonBlankString('existing-tenant-id'),
+        'azure:clientId': toNonBlankString('existing-client-id'),
+        'azure:storageAccountName': toNonBlankString('existingaccount'),
+        'azure:storageContainerName': toNonBlankString('existing-container'),
+      });
+      await configManager.loadConfigs();
+    };
+
+    it('should preserve existing azureClientSecret when not included in request', async() => {
+      const existingSecret = 'existing-azure-secret-12345';
+      await setupAzureSecret(existingSecret);
+
+      expect(configManager.getConfig('azure:clientSecret')).toBe(existingSecret);
+
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'azure',
+          azureTenantId: 'new-tenant-id',
+          azureClientId: 'new-client-id',
+          azureStorageAccountName: 'newaccount',
+          azureStorageContainerName: 'new-container',
+          azureReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('azure:clientSecret')).toBe(existingSecret);
+      expect(response.body.responseParams.fileUploadType).toBe('azure');
+    });
+
+    it('should update azureClientSecret when new value is provided in request', async() => {
+      const existingSecret = 'existing-azure-secret-12345';
+      await setupAzureSecret(existingSecret);
+
+      expect(configManager.getConfig('azure:clientSecret')).toBe(existingSecret);
+
+      const newSecret = 'new-azure-secret-67890';
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'azure',
+          azureTenantId: 'new-tenant-id',
+          azureClientId: 'new-client-id',
+          azureStorageAccountName: 'newaccount',
+          azureStorageContainerName: 'new-container',
+          azureClientSecret: newSecret,
+          azureReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('azure:clientSecret')).toBe(newSecret);
+      expect(response.body.responseParams.fileUploadType).toBe('azure');
+    });
+
+    it('should remove azureClientSecret when empty string is provided in request', async() => {
+      const existingSecret = 'existing-azure-secret-12345';
+      await setupAzureSecret(existingSecret);
+
+      expect(configManager.getConfig('azure:clientSecret')).toBe(existingSecret);
+
+      const response = await request(app)
+        .put('/')
+        .send({
+          fileUploadType: 'azure',
+          azureTenantId: 'new-tenant-id',
+          azureClientId: 'new-client-id',
+          azureStorageAccountName: 'newaccount',
+          azureStorageContainerName: 'new-container',
+          azureClientSecret: '',
+          azureReferenceFileWithRelayMode: false,
+        })
+        .expect(200);
+
+      await configManager.loadConfigs();
+
+      expect(configManager.getConfig('azure:clientSecret')).toBeUndefined();
+      expect(response.body.responseParams.fileUploadType).toBe('azure');
+    });
   });
 });