@@ -27,21 +27,16 @@ import (
27
27
28
28
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
29
29
"github.com/google/uuid"
30
- "github.com/joho/godotenv"
31
30
"google.golang.org/api/option"
32
31
grpccodes "google.golang.org/grpc/codes"
33
32
grpcstatus "google.golang.org/grpc/status"
34
33
)
35
34
35
+ // testLocation returns the test location from the environment variable `GOLANG_SAMPLES_LOCATION`.
36
+ // If not set, the test is skipped.
36
37
func testLocation (t * testing.T ) string {
37
38
t .Helper ()
38
39
39
- // Load the test.env file
40
- err := godotenv .Load ("./testdata/env/test.env" )
41
- if err != nil {
42
- t .Fatalf ("failed to load test environment file: %v" , err )
43
- }
44
-
45
40
v := os .Getenv ("GOLANG_SAMPLES_LOCATION" )
46
41
if v == "" {
47
42
t .Skip ("testIamUser: missing GOLANG_SAMPLES_LOCATION" )
@@ -50,36 +45,76 @@ func testLocation(t *testing.T) string {
50
45
return v
51
46
}
52
47
48
+ // testClient initializes and returns a new Model Armor client and context
49
+ // for use in tests. The client is created using the location-specific endpoint.
53
50
func testClient (t * testing.T ) (* modelarmor.Client , context.Context ) {
54
51
t .Helper ()
55
52
56
53
ctx := context .Background ()
57
54
58
- locationId := testLocation (t )
55
+ locationID := testLocation (t )
59
56
60
- //Endpoint to send the request to regional server
61
- client , err := modelarmor . NewClient ( ctx ,
62
- option . WithEndpoint ( fmt . Sprintf ( "modelarmor.%s.rep.googleapis.com:443" , locationId )),
63
- )
57
+ // Create options for Model Armor client.
58
+ opts := option . WithEndpoint ( fmt . Sprintf ( "modelarmor.%s.rep.googleapis.com:443" , locationID ))
59
+ // Create the Model Armor client.
60
+ client , err := modelarmor . NewClient ( ctx , opts )
64
61
if err != nil {
65
62
t .Fatalf ("failed to create client: %v" , err )
66
63
}
67
64
68
65
return client , ctx
69
66
}
70
67
68
+ // testModelArmorTemplate creates a new ModelArmor template for use in tests.
69
+ // It returns the created template or an error.
70
+ func testModelArmorTemplate (t * testing.T , templateID string ) (* modelarmorpb.Template , error ) {
71
+ t .Helper ()
72
+ tc := testutil .SystemTest (t )
73
+ locationID := testLocation (t )
74
+ client , ctx := testClient (t )
75
+
76
+ template := & modelarmorpb.Template {
77
+ FilterConfig : & modelarmorpb.FilterConfig {
78
+ PiAndJailbreakFilterSettings : & modelarmorpb.PiAndJailbreakFilterSettings {
79
+ FilterEnforcement : modelarmorpb .PiAndJailbreakFilterSettings_ENABLED ,
80
+ ConfidenceLevel : modelarmorpb .DetectionConfidenceLevel_MEDIUM_AND_ABOVE ,
81
+ },
82
+ MaliciousUriFilterSettings : & modelarmorpb.MaliciousUriFilterSettings {
83
+ FilterEnforcement : modelarmorpb .MaliciousUriFilterSettings_ENABLED ,
84
+ },
85
+ },
86
+ }
87
+
88
+ req := & modelarmorpb.CreateTemplateRequest {
89
+ Parent : fmt .Sprintf ("projects/%s/locations/%s" , tc .ProjectID , locationID ),
90
+ TemplateId : templateID ,
91
+ Template : template ,
92
+ }
93
+
94
+ response , err := client .CreateTemplate (ctx , req )
95
+ if err != nil {
96
+ return nil , fmt .Errorf ("failed to create template: %v" , err )
97
+ }
98
+
99
+ return response , err
100
+ }
101
+
102
+ // testCleanupTemplate deletes a Model Armor template by name, used for cleanup
103
+ // after tests. Ignores errors if the template is already deleted.
71
104
func testCleanupTemplate (t * testing.T , templateName string ) {
72
105
t .Helper ()
73
106
74
107
client , ctx := testClient (t )
75
108
if err := client .DeleteTemplate (ctx , & modelarmorpb.DeleteTemplateRequest {Name : templateName }); err != nil {
76
109
if terr , ok := grpcstatus .FromError (err ); ! ok || terr .Code () != grpccodes .NotFound {
77
- t .Fatalf ("testCleanupTemplate: failed to delete template %s : %v" , templateName , err )
110
+ t .Fatalf ("testCleanupTemplate: failed to delete template: %v" , err )
78
111
}
79
112
}
80
113
81
114
}
82
115
116
+ // TestScreenPDFFile scrrens the pdf file content and Sanitize
117
+ // the content with the Model Armor.
83
118
func TestScreenPDFFile (t * testing.T ) {
84
119
pdfContentBase := `"JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PC9UaXRsZSAoVW50aXRsZWQgZG9jdW1lbnQpCi9Qcm9kdWNlciAoU2tp
85
120
YS9QREYgbTEzNSBHb29nbGUgRG9jcyBSZW5kZXJlcik+PgplbmRvYmoKMyAwIG9iago8PC9jYSAxCi9CTSAvTm9y
@@ -391,18 +426,19 @@ MDAwMTg5MTQgMDAwMDAgbiAKMDAwMDAxOTMyOSAwMDAwMCBuIAp0cmFpbGVyCjw8L1NpemUgMTkKL1Jv
391
426
MCBSCi9JbmZvIDEgMCBSPj4Kc3RhcnR4cmVmCjE5ODQ0CiUlRU9GCg=="`
392
427
tc := testutil .SystemTest (t )
393
428
templateID := fmt .Sprintf ("test-model-armor-%s" , uuid .New ().String ())
394
-
395
- var b bytes.Buffer
396
- if _ , err := createModelArmorTemplate (& b , tc .ProjectID , testLocation (t ), templateID ); err != nil {
429
+ locationID := testLocation (t )
430
+ templateName := fmt .Sprintf ("projects/%s/locations/%s/templates/%s" , tc .ProjectID , locationID , templateID )
431
+ var buf bytes.Buffer
432
+ if _ , err := testModelArmorTemplate (t , templateID ); err != nil {
397
433
t .Fatal (err )
398
434
}
399
- defer testCleanupTemplate (t , fmt . Sprintf ( "projects/%s/locations/%s/templates/%s" , tc . ProjectID , testLocation ( t ), templateID ) )
435
+ defer testCleanupTemplate (t , templateName )
400
436
401
- if _ , err := screenPDFFile (& b , tc .ProjectID , testLocation (t ), templateID , pdfContentBase ); err != nil {
437
+ if err := screenPDFFile (& buf , tc .ProjectID , testLocation (t ), templateID , pdfContentBase ); err != nil {
402
438
t .Fatal (err )
403
439
}
404
440
405
- if got , want := b .String (), "PDF screening sanitization result: " ; ! strings .Contains (got , want ) {
441
+ if got , want := buf .String (), "PDF screening sanitization result: " ; ! strings .Contains (got , want ) {
406
442
t .Errorf ("screenPdf: expected %q to contain %q" , got , want )
407
443
}
408
444
}
0 commit comments