Skip to content

chore(modelarmor): added floorsettings tests #2144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
164 changes: 162 additions & 2 deletions modelarmor/test/modelarmorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
use Google\Cloud\ModelArmor\V1\RaiFilterType;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings\RaiFilter;
use Google\Cloud\ModelArmor\V1\FloorSetting;
use Google\Cloud\ModelArmor\V1\UpdateFloorSettingRequest;

class modelarmorTest extends TestCase
{
Expand All @@ -76,6 +78,8 @@ class modelarmorTest extends TestCase
protected static $testRaiTemplateId;
protected static $testMaliciousTemplateId;
protected static $testPIandJailbreakTemplateId;
protected static $organizationId;
protected static $folderId;

public static function setUpBeforeClass(): void
{
Expand All @@ -96,7 +100,9 @@ public static function setUpBeforeClass(): void
self::$testSanitizeModelResponseUserPromptId = self::getTemplateId('php-sanitize-model-response-user-prompt-');
self::$testRaiTemplateId = self::getTemplateId('php-rai-template-');
self::$testMaliciousTemplateId = self::getTemplateId('php-malicious-template-');
self::$testPIandJailbreakTemplateId = self::getTemplateId('php-pi-and-jailbreak-template-');
self::$testPIandJailbreakTemplateId = self::getTemplateId('php-template-with-pijailbreak-');
self::$organizationId = getenv('MA_ORG_ID');
self::$folderId = getenv('MA_FOLDER_ID');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use requireEnv for these, that way people running the tests will know they're required:

Suggested change
self::$organizationId = getenv('MA_ORG_ID');
self::$folderId = getenv('MA_FOLDER_ID');
self::$organizationId = self::requireEnv('MA_ORG_ID');
self::$folderId = self::requireEnv('MA_FOLDER_ID');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks.

self::createTemplateWithMaliciousURI();
self::createTemplateWithPIJailbreakFilter();
self::createTemplateWithRAI();
Expand All @@ -122,6 +128,18 @@ public static function tearDownAfterClass(): void
self::deleteTemplate(self::$projectId, self::$locationId, self::$testMaliciousTemplateId);
self::deleteTemplate(self::$projectId, self::$locationId, self::$testPIandJailbreakTemplateId);
self::deleteDlpTemplates(self::$inspectTemplateName, self::$deidentifyTemplateName, self::$locationId);

// Reset floor settings after tests
if (self::$projectId) {
self::resetProjectFloorSettings();
}
if (self::$folderId) {
self::resetFolderFloorSettings();
}
if (self::$organizationId) {
self::resetOrganizationFloorSettings();
}

self::$client->close();
}

Expand All @@ -143,6 +161,90 @@ public static function getTemplateId(string $testId): string
return uniqid($testId);
}

/**
* Reset project floor settings to default values
*/
protected static function resetProjectFloorSettings(): void

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code across all the methods of type reset...FloorSettings seems redundant. Can you please extract the common code in a separate method & use it for Project/Folder/Organization.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated logic for resetting floor settings to remove redundant logic.

{
try {
$client = new ModelArmorClient();
$floorSettingsName = sprintf('projects/%s/locations/global/floorSetting', self::$projectId);

// Create an empty filter config
$filterConfig = new FilterConfig();

// Create floor setting with enforcement disabled
$floorSetting = (new FloorSetting())
->setName($floorSettingsName)
->setFilterConfig($filterConfig)
->setEnableFloorSettingEnforcement(false);

$updateRequest = (new UpdateFloorSettingRequest())->setFloorSetting($floorSetting);
$response = $client->updateFloorSetting($updateRequest);

echo 'Floor settings reset for project ' . self::$projectId . "\n";
} catch (\Exception $e) {
// Log but don't fail teardown if reset fails
echo 'Warning: Failed to reset project floor settings: ' . $e->getMessage() . "\n";
}
}

/**
* Reset folder floor settings to default values
*/
protected static function resetFolderFloorSettings(): void
{
try {
$client = new ModelArmorClient();
$floorSettingsName = sprintf('folders/%s/locations/global/floorSetting', self::$folderId);

// Create an empty filter config
$filterConfig = new FilterConfig();

// Create floor setting with enforcement disabled
$floorSetting = (new FloorSetting())
->setName($floorSettingsName)
->setFilterConfig($filterConfig)
->setEnableFloorSettingEnforcement(false);

$updateRequest = (new UpdateFloorSettingRequest())->setFloorSetting($floorSetting);
$response = $client->updateFloorSetting($updateRequest);

echo 'Floor settings reset for folder ' . self::$folderId . "\n";
} catch (\Exception $e) {
// Log but don't fail teardown if reset fails
echo 'Warning: Failed to reset folder floor settings: ' . $e->getMessage() . "\n";
}
}

/**
* Reset organization floor settings to default values
*/
protected static function resetOrganizationFloorSettings(): void
{
try {
$client = new ModelArmorClient();
$floorSettingsName = sprintf('organizations/%s/locations/global/floorSetting', self::$organizationId);

// Create an empty filter config
$filterConfig = new FilterConfig();

// Create floor setting with enforcement disabled
$floorSetting = (new FloorSetting())
->setName($floorSettingsName)
->setFilterConfig($filterConfig)
->setEnableFloorSettingEnforcement(false);

$updateRequest = (new UpdateFloorSettingRequest())->setFloorSetting($floorSetting);
$response = $client->updateFloorSetting($updateRequest);

echo 'Floor settings reset for organization ' . self::$organizationId . "\n";
} catch (\Exception $e) {
// Log but don't fail teardown if reset fails
echo 'Warning: Failed to reset organization floor settings: ' . $e->getMessage() . "\n";
}
}

public function testCreateTemplate()
{
$output = $this->runFunctionSnippet('create_template', [
Expand Down Expand Up @@ -696,5 +798,63 @@ protected static function createTemplate($templateId, $template)
}
}

# TODO: Add tests for floor settings once API issues are resolved.
public function testGetFolderFloorSettings()
{
$output = $this->runSnippet('get_folder_floor_settings', [
self::$folderId,
]);

$expectedResponseString = 'Floor settings retrieved successfully:';
$this->assertStringContainsString($expectedResponseString, $output);
}

public function testGetProjectFloorSettings()
{
$output = $this->runSnippet('get_project_floor_settings', [
self::$projectId,
]);

$expectedResponseString = 'Floor settings retrieved successfully:';
$this->assertStringContainsString($expectedResponseString, $output);
}

public function testGetOrganizationFloorSettings()
{
$output = $this->runSnippet('get_organization_floor_settings', [
self::$organizationId,
]);

$expectedResponseString = 'Floor settings retrieved successfully:';
$this->assertStringContainsString($expectedResponseString, $output);
}

public function testUpdateFolderFloorSettings()
{
$output = $this->runSnippet('update_folder_floor_settings', [
self::$folderId,
]);

$expectedResponseString = 'Floor setting updated';
$this->assertStringContainsString($expectedResponseString, $output);
}

public function testUpdateProjectFloorSettings()
{
$output = $this->runSnippet('update_project_floor_settings', [
self::$projectId,
]);

$expectedResponseString = 'Floor setting updated';
$this->assertStringContainsString($expectedResponseString, $output);
}

public function testUpdateOrganizationFloorSettings()
{
$output = $this->runSnippet('update_organization_floor_settings', [
self::$organizationId,
]);

$expectedResponseString = 'Floor setting updated';
$this->assertStringContainsString($expectedResponseString, $output);
}
}