def test_milestone_patch_dateMet_when_it_already_set(self):
        contract, milestones = prepare_milestones_approval(self)
        approval_milestone = milestones[1]
        assert approval_milestone.type_ == 'approval'
        reporting_milestone = milestones[2]
        dueDate_to_set = approval_milestone.dueDate + timedelta(days=500)
        approval_dateMet = approval_milestone.dueDate - timedelta(days=5)

        # patch dueDate
        response = patch_milestone(
            self, contract, reporting_milestone['id'],
            {'data': {
                'dueDate': dueDate_to_set.isoformat()
            }})

        self.assertEqual(response.status, '200 OK')
        patched_reporting_milestone = get_milestone(self, contract.data.id,
                                                    reporting_milestone['id'])
        assert patched_reporting_milestone.dueDate == dueDate_to_set, 'dueDate was not patched'

        response = patch_milestone(
            self, contract, approval_milestone['id'],
            {'data': {
                'dateMet': approval_dateMet.isoformat()
            }})

        reporting_milestone = get_milestone(self, contract.data.id,
                                            reporting_milestone.id)
        assert reporting_milestone.dueDate == dueDate_to_set, 'dueDate must not be changed'
    def test_milestone_patch_approval_dateMet(self):
        contract, milestones = prepare_milestones_approval(self)
        approval_milestone = Milestone(milestones[1])
        assert approval_milestone.type_ == 'approval'
        dateMet_to_set = approval_milestone.dueDate - timedelta(days=5)

        response = patch_milestone(
            self, contract, approval_milestone.id,
            {'data': {
                'dateMet': dateMet_to_set.isoformat()
            }})

        self.assertEqual(response.status, '200 OK')
        patched_approval_milestone = get_milestone(self, contract.data.id,
                                                   approval_milestone['id'])
        assert patched_approval_milestone.dateMet == dateMet_to_set, 'dateMet was not set'
        assert patched_approval_milestone.status == 'met'
        reporting_milestone = get_milestone(self, contract.data.id,
                                            milestones[2]['id'])
        assert reporting_milestone.status == 'processing', 'Reporting milestone status must be `processing`'
        assert reporting_milestone.dueDate is not None
        updated_contract = get_contract(self, contract.data.id)
        assert updated_contract.status == 'active', 'Contract status was not updated'
    def test_milestone_patch_financing_dateMet_patrially_met(self):
        contract, milestones = prepare_milestones(self)
        financing_milestone = Milestone(milestones[0])
        assert financing_milestone.type_ == 'financing'
        dateMet_to_set = financing_milestone.dueDate + timedelta(days=5)

        response = patch_milestone(
            self, contract, financing_milestone.id,
            {'data': {
                'dateMet': dateMet_to_set.isoformat()
            }})

        self.assertEqual(response.status, '200 OK')
        patched_financing_milestone = get_milestone(self, contract.data.id,
                                                    financing_milestone.id)
        assert patched_financing_milestone.dateMet == dateMet_to_set, 'dateMet was not set'
        assert patched_financing_milestone.status == 'partiallyMet', 'dateMet was not set'
        approval_milestone = get_milestone(self, contract.data.id,
                                           milestones[1]['id'])
        assert approval_milestone.status == 'processing'
        assert approval_milestone.dueDate is not None
        updated_contract = get_contract(self, contract.data.id)
        assert updated_contract.status == 'active.approval', 'Contract status was not updated'
    def test_update_dateModified_forbidden(self):
        contract, milestones = prepare_milestones_approval(self)
        reporting_milestone = Milestone(milestones[2])
        assert reporting_milestone.type_ == 'reporting'
        old_dateModified = reporting_milestone.dateModified

        patch_milestone(self,
                        contract,
                        reporting_milestone.id,
                        {'data': {
                            'type_': '937-99-92'
                        }},
                        status=422)
        patched_milestone = get_milestone(self, contract.data.id,
                                          reporting_milestone.id)
        assert old_dateModified == patched_milestone.dateModified, 'dateModified must not be updated'
    def test_patch_description(self):
        contract, milestones = prepare_milestones_approval(self)
        reporting_milestone = Milestone(milestones[2])
        assert reporting_milestone.type_ == 'reporting'

        patch_milestone(
            self,
            contract,
            reporting_milestone.id,
            {'data': {
                'description': '937-99-92'
            }},
        )
        patched_milestone = get_milestone(self, contract.data.id,
                                          reporting_milestone.id)
        self.assertEqual(patched_milestone.description, '937-99-92')
    def test_patch_status(self):
        contract, milestones = prepare_milestones_approval(self)
        reporting_milestone = Milestone(milestones[2])
        assert reporting_milestone.type_ == 'reporting'

        patch_milestone(
            self,
            contract,
            reporting_milestone.id,
            {'data': {
                'status': 'met'
            }},
        )
        patched_milestone = get_milestone(self, contract.data.id,
                                          reporting_milestone.id)
        self.assertEqual(patched_milestone.status, 'scheduled')
    def test_update_dateModified(self):
        contract, milestones = prepare_milestones_approval(self)
        reporting_milestone = Milestone(milestones[2])
        assert reporting_milestone.type_ == 'reporting'
        old_dateModified = reporting_milestone.dateModified

        patch_milestone(
            self,
            contract,
            reporting_milestone['id'],
            {'data': {
                'description': '937-99-92'
            }},
        )
        patched_milestone = get_milestone(self, contract.data.id,
                                          reporting_milestone.id)
        assert old_dateModified != patched_milestone.dateModified, 'dateModified must be updated'
    def test_milestone_patch_reporting_dueDate_when_scheduled(self):
        contract, milestones = prepare_milestones_approval(self)
        approval_milestone = milestones[1]
        assert approval_milestone.type_ == 'approval'
        reporting_milestone = milestones[2]
        dueDate_to_set = approval_milestone.dueDate + timedelta(days=500)

        response = patch_milestone(
            self, contract, reporting_milestone['id'],
            {'data': {
                'dueDate': dueDate_to_set.isoformat()
            }})

        self.assertEqual(response.status, '200 OK')
        patched_reporting_milestone = get_milestone(self, contract.data.id,
                                                    reporting_milestone['id'])
        assert patched_reporting_milestone.dueDate == dueDate_to_set, 'dueDate was not patched'
    def test_milestone_patch_reporting_dateMet_partiallyMet(self):
        contract, milestones = prepare_milestones_reporting(self)
        reporting_milestone = milestones[2]
        assert reporting_milestone.type_ == 'reporting'
        dateMet_to_set = reporting_milestone.dueDate + timedelta(days=5)

        response = patch_milestone(
            self, contract, reporting_milestone.id,
            {'data': {
                'dateMet': dateMet_to_set.isoformat()
            }})

        self.assertEqual(response.status, '200 OK')
        patched_reporting_milestone = get_milestone(self, contract.data.id,
                                                    reporting_milestone['id'])
        assert patched_reporting_milestone.dateMet == dateMet_to_set, 'dateMet was not set'
        assert patched_reporting_milestone.status == 'partiallyMet'
        updated_contract = get_contract(self, contract.data.id)
        assert updated_contract.status == 'terminated', 'Contract status was not updated'