Beispiel #1
0
 def test_conform_target_subscribers(self):
     doc = {'headline': 'test'}
     subscriber = {'_id': 1}
     self.assertTupleEqual(
         (True, False),
         EnqueueService().conforms_subscriber_targets(subscriber, doc))
     doc = {'headline': 'test', 'target_subscribers': []}
     self.assertTupleEqual(
         (True, False),
         EnqueueService().conforms_subscriber_targets(subscriber, doc))
     doc = {'headline': 'test', 'target_subscribers': [{'_id': 2}]}
     self.assertTupleEqual(
         (False, False),
         EnqueueService().conforms_subscriber_targets(subscriber, doc))
     doc = {'headline': 'test', 'target_subscribers': [{'_id': 1}]}
     self.assertTupleEqual(
         (True, True),
         EnqueueService().conforms_subscriber_targets(subscriber, doc))
     doc = {
         'headline': 'test',
         'target_subscribers': [{
             '_id': 2
         }],
         'target_regions': [{
             'name': 'Victoria'
         }]
     }
     self.assertTupleEqual(
         (True, False),
         EnqueueService().conforms_subscriber_targets(subscriber, doc))
Beispiel #2
0
    def test_products(self, article):
        req = ParsedRequest()
        results = []
        products = list(
            get_resource_service('products').get(req=req, lookup=None))
        for product in products:
            result = {}
            result['product_id'] = product['_id']
            result['matched'] = True

            reason = ''
            if not EnqueueService().conforms_product_targets(product, article):
                # Here it fails to match due to geo restriction
                # story has target_region and product has geo restriction
                result['matched'] = False

                if BasePublishService().is_targeted(article, 'target_regions'):
                    reason = 'Story has target_region'

                if product.get('geo_restrictions'):
                    reason = '{} {}'.format(reason,
                                            'Product has target_region')

            if not EnqueueService().conforms_content_filter(product, article):
                # Here it fails to match due to content filter
                content_filter = product.get('content_filter')
                filter = get_resource_service('content_filters').find_one(
                    req=None, _id=content_filter['filter_id'])
                result['matched'] = False
                reason = 'Story does not match the filter: {}'.format(
                    filter.get('name'))

            result['reason'] = reason
            results.append(result)
        return results
Beispiel #3
0
    def test_can_publish_article(self):
        product = self.products[0]
        self._add_content_filters(product, is_global=False)

        can_it = EnqueueService().conforms_content_filter(product, self.articles[8])
        self.assertFalse(can_it)
        product['content_filter']['filter_type'] = 'permitting'

        can_it = EnqueueService().conforms_content_filter(product, self.articles[8])
        self.assertTrue(can_it)
        product.pop('content_filter')
    def setUp(self):
        super().setUp()

        self.product_ids = self.app.data.insert('products', [
            {
                'name': 'all'
            },
        ])

        self.subscriber_ids = self.app.data.insert('subscribers', [
            {
                'name': 'digi',
                'subscriber_type': 'digital',
                'is_targetable': True,
                'products': self.product_ids
            },
        ])

        self.desk_ids = self.app.data.insert('desks', [
            {
                'name': 'sports'
            },
        ])

        self.service = EnqueueService()
 def test_previously_sent_item_association_for_removed_associations(self):
     service = EnqueueService()
     subscribers, subscriber_codes, associated_items = service._get_subscribers_for_previously_sent_items(
         {"item_id": "3"})
     self.assertEqual(len(associated_items.keys()), 1)
     self.assertIn("sub3", list(associated_items.keys()))
     self.assertIn("786", associated_items["sub3"])
Beispiel #6
0
 def test_previously_sent_item_association_for_removed_associations(self):
     service = EnqueueService()
     subscribers, subscriber_codes, associated_items = \
         service._get_subscribers_for_previously_sent_items({'item_id': '3'})
     self.assertEqual(len(associated_items.keys()), 1)
     self.assertIn('sub3', list(associated_items.keys()))
     self.assertIn('786', associated_items['sub3'])
