|
| 1 | +from django.test import TestCase |
| 2 | +from django.utils import timezone |
| 3 | +from django.utils.dateparse import parse_datetime |
| 4 | +from rest_framework.test import APIClient |
| 5 | + |
| 6 | +from partner_programs.models import Application, PartnerProgram |
| 7 | +from partner_programs.tests.helpers import ( |
| 8 | + create_partner_program, |
| 9 | + create_program_member, |
| 10 | + create_user, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class PartnerProgramApplicationPolicyAPITests(TestCase): |
| 15 | + def setUp(self): |
| 16 | + self.client = APIClient() |
| 17 | + |
| 18 | + def get_policy(self, program: PartnerProgram) -> dict: |
| 19 | + response = self.client.get(f"/programs/{program.pk}/") |
| 20 | + |
| 21 | + self.assertEqual(response.status_code, 200) |
| 22 | + self.assertIn("application_policy", response.data) |
| 23 | + return response.data["application_policy"] |
| 24 | + |
| 25 | + def test_individual_only_detail_exposes_individual_policy(self): |
| 26 | + program = create_partner_program( |
| 27 | + participation_format=(PartnerProgram.PARTICIPATION_FORMAT_INDIVIDUAL_ONLY), |
| 28 | + team_min_size=None, |
| 29 | + team_max_size=None, |
| 30 | + datetime_application_ends=None, |
| 31 | + ) |
| 32 | + |
| 33 | + policy = self.get_policy(program) |
| 34 | + |
| 35 | + self.assertEqual( |
| 36 | + policy, |
| 37 | + { |
| 38 | + "participation_format": "individual_only", |
| 39 | + "allowed_participation_modes": [ |
| 40 | + Application.PARTICIPATION_MODE_INDIVIDUAL |
| 41 | + ], |
| 42 | + "team_min_size": None, |
| 43 | + "team_max_size": None, |
| 44 | + "datetime_application_ends": None, |
| 45 | + "is_application_deadline_passed": False, |
| 46 | + }, |
| 47 | + ) |
| 48 | + self.assertNotIn( |
| 49 | + Application.PARTICIPATION_MODE_UNDECIDED, |
| 50 | + policy["allowed_participation_modes"], |
| 51 | + ) |
| 52 | + |
| 53 | + def test_team_only_detail_exposes_sizes_and_future_deadline(self): |
| 54 | + deadline = timezone.now() + timezone.timedelta(days=10) |
| 55 | + program = create_partner_program( |
| 56 | + participation_format=PartnerProgram.PARTICIPATION_FORMAT_TEAM_ONLY, |
| 57 | + team_min_size=2, |
| 58 | + team_max_size=5, |
| 59 | + datetime_application_ends=deadline, |
| 60 | + ) |
| 61 | + |
| 62 | + policy = self.get_policy(program) |
| 63 | + |
| 64 | + self.assertEqual(policy["participation_format"], "team_only") |
| 65 | + self.assertEqual( |
| 66 | + policy["allowed_participation_modes"], |
| 67 | + [Application.PARTICIPATION_MODE_TEAM], |
| 68 | + ) |
| 69 | + self.assertEqual(policy["team_min_size"], 2) |
| 70 | + self.assertEqual(policy["team_max_size"], 5) |
| 71 | + self.assertEqual(parse_datetime(policy["datetime_application_ends"]), deadline) |
| 72 | + self.assertFalse(policy["is_application_deadline_passed"]) |
| 73 | + self.assertNotIn( |
| 74 | + Application.PARTICIPATION_MODE_UNDECIDED, |
| 75 | + policy["allowed_participation_modes"], |
| 76 | + ) |
| 77 | + |
| 78 | + def test_individual_or_team_detail_marks_passed_deadline(self): |
| 79 | + deadline = timezone.now() - timezone.timedelta(seconds=1) |
| 80 | + program = create_partner_program( |
| 81 | + participation_format=(PartnerProgram.PARTICIPATION_FORMAT_INDIVIDUAL_OR_TEAM), |
| 82 | + team_min_size=2, |
| 83 | + team_max_size=4, |
| 84 | + datetime_application_ends=deadline, |
| 85 | + ) |
| 86 | + |
| 87 | + policy = self.get_policy(program) |
| 88 | + |
| 89 | + self.assertEqual(policy["participation_format"], "individual_or_team") |
| 90 | + self.assertEqual( |
| 91 | + policy["allowed_participation_modes"], |
| 92 | + [ |
| 93 | + Application.PARTICIPATION_MODE_INDIVIDUAL, |
| 94 | + Application.PARTICIPATION_MODE_TEAM, |
| 95 | + ], |
| 96 | + ) |
| 97 | + self.assertEqual(policy["team_min_size"], 2) |
| 98 | + self.assertEqual(policy["team_max_size"], 4) |
| 99 | + self.assertEqual(parse_datetime(policy["datetime_application_ends"]), deadline) |
| 100 | + self.assertTrue(policy["is_application_deadline_passed"]) |
| 101 | + self.assertNotIn( |
| 102 | + Application.PARTICIPATION_MODE_UNDECIDED, |
| 103 | + policy["allowed_participation_modes"], |
| 104 | + ) |
| 105 | + |
| 106 | + def test_member_detail_uses_the_same_public_policy_contract(self): |
| 107 | + program = create_partner_program() |
| 108 | + member = create_user(prefix="policy-program-member") |
| 109 | + create_program_member(program, user=member) |
| 110 | + self.client.force_authenticate(member) |
| 111 | + |
| 112 | + policy = self.get_policy(program) |
| 113 | + |
| 114 | + self.assertEqual(policy["participation_format"], "individual_only") |
| 115 | + self.assertEqual(policy["allowed_participation_modes"], ["individual"]) |
| 116 | + |
| 117 | + def test_program_list_contract_does_not_include_application_policy(self): |
| 118 | + create_partner_program() |
| 119 | + |
| 120 | + response = self.client.get("/programs/") |
| 121 | + |
| 122 | + self.assertEqual(response.status_code, 200) |
| 123 | + self.assertNotIn("application_policy", response.data["results"][0]) |
0 commit comments