|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 | from b2b.factories import ContractPageFactory, OrganizationPageFactory |
7 | | -from courses.factories import CourseRunFactory, ProgramFactory |
| 7 | +from courses.factories import ( |
| 8 | + CourseRunFactory, |
| 9 | + ProgramFactory, |
| 10 | +) |
| 11 | +from users.factories import UserFactory |
8 | 12 |
|
9 | 13 | pytestmark = [pytest.mark.django_db] |
10 | 14 | FAKE = faker.Faker() |
@@ -94,3 +98,93 @@ def test_organization_page_slug_not_overwritten_if_set(): |
94 | 98 | # The slug should still be the custom one |
95 | 99 | assert org.slug == "custom-slug" |
96 | 100 | assert org.title == "Test Org Updated" |
| 101 | + |
| 102 | + |
| 103 | +def test_remove_user_contracts_only_affects_specified_user(): |
| 104 | + """Test that remove_user_contracts only removes contracts for the specified user.""" |
| 105 | + |
| 106 | + # Create an organization and contracts |
| 107 | + organization = OrganizationPageFactory.create() |
| 108 | + contract1 = ContractPageFactory.create( |
| 109 | + organization=organization, |
| 110 | + membership_type="auto", |
| 111 | + integration_type="auto", |
| 112 | + ) |
| 113 | + contract2 = ContractPageFactory.create( |
| 114 | + organization=organization, |
| 115 | + membership_type="managed", |
| 116 | + integration_type="managed", |
| 117 | + ) |
| 118 | + |
| 119 | + # Create two users and add them both to the contracts |
| 120 | + user1 = UserFactory.create() |
| 121 | + user2 = UserFactory.create() |
| 122 | + |
| 123 | + user1.b2b_contracts.add(contract1, contract2) |
| 124 | + user2.b2b_contracts.add(contract1, contract2) |
| 125 | + |
| 126 | + # Verify both users have the contracts |
| 127 | + assert user1.b2b_contracts.count() == 2 |
| 128 | + assert user2.b2b_contracts.count() == 2 |
| 129 | + |
| 130 | + # Remove contracts from user1 |
| 131 | + organization.remove_user_contracts(user1) |
| 132 | + |
| 133 | + # Verify user1's contracts are removed |
| 134 | + user1.refresh_from_db() |
| 135 | + assert user1.b2b_contracts.count() == 0 |
| 136 | + |
| 137 | + # Verify user2's contracts are NOT affected |
| 138 | + user2.refresh_from_db() |
| 139 | + assert user2.b2b_contracts.count() == 2 |
| 140 | + assert user2.b2b_contracts.filter(id=contract1.id).exists() |
| 141 | + assert user2.b2b_contracts.filter(id=contract2.id).exists() |
| 142 | + |
| 143 | + |
| 144 | +def test_remove_user_contracts_only_removes_managed_contracts(): |
| 145 | + """Test that remove_user_contracts only removes automatically managed contracts.""" |
| 146 | + |
| 147 | + # Create an organization with both managed and non-managed contracts |
| 148 | + organization = OrganizationPageFactory.create() |
| 149 | + |
| 150 | + # Automatically managed contracts (should be removed) |
| 151 | + auto_contract = ContractPageFactory.create( |
| 152 | + organization=organization, |
| 153 | + membership_type="auto", |
| 154 | + integration_type="auto", |
| 155 | + ) |
| 156 | + managed_contract = ContractPageFactory.create( |
| 157 | + organization=organization, |
| 158 | + membership_type="managed", |
| 159 | + integration_type="managed", |
| 160 | + ) |
| 161 | + sso_contract = ContractPageFactory.create( |
| 162 | + organization=organization, |
| 163 | + membership_type="sso", |
| 164 | + integration_type="sso", |
| 165 | + ) |
| 166 | + |
| 167 | + # Non-managed contract (should NOT be removed) |
| 168 | + code_contract = ContractPageFactory.create( |
| 169 | + organization=organization, |
| 170 | + membership_type="code", |
| 171 | + integration_type="code", |
| 172 | + ) |
| 173 | + |
| 174 | + # Create a user and add all contracts |
| 175 | + user = UserFactory.create() |
| 176 | + user.b2b_contracts.add(auto_contract, managed_contract, sso_contract, code_contract) |
| 177 | + |
| 178 | + # Verify user has all 4 contracts |
| 179 | + assert user.b2b_contracts.count() == 4 |
| 180 | + |
| 181 | + # Remove managed contracts from user |
| 182 | + organization.remove_user_contracts(user) |
| 183 | + |
| 184 | + # Verify only managed contracts are removed, code contract remains |
| 185 | + user.refresh_from_db() |
| 186 | + assert user.b2b_contracts.count() == 1 |
| 187 | + assert not user.b2b_contracts.filter(id=auto_contract.id).exists() |
| 188 | + assert not user.b2b_contracts.filter(id=managed_contract.id).exists() |
| 189 | + assert not user.b2b_contracts.filter(id=sso_contract.id).exists() |
| 190 | + assert user.b2b_contracts.filter(id=code_contract.id).exists() |
0 commit comments