Beispiel #7
0
    def setUp(self):
        super().setUp()

        self.product_ids = self.app.data.insert(
            "products",
            [
                {"name": "all"},
            ],
        )

        self.subscriber_ids = self.app.data.insert(
            "subscribers",
            [
                {"name": "digi", "subscriber_type": "digital", "is_targetable": True, "products": self.product_ids},
            ],
        )

        self.desk_ids = self.app.data.insert(
            "desks",
            [
                {"name": "sports"},
            ],
        )

        self.service = EnqueueService()
Beispiel #8
0
 def create(self, docs, **kwargs):
     doc = docs[0] if len(docs) > 0 else {}
     article_id = request.view_args['original_id']
     article_version = doc.get('version')
     article = self._validate_article(article_id, article_version)
     subscribers = self._validate_subscribers(doc.get('subscribers'))
     EnqueueService().resend(article, subscribers)
     return [article_id]
Beispiel #9
0
 def test_get_changed_items_no_item_found(self):
     # dummy publishing so that elastic mappings are created.
     doc = self.articles[3].copy()
     get_resource_service(ARCHIVE_PUBLISH).patch(
         id=doc['_id'], updates={ITEM_STATE: CONTENT_STATE.PUBLISHED})
     removed_items, added_items = EnqueueService()._get_changed_items(
         {}, {'item_id': 'test'})
     self.assertEqual(len(removed_items), 0)
     self.assertEqual(len(added_items), 0)
Beispiel #10
0
    def test_can_publish_article_with_global_filters(self):
        subscriber = self.subscribers[0]
        product = self.products[0]
        self._add_content_filters(product, is_global=True)

        service = get_resource_service('content_filters')
        req = ParsedRequest()
        req.args = {'is_global': True}
        global_filters = service.get(req=req, lookup=None)

        can_it = EnqueueService().conforms_global_filter(subscriber, global_filters, self.articles[8])
        self.assertFalse(can_it)

        subscriber['global_filters'] = {'1': False}
        can_it = EnqueueService().conforms_global_filter(subscriber, global_filters, self.articles[8])
        self.assertTrue(can_it)

        product.pop('content_filter')
 def test_previously_sent_item_association_for_multiple_subscribers(self):
     service = EnqueueService()
     subscribers, subscriber_codes, associated_items = service._get_subscribers_for_previously_sent_items(
         {"item_id": "2"})
     self.assertEqual(len(associated_items.keys()), 2)
     self.assertIn("sub2", associated_items)
     self.assertIn("sub4", associated_items)
     self.assertIn("123", associated_items["sub2"])
     self.assertIn("456", associated_items["sub2"])
     self.assertIn("123", associated_items["sub4"])
     self.assertIn("456", associated_items["sub4"])
Beispiel #12
0
 def create(self, docs, **kwargs):
     doc = docs[0] if len(docs) > 0 else {}
     article_id = request.view_args['original_id']
     article_version = doc.get('version')
     article = self._validate_article(article_id, article_version)
     subscribers = self._validate_subscribers(doc.get('subscribers'),
                                              article)
     EnqueueService().resend(article, subscribers)
     app.on_archive_item_updated({'subscribers': doc.get('subscribers')},
                                 article, ITEM_RESEND)
     return [article_id]
Beispiel #13
0
 def test_previously_sent_item_association_for_multiple_subscribers(self):
     service = EnqueueService()
     subscribers, subscriber_codes, associated_items = \
         service._get_subscribers_for_previously_sent_items({'item_id': '2'})
     self.assertEqual(len(associated_items.keys()), 2)
     self.assertIn('sub2', associated_items)
     self.assertIn('sub4', associated_items)
     self.assertIn('123', associated_items['sub2'])
     self.assertIn('456', associated_items['sub2'])
     self.assertIn('123', associated_items['sub4'])
     self.assertIn('456', associated_items['sub4'])
