|
| 1 | +import pytest |
1 | 2 | from mock import patch |
2 | 3 |
|
3 | 4 | from prowler.lib.check.checks_loader import ( |
@@ -190,18 +191,22 @@ def test_load_checks_to_execute_with_severities_and_services_multiple(self): |
190 | 191 | def test_load_checks_to_execute_with_severities_and_services_not_within_severity( |
191 | 192 | self, |
192 | 193 | ): |
| 194 | + """Test that service not in metadata causes sys.exit(1) when used with severities""" |
193 | 195 | bulk_checks_metatada = { |
194 | 196 | S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
195 | 197 | } |
196 | 198 | service_list = ["ec2"] |
197 | 199 | severities = [S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_SEVERITY] |
198 | 200 |
|
199 | | - assert set() == load_checks_to_execute( |
200 | | - bulk_checks_metadata=bulk_checks_metatada, |
201 | | - service_list=service_list, |
202 | | - severities=severities, |
203 | | - provider=self.provider, |
204 | | - ) |
| 201 | + # ec2 service doesn't exist in the metadata, so it should exit with error |
| 202 | + with pytest.raises(SystemExit) as exc_info: |
| 203 | + load_checks_to_execute( |
| 204 | + bulk_checks_metadata=bulk_checks_metatada, |
| 205 | + service_list=service_list, |
| 206 | + severities=severities, |
| 207 | + provider=self.provider, |
| 208 | + ) |
| 209 | + assert exc_info.value.code == 1 |
205 | 210 |
|
206 | 211 | def test_load_checks_to_execute_with_checks_file( |
207 | 212 | self, |
@@ -382,3 +387,140 @@ def test_threat_detection_single_check(self): |
382 | 387 | categories=categories, |
383 | 388 | provider=self.provider, |
384 | 389 | ) |
| 390 | + |
| 391 | + def test_load_checks_to_execute_with_invalid_check(self): |
| 392 | + """Test that invalid check names cause sys.exit(1)""" |
| 393 | + bulk_checks_metatada = { |
| 394 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 395 | + } |
| 396 | + check_list = ["invalid_check_name"] |
| 397 | + |
| 398 | + with pytest.raises(SystemExit) as exc_info: |
| 399 | + load_checks_to_execute( |
| 400 | + bulk_checks_metadata=bulk_checks_metatada, |
| 401 | + check_list=check_list, |
| 402 | + provider=self.provider, |
| 403 | + ) |
| 404 | + assert exc_info.value.code == 1 |
| 405 | + |
| 406 | + def test_load_checks_to_execute_with_multiple_invalid_checks(self): |
| 407 | + """Test that multiple invalid check names cause sys.exit(1)""" |
| 408 | + bulk_checks_metatada = { |
| 409 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 410 | + } |
| 411 | + check_list = ["invalid_check_1", "invalid_check_2", "invalid_check_3"] |
| 412 | + |
| 413 | + with pytest.raises(SystemExit) as exc_info: |
| 414 | + load_checks_to_execute( |
| 415 | + bulk_checks_metadata=bulk_checks_metatada, |
| 416 | + check_list=check_list, |
| 417 | + provider=self.provider, |
| 418 | + ) |
| 419 | + assert exc_info.value.code == 1 |
| 420 | + |
| 421 | + def test_load_checks_to_execute_with_mixed_valid_invalid_checks(self): |
| 422 | + """Test that mix of valid and invalid checks cause sys.exit(1)""" |
| 423 | + bulk_checks_metatada = { |
| 424 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 425 | + } |
| 426 | + check_list = [S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME, "invalid_check"] |
| 427 | + |
| 428 | + with pytest.raises(SystemExit) as exc_info: |
| 429 | + load_checks_to_execute( |
| 430 | + bulk_checks_metadata=bulk_checks_metatada, |
| 431 | + check_list=check_list, |
| 432 | + provider=self.provider, |
| 433 | + ) |
| 434 | + assert exc_info.value.code == 1 |
| 435 | + |
| 436 | + def test_load_checks_to_execute_with_invalid_service(self): |
| 437 | + """Test that invalid service names cause sys.exit(1)""" |
| 438 | + bulk_checks_metatada = { |
| 439 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 440 | + } |
| 441 | + service_list = ["invalid_service"] |
| 442 | + |
| 443 | + with pytest.raises(SystemExit) as exc_info: |
| 444 | + load_checks_to_execute( |
| 445 | + bulk_checks_metadata=bulk_checks_metatada, |
| 446 | + service_list=service_list, |
| 447 | + provider=self.provider, |
| 448 | + ) |
| 449 | + assert exc_info.value.code == 1 |
| 450 | + |
| 451 | + def test_load_checks_to_execute_with_invalid_service_and_severity(self): |
| 452 | + """Test that invalid service names with severity cause sys.exit(1)""" |
| 453 | + bulk_checks_metatada = { |
| 454 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 455 | + } |
| 456 | + service_list = ["invalid_service"] |
| 457 | + severities = [S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_SEVERITY] |
| 458 | + |
| 459 | + with pytest.raises(SystemExit) as exc_info: |
| 460 | + load_checks_to_execute( |
| 461 | + bulk_checks_metadata=bulk_checks_metatada, |
| 462 | + service_list=service_list, |
| 463 | + severities=severities, |
| 464 | + provider=self.provider, |
| 465 | + ) |
| 466 | + assert exc_info.value.code == 1 |
| 467 | + |
| 468 | + def test_load_checks_to_execute_with_multiple_invalid_services(self): |
| 469 | + """Test that multiple invalid service names cause sys.exit(1)""" |
| 470 | + bulk_checks_metatada = { |
| 471 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 472 | + } |
| 473 | + service_list = ["invalid_service_1", "invalid_service_2"] |
| 474 | + |
| 475 | + with pytest.raises(SystemExit) as exc_info: |
| 476 | + load_checks_to_execute( |
| 477 | + bulk_checks_metadata=bulk_checks_metatada, |
| 478 | + service_list=service_list, |
| 479 | + provider=self.provider, |
| 480 | + ) |
| 481 | + assert exc_info.value.code == 1 |
| 482 | + |
| 483 | + def test_load_checks_to_execute_with_invalid_category(self): |
| 484 | + """Test that invalid category names cause sys.exit(1)""" |
| 485 | + bulk_checks_metatada = { |
| 486 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 487 | + } |
| 488 | + categories = {"invalid_category"} |
| 489 | + |
| 490 | + with pytest.raises(SystemExit) as exc_info: |
| 491 | + load_checks_to_execute( |
| 492 | + bulk_checks_metadata=bulk_checks_metatada, |
| 493 | + categories=categories, |
| 494 | + provider=self.provider, |
| 495 | + ) |
| 496 | + assert exc_info.value.code == 1 |
| 497 | + |
| 498 | + def test_load_checks_to_execute_with_multiple_invalid_categories(self): |
| 499 | + """Test that multiple invalid category names cause sys.exit(1)""" |
| 500 | + bulk_checks_metatada = { |
| 501 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 502 | + } |
| 503 | + categories = {"invalid_category_1", "invalid_category_2"} |
| 504 | + |
| 505 | + with pytest.raises(SystemExit) as exc_info: |
| 506 | + load_checks_to_execute( |
| 507 | + bulk_checks_metadata=bulk_checks_metatada, |
| 508 | + categories=categories, |
| 509 | + provider=self.provider, |
| 510 | + ) |
| 511 | + assert exc_info.value.code == 1 |
| 512 | + |
| 513 | + def test_load_checks_to_execute_with_mixed_valid_invalid_categories(self): |
| 514 | + """Test that mix of valid and invalid categories cause sys.exit(1)""" |
| 515 | + bulk_checks_metatada = { |
| 516 | + S3_BUCKET_LEVEL_PUBLIC_ACCESS_BLOCK_NAME: self.get_custom_check_s3_metadata() |
| 517 | + } |
| 518 | + categories = {"internet-exposed", "invalid_category"} |
| 519 | + |
| 520 | + with pytest.raises(SystemExit) as exc_info: |
| 521 | + load_checks_to_execute( |
| 522 | + bulk_checks_metadata=bulk_checks_metatada, |
| 523 | + categories=categories, |
| 524 | + provider=self.provider, |
| 525 | + ) |
| 526 | + assert exc_info.value.code == 1 |
0 commit comments