def test_url_lists(self):
        """weblinkback - test URL lists"""
        add_url_to_list('url1', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        add_url_to_list('url2', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        add_url_to_list('url3', CFG_WEBLINKBACK_LIST_TYPE['WHITELIST'], self.user_info)

        self.assertEqual(('url1', 'url2'), get_url_list(CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST']))
        self.assertEqual(('url3', ), get_url_list(CFG_WEBLINKBACK_LIST_TYPE['WHITELIST']))

        remove_url('url2')
        remove_url('url3')

        self.assertEqual(('url1', ), get_url_list(CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST']))
        self.assertEqual(tuple(), get_url_list(CFG_WEBLINKBACK_LIST_TYPE['WHITELIST']))
    def test_url_exists(self):
        """weblinkback - test URL existence"""
        add_url_to_list('url1', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        add_url_to_list('url2', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        add_url_to_list('url3', CFG_WEBLINKBACK_LIST_TYPE['WHITELIST'], self.user_info)

        self.assertTrue(url_exists('url1'))
        self.assertTrue(url_exists('url1', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST']))
        self.assertFalse(url_exists('url1', CFG_WEBLINKBACK_LIST_TYPE['WHITELIST']))

        remove_url('url1')
        self.assertFalse(url_exists('url1'))
        self.assertFalse(url_exists('url1', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST']))
        self.assertFalse(url_exists('url1', CFG_WEBLINKBACK_LIST_TYPE['WHITELIST']))

        self.assertTrue(url_exists('url3'))
        self.assertFalse(url_exists('url3', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST']))
        self.assertTrue(url_exists('url3', CFG_WEBLINKBACK_LIST_TYPE['WHITELIST']))
    def test_url_remove(self):
        """weblinkback - test remove URL rom list"""
        add_url_to_list('url1', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        add_url_to_list('url2', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)
        remove_url('url2')
        # creating a different log, might detect a bug in the logging ids
        approve_linkback(1 + self._max_id_lnkENTRY, self.user_info)
        add_url_to_list('url3', CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'], self.user_info)

        admin_url_table = self.get_all_from_table('lnkADMINURL')
        admin_url_log_table = self.get_all_from_table('lnkADMINURLLOG')
        logTable = self.get_all_from_table('lnkLOG')

        self.assertEqual(2, len(admin_url_table))
        self.assertEqual(2, len(admin_url_log_table))
        # there are more logs due to the inserted linkbacks in setUp()
        self.assertEqual(12, len(logTable))

        self.assertEqual(1 + self._max_id_lnkADMINURL, admin_url_table[0][0])
        # there are more logs due to the inserted linkbacks in setUp()
        self.assertEqual(1 + self._max_id_lnkADMINURL, admin_url_log_table[0][0])
        self.assertEqual(10 + self._max_id_lnkLOG, admin_url_log_table[0][1])

        self.assertEqual(3 + self._max_id_lnkADMINURL, admin_url_table[1][0])
        # there are more logs due to the inserted linkbacks in setUp()
        self.assertEqual(3 + self._max_id_lnkADMINURL, admin_url_log_table[1][0])
        # 9 linkbacks inserted  (9)
        # 2 urls inserted      (11)
        # 1 url removed        (11) (log id 10 removed)
        # 1 linkback approved  (12)
        # 1 url inserted       (13)
        self.assertEqual(13 + self._max_id_lnkLOG, admin_url_log_table[1][1])

        url = CFG_SITE_URL + '/admin/weblinkback/weblinkbackadmin.py/lists'
        expected_texts = ('url1', 'url3')
        for text in expected_texts:
            self.assertEqual([], test_web_page_content(url,
                                                       username='******',
                                                       expected_text=text))
        unexpected_texts = ('url2', )
        for text in unexpected_texts:
            self.assertEqual([], test_web_page_content(url,
                                                       username='******',
                                                       unexpected_text=text))
def perform_moderate_url(req, url, action, list_type):
    """
    Perform a url action
    @param url
    @param action: CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['INSERT'] or CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['DELETE']
    @param list_type: of CFG_WEBLINKBACK_LIST_TYPE
    @return (String, CFG_WEBLINKBACK_ACTION_RETURN_CODE) the String is url if CFG_WEBLINKBACK_ACTION_RETURN_CODE['BAD_INPUT')
    """
    if url == '' or ' ' in url:
        return (url, CFG_WEBLINKBACK_ACTION_RETURN_CODE['BAD_INPUT'])
    elif action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['INSERT']:
        if url_exists(url):
            return ('', CFG_WEBLINKBACK_ACTION_RETURN_CODE['DUPLICATE'])
        else:
            add_url_to_list(url, list_type, collect_user_info(req))
    elif action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['DELETE']:
        remove_url(url)
    else:
        return ('', CFG_WEBLINKBACK_ACTION_RETURN_CODE['INVALID_ACTION'])

    return ('', CFG_WEBLINKBACK_ACTION_RETURN_CODE['OK'])
def perform_moderate_url(req, url, action, list_type):
    """
    Perform a url action
    @param url
    @param action: CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['INSERT'] or CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['DELETE']
    @param list_type: of CFG_WEBLINKBACK_LIST_TYPE
    @return (String, CFG_WEBLINKBACK_ACTION_RETURN_CODE) the String is url if CFG_WEBLINKBACK_ACTION_RETURN_CODE['BAD_INPUT')
    """
    if url == '' or ' ' in url:
        return (url, CFG_WEBLINKBACK_ACTION_RETURN_CODE['BAD_INPUT'])
    elif action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['INSERT']:
        if url_exists(url):
            return ('', CFG_WEBLINKBACK_ACTION_RETURN_CODE['DUPLICATE'])
        else:
            add_url_to_list(url, list_type, collect_user_info(req))
    elif action == CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['DELETE']:
        remove_url(url)
    else:
        return ('', CFG_WEBLINKBACK_ACTION_RETURN_CODE['INVALID_ACTION'])

    return ('', CFG_WEBLINKBACK_ACTION_RETURN_CODE['OK'])