def test_edit_documents_only_apply_edits_with_edited_flag_set(self, agency_service, briefs, users):
        atm_brief = brief_service.get(1)
        user = user_service.get(2)
        agency_service.get_agency_name.return_value = 'DTA'
        original_attachments = atm_brief.data['attachments']

        edits = {
            'closingDate': '',
            'title': 'test',
            'summary': 'test',
            'sellers': {},
            'attachments': [],
            'documentsEdited': False
        }

        brief = brief_edit_business.edit_opportunity(user.id, atm_brief.id, edits)
        assert len(original_attachments) > 0
        assert brief.data['attachments'] == original_attachments

        edits = {
            'closingDate': '',
            'title': 'test',
            'summary': 'test',
            'sellers': {},
            'attachments': ['ABC.PDF'],
            'documentsEdited': True
        }

        brief = brief_edit_business.edit_opportunity(user.id, atm_brief.id, edits)
        assert brief.data['attachments'] == ['ABC.PDF']
    def test_edit_opportunity_creates_history_record(self, agency_service, briefs, users):
        specialist_brief = brief_service.get(2)
        user = user_service.get(2)
        agency_service.get_agency_name.return_value = 'DTA'

        edits = {
            'closingDate': '',
            'title': 'test',
            'sellers': {},
            'summary': ''
        }

        brief = brief_edit_business.edit_opportunity(user.id, specialist_brief.id, edits)
        history = brief_history_service.all()

        assert len(history) == 1
        assert history[0].brief_id == brief.id
        assert history[0].user_id == user.id
    def test_edit_opportunity_creates_audit_event(self, agency_service, briefs, users):
        specialist_brief = brief_service.get(2)
        user = user_service.get(2)
        agency_service.get_agency_name.return_value = 'DTA'

        edits = {
            'closingDate': '',
            'title': 'test',
            'sellers': {},
            'summary': ''
        }

        brief = brief_edit_business.edit_opportunity(user.id, specialist_brief.id, edits)
        audit_event = audit_service.find(
            object_id=specialist_brief.id,
            object_type='Brief',
            type=audit_types.opportunity_edited.value
        ).one_or_none()

        assert audit_event is not None
    def test_edit_opportunity_creates_history_record_with_original_data(self, agency_service, briefs, users):
        specialist_brief = brief_service.get(2)
        original_closed_at = copy.deepcopy(specialist_brief.closed_at)
        original_specialist_data = copy.deepcopy(specialist_brief.data)
        user = user_service.get(2)
        agency_service.get_agency_name.return_value = 'DTA'

        edits = {
            'closingDate': '',
            'title': 'test',
            'sellers': {},
            'summary': ''
        }

        brief = brief_edit_business.edit_opportunity(user.id, specialist_brief.id, edits)
        history = brief_history_service.all()

        assert pendulum.parse(history[0].data['closed_at'], tz='utc') == original_closed_at
        assert history[0].data['sellers'] == original_specialist_data['sellers']
        assert history[0].data['sellerSelector'] == original_specialist_data['sellerSelector']
        assert history[0].data['title'] == original_specialist_data['title']
    def test_only_sellers_were_edited_is_false_when_summary_edited(self, agency_service, briefs, users):
        specialist_brief = brief_service.get(2)
        user = user_service.get(2)
        agency_service.get_agency_name.return_value = 'DTA'

        edits = {
            'closingDate': pendulum.now('Australia/Canberra').add(days=7).to_date_string(),
            'title': '',
            'sellers': {
                '1': {
                    'name': 'Seller 1'
                },
                '2': {
                    'name': 'Seller 2'
                }
            },
            'summary': 'New summary'
        }

        brief = brief_edit_business.edit_opportunity(user.id, specialist_brief.id, edits)
        only_sellers_edited = brief_edit_business.only_sellers_were_edited(specialist_brief.id)
        assert only_sellers_edited is False
    def test_only_sellers_were_edited_is_false_when_title_edited(self, agency_service, briefs, users):
        specialist_brief = brief_service.get(2)
        user = user_service.get(2)
        agency_service.get_agency_name.return_value = 'DTA'

        edits = {
            'closingDate': '',
            'title': 'test',
            'sellers': {
                '1': {
                    'name': 'Seller 1'
                },
                '2': {
                    'name': 'Seller 2'
                }
            },
            'summary': ''
        }

        brief = brief_edit_business.edit_opportunity(user.id, specialist_brief.id, edits)
        only_sellers_edited = brief_edit_business.only_sellers_were_edited(specialist_brief.id)
        assert only_sellers_edited is False