|
1 | 1 | """Tests for SophosFirewall module
|
2 | 2 | """
|
3 | 3 | import unittest
|
4 |
| -from unittest.mock import patch, Mock |
| 4 | +from unittest.mock import patch, Mock, MagicMock |
5 | 5 | from firewallapi import (
|
6 | 6 | SophosFirewall,
|
7 | 7 | SophosFirewallZeroRecords,
|
@@ -365,7 +365,7 @@ def test_create_rule(self, mocked_post):
|
365 | 365 | assert self.fw.create_rule(rule_params=rule_params) == expected_result
|
366 | 366 |
|
367 | 367 | @patch.object(SophosFirewall, "_post")
|
368 |
| - def test_failed_create(self, mocked_post): |
| 368 | + def test_failed_create_rule(self, mocked_post): |
369 | 369 | """Test failed creation response"""
|
370 | 370 | mock_response = Mock()
|
371 | 371 | mock_response.content = (
|
@@ -403,6 +403,220 @@ def test_failed_create(self, mocked_post):
|
403 | 403 | SophosFirewallAPIError, self.fw.create_rule, {"rule_params": rule_params}
|
404 | 404 | )
|
405 | 405 |
|
| 406 | + @patch.object(SophosFirewall, "_post") |
| 407 | + def test_create_user(self, mocked_post): |
| 408 | + """Test create_user() method""" |
| 409 | + mock_response = Mock() |
| 410 | + mock_response.content = ( |
| 411 | + """ |
| 412 | + <?xml version="1.0" encoding="utf-8"?> |
| 413 | + <Response APIVersion="1905.1" IPS_CAT_VER="1"> |
| 414 | + <Login> |
| 415 | + <status>Authentication Successful</status> |
| 416 | + </Login><User transactionid=""> |
| 417 | + <Status code="200">Configuration applied successfully.</Status> |
| 418 | + </User> |
| 419 | + </Response>""".replace( |
| 420 | + "\n", "" |
| 421 | + ) |
| 422 | + .strip() |
| 423 | + .encode() |
| 424 | + ) |
| 425 | + mocked_post.return_value = mock_response |
| 426 | + |
| 427 | + user_params = {'user': 'testuser', |
| 428 | + 'name': 'Test User', |
| 429 | + 'description': 'Test User', |
| 430 | + 'user_password': 'C1sc0.123456', |
| 431 | + 'user_type': 'Administrator', |
| 432 | + 'profile': 'Administrator', |
| 433 | + 'group': 'Open Group', |
| 434 | + |
| 435 | + |
| 436 | + expected_result = { |
| 437 | + "Response": { |
| 438 | + "@APIVersion": "1905.1", |
| 439 | + "@IPS_CAT_VER": "1", |
| 440 | + "Login": {"status": "Authentication Successful"}, |
| 441 | + "User": { |
| 442 | + "@transactionid": "", |
| 443 | + "Status": { |
| 444 | + "@code": "200", |
| 445 | + "#text": "Configuration applied successfully.", |
| 446 | + }, |
| 447 | + }, |
| 448 | + } |
| 449 | + } |
| 450 | + |
| 451 | + assert self.fw.create_user(**user_params) == expected_result |
| 452 | + |
| 453 | + @patch.object(SophosFirewall, "_post") |
| 454 | + def test_update_backup(self, mocked_post): |
| 455 | + """Test update_backup() method""" |
| 456 | + mock_response = Mock() |
| 457 | + mock_response.content = ( |
| 458 | + """ |
| 459 | + <?xml version="1.0" encoding="utf-8"?> |
| 460 | + <Response APIVersion="1905.1" IPS_CAT_VER="1"> |
| 461 | + <Login> |
| 462 | + <status>Authentication Successful</status> |
| 463 | + </Login><BackupRestore transactionid=""> |
| 464 | + <Status code="200">Configuration applied successfully.</Status> |
| 465 | + </BackupRestore> |
| 466 | + </Response>""".replace( |
| 467 | + "\n", "" |
| 468 | + ) |
| 469 | + .strip() |
| 470 | + .encode() |
| 471 | + ) |
| 472 | + mocked_post.return_value = mock_response |
| 473 | + |
| 474 | + backup_params = {'BackupFrequency': 'Weekly', |
| 475 | + 'BackupMode': 'Local', |
| 476 | + 'BackupPrefix': 'us-bos-utm-stag-1', |
| 477 | + 'Day': 'Sunday', |
| 478 | + 'EmailAddress': '[email protected]', |
| 479 | + 'FTPServer': None, |
| 480 | + 'FtpPath': None, |
| 481 | + 'Hour': '22', |
| 482 | + 'Minute': '00', |
| 483 | + 'Username': None, |
| 484 | + 'ftp': False, |
| 485 | + 'username': 'apiuser', |
| 486 | + 'password': 'password'} |
| 487 | + |
| 488 | + expected_result = { |
| 489 | + "Response": { |
| 490 | + "@APIVersion": "1905.1", |
| 491 | + "@IPS_CAT_VER": "1", |
| 492 | + "Login": {"status": "Authentication Successful"}, |
| 493 | + "BackupRestore": { |
| 494 | + "@transactionid": "", |
| 495 | + "Status": { |
| 496 | + "@code": "200", |
| 497 | + "#text": "Configuration applied successfully.", |
| 498 | + }, |
| 499 | + }, |
| 500 | + } |
| 501 | + } |
| 502 | + |
| 503 | + assert self.fw.update_backup(backup_params=backup_params) == expected_result |
| 504 | + |
| 505 | + |
| 506 | + @patch.object(SophosFirewall, "_post") |
| 507 | + @patch.object(SophosFirewall, "get_acl_rule") |
| 508 | + def test_update_service_acl(self, mocked_get_acl_rule, mocked_post): |
| 509 | + """Test update_service_acl() method""" |
| 510 | + mock_get = MagicMock() |
| 511 | + mock_get.__getitem__.return_value = {'@APIVersion': '2000.1', |
| 512 | + '@IPS_CAT_VER': '1', |
| 513 | + 'Login': {'status': 'Authentication Successful'}, |
| 514 | + 'LocalServiceACL': {'@transactionid': '', |
| 515 | + 'RuleName': 'Appliance Access', |
| 516 | + 'Description': None, |
| 517 | + 'Position': 'Top', |
| 518 | + 'IPFamily': 'IPv4', |
| 519 | + 'SourceZone': 'Any', |
| 520 | + 'Hosts': {'Host': ['Sophos Internal ACL', |
| 521 | + 'Sophos External ACL', |
| 522 | + 'All EAA Hosts']}, |
| 523 | + 'Services': {'Service': ['Ping', |
| 524 | + 'HTTPS', |
| 525 | + 'SSH', |
| 526 | + 'Ping', |
| 527 | + 'UserPortal', |
| 528 | + 'VPNPortal']}, |
| 529 | + 'Action': 'accept'}} |
| 530 | + |
| 531 | + mock_response = Mock() |
| 532 | + mock_response.content = ( |
| 533 | + """ |
| 534 | + <?xml version="1.0" encoding="utf-8"?> |
| 535 | + <Response APIVersion="1905.1" IPS_CAT_VER="1"> |
| 536 | + <Login> |
| 537 | + <status>Authentication Successful</status> |
| 538 | + </Login><LocalServiceACL transactionid=""> |
| 539 | + <Status code="200">Configuration applied successfully.</Status> |
| 540 | + </LocalServiceACL> |
| 541 | + </Response>""".replace( |
| 542 | + "\n", "" |
| 543 | + ) |
| 544 | + .strip() |
| 545 | + .encode() |
| 546 | + ) |
| 547 | + |
| 548 | + mocked_get_acl_rule.return_value = mock_get |
| 549 | + mocked_post.return_value = mock_response |
| 550 | + |
| 551 | + expected_result = { |
| 552 | + "Response": { |
| 553 | + "@APIVersion": "1905.1", |
| 554 | + "@IPS_CAT_VER": "1", |
| 555 | + "Login": {"status": "Authentication Successful"}, |
| 556 | + "LocalServiceACL": { |
| 557 | + "@transactionid": "", |
| 558 | + "Status": { |
| 559 | + "@code": "200", |
| 560 | + "#text": "Configuration applied successfully.", |
| 561 | + }, |
| 562 | + }, |
| 563 | + } |
| 564 | + } |
| 565 | + |
| 566 | + assert self.fw.update_service_acl(host_list=["Any"], action="add") == expected_result |
| 567 | + |
| 568 | + @patch.object(SophosFirewall, "_post") |
| 569 | + @patch.object(SophosFirewall, "get_urlgroup") |
| 570 | + def test_update_urlgroup(self, mocked_get_urlgroup, mocked_post): |
| 571 | + """Test update_urlgroup() method""" |
| 572 | + mock_get = MagicMock() |
| 573 | + mock_get.__getitem__.return_value = {'@APIVersion': '2000.1', |
| 574 | + '@IPS_CAT_VER': '1', |
| 575 | + 'Login': {'status': 'Authentication Successful'}, |
| 576 | + 'WebFilterURLGroup': {'@transactionid': '', |
| 577 | + 'Name': 'TEST1', |
| 578 | + 'Description': 'Test URL list', |
| 579 | + 'IsDefault': 'No', |
| 580 | + 'URLlist': {'URL': ['testdomain1.com', 'testdomain2.com']}}} |
| 581 | + |
| 582 | + mock_response = Mock() |
| 583 | + mock_response.content = ( |
| 584 | + """ |
| 585 | + <?xml version="1.0" encoding="utf-8"?> |
| 586 | + <Response APIVersion="1905.1" IPS_CAT_VER="1"> |
| 587 | + <Login> |
| 588 | + <status>Authentication Successful</status> |
| 589 | + </Login><WebFilterURLGroup transactionid=""> |
| 590 | + <Status code="200">Configuration applied successfully.</Status> |
| 591 | + </WebFilterURLGroup> |
| 592 | + </Response>""".replace( |
| 593 | + "\n", "" |
| 594 | + ) |
| 595 | + .strip() |
| 596 | + .encode() |
| 597 | + ) |
| 598 | + |
| 599 | + mocked_get_urlgroup.return_value = mock_get |
| 600 | + mocked_post.return_value = mock_response |
| 601 | + |
| 602 | + expected_result = { |
| 603 | + "Response": { |
| 604 | + "@APIVersion": "1905.1", |
| 605 | + "@IPS_CAT_VER": "1", |
| 606 | + "Login": {"status": "Authentication Successful"}, |
| 607 | + "WebFilterURLGroup": { |
| 608 | + "@transactionid": "", |
| 609 | + "Status": { |
| 610 | + "@code": "200", |
| 611 | + "#text": "Configuration applied successfully.", |
| 612 | + }, |
| 613 | + }, |
| 614 | + } |
| 615 | + } |
| 616 | + |
| 617 | + assert self.fw.update_urlgroup(name='TEST1', domain="test.com") == expected_result |
| 618 | + |
| 619 | + |
406 | 620 | @patch.object(SophosFirewall, "_post")
|
407 | 621 | def test_get_ip_host_all(self, mocked_post):
|
408 | 622 | """Test get_ip_host() method for all hosts"""
|
|
0 commit comments