@@ -57,6 +57,18 @@ def test_fields_disabled(self) -> None:
5757 self .assertTrue (form .fields ["start_date" ].disabled )
5858 self .assertTrue (form .fields ["end_date" ].disabled )
5959
60+ def test_registration_code_field_disabled (self ) -> None :
61+ # Act
62+ form = AccountBenefitForm (disable_registration_code = True )
63+ # Assert
64+ self .assertTrue (form .fields ["registration_code" ].disabled )
65+
66+ def test_registration_code_field_enabled_by_default (self ) -> None :
67+ # Act
68+ form = AccountBenefitForm ()
69+ # Assert
70+ self .assertFalse (form .fields ["registration_code" ].disabled )
71+
6072 def test_clean__valid (self ) -> None :
6173 # Arrange
6274 org = Organization .objects .create (fullname = "Test Org" , domain = "example.com" )
@@ -72,6 +84,7 @@ def test_clean__valid(self) -> None:
7284 data = {
7385 "account" : account .pk ,
7486 "benefit" : benefit .pk ,
87+ "registration_code" : "TESTCODE" ,
7588 "start_date" : "2025-01-01" ,
7689 "end_date" : "2025-12-31" ,
7790 "allocation" : 10 ,
@@ -283,8 +296,73 @@ def test_clean__registration_code_unique(self) -> None:
283296 # Assert
284297 self .assertTrue (form .is_valid ())
285298
299+ def test_clean__valid__with_partnership (self ) -> None :
300+ """Form is valid when partnership is set and registration code is absent."""
301+ # Arrange
302+ org = Organization .objects .create (fullname = "Test Org" , domain = "example.com" )
303+ account = Account .objects .create (
304+ account_type = Account .AccountTypeChoices .ORGANISATION ,
305+ generic_relation = org ,
306+ )
307+ partnership = Partnership .objects .create (
308+ name = "Test Partnership" ,
309+ partner_organisation = org ,
310+ account = account ,
311+ agreement_start = date (2025 , 1 , 1 ),
312+ agreement_end = date (2025 , 12 , 31 ),
313+ credits = 10 ,
314+ )
315+ benefit = Benefit .objects .create (name = "Test Benefit" , unit_type = "seat" , credits = 2 )
316+ data = {
317+ "account" : account .pk ,
318+ "partnership" : partnership .pk ,
319+ "benefit" : benefit .pk ,
320+ "registration_code" : "" ,
321+ "allocation" : 10 ,
322+ }
323+
324+ # Act
325+ form = AccountBenefitForm (data )
326+
327+ # Assert
328+ self .assertTrue (form .is_valid ())
329+
330+ def test_clean__registration_code_present_with_partnership (self ) -> None :
331+ """Form is invalid when both a partnership and a registration code are provided."""
332+ # Arrange
333+ org = Organization .objects .create (fullname = "Test Org" , domain = "example.com" )
334+ account = Account .objects .create (
335+ account_type = Account .AccountTypeChoices .ORGANISATION ,
336+ generic_relation = org ,
337+ )
338+ partnership = Partnership .objects .create (
339+ name = "Test Partnership" ,
340+ partner_organisation = org ,
341+ account = account ,
342+ agreement_start = date (2025 , 1 , 1 ),
343+ agreement_end = date (2025 , 12 , 31 ),
344+ credits = 10 ,
345+ )
346+ benefit = Benefit .objects .create (name = "Test Benefit" , unit_type = "seat" , credits = 2 )
347+ data = {
348+ "account" : account .pk ,
349+ "partnership" : partnership .pk ,
350+ "benefit" : benefit .pk ,
351+ "registration_code" : "SHOULD-NOT-BE-HERE" ,
352+ "allocation" : 10 ,
353+ }
354+
355+ # Act
356+ form = AccountBenefitForm (data )
357+
358+ # Assert
359+ self .assertFalse (form .is_valid ())
360+ self .assertIn (
361+ "Registration code must be empty when a partnership is selected." , form .errors ["registration_code" ]
362+ )
363+
286364 def test_clean__registration_code_empty (self ) -> None :
287- """Form is valid when registration code is empty (it's optional) ."""
365+ """Form is invalid when registration code is empty and no partnership is selected ."""
288366 # Arrange
289367 org = Organization .objects .create (fullname = "Test Org" , domain = "example.com" )
290368 account = Account .objects .create (
@@ -305,4 +383,5 @@ def test_clean__registration_code_empty(self) -> None:
305383 form = AccountBenefitForm (data )
306384
307385 # Assert
308- self .assertTrue (form .is_valid ())
386+ self .assertFalse (form .is_valid ())
387+ self .assertIn ("registration_code" , form .errors )
0 commit comments