def test_update_fail_due_to_amendments_unsigned(self): """Ensure agreement update fails if amendments aren't signed. I don't think it's possible to supply amendments when creating via the serializer, so this only tests update. """ agreement = AgreementFactory(agreement_type=Agreement.MOU, signed_by_unicef_date=None, signed_by_partner_date=None) amendment = AgreementAmendmentFactory(agreement=agreement) data = { 'agreement': agreement, 'amendments': [amendment], } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertAmendmentExceptionFundamentals( context_manager, 'Please check that the Document is attached and signatures are not in the future' )
def test_update_fail_due_to_amendments_unsigned(self): """Ensure agreement update fails if amendments aren't signed. I don't think it's possible to supply amendments when creating via the serializer, so this only tests update. """ agreement = AgreementFactory(agreement_type=Agreement.MOU, signed_by_unicef_date=None, signed_by_partner_date=None) amendment = AgreementAmendmentFactory(agreement=agreement) data = { 'agreement': agreement, 'amendments': [amendment], } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertAmendmentExceptionFundamentals( context_manager, 'Please check that the Document is attached and signatures are not in the future' )
def test_fail_transition_to_signed_start_and_end_dates(self): """Exercise transition to signed, and validation related to start and end dates.""" agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.DRAFT, signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "status": Agreement.SIGNED, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request # Should fail because start date is empty. with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'Agreement cannot transition to signed until the start date is less than or equal to today' ) # Populate start date, but with a date that's still invalid (in the future) agreement.start = self.today + datetime.timedelta(days=5) agreement.save() # Should fail because start date is in the future with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'Agreement cannot transition to signed until the start date is less than or equal to today' ) # Fix problem with start date, now allow blank end date to cause a failure. agreement.start = self.today - datetime.timedelta(days=5) agreement.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'Agreement cannot transition to signed unless the end date is defined' ) # Populate end date with a date in the past - should pass validation for MOU's agreement.end = self.today - datetime.timedelta(days=3) self.assertTrue(serializer.validate(data=data)) # Fix end date agreement.end = self.today agreement.save() # Should not raise an exception serializer.validate(data=data)
def test_fail_transition_to_signed_start_and_end_dates(self): """Exercise transition to signed, and validation related to start and end dates.""" agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.DRAFT, signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "status": Agreement.SIGNED, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request # Should fail because start date is empty. with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'Agreement cannot transition to signed until the start date is less than or equal to today' ) # Populate start date, but with a date that's still invalid (in the future) agreement.start = self.today + datetime.timedelta(days=5) agreement.save() # Should fail because start date is in the future with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'Agreement cannot transition to signed until the start date is less than or equal to today' ) # Fix problem with start date, now allow blank end date to cause a failure. agreement.start = self.today - datetime.timedelta(days=5) agreement.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'Agreement cannot transition to signed unless the end date is defined' ) # Populate end date with a date in the past - should pass validation for MOU's agreement.end = self.today - datetime.timedelta(days=3) self.assertTrue(serializer.validate(data=data)) # Fix end date agreement.end = self.today agreement.save() # Should not raise an exception serializer.validate(data=data)
def test_fail_transition_to_ended(self): """Exercise transition to ended.""" # First test valid/positive case agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.SIGNED, end=self.today - datetime.timedelta(days=3), signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "status": Agreement.ENDED, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF? # # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request # This should succeed serializer.validate(data=data) # Should fail; no end date set. agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.SIGNED, signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "status": Agreement.ENDED, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF? # # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'agreement_transition_to_ended_invalid' )
def test_fail_transition_to_ended(self): """Exercise transition to ended.""" # First test valid/positive case agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.SIGNED, end=self.today - datetime.timedelta(days=3), signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "status": Agreement.ENDED, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF? # # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request # This should succeed serializer.validate(data=data) # Should fail; no end date set. agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.SIGNED, signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "status": Agreement.ENDED, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF? # # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, 'agreement_transition_to_ended_invalid' )
def test_update_with_due_to_amendments_signed_date(self): """Ensure agreement update fails if amendments don't have a signed_date or if it's in the future, and that update succeeds when the amendments signatures meet criteria. I don't think it's possible to supply amendments when creating via the serializer, so this only tests update. """ agreement = AgreementFactory(agreement_type=Agreement.MOU, signed_by_unicef_date=None, signed_by_partner_date=None) amendment = AgreementAmendmentFactory(agreement=agreement) # I need to give amendment.signed_amendment a name to exercise the date part of the amendment validator. amendment.signed_amendment.name = 'fake_amendment.pdf' amendment.save() data = { 'agreement': agreement, 'amendments': [amendment], } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertAmendmentExceptionFundamentals( context_manager, 'Please check that the Document is attached and signatures are not in the future' ) # Set the signed date, but set it to the future which should cause a failure. amendment.signed_date = self.today + datetime.timedelta(days=5) amendment.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertAmendmentExceptionFundamentals( context_manager, 'Please check that the Document is attached and signatures are not in the future' ) # Change the amendment so it will pass validation. amendment.signed_date = self.today amendment.save() # Should not raise an error. serializer.validate(data=data)
def test_update_with_due_to_amendments_signed_date(self): """Ensure agreement update fails if amendments don't have a signed_date or if it's in the future, and that update succeeds when the amendments signatures meet criteria. I don't think it's possible to supply amendments when creating via the serializer, so this only tests update. """ agreement = AgreementFactory(agreement_type=Agreement.MOU, signed_by_unicef_date=None, signed_by_partner_date=None) amendment = AgreementAmendmentFactory(agreement=agreement) # I need to give amendment.signed_amendment a name to exercise the date part of the amendment validator. amendment.signed_amendment.name = 'fake_amendment.pdf' amendment.save() data = { 'agreement': agreement, 'amendments': [amendment], } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertAmendmentExceptionFundamentals( context_manager, 'Please check that the Document is attached and signatures are not in the future' ) # Set the signed date, but set it to the future which should cause a failure. amendment.signed_date = self.today + datetime.timedelta(days=5) amendment.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertAmendmentExceptionFundamentals( context_manager, 'Please check that the Document is attached and signatures are not in the future' ) # Change the amendment so it will pass validation. amendment.signed_date = self.today amendment.save() # Should not raise an error. serializer.validate(data=data)
def test_update_fail_due_to_uneditable_field(self): """Exercise changing a field that can't be changed while the agreement has this status.""" agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.DRAFT, signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "partner": self.partner, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Cannot change fields while in draft: partner")
def test_update_fail_due_to_uneditable_field(self): """Exercise changing a field that can't be changed while the agreement has this status.""" agreement = AgreementFactory(agreement_type=Agreement.MOU, status=Agreement.DRAFT, signed_by_unicef_date=None, signed_by_partner_date=None) data = { "agreement": agreement, "partner": self.partner, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Cannot change fields while in draft: partner" )
def test_update_intervention(self): """Ensure agreement update fails if intervention dates aren't appropriate. I don't think it's possible to supply interventions when creating via the serializer, so this only tests update. """ agreement = AgreementFactory( agreement_type=Agreement.SSFA, partner=self.partner, status=Agreement.DRAFT, start=self.today - datetime.timedelta(days=5), end=self.today + datetime.timedelta(days=5), signed_by_unicef_date=None, signed_by_partner_date=None, reference_number_year=datetime.date.today().year) intervention = InterventionFactory(agreement=agreement) # Start w/an invalid intervention. data = { "agreement": agreement, "intervention": intervention, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Start and end dates don't match the Document's start and end") # Set start date and save again; it should still fail because end date isn't set. intervention.start = agreement.start intervention.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Start and end dates don't match the Document's start and end") # Set start date and save again; it should still fail because end date doesn't match agreement end date. intervention.end = agreement.end + datetime.timedelta(days=100) intervention.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Start and end dates don't match the Document's start and end") # Set start date and save again; it should now succeed. intervention.end = agreement.end intervention.save() # Should not raise an exception serializer.validate(data=data)
def test_update_intervention(self): """Ensure agreement update fails if intervention dates aren't appropriate. I don't think it's possible to supply interventions when creating via the serializer, so this only tests update. """ agreement = AgreementFactory(agreement_type=Agreement.SSFA, partner=self.partner, status=Agreement.DRAFT, start=self.today - datetime.timedelta(days=5), end=self.today + datetime.timedelta(days=5), signed_by_unicef_date=None, signed_by_partner_date=None, reference_number_year=datetime.date.today().year) intervention = InterventionFactory(agreement=agreement) # Start w/an invalid intervention. data = { "agreement": agreement, "intervention": intervention, } serializer = AgreementCreateUpdateSerializer() # If I don't set serializer.instance, the validator gets confused. I guess (?) this is ordinarily set by DRF # during an update? serializer.instance = agreement serializer.context['request'] = self.fake_request with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Start and end dates don't match the Document's start and end" ) # Set start date and save again; it should still fail because end date isn't set. intervention.start = agreement.start intervention.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Start and end dates don't match the Document's start and end" ) # Set start date and save again; it should still fail because end date doesn't match agreement end date. intervention.end = agreement.end + datetime.timedelta(days=100) intervention.save() with self.assertRaises(serializers.ValidationError) as context_manager: serializer.validate(data=data) self.assertSimpleExceptionFundamentals( context_manager, "Start and end dates don't match the Document's start and end" ) # Set start date and save again; it should now succeed. intervention.end = agreement.end intervention.save() # Should not raise an exception serializer.validate(data=data)