|
10 | 10 | # See the License for the specific language governing permissions and
|
11 | 11 | # limitations under the License.
|
12 | 12 |
|
| 13 | +from datetime import datetime |
13 | 14 |
|
14 | 15 | import pretend
|
15 | 16 | import pytest
|
@@ -516,8 +517,11 @@ def test_acl(self, monkeypatch, policy_class, principals, expected):
|
516 | 517 |
|
517 | 518 | request = pretend.stub(
|
518 | 519 | identity=pretend.stub(
|
519 |
| - __principals__=lambda: principals, has_primary_verified_email=True |
520 |
| - ) |
| 520 | + __principals__=lambda: principals, |
| 521 | + has_primary_verified_email=True, |
| 522 | + has_two_factor=False, |
| 523 | + ), |
| 524 | + matched_route=pretend.stub(name="random.route"), |
521 | 525 | )
|
522 | 526 | context = pretend.stub(__acl__=[(Allow, "user:5", "myperm")])
|
523 | 527 |
|
@@ -545,6 +549,7 @@ def test_2fa_owner_requires(
|
545 | 549 | has_primary_verified_email=True,
|
546 | 550 | has_two_factor=has_mfa,
|
547 | 551 | ),
|
| 552 | + matched_route=pretend.stub(name="random.route"), |
548 | 553 | registry=pretend.stub(
|
549 | 554 | settings={
|
550 | 555 | "warehouse.two_factor_requirement.enabled": True,
|
@@ -581,6 +586,7 @@ def test_2fa_pypi_mandates_2fa(
|
581 | 586 | has_primary_verified_email=True,
|
582 | 587 | has_two_factor=has_mfa,
|
583 | 588 | ),
|
| 589 | + matched_route=pretend.stub(name="random.route"), |
584 | 590 | registry=pretend.stub(
|
585 | 591 | settings={
|
586 | 592 | "warehouse.two_factor_requirement.enabled": False,
|
@@ -617,6 +623,7 @@ def test_2fa_pypi_mandates_2fa_with_warning(
|
617 | 623 | has_primary_verified_email=True,
|
618 | 624 | has_two_factor=has_mfa,
|
619 | 625 | ),
|
| 626 | + matched_route=pretend.stub(name="random.route"), |
620 | 627 | registry=pretend.stub(
|
621 | 628 | settings={
|
622 | 629 | "warehouse.two_factor_requirement.enabled": False,
|
@@ -650,11 +657,97 @@ def test_permits_with_unverified_email(self, monkeypatch, policy_class):
|
650 | 657 |
|
651 | 658 | request = pretend.stub(
|
652 | 659 | identity=pretend.stub(
|
653 |
| - __principals__=lambda: ["user:5"], has_primary_verified_email=False |
| 660 | + __principals__=lambda: ["user:5"], |
| 661 | + has_primary_verified_email=False, |
| 662 | + has_two_factor=False, |
| 663 | + ), |
| 664 | + matched_route=pretend.stub(name="manage.projects"), |
| 665 | + ) |
| 666 | + context = pretend.stub(__acl__=[(Allow, "user:5", "myperm")]) |
| 667 | + |
| 668 | + policy = policy_class() |
| 669 | + assert not policy.permits(request, context, "myperm") |
| 670 | + |
| 671 | + # TODO: remove this test when we remove the conditional |
| 672 | + def test_permits_manage_projects_without_2fa_for_older_users( |
| 673 | + self, monkeypatch, policy_class |
| 674 | + ): |
| 675 | + monkeypatch.setattr(security_policy, "User", pretend.stub) |
| 676 | + |
| 677 | + request = pretend.stub( |
| 678 | + identity=pretend.stub( |
| 679 | + __principals__=lambda: ["user:5"], |
| 680 | + has_primary_verified_email=True, |
| 681 | + has_two_factor=False, |
| 682 | + date_joined=datetime(2019, 1, 1), |
| 683 | + ), |
| 684 | + matched_route=pretend.stub(name="manage.projects"), |
| 685 | + ) |
| 686 | + context = pretend.stub(__acl__=[(Allow, "user:5", "myperm")]) |
| 687 | + |
| 688 | + policy = policy_class() |
| 689 | + assert policy.permits(request, context, "myperm") |
| 690 | + |
| 691 | + def test_permits_manage_projects_with_2fa(self, monkeypatch, policy_class): |
| 692 | + monkeypatch.setattr(security_policy, "User", pretend.stub) |
| 693 | + |
| 694 | + request = pretend.stub( |
| 695 | + identity=pretend.stub( |
| 696 | + __principals__=lambda: ["user:5"], |
| 697 | + has_primary_verified_email=True, |
| 698 | + has_two_factor=True, |
| 699 | + ), |
| 700 | + matched_route=pretend.stub(name="manage.projects"), |
| 701 | + ) |
| 702 | + context = pretend.stub(__acl__=[(Allow, "user:5", "myperm")]) |
| 703 | + |
| 704 | + policy = policy_class() |
| 705 | + assert policy.permits(request, context, "myperm") |
| 706 | + |
| 707 | + def test_deny_manage_projects_without_2fa(self, monkeypatch, policy_class): |
| 708 | + monkeypatch.setattr(security_policy, "User", pretend.stub) |
| 709 | + |
| 710 | + request = pretend.stub( |
| 711 | + identity=pretend.stub( |
| 712 | + __principals__=lambda: ["user:5"], |
| 713 | + has_primary_verified_email=True, |
| 714 | + has_two_factor=False, |
| 715 | + date_joined=datetime.now(), |
654 | 716 | ),
|
655 | 717 | matched_route=pretend.stub(name="manage.projects"),
|
656 | 718 | )
|
657 | 719 | context = pretend.stub(__acl__=[(Allow, "user:5", "myperm")])
|
658 | 720 |
|
659 | 721 | policy = policy_class()
|
660 | 722 | assert not policy.permits(request, context, "myperm")
|
| 723 | + |
| 724 | + @pytest.mark.parametrize( |
| 725 | + "matched_route", |
| 726 | + [ |
| 727 | + "manage.account", |
| 728 | + "manage.account.recovery-codes", |
| 729 | + "manage.account.totp-provision", |
| 730 | + "manage.account.two-factor", |
| 731 | + "manage.account.webauthn-provision", |
| 732 | + "manage.account.webauthn-provision.validate", |
| 733 | + ], |
| 734 | + ) |
| 735 | + def test_permits_2fa_routes_without_2fa( |
| 736 | + self, monkeypatch, policy_class, matched_route |
| 737 | + ): |
| 738 | + monkeypatch.setattr(security_policy, "User", pretend.stub) |
| 739 | + |
| 740 | + request = pretend.stub( |
| 741 | + identity=pretend.stub( |
| 742 | + __principals__=lambda: ["user:5"], |
| 743 | + has_primary_verified_email=True, |
| 744 | + has_two_factor=False, |
| 745 | + date_joined=datetime.now(), |
| 746 | + ), |
| 747 | + matched_route=pretend.stub(name=matched_route), |
| 748 | + ) |
| 749 | + |
| 750 | + context = pretend.stub(__acl__=[(Allow, "user:5", "myperm")]) |
| 751 | + |
| 752 | + policy = policy_class() |
| 753 | + assert policy.permits(request, context, "myperm") |
0 commit comments