def test_milestone_get(self):
     contract, milestones = prepare_milestones(self)
     response = self.app.get(ENDPOINTS['milestones'].format(
         contract_id=contract.data.id, milestone_id=milestones[0]['id']))
     assert response.status == '200 OK', 'Cannot get milestone'
     assert response.json['data']['status'] == 'processing', \
         'Financing milestone must be created with status `processing`'
    def test_milestone_patch_financing_wrong_dateMet(self):
        contract, milestones = prepare_milestones(self)
        financing_milestone = Milestone(milestones[0])
        assert financing_milestone.type_ == 'financing'
        dateMet_to_set = financing_milestone.dueDate - timedelta(days=500)

        patch_milestone(self,
                        contract,
                        financing_milestone.id,
                        {'data': {
                            'dateMet': dateMet_to_set.isoformat()
                        }},
                        status=422)
    def test_patch_notMet(self):
        contract, milestones = prepare_milestones(self)
        financing_milestone = Milestone(milestones[0])
        assert financing_milestone.type_ == 'financing'

        response = patch_milestone(
            self,
            contract,
            financing_milestone.id,
            {'data': {
                'status': 'notMet'
            }},
        )

        self.assertEqual(response.status, '200 OK')
        related_contract = get_contract(self, contract.data.id)
        self.assertEqual(related_contract.status, 'unsuccessful')
    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'