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 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 120 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 = self::requireEnv('MA_ORG_ID');
self::$folderId = self::requireEnv('MA_FOLDER_ID');
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::resetFloorSettings('project', self::$projectId);
}
if (self::$folderId) {
self::resetFloorSettings('folder', self::$folderId);
}
if (self::$organizationId) {
self::resetFloorSettings('organization', self::$organizationId);
}

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

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

/**
* Resets floor settings to default values for various resource types
*
* @param string $resourceType The type of resource (project, folder, organization)
* @param string $resourceId The ID of the resource
*/
protected static function resetFloorSettings(string $resourceType, string $resourceId): void
{
try {
$client = new ModelArmorClient();

// Format resource path based on resource type
$resourcePathFormat = match($resourceType) {
'project' => 'projects/%s/locations/global/floorSetting',
'folder' => 'folders/%s/locations/global/floorSetting',
'organization' => 'organizations/%s/locations/global/floorSetting',
default => throw new \InvalidArgumentException("Invalid resource type: {$resourceType}"),
};

$floorSettingsName = sprintf($resourcePathFormat, $resourceId);

// 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 {$resourceType} {$resourceId}\n";
} catch (\Exception $e) {
// Log but don't fail teardown if reset fails
echo "Warning: Failed to reset {$resourceType} floor settings: " . $e->getMessage() . "\n";
}
}

// Wrapper methods removed in favor of directly calling resetFloorSettings

public function testCreateTemplate()
{
$output = $this->runFunctionSnippet('create_template', [
Expand Down Expand Up @@ -696,5 +756,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);
}
}