Beispiel #14
0
    def test_queue_transmission_wrong_article_type_fails(self):
        self._is_publish_queue_empty()

        doc = copy(self.articles[0])
        doc['item_id'] = doc['_id']
        doc[ITEM_TYPE] = CONTENT_TYPE.PICTURE

        subscribers, subscribers_yet_to_receive, subscriber_codes = \
            EnqueuePublishedService().get_subscribers(doc, SUBSCRIBER_TYPES.DIGITAL)
        no_formatters, queued = EnqueueService().queue_transmission(doc, subscribers, subscriber_codes)
        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)
        self.assertEqual(1, queue_items.count())
        self.assertEqual(0, len(no_formatters))
        self.assertTrue(queued)

        subscribers, subscribers_yet_to_receive, subscriber_codes = \
            EnqueuePublishedService().get_subscribers(doc, SUBSCRIBER_TYPES.WIRE)
        no_formatters, queued = EnqueueService().queue_transmission(doc, subscribers)
        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)
        self.assertEqual(2, queue_items.count())
        self.assertEqual(0, len(no_formatters))
        self.assertTrue(queued)
Beispiel #15
0
    def test_queue_transmission_for_digital_channels(self):
        self._is_publish_queue_empty()

        doc = copy(self.articles[1])
        doc['item_id'] = doc['_id']

        subscribers, subscribers_yet_to_receive, subscriber_codes = \
            EnqueuePublishedService().get_subscribers(doc, SUBSCRIBER_TYPES.DIGITAL)
        EnqueueService().queue_transmission(doc, subscribers, subscriber_codes)

        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)
        self.assertEqual(1, queue_items.count())
        expected_subscribers = ['5']
        for item in queue_items:
            self.assertIn(item["subscriber_id"], expected_subscribers, 'item {}'.format(item))
Beispiel #16
0
    def test_queue_transmission_for_wire_channels_with_codes(self):
        self._is_publish_queue_empty()

        doc = copy(self.articles[1])
        doc['item_id'] = doc['_id']

        subscribers, subscribers_yet_to_receive, subscriber_codes = \
            EnqueuePublishedService().get_subscribers(doc, SUBSCRIBER_TYPES.WIRE)
        EnqueueService().queue_transmission(doc, subscribers, subscriber_codes)
        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)

        self.assertEqual(5, queue_items.count())
        expected_subscribers = ['1', '2', '4', '5']
        for item in queue_items:
            self.assertIn(item['subscriber_id'], expected_subscribers, 'item {}'.format(item))
            if item['subscriber_id'] == '5':
                self.assertIn('def', item['codes'])
                self.assertIn('abc', item['codes'])
Beispiel #17
0
 def test_conform_target_regions(self):
     doc = {'headline': 'test'}
     product = {'geo_restrictions': 'QLD'}
     self.assertFalse(EnqueueService().conforms_product_targets(
         product, doc))
     doc = {'headline': 'test', 'target_regions': []}
     self.assertFalse(EnqueueService().conforms_product_targets(
         product, doc))
     doc = {
         'headline': 'test',
         'target_regions': [{
             'qcode': 'VIC',
             'name': 'Victoria',
             'allow': True
         }]
     }
     self.assertFalse(EnqueueService().conforms_product_targets(
         product, doc))
     doc = {
         'headline':
         'test',
         'target_regions': [{
             'qcode': 'VIC',
             'name': 'Victoria',
             'allow': False
         }]
     }
     self.assertTrue(EnqueueService().conforms_product_targets(
         product, doc))
     doc = {
         'headline':
         'test',
         'target_regions': [{
             'qcode': 'QLD',
             'name': 'Queensland',
             'allow': True
         }]
     }
     self.assertTrue(EnqueueService().conforms_product_targets(
         product, doc))
     doc = {
         'headline':
         'test',
         'target_regions': [{
             'qcode': 'QLD',
             'name': 'Queensland',
             'allow': False
         }]
     }
     self.assertFalse(EnqueueService().conforms_product_targets(
         product, doc))
 def test_previously_sent_item_association_for_no_associations(self):
     service = EnqueueService()
     subscribers, subscriber_codes, associated_items = service._get_subscribers_for_previously_sent_items(
         {"item_id": "5"})
     self.assertEqual(len(associated_items.keys()), 0)
 def test_content_api_package_publishing(self, content_api_publish):
     service = EnqueueService()
     service.enqueue_item(self.content_api_package)
     # Mock.assert_called_once is only available in Python 3.6
     # so we emulate it by counting the number of calls
     assert content_api_publish.call_count == 1