-
Notifications
You must be signed in to change notification settings - Fork 1k
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
545eefb
643e577
5cce9d2
99be252
745500f
23940a7
cddc4f9
3ef1a3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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 | ||
{ | ||
|
@@ -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'); | ||
self::createTemplateWithMaliciousURI(); | ||
self::createTemplateWithPIJailbreakFilter(); | ||
self::createTemplateWithRAI(); | ||
|
@@ -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(); | ||
} | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code across all the methods of type There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', [ | ||
|
@@ -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); | ||
} | ||
} |
There was a problem hiding this comment.
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:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated, thanks.