Exemplo n.º 1
0
    def test_lastmod(self):
        attachment = AttachmentFileFactory(id=123)
        with freeze_time(datetime(2018, 4, 4, 12, 0, 1, tzinfo=pytz.utc)):
            attachment.title = 'title'
            attachment.save()

        attachment.refresh_from_db()

        expect(AttachmentSitemap().lastmod(attachment)).to.eq(
            datetime(2018, 4, 4, 12, 0, 1, tzinfo=pytz.utc))
    def test_update_attachment_downloads_and_views_count(self):
        attachment1 = AttachmentFileFactory(id=1,
                                            views_count=0,
                                            downloads_count=1)
        attachment2 = AttachmentFileFactory(id=2,
                                            views_count=0,
                                            downloads_count=0)
        attachment3 = AttachmentFileFactory(id=3,
                                            views_count=1,
                                            downloads_count=2)

        AttachmentTrackingFactory(attachment_file=attachment1,
                                  kind=constants.VIEW_EVENT_TYPE)
        AttachmentTrackingFactory(attachment_file=attachment1,
                                  kind=constants.DOWNLOAD_EVENT_TYPE)
        AttachmentTrackingFactory(attachment_file=attachment2,
                                  kind=constants.VIEW_EVENT_TYPE)
        AttachmentTrackingFactory(attachment_file=attachment2,
                                  kind=constants.VIEW_EVENT_TYPE)

        management.call_command('update_attachment_downloads_and_views_count')

        attachment1.refresh_from_db()
        attachment2.refresh_from_db()
        attachment3.refresh_from_db()

        expect(attachment1.views_count).to.eq(1)
        expect(attachment1.downloads_count).to.eq(1)

        expect(attachment2.views_count).to.eq(2)
        expect(attachment2.downloads_count).to.eq(0)

        expect(attachment3.views_count).to.eq(0)
        expect(attachment3.downloads_count).to.eq(0)
Exemplo n.º 3
0
    def test_send_cr_attachment_available_email(self,
                                                MockEmailMultiAlternatives):
        EmailTemplateFactory(
            subject='To {name}',
            body='This message is related to crid {pk} with url {url}',
            from_email='*****@*****.**',
            type=CR_ATTACHMENT_AVAILABLE)

        allegation_123 = AllegationFactory(crid='123')
        allegation_456 = AllegationFactory(crid='456')
        allegation_789 = AllegationFactory(crid='789')
        AttachmentRequestFactory(allegation=allegation_123,
                                 email='*****@*****.**',
                                 noti_email_sent=False)
        AttachmentRequestFactory(allegation=allegation_123,
                                 email='*****@*****.**',
                                 noti_email_sent=True)
        AttachmentRequestFactory(allegation=allegation_123,
                                 email='*****@*****.**',
                                 noti_email_sent=False)
        AttachmentRequestFactory(allegation=allegation_456,
                                 email='*****@*****.**',
                                 noti_email_sent=False)
        AttachmentRequestFactory(allegation=allegation_456,
                                 email='*****@*****.**',
                                 noti_email_sent=False)
        AttachmentRequestFactory(allegation=allegation_789,
                                 email='*****@*****.**',
                                 noti_email_sent=False)

        attachment_file_123_a = AttachmentFileFactory(
            allegation=allegation_123)
        attachment_file_123_b = AttachmentFileFactory(
            allegation=allegation_123)
        attachment_file_789 = AttachmentFileFactory(allegation=allegation_789)

        send_cr_attachment_available_email([
            attachment_file_123_a, attachment_file_123_b, attachment_file_789
        ])

        expect(MockEmailMultiAlternatives).to.be.any_call(
            subject='To to.be.notified',
            body=
            'This message is related to crid 123 with url http://foo.com/complaint/123/\n',
            from_email='*****@*****.**',
            to=['*****@*****.**'],
            cc=['*****@*****.**'],
        )
        expect(MockEmailMultiAlternatives().attach_alternative).to.be.any_call(
            '<p>This message is related to crid 123 with url http://foo.com/complaint/123/</p>\n',
            'text/html')

        expect(MockEmailMultiAlternatives).to.be.any_call(
            subject='To to.be.notified',
            body=
            'This message is related to crid 789 with url http://foo.com/complaint/789/\n',
            from_email='*****@*****.**',
            to=['*****@*****.**'],
            cc=['*****@*****.**'],
        )
        expect(MockEmailMultiAlternatives().attach_alternative).to.be.any_call(
            '<p>This message is related to crid 789 with url http://foo.com/complaint/789/</p>\n',
            'text/html')

        expect(MockEmailMultiAlternatives).to.be.any_call(
            subject='To another.to.be.notified',
            body=
            'This message is related to crid 123 with url http://foo.com/complaint/123/\n',
            from_email='*****@*****.**',
            to=['*****@*****.**'],
            cc=['*****@*****.**'],
        )
        expect(MockEmailMultiAlternatives().attach_alternative).to.be.any_call(
            '<p>This message is related to crid 123 with url http://foo.com/complaint/123/</p>\n',
            'text/html')

        expect(MockEmailMultiAlternatives().send.call_count).to.be.eq(3)

        expect(
            AttachmentRequest.objects.get(allegation=allegation_123,
                                          email='*****@*****.**').
            noti_email_sent).to.be.true()
        expect(
            AttachmentRequest.objects.get(
                allegation=allegation_123,
                email='*****@*****.**').noti_email_sent
        ).to.be.true()
        expect(
            AttachmentRequest.objects.get(allegation=allegation_789,
                                          email='*****@*****.**').
            noti_email_sent).to.be.true()
        expect(
            AttachmentRequest.objects.get(allegation=allegation_456,
                                          email='*****@*****.**').
            noti_email_sent).to.be.false()

        attachment_file_123_a.refresh_from_db()
        attachment_file_123_b.refresh_from_db()
        attachment_file_789.refresh_from_db()

        expect(attachment_file_123_a.notifications_count).to.eq(2)
        expect(attachment_file_123_b.notifications_count).to.eq(2)
        expect(attachment_file_789.notifications_count).to.eq(1)