def test_upload_cr_attachment_request_to_foia_with_cpd_after_2006_has_badge_number(
            self, airtable_mock):
        airtable_mock.insert.return_value = {'id': 'some_airtable_record_id'}

        allegation = AllegationFactory(crid='123456',
                                       incident_date=datetime(2007,
                                                              12,
                                                              31,
                                                              tzinfo=pytz.utc))
        attachment_request = AttachmentRequestFactory(
            allegation=allegation, email='*****@*****.**')
        officer_1 = OfficerFactory(id=1, first_name='Marry', last_name='Jane')
        officer_2 = OfficerFactory(id=2, first_name='John', last_name='Henry')
        OfficerBadgeNumberFactory(officer=officer_1, star='12345')
        OfficerBadgeNumberFactory(officer=officer_2, star='56789')
        investigator_1 = InvestigatorFactory(officer=officer_1)
        investigator_2 = InvestigatorFactory(officer=officer_1)
        InvestigatorAllegationFactory(allegation=allegation,
                                      current_star=None,
                                      investigator=investigator_1)
        InvestigatorAllegationFactory(allegation=allegation,
                                      current_star=None,
                                      investigator=investigator_2)
        OfficerAllegationFactory(allegation=allegation, officer=officer_1)
        OfficerAllegationFactory(allegation=allegation, officer=officer_2)

        expected_airtable_data = {
            'Explanation':
            'Officers: John Henry(ID 2), Marry Jane(ID 1)',
            'Project': ['CPDP'],
            'Agency': ['CPD_AGENCY_ID'],
            'Requested For':
            'CR 123456',
            'Requestor': [{
                'id': 'usrGiZFcyZ6wHTYWd',
                'email': '*****@*****.**',
                'name': 'Rajiv Sinclair'
            }],
            'Date requested by user':
            attachment_request.created_at.strftime('%Y-%m-%d'),
            'Requester Email':
            '*****@*****.**'
        }

        CRRequestAirTableUploader.upload()
        attachment_request.refresh_from_db()

        airtable_mock.insert.assert_called_with(expected_airtable_data)
        expect(
            attachment_request.airtable_id).to.be.eq('some_airtable_record_id')
    def test_update_cr_attachment_request_to_foia_with_invalid_airtable_id(
            self, airtable_mock):
        airtable_mock.update = Mock(side_effect=[HTTPError('404')])
        airtable_mock.insert.return_value = {'id': 'airtable_id'}

        allegation = AllegationFactory(crid='123456',
                                       incident_date=datetime(2010,
                                                              1,
                                                              1,
                                                              tzinfo=pytz.utc))
        attachment_request = AttachmentRequestFactory(
            allegation=allegation,
            email='*****@*****.**',
            airtable_id='invalid_airtable_id')
        officer_1 = OfficerFactory(id=1, first_name='Marry', last_name='Jane')
        officer_2 = OfficerFactory(id=2, first_name='John', last_name='Henry')
        OfficerAllegationFactory(allegation=allegation, officer=officer_1)
        OfficerAllegationFactory(allegation=allegation, officer=officer_2)

        expected_airtable_data = {
            'Explanation':
            'Officers: John Henry(ID 2), Marry Jane(ID 1)',
            'Project': ['CPDP'],
            'Agency': ['COPA_AGENCY_ID'],
            'Requested For':
            'CR 123456',
            'Requestor': [{
                'id': 'usrGiZFcyZ6wHTYWd',
                'email': '*****@*****.**',
                'name': 'Rajiv Sinclair'
            }],
            'Date requested by user':
            attachment_request.created_at.strftime('%Y-%m-%d'),
            'Requester Email':
            '*****@*****.**'
        }

        CRRequestAirTableUploader.upload(update_all_records=True)
        attachment_request.refresh_from_db()

        airtable_mock.update.assert_called_with('invalid_airtable_id',
                                                expected_airtable_data)
        airtable_mock.insert.assert_called_with(expected_airtable_data)
        expect(attachment_request.airtable_id).to.be.eq('airtable_id')
    def test_upload_cr_attachment_request_to_foia_with_cpd_for_pre_2006_but_no_incident_date(
            self, airtable_mock):
        airtable_mock.insert.return_value = {'id': 'some_airtable_record_id'}

        allegation = AllegationFactory(crid='123456', incident_date=None)
        attachment_request = AttachmentRequestFactory(
            allegation=allegation, email='*****@*****.**')
        officer_1 = OfficerFactory(id=1, first_name='Marry', last_name='Jane')
        officer_2 = OfficerFactory(id=2, first_name='John', last_name='Henry')
        OfficerAllegationFactory(allegation=allegation, officer=officer_1)
        OfficerAllegationFactory(allegation=allegation, officer=officer_2)

        expected_airtable_data = {
            'Explanation':
            'Officers: John Henry(ID 2), Marry Jane(ID 1)',
            'Project': ['CPDP'],
            'Agency': ['COPA_AGENCY_ID'],
            'Requested For':
            'CR 123456',
            'Requestor': [{
                'id': 'usrGiZFcyZ6wHTYWd',
                'email': '*****@*****.**',
                'name': 'Rajiv Sinclair'
            }],
            'Date requested by user':
            attachment_request.created_at.strftime('%Y-%m-%d'),
            'Requester Email':
            '*****@*****.**'
        }

        expect(attachment_request.airtable_id).to.be.eq('')

        CRRequestAirTableUploader.upload()
        attachment_request.refresh_from_db()

        airtable_mock.insert.assert_called_with(expected_airtable_data)
        expect(
            attachment_request.airtable_id).to.be.eq('some_airtable_record_id')
    def test_Airtable_insert_raise_HTTPError(self, airtable_mock):
        AirTableUploader._get_foia_airtable().insert = Mock(
            side_effect=[{
                'id': 'some_airtable_record_id'
            }, HTTPError])

        allegation123 = AllegationFactory(crid='123',
                                          incident_date=datetime(
                                              2005, 1, 1, tzinfo=pytz.utc))
        officer_1 = OfficerFactory(id=1, first_name='Marry', last_name='Jane')
        officer_2 = OfficerFactory(id=2, first_name='John', last_name='Henry')
        OfficerAllegationFactory(allegation=allegation123, officer=officer_1)
        OfficerAllegationFactory(allegation=allegation123, officer=officer_2)
        investigator = InvestigatorFactory(officer=officer_1)
        InvestigatorAllegationFactory(allegation=allegation123,
                                      investigator=investigator)
        attachment_request_1 = AttachmentRequestFactory(
            allegation=allegation123, email='*****@*****.**')

        allegation456 = AllegationFactory(crid='456',
                                          incident_date=datetime(
                                              2011, 1, 1, tzinfo=pytz.utc))
        officer_3 = OfficerFactory(id=3, first_name='Marry', last_name='Jane')
        officer_4 = OfficerFactory(id=4, first_name='John', last_name='Henry')
        OfficerAllegationFactory(allegation=allegation456, officer=officer_3)
        OfficerAllegationFactory(allegation=allegation456, officer=officer_4)
        attachment_request_2 = AttachmentRequestFactory(
            allegation=allegation456, email='*****@*****.**')

        expect(attachment_request_1.airtable_id).to.eq('')
        expect(attachment_request_2.airtable_id).to.eq('')

        CRRequestAirTableUploader.upload()
        attachment_request_1.refresh_from_db()
        attachment_request_2.refresh_from_db()

        expected_calls = [
            call({
                'Explanation':
                'Officers: John Henry(ID 2), Marry Jane(ID 1)',
                'Project': ['CPDP'],
                'Agency': ['CPD_AGENCY_ID'],
                'Requested For':
                'CR 123',
                'Requestor': [{
                    'id': 'usrGiZFcyZ6wHTYWd',
                    'email': '*****@*****.**',
                    'name': 'Rajiv Sinclair'
                }],
                'Date requested by user':
                attachment_request_1.created_at.strftime('%Y-%m-%d'),
                'Requester Email':
                '*****@*****.**'
            }),
            call({
                'Explanation':
                'Officers: John Henry(ID 4), Marry Jane(ID 3)',
                'Project': ['CPDP'],
                'Agency': ['COPA_AGENCY_ID'],
                'Requested For':
                'CR 456',
                'Requestor': [{
                    'id': 'usrGiZFcyZ6wHTYWd',
                    'email': '*****@*****.**',
                    'name': 'Rajiv Sinclair'
                }],
                'Date requested by user':
                attachment_request_2.created_at.strftime('%Y-%m-%d'),
                'Requester Email':
                '*****@*****.**'
            })
        ]
        airtable_mock.insert.assert_has_calls(expected_calls, any_order=True)

        expect(
            attachment_request_1.airtable_id).to.eq('some_airtable_record_id')
        expect(attachment_request_2.airtable_id).to.eq('')
Ejemplo n.º 5
0
 def handle(self, *args, **options):
     CRRequestAirTableUploader.upload(options['all'])
     TRRRequestAirTableUploader.upload(options['all'])