|
| 1 | +import logging |
| 2 | + |
| 3 | +from odoo.tests.common import TransactionCase |
| 4 | + |
| 5 | +_logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +class SppRegistryApprovalTest(TransactionCase): |
| 9 | + @classmethod |
| 10 | + def setUpClass(cls): |
| 11 | + super().setUpClass() |
| 12 | + # Set context to avoid job queue delay |
| 13 | + cls.env = cls.env( |
| 14 | + context=dict( |
| 15 | + cls.env.context, |
| 16 | + test_queue_job_no_delay=True, |
| 17 | + ) |
| 18 | + ) |
| 19 | + |
| 20 | + # Get the security groups |
| 21 | + cls.approve_group = cls.env.ref("spp_registry_approval.approve_registry") |
| 22 | + cls.reject_group = cls.env.ref("spp_registry_approval.reject_registry") |
| 23 | + cls.reset_group = cls.env.ref("spp_registry_approval.reset_to_draft_registry") |
| 24 | + |
| 25 | + # Create test users with different permissions |
| 26 | + cls.user_with_approve = cls.env["res.users"].create( |
| 27 | + { |
| 28 | + "name": "User with Approve Permission", |
| 29 | + "login": "approve_user", |
| 30 | + "email": "approve@test.com", |
| 31 | + "groups_id": [(6, 0, [cls.approve_group.id])], |
| 32 | + } |
| 33 | + ) |
| 34 | + |
| 35 | + cls.user_with_reject = cls.env["res.users"].create( |
| 36 | + { |
| 37 | + "name": "User with Reject Permission", |
| 38 | + "login": "reject_user", |
| 39 | + "email": "reject@test.com", |
| 40 | + "groups_id": [(6, 0, [cls.reject_group.id])], |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + cls.user_with_reset = cls.env["res.users"].create( |
| 45 | + { |
| 46 | + "name": "User with Reset Permission", |
| 47 | + "login": "reset_user", |
| 48 | + "email": "reset@test.com", |
| 49 | + "groups_id": [(6, 0, [cls.reset_group.id])], |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + cls.user_without_permissions = cls.env["res.users"].create( |
| 54 | + { |
| 55 | + "name": "User without Permissions", |
| 56 | + "login": "no_perms_user", |
| 57 | + "email": "noperms@test.com", |
| 58 | + "groups_id": [(6, 0, [])], |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + # Create test registries |
| 63 | + cls.registry_draft = cls.env["res.partner"].create( |
| 64 | + { |
| 65 | + "name": "Test Registry Draft", |
| 66 | + "is_registrant": True, |
| 67 | + "is_group": False, |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + cls.registry_approved = cls.env["res.partner"].create( |
| 72 | + { |
| 73 | + "name": "Test Registry Approved", |
| 74 | + "is_registrant": True, |
| 75 | + "is_group": False, |
| 76 | + "state": "approved", |
| 77 | + } |
| 78 | + ) |
| 79 | + |
| 80 | + cls.registry_rejected = cls.env["res.partner"].create( |
| 81 | + { |
| 82 | + "name": "Test Registry Rejected", |
| 83 | + "is_registrant": True, |
| 84 | + "is_group": False, |
| 85 | + "state": "rejected", |
| 86 | + } |
| 87 | + ) |
| 88 | + |
| 89 | + def test_01_default_state(self): |
| 90 | + """Test that new registries default to draft state""" |
| 91 | + new_registry = self.env["res.partner"].create( |
| 92 | + { |
| 93 | + "name": "New Test Registry", |
| 94 | + "is_registrant": True, |
| 95 | + "is_group": False, |
| 96 | + } |
| 97 | + ) |
| 98 | + self.assertEqual( |
| 99 | + new_registry.state, |
| 100 | + "draft", |
| 101 | + "New registries should default to draft state", |
| 102 | + ) |
| 103 | + |
| 104 | + def test_02_approve_registry_with_permission(self): |
| 105 | + """Test approve_registry method with proper permissions""" |
| 106 | + # Test with user having approve permission |
| 107 | + self.registry_draft.with_user(self.user_with_approve).approve_registry() |
| 108 | + self.assertEqual( |
| 109 | + self.registry_draft.state, |
| 110 | + "approved", |
| 111 | + "Registry should be approved when user has permission", |
| 112 | + ) |
| 113 | + |
| 114 | + def test_03_approve_registry_without_permission(self): |
| 115 | + """Test approve_registry method without proper permissions""" |
| 116 | + # Reset to draft first |
| 117 | + self.registry_draft.state = "draft" |
| 118 | + |
| 119 | + # Test with user without approve permission |
| 120 | + self.registry_draft.with_user(self.user_without_permissions).approve_registry() |
| 121 | + self.assertEqual( |
| 122 | + self.registry_draft.state, |
| 123 | + "draft", |
| 124 | + "Registry should remain draft when user lacks permission", |
| 125 | + ) |
| 126 | + |
| 127 | + def test_04_reject_registry_with_permission(self): |
| 128 | + """Test reject_registry method with proper permissions""" |
| 129 | + # Test with user having reject permission |
| 130 | + self.registry_draft.with_user(self.user_with_reject).reject_registry() |
| 131 | + self.assertEqual( |
| 132 | + self.registry_draft.state, |
| 133 | + "rejected", |
| 134 | + "Registry should be rejected when user has permission", |
| 135 | + ) |
| 136 | + |
| 137 | + def test_05_reject_registry_without_permission(self): |
| 138 | + """Test reject_registry method without proper permissions""" |
| 139 | + # Reset to draft first |
| 140 | + self.registry_draft.state = "draft" |
| 141 | + |
| 142 | + # Test with user without reject permission |
| 143 | + self.registry_draft.with_user(self.user_without_permissions).reject_registry() |
| 144 | + self.assertEqual( |
| 145 | + self.registry_draft.state, |
| 146 | + "draft", |
| 147 | + "Registry should remain draft when user lacks permission", |
| 148 | + ) |
| 149 | + |
| 150 | + def test_06_reset_to_draft_registry_with_permission(self): |
| 151 | + """Test reset_to_draft_registry method with proper permissions""" |
| 152 | + # Test with user having reset permission |
| 153 | + self.registry_approved.with_user(self.user_with_reset).reset_to_draft_registry() |
| 154 | + self.assertEqual( |
| 155 | + self.registry_approved.state, |
| 156 | + "draft", |
| 157 | + "Registry should be reset to draft when user has permission", |
| 158 | + ) |
| 159 | + |
| 160 | + def test_07_reset_to_draft_registry_without_permission(self): |
| 161 | + """Test reset_to_draft_registry method without proper permissions""" |
| 162 | + # Reset to approved first |
| 163 | + self.registry_approved.state = "approved" |
| 164 | + |
| 165 | + # Test with user without reset permission |
| 166 | + self.registry_approved.with_user(self.user_without_permissions).reset_to_draft_registry() |
| 167 | + self.assertEqual( |
| 168 | + self.registry_approved.state, |
| 169 | + "approved", |
| 170 | + "Registry should remain approved when user lacks permission", |
| 171 | + ) |
| 172 | + |
| 173 | + def test_08_multiple_records_approval(self): |
| 174 | + """Test approve_registry method with multiple records""" |
| 175 | + # Create multiple registries |
| 176 | + registries = self.env["res.partner"].create( |
| 177 | + [ |
| 178 | + { |
| 179 | + "name": "Registry 1", |
| 180 | + "is_registrant": True, |
| 181 | + "is_group": False, |
| 182 | + "state": "draft", |
| 183 | + }, |
| 184 | + { |
| 185 | + "name": "Registry 2", |
| 186 | + "is_registrant": True, |
| 187 | + "is_group": False, |
| 188 | + "state": "draft", |
| 189 | + }, |
| 190 | + ] |
| 191 | + ) |
| 192 | + |
| 193 | + # Approve all registries |
| 194 | + registries.with_user(self.user_with_approve).approve_registry() |
| 195 | + |
| 196 | + # Verify all are approved |
| 197 | + for registry in registries: |
| 198 | + self.assertEqual( |
| 199 | + registry.state, |
| 200 | + "approved", |
| 201 | + f"Registry {registry.name} should be approved", |
| 202 | + ) |
| 203 | + |
| 204 | + def test_09_state_constants(self): |
| 205 | + """Test that state constants are properly defined""" |
| 206 | + self.assertEqual( |
| 207 | + self.env["res.partner"].DRAFT, |
| 208 | + "draft", |
| 209 | + "DRAFT constant should be 'draft'", |
| 210 | + ) |
| 211 | + self.assertEqual( |
| 212 | + self.env["res.partner"].APPROVED, |
| 213 | + "approved", |
| 214 | + "APPROVED constant should be 'approved'", |
| 215 | + ) |
| 216 | + self.assertEqual( |
| 217 | + self.env["res.partner"].REJECTED, |
| 218 | + "rejected", |
| 219 | + "REJECTED constant should be 'rejected'", |
| 220 | + ) |
| 221 | + |
| 222 | + def test_10_edge_case_empty_recordset(self): |
| 223 | + """Test methods with empty recordset""" |
| 224 | + empty_recordset = self.env["res.partner"].browse([]) |
| 225 | + |
| 226 | + # These should not raise errors |
| 227 | + empty_recordset.with_user(self.user_with_approve).approve_registry() |
| 228 | + empty_recordset.with_user(self.user_with_reject).reject_registry() |
| 229 | + empty_recordset.with_user(self.user_with_reset).reset_to_draft_registry() |
| 230 | + |
| 231 | + def test_11_mixed_permissions_user(self): |
| 232 | + """Test user with multiple permissions""" |
| 233 | + # Create user with multiple permissions |
| 234 | + user_multi = self.env["res.users"].create( |
| 235 | + { |
| 236 | + "name": "User with Multiple Permissions", |
| 237 | + "login": "multi_user", |
| 238 | + "email": "multi@test.com", |
| 239 | + "groups_id": [(6, 0, [self.approve_group.id, self.reject_group.id, self.reset_group.id])], |
| 240 | + } |
| 241 | + ) |
| 242 | + |
| 243 | + # Test all operations with multi-permission user |
| 244 | + registry = self.env["res.partner"].create( |
| 245 | + { |
| 246 | + "name": "Multi Test Registry", |
| 247 | + "is_registrant": True, |
| 248 | + "is_group": False, |
| 249 | + "state": "draft", |
| 250 | + } |
| 251 | + ) |
| 252 | + |
| 253 | + # Approve |
| 254 | + registry.with_user(user_multi).approve_registry() |
| 255 | + self.assertEqual(registry.state, "approved") |
| 256 | + |
| 257 | + # Reject |
| 258 | + registry.with_user(user_multi).reject_registry() |
| 259 | + self.assertEqual(registry.state, "rejected") |
| 260 | + |
| 261 | + # Reset to draft |
| 262 | + registry.with_user(user_multi).reset_to_draft_registry() |
| 263 | + self.assertEqual(registry.state, "draft") |
| 264 | + |
| 265 | + def test_12_sudo_usage(self): |
| 266 | + """Test that sudo() is properly used in methods""" |
| 267 | + # This test verifies that the methods use sudo() correctly |
| 268 | + # by checking that the state changes are applied even when |
| 269 | + # the user doesn't have direct write access to the record |
| 270 | + |
| 271 | + # Create a registry with restricted access |
| 272 | + registry = self.env["res.partner"].create( |
| 273 | + { |
| 274 | + "name": "Restricted Registry", |
| 275 | + "is_registrant": True, |
| 276 | + "is_group": False, |
| 277 | + "state": "draft", |
| 278 | + } |
| 279 | + ) |
| 280 | + |
| 281 | + # Test that sudo() allows the operation to succeed |
| 282 | + registry.with_user(self.user_with_approve).approve_registry() |
| 283 | + self.assertEqual( |
| 284 | + registry.state, |
| 285 | + "approved", |
| 286 | + "sudo() should allow approval even with restricted access", |
| 287 | + ) |
0 commit comments