def test_get_approved_latest_added_linkback(self):
        """weblinkback - get approved latest added linkbacks"""
        for linkbackid in (1, 2, 5, 6, 7):
            approve_linkback(linkbackid + self._max_id_lnkENTRY, self.user_info)

        reject_linkback(4 + self._max_id_lnkENTRY, self.user_info)

        self.assertEqual(0, len(get_approved_latest_added_linkbacks(0)))
        self.assertEqual(1, len(get_approved_latest_added_linkbacks(1)))
        self.assertEqual(2, len(get_approved_latest_added_linkbacks(2)))
        self.assertEqual(3, len(get_approved_latest_added_linkbacks(3)))
        self.assertEqual(4, len(get_approved_latest_added_linkbacks(4)))
        self.assertEqual(5, len(get_approved_latest_added_linkbacks(5)))

        approved_linkbacks = get_approved_latest_added_linkbacks(6)
        self.assertEqual(5, len(approved_linkbacks))
        self.assertEqual(1 + self._max_id_lnkENTRY, approved_linkbacks[0][0])
        self.assertEqual(2 + self._max_id_lnkENTRY, approved_linkbacks[1][0])
        self.assertEqual(5 + self._max_id_lnkENTRY, approved_linkbacks[2][0])
        self.assertEqual(6 + self._max_id_lnkENTRY, approved_linkbacks[3][0])
        self.assertEqual(7 + self._max_id_lnkENTRY, approved_linkbacks[4][0])

        url = CFG_SITE_URL + '/linkbacks/'
        expected_texts = ('URL1', 'URL2', 'URL5', 'URL6', 'URL7')
        for text in expected_texts:
            self.assertEqual([], test_web_page_content(url,
                                                       username='******',
                                                       expected_text=text))
        unexpected_texts = ('URL3', 'URL4', 'URL8')
        for text in unexpected_texts:
            self.assertEqual([], test_web_page_content(url,
                                                       username='******',
                                                       unexpected_text=text))
    def reject(self, req, form):
        """
        Reject a linkback
        """
        argd = wash_urlargd(form, {'linkbackid': (int, -1)})

        authorization = self.check_authorization_moderatelinkbacks(req, argd)
        if not authorization:
            reject_linkback(argd['linkbackid'], collect_user_info(req))
            return self.display(req, form)
        else:
            return authorization
    def reject(self, req, form):
        """
        Reject a linkback
        """
        argd = wash_urlargd(form, {'linkbackid': (int, -1)})

        authorization = self.check_authorization_moderatelinkbacks(req, argd)
        if not authorization:
            reject_linkback(argd['linkbackid'], collect_user_info(req))
            return self.display(req, form)
        else:
            return authorization
def perform_moderate_linkback(req, linkbackid, action):
    """
    Moderate linkbacks
    @param linkbackid: linkback id
    @param action: of CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION
    @return CFG_WEBLINKBACK_ACTION_RETURN_CODE
    """
    if action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['APPROVE']:
        approve_linkback(linkbackid, collect_user_info(req))
    elif action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['REJECT']:
        reject_linkback(linkbackid, collect_user_info(req))
    else:
        return CFG_WEBLINKBACK_ACTION_RETURN_CODE['INVALID_ACTION']

    return CFG_WEBLINKBACK_ACTION_RETURN_CODE['OK']
def perform_moderate_linkback(req, linkbackid, action):
    """
    Moderate linkbacks
    @param linkbackid: linkback id
    @param action: of CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION
    @return CFG_WEBLINKBACK_ACTION_RETURN_CODE
    """
    if action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['APPROVE']:
        approve_linkback(linkbackid, collect_user_info(req))
    elif action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['REJECT']:
        reject_linkback(linkbackid, collect_user_info(req))
    else:
        return CFG_WEBLINKBACK_ACTION_RETURN_CODE['INVALID_ACTION']

    return CFG_WEBLINKBACK_ACTION_RETURN_CODE['OK']
    def test_reject_linkback(self):
        """weblinkback - reject linkback"""
        linkback = get_all_linkbacks(recid=42)[1]
        linkbackid = linkback[0]
        linkback_status = linkback[5]
        self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_status)

        reject_linkback(linkbackid, self.user_info)

        linkback = get_all_linkbacks(recid=42)[1]
        linkback_status = linkback[5]
        self.assertEqual(CFG_WEBLINKBACK_STATUS['REJECTED'], linkback_status)

        url = CFG_SITE_URL + '/%s/42/linkbacks/' % CFG_SITE_RECORD
        expected_texts = ('Linkbacks to review: 4', 'Linkbacks: 0')
        for text in expected_texts:
            self.assertEqual([], test_web_page_content(url,
                                                       username='******',
                                                       expected_text=text))
    def test_delete_linkbacks_on_blacklist(self):
        """weblinkback - test delete linkbacks on blacklist"""
        for linkbackid in (1, 2, 3, 4):
            approve_linkback(linkbackid + self._max_id_lnkENTRY, self.user_info)
        for linkbackid in (5, 6):
            reject_linkback(linkbackid + self._max_id_lnkENTRY, self.user_info)

        add_url_to_list('RL1', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        add_url_to_list('URL5', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        add_url_to_list('RL7', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        set_url_broken('URL1')
        entry_table = self.get_all_from_table('lnkENTRY')
        self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], entry_table[0][5])
        self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], entry_table[8][5])
        self.assertEqual(9, len(self.get_all_from_table("lnkENTRY")))
        self.assertEqual(17, len(self.get_all_from_table("lnkENTRYLOG")))
        self.assertEqual(20, len(self.get_all_from_table("lnkLOG")))

        delete_linkbacks_on_blacklist()
        self.assertEqual(5, len(self.get_all_from_table("lnkENTRY")))
        self.assertEqual(9, len(self.get_all_from_table("lnkENTRYLOG")))
        self.assertEqual(12, len(self.get_all_from_table("lnkLOG")))
        def test_detect_and_disable_broken_linkbacks(self):
            """weblinkback - test detect and disable broken linkbacks"""
            self.assertNotEqual([], test_web_page_content(CFG_SITE_URL + '/%s/41/linkbacks/sendtrackback?url=http://www.google.au&title=Google' % CFG_SITE_RECORD, username='******'))
            self.assertNotEqual([], test_web_page_content(CFG_SITE_URL + '/%s/41/linkbacks/sendtrackback?url=http://www.google.at' % CFG_SITE_RECORD, username='******'))
            self.assertNotEqual([], test_web_page_content(CFG_SITE_URL + '/%s/41/linkbacks/sendtrackback?url=http://www.google.co.za&title=GoogleCOZA' % CFG_SITE_RECORD, username='******'))
            self.assertNotEqual([], test_web_page_content(CFG_SITE_URL + '/%s/41/linkbacks/sendtrackback?url=http://www.google.co.za&title=Google' % CFG_SITE_RECORD, username='******'))

            run_sql("""INSERT INTO lnkENTRYURLTITLE (url, title, manual_set, broken_count)
                          VALUES
                          (%s, %s, %s, %s)
                    """, ("http://www.google.de", "Google DE", 0, 3))

            p = patch('invenio.weblinkback.get_title_of_page', get_title_of_page_mock_broken)
            p.start()
            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual("Google", url_titles[0][2])
            self.assertEqual(1, url_titles[0][4])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[0][5])
            self.assertEqual("", url_titles[1][2])
            self.assertEqual(1, url_titles[1][4])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual("GoogleCOZA", url_titles[2][2])
            self.assertEqual(1, url_titles[2][4])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[3][5])
            self.assertEqual("Google DE", url_titles[3][2])
            self.assertEqual(4, url_titles[3][4])

            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual("Google", url_titles[0][2])
            self.assertEqual(2, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[0][5])
            self.assertEqual("", url_titles[1][2])
            self.assertEqual(2, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual("GoogleCOZA", url_titles[2][2])
            self.assertEqual(2, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])

            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual("Google", url_titles[0][2])
            self.assertEqual(3, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[0][5])
            self.assertEqual("", url_titles[1][2])
            self.assertEqual(3, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual("GoogleCOZA", url_titles[2][2])
            self.assertEqual(3, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])
            p.stop()

            p = patch('invenio.weblinkback.get_title_of_page', get_title_of_page_mock1)
            p.start()
            update_linkbacks(1)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual("Google", url_titles[0][2])
            self.assertEqual(3, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[0][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[1][2])
            self.assertEqual(0, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual("GoogleCOZA", url_titles[2][2])
            self.assertEqual(3, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])

            approve_linkback(4 + self._max_id_lnkENTRY, self.user_info)

            update_linkbacks(3)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual(get_title_of_page_mock1(), url_titles[0][2])
            self.assertEqual(0, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[0][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[1][2])
            self.assertEqual(0, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[2][2])
            self.assertEqual(0, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['APPROVED'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])
            p.stop()

            p = patch('invenio.weblinkback.get_title_of_page', get_title_of_page_mock_broken)
            p.start()
            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual(get_title_of_page_mock1(), url_titles[0][2])
            self.assertEqual(1, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[0][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[1][2])
            self.assertEqual(1, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[2][2])
            self.assertEqual(1, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['APPROVED'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])

            reject_linkback(1 + self._max_id_lnkENTRY, self.user_info)
            reject_linkback(3 + self._max_id_lnkENTRY, self.user_info)

            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual(get_title_of_page_mock1(), url_titles[0][2])
            self.assertEqual(2, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['REJECTED'], linkback_entries[0][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[1][2])
            self.assertEqual(2, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[2][2])
            self.assertEqual(2, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['REJECTED'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['APPROVED'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])

            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual(get_title_of_page_mock1(), url_titles[0][2])
            self.assertEqual(3, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['REJECTED'], linkback_entries[0][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[1][2])
            self.assertEqual(3, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[2][2])
            self.assertEqual(3, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['REJECTED'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['APPROVED'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])

            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual(get_title_of_page_mock1(), url_titles[0][2])
            self.assertEqual(4, url_titles[0][4])
            self.assertEqual(0, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['REJECTED'], linkback_entries[0][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[1][2])
            self.assertEqual(4, url_titles[1][4])
            self.assertEqual(0, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['PENDING'], linkback_entries[1][5])
            self.assertEqual(get_title_of_page_mock1(), url_titles[2][2])
            self.assertEqual(4, url_titles[2][4])
            self.assertEqual(0, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['REJECTED'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['APPROVED'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])

            update_linkbacks(4)
            url_titles = self.get_all_from_table("lnkENTRYURLTITLE")
            linkback_entries = self.get_all_from_table("lnkENTRY")
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[0][2])
            self.assertEqual(5, url_titles[0][4])
            self.assertEqual(1, url_titles[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], linkback_entries[0][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[1][2])
            self.assertEqual(5, url_titles[1][4])
            self.assertEqual(1, url_titles[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], linkback_entries[1][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[2][2])
            self.assertEqual(5, url_titles[2][4])
            self.assertEqual(1, url_titles[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], linkback_entries[2][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], linkback_entries[3][5])
            self.assertEqual(CFG_WEBLINKBACK_STATUS['BROKEN'], url_titles[3][2])
            self.assertEqual(5, url_titles[3][4])
            self.assertEqual(1, url_titles[3][5])
            p.stop()