@@ -35,7 +35,8 @@ def test_b2b_contract_attachment_bad_code(user):
3535 url = reverse ("b2b:attach-user" , kwargs = {"enrollment_code" : "not a code" })
3636 resp = client .post (url )
3737
38- assert resp .status_code == 200
38+ assert resp .status_code == 404
39+ assert resp .json ()["detail" ] == "Invalid or expired enrollment code."
3940 assert user .b2b_contracts .count () == 0
4041
4142
@@ -90,18 +91,21 @@ def test_b2b_contract_attachment(mocker, max_learners, code_used):
9091 )
9192 resp = client .post (url )
9293
93- assert resp .status_code == 200
94-
9594 user .refresh_from_db ()
9695
9796 if code_used and max_learners :
97+ # Code already used for attachment and is not unlimited - should return 404
98+ assert resp .status_code == 404
99+ assert resp .json ()["detail" ] == "Invalid or expired enrollment code."
98100 assert not user .b2b_organizations .filter (pk = contract .organization .id ).exists ()
99101 assert not user .b2b_contracts .filter (pk = contract .id ).exists ()
100102
101103 assert not DiscountContractAttachmentRedemption .objects .filter (
102104 contract = contract , user = user , discount = contract_codes [0 ]
103105 ).exists ()
104106 else :
107+ # Successfully attached to contract - should return 201
108+ assert resp .status_code == 201
105109 mocked_attach_user .assert_called ()
106110
107111 assert user .b2b_organizations .filter (pk = contract .organization .id ).exists ()
@@ -157,7 +161,9 @@ def test_b2b_contract_attachment_invalid_code_dates(user, bad_start_or_end):
157161 with freeze_time (slightly_future_time ):
158162 resp = client .post (url )
159163
160- assert resp .status_code == 200
164+ # Code is expired/not yet active - should return 404
165+ assert resp .status_code == 404
166+ assert resp .json ()["detail" ] == "Invalid or expired enrollment code."
161167
162168 user .refresh_from_db ()
163169 assert not user .b2b_contracts .filter (pk = contract .id ).exists ()
@@ -211,6 +217,7 @@ def test_b2b_contract_attachment_invalid_contract_dates(user, bad_start_or_end):
211217 with freeze_time (slightly_future_time ):
212218 resp = client .post (url )
213219
220+ # Contract dates are invalid - code is valid but no contracts to attach - should return 200
214221 assert resp .status_code == 200
215222
216223 user .refresh_from_db ()
@@ -248,7 +255,8 @@ def test_b2b_contract_attachment_full_contract(mocker):
248255 )
249256 resp = client .post (url )
250257
251- assert resp .status_code == 200
258+ # Successfully attached - should return 201
259+ assert resp .status_code == 201
252260 mocked_attach_user .assert_called ()
253261
254262 user .refresh_from_db ()
@@ -265,7 +273,9 @@ def test_b2b_contract_attachment_full_contract(mocker):
265273 )
266274 resp = client .post (url )
267275
268- assert resp .status_code == 200
276+ # Code already used for attachment and contract has max_learners=1 - should return 404
277+ assert resp .status_code == 404
278+ assert resp .json ()["detail" ] == "Invalid or expired enrollment code."
269279 mocked_attach_user .assert_not_called ()
270280
271281 user .refresh_from_db ()
0 commit comments