Exemplo n.º 1
0
    def setUp(self):
        super().setUp()
        self.init_data()

        self.app.data.insert('users', self.users)
        self.app.data.insert('desks', self.desks)
        self.app.data.insert('products', self.products)
        self.app.data.insert('subscribers', self.subscribers)
        self.app.data.insert(ARCHIVE, self.articles)

        self.filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), "validators.json")
        self.json_data = [
            {"_id": "kill_text", "act": "kill", "type": "text", "schema": {"headline": {"type": "string"}}},
            {"_id": "publish_text", "act": "publish", "type": "text", "schema": {}},
            {"_id": "correct_text", "act": "correct", "type": "text", "schema": {}},
            {"_id": "publish_composite", "act": "publish", "type": "composite", "schema": {}},
        ]
        self.article_versions = self._init_article_versions()

        with open(self.filename, "w+") as file:
            json.dump(self.json_data, file)
        init_app(self.app)
        ValidatorsPopulateCommand().run(self.filename)

        self.app.media.url_for_media = MagicMock(return_value='url_for_media')
        self.app.media.put = MagicMock(return_value='media_id')
Exemplo n.º 2
0
    def test_maintain_latest_version_for_published(self):
        def get_publish_items(item_id, last_version):
            query = {
                'query': {
                    'filtered': {
                        'filter': {
                            'and': [{
                                'term': {
                                    'item_id': item_id
                                }
                            }, {
                                'term': {
                                    LAST_PUBLISHED_VERSION: last_version
                                }
                            }]
                        }
                    }
                }
            }
            request = ParsedRequest()
            request.args = {'source': json.dumps(query)}
            return self.app.data.find(PUBLISHED, req=request, lookup=None)

        ValidatorsPopulateCommand().run(self.filename)
        get_resource_service(ARCHIVE).patch(
            id=self.articles[1][config.ID_FIELD],
            updates={'publish_schedule': None})

        doc = get_resource_service(ARCHIVE).find_one(
            req=None, _id=self.articles[1][config.ID_FIELD])
        get_resource_service(ARCHIVE_PUBLISH).patch(
            id=doc[config.ID_FIELD],
            updates={ITEM_STATE: CONTENT_STATE.PUBLISHED})

        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)
        self.assertEqual(7, queue_items.count())
        published_items = self.app.data.find(PUBLISHED, None, None)
        self.assertEqual(2, published_items.count())
        published_digital_doc = next(
            (item for item in published_items
             if item.get(PACKAGE_TYPE) == TAKES_PACKAGE), None)
        published_doc = next((item for item in published_items
                              if item.get('item_id') == doc[config.ID_FIELD]),
                             None)
        self.assertEqual(published_doc[LAST_PUBLISHED_VERSION], True)
        self.assertEqual(published_digital_doc[LAST_PUBLISHED_VERSION], True)

        get_resource_service(ARCHIVE_CORRECT).patch(
            id=doc[config.ID_FIELD],
            updates={ITEM_STATE: CONTENT_STATE.CORRECTED})
        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)
        self.assertEqual(14, queue_items.count())
        published_items = self.app.data.find(PUBLISHED, None, None)
        self.assertEqual(4, published_items.count())
        last_published_digital = get_publish_items(
            published_digital_doc['item_id'], True)
        self.assertEqual(1, last_published_digital.count())
        last_published = get_publish_items(published_doc['item_id'], True)
        self.assertEqual(1, last_published.count())
Exemplo n.º 3
0
    def test_targeted_for_includes_digital_subscribers(self):
        ValidatorsPopulateCommand().run(self.filename)
        updates = {'target_regions': [{'qcode': 'NSW', 'name': 'New South Wales', 'allow': True}]}
        doc_id = self.articles[9][config.ID_FIELD]
        get_resource_service(ARCHIVE).patch(id=doc_id, updates=updates)

        get_resource_service(ARCHIVE_PUBLISH).patch(id=doc_id, updates={ITEM_STATE: CONTENT_STATE.PUBLISHED})
        enqueue_published()
        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)
        self.assertEqual(7, queue_items.count())
        expected_subscribers = ['1', '2', '3', '4', '5']
        for item in queue_items:
            self.assertIn(item["subscriber_id"], expected_subscribers, 'item {}'.format(item))
Exemplo n.º 4
0
    def setUp(self):
        super().setUp()
        self._init_data()

        self.app.data.insert('users', self.users)
        self.app.data.insert('desks', self.desks)
        self.app.data.insert('vocabularies', self.vocabularies)
        self.app.data.insert('subscribers', self.subscribers)
        self.app.data.insert(ARCHIVE, self.articles)

        self.filename = os.path.join(
            os.path.abspath(os.path.dirname(__file__)), "validators.json")
        self.json_data = [
            {
                "_id": "kill_text",
                "act": "kill",
                "type": "text",
                "schema": {
                    "headline": {
                        "type": "string"
                    }
                }
            },
            {
                "_id": "publish_text",
                "act": "publish",
                "type": "text",
                "schema": {}
            },
            {
                "_id": "correct_text",
                "act": "correct",
                "type": "text",
                "schema": {}
            },
            {
                "_id": "publish_composite",
                "act": "publish",
                "type": "composite",
                "schema": {}
            },
        ]

        with open(self.filename, "w+") as file:
            json.dump(self.json_data, file)
        init_app(self.app)
        ValidatorsPopulateCommand().run(self.filename)

        self.package_service = PackageService()
Exemplo n.º 5
0
    def test_targeted_for_excludes_digital_subscribers(self):
        ValidatorsPopulateCommand().run(self.filename)
        updates = {
            'targeted_for': [{
                'name': 'New South Wales',
                'allow': True
            }]
        }
        doc_id = self.articles[9][config.ID_FIELD]
        get_resource_service(ARCHIVE).patch(id=doc_id, updates=updates)

        get_resource_service(ARCHIVE_PUBLISH).patch(
            id=doc_id, updates={ITEM_STATE: CONTENT_STATE.PUBLISHED})

        queue_items = self.app.data.find(PUBLISH_QUEUE, None, None)
        self.assertEqual(4, queue_items.count())
        expected_subscribers = ['1', '2']
        self.assertIn(queue_items[0]["subscriber_id"], expected_subscribers)
        self.assertIn(queue_items[1]["subscriber_id"], expected_subscribers)
        self.assertIn(queue_items[2]["subscriber_id"], expected_subscribers)
Exemplo n.º 6
0
    def test_remove_published_and_killed_content_separately(self):
        cmd = ValidatorsPopulateCommand()

        with self.app.app_context():
            cmd.run(self.filename)
            self.app.data.insert('archive_versions', self.article_versions)

            published_service = get_resource_service('published')
            text_archive = get_resource_service('text_archive')

            # Publishing an Article
            doc = self.articles[7]
            original = doc.copy()
            get_resource_service('archive_publish').patch(id=doc['_id'], updates={config.CONTENT_STATE: 'published'})

            published_items = published_service.get_other_published_items(original[config.ID_FIELD])
            self.assertEqual(1, published_items.count())

            article_in_production = get_resource_service(ARCHIVE).find_one(req=None, _id=original[config.ID_FIELD])
            self.assertIsNotNone(article_in_production)
            self.assertEqual(article_in_production[config.CONTENT_STATE], 'published')
            self.assertEqual(article_in_production[config.VERSION], original[config.VERSION] + 1)
            insert_into_versions(doc=article_in_production)

            # Setting the expiry date of the published article to 1 hr back from now
            published_service.update_published_items(
                original[config.ID_FIELD], 'expiry', utcnow() + timedelta(minutes=-60))

            # Killing the published article and manually inserting the version of the article as unittests use
            # service directly
            get_resource_service('archive_kill').patch(id=doc['_id'], updates={config.CONTENT_STATE: 'killed'})

            # Executing the Expiry Job for the Published Article and asserting the collections
            RemoveExpiredPublishContent().run()

            articles_in_text_archive = text_archive.get(req=None, lookup={'item_id': original[config.ID_FIELD]})
            self.assertEqual(articles_in_text_archive.count(), 0)

            published_items = published_service.get_other_published_items(str(original[config.ID_FIELD]))
            self.assertEqual(1, published_items.count())

            article_in_production = get_resource_service(ARCHIVE).find_one(req=None, _id=original[config.ID_FIELD])
            self.assertIsNotNone(article_in_production)
            self.assertEqual(article_in_production[config.CONTENT_STATE], 'killed')
            self.assertEqual(article_in_production[config.VERSION], original[config.VERSION] + 2)
            insert_into_versions(doc=article_in_production)

            # Validate the collections in Legal Archive
            article_in_legal_archive, article_versions_in_legal_archive, queue_items = \
                self._get_legal_archive_details(original[config.ID_FIELD])

            self.assertIsNotNone(article_in_legal_archive, 'Article cannot be none in Legal Archive')
            self.assertEqual(article_in_legal_archive[config.CONTENT_STATE], 'published')

            self.assertIsNotNone(article_versions_in_legal_archive, 'Article Versions cannot be none in Legal Archive')
            self.assertEqual(article_versions_in_legal_archive.count(), 5)

            self.assertGreaterEqual(queue_items.count(), 1, 'Publish Queue Items must be greater than or equal to 1')

            # Setting the expiry date of the killed article to 1 hr back from now and running the job again
            published_service.update_published_items(
                original[config.ID_FIELD], 'expiry', utcnow() + timedelta(minutes=-60))
            RemoveExpiredPublishContent().run()

            articles_in_text_archive = text_archive.get(req=None, lookup={'item_id': original[config.ID_FIELD]})
            self.assertEqual(articles_in_text_archive.count(), 0)

            published_items = published_service.get_other_published_items(str(original[config.ID_FIELD]))
            self.assertEqual(0, published_items.count())

            article_in_production = get_resource_service(ARCHIVE).find_one(req=None, _id=original[config.ID_FIELD])
            self.assertIsNone(article_in_production)

            # Validate the collections in Legal Archive
            article_in_legal_archive, article_versions_in_legal_archive, queue_items = \
                self._get_legal_archive_details(original[config.ID_FIELD], publishing_action='killed')

            self.assertIsNotNone(article_in_legal_archive, 'Article cannot be none in Legal Archive')
            self.assertEqual(article_in_legal_archive['state'], 'killed')

            self.assertIsNotNone(article_versions_in_legal_archive, 'Article Versions cannot be none in Legal Archive')
            self.assertEqual(article_versions_in_legal_archive.count(), 6)

            for queue_item in queue_items:
                self.assertEqual(queue_item['item_id'], original[config.ID_FIELD])
                self.assertEqual(queue_item['item_version'], original[config.VERSION] + 2)

            self.assertGreaterEqual(queue_items.count(), 1, 'Publish Queue Items must be greater than or equal to 1')
    def test_remove_expired_published_and_killed_content(self):
        cmd = ValidatorsPopulateCommand()

        with self.app.app_context():
            cmd.run(self.filename)
            self.app.data.insert('archive_versions', self.article_versions)

            published_service = get_resource_service('published')
            text_archive = get_resource_service('text_archive')

            # Publishing an Article
            doc = self.articles[0]
            original = doc.copy()
            get_resource_service('archive_publish').queue_transmission(original)
            published_service.post([original])

            published_items = published_service.get_other_published_items(original['item_id'])
            self.assertEquals(1, published_items.count())

            # Setting the expiry date of the published article to 1 hr back from now
            published_service.update_published_items(original['item_id'], 'expiry', utcnow() + timedelta(minutes=-60))

            # Killing the published article and manually inserting the version of the article as unittests use
            # service directly
            _current_version = doc[config.VERSION] + 1
            get_resource_service('archive_kill').patch(id=doc['_id'],
                                                       updates={config.VERSION: _current_version})
            killed_version = {
                'guid': 'tag:localhost:2015:69b961ab-2816-4b8a-a584-a7b402fed4f9',
                versioned_id_field(): '1',
                'type': 'text',
                config.VERSION: _current_version,
                'body_html': 'Test body',
                'destination_groups': ['4'],
                'urgency': 4,
                'headline': 'Two students missing',
                'pubstatus': 'usable',
                'firstcreated': utcnow(),
                'byline': 'By Alan Karben',
                'ednote': 'Andrew Marwood contributed to this article',
                'dateline': 'Sydney',
                'keywords': ['Student', 'Crime', 'Police', 'Missing'],
                'subject': [{'qcode': '17004000', 'name': 'Statistics'}, {'qcode': '04001002', 'name': 'Weather'}],
                'state': 'published',
                'expiry': utcnow() + timedelta(minutes=20),
                'unique_name': '#2'
            }
            self.app.data.insert('archive_versions', [killed_version])

            # Executing the Expiry Job for the Published Article and asserting the collections
            RemoveExpiredPublishContent().run()

            articles_in_text_archive = text_archive.get(req=None, lookup={'item_id': original['item_id']})
            self.assertEquals(articles_in_text_archive.count(), 0)

            published_items = published_service.get_other_published_items(str(original['item_id']))
            self.assertEquals(1, published_items.count())

            article_in_production = get_resource_service(ARCHIVE).find_one(req=None, _id=original['item_id'])
            self.assertIsNotNone(article_in_production)
            self.assertEquals(article_in_production['state'], 'killed')
            self.assertEquals(article_in_production[config.VERSION], _current_version)

            # Validate the collections in Legal Archive
            article_in_legal_archive, article_versions_in_legal_archive, formatted_items, queue_items = \
                self.__get_legal_archive_details(original['item_id'])

            self.assertIsNotNone(article_in_legal_archive, 'Article cannot be none in Legal Archive')
            self.assertEquals(article_in_legal_archive['state'], 'published')

            self.assertIsNotNone(article_versions_in_legal_archive, 'Article Versions cannot be none in Legal Archive')
            self.assertEquals(article_versions_in_legal_archive.count(), 4)

            self.assertGreaterEqual(formatted_items.count(), 1, 'Formatted Items must be greater than or equal to 1')
            for formatted_item in formatted_items:
                self.assertEquals(formatted_item['item_id'], original['item_id'])
                self.assertEquals(formatted_item['item_version'], self.articles[0][config.VERSION])

            self.assertGreaterEqual(queue_items.count(), 1, 'Publish Queue Items must be greater than or equal to 1')

            # Setting the expiry date of the killed article to 1 hr back from now and running the job again
            published_service.update_published_items(original['item_id'], 'expiry', utcnow() + timedelta(minutes=-60))
            RemoveExpiredPublishContent().run()

            articles_in_text_archive = text_archive.get(req=None, lookup={'item_id': original['item_id']})
            self.assertEquals(articles_in_text_archive.count(), 0)

            published_items = published_service.get_other_published_items(str(original['item_id']))
            self.assertEquals(0, published_items.count())

            article_in_production = get_resource_service(ARCHIVE).find_one(req=None, _id=original['item_id'])
            self.assertIsNone(article_in_production)

            # Validate the collections in Legal Archive
            article_in_legal_archive, article_versions_in_legal_archive, formatted_items, queue_items = \
                self.__get_legal_archive_details(original['item_id'], article_version=_current_version,
                                                 publishing_action='killed')

            self.assertIsNotNone(article_in_legal_archive, 'Article cannot be none in Legal Archive')
            self.assertEquals(article_in_legal_archive['state'], 'killed')

            self.assertIsNotNone(article_versions_in_legal_archive, 'Article Versions cannot be none in Legal Archive')
            self.assertEquals(article_versions_in_legal_archive.count(), 5)

            self.assertGreaterEqual(formatted_items.count(), 1, 'Formatted Items must be greater than or equal to 1')
            for formatted_item in formatted_items:
                self.assertEquals(formatted_item['item_id'], original['item_id'])
                self.assertEquals(formatted_item['item_version'], _current_version)

            self.assertGreaterEqual(queue_items.count(), 1, 'Publish Queue Items must be greater than or equal to 1')