def testSimpleQuerySearch(self, m):
     web.app.template_folder = "../templates"
 
     # Query only
     expectedItems = self.prepareSearchMocks(m, 1, 1)
     with web.app.test_request_context('/api?t=search&q=query&apikey=%s' % config.settings.main.apikey):
         response = web.api()
         entries, _, _ = newznab.NewzNab(Bunch.fromDict({"name": "forTest", "score": 0, "host": "host"})).parseXml(response.data)
         self.assertSearchResults(entries, expectedItems)
         calledUrls = sorted([x.url for x in m.request_history])
         self.assertTrue(compare('http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query', calledUrls[0]))
 
     # Query with category
     expectedItems = self.prepareSearchMocks(m, 1, 1)
     with web.app.test_request_context('/api?t=search&q=query&apikey=%s&cat=2000' % config.settings.main.apikey):
         response = web.api()
         entries, _, _ = newznab.NewzNab(Bunch.fromDict({"name": "forTest", "score": 0, "host": "host"})).parseXml(response.data)
         self.assertSearchResults(entries, expectedItems)
         calledUrls = sorted([x.url for x in m.request_history])
         self.assertTrue(compare('http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query&cat=2000', calledUrls[0]))
    def testIgnoreByCategories(self, m):
        web.app.template_folder = "../templates"

        config.settings.categories.categories[
            "moviessd"].ignoreResults = "always"
        config.settings.categories.categories["xxx"].ignoreResults = "always"
        config.settings.categories.categories["pc"].ignoreResults = "internal"

        movieSdResult = mockbuilder.buildNewznabItem(
            title="result1", indexer_name="newznab1", categories=[2030]
        )  #MoviesSD: Is dismissed because its specific category is ignored
        xxxResult = mockbuilder.buildNewznabItem(
            title="result2", indexer_name="newznab1", categories=[6000]
        )  #XXX: Is dismissed because its category is ignored (no more or less specific category exists)
        movieResult = mockbuilder.buildNewznabItem(
            title="result3", indexer_name="newznab1", categories=[2000]
        )  #Is kept because more specific category Movies SD is ignored but not movies in general
        movieHdResult = mockbuilder.buildNewznabItem(
            title="result4", indexer_name="newznab1", categories=[2040]
        )  #MoviesHD: Is kept because the other specific category is ignored but not this one
        tvResult = mockbuilder.buildNewznabItem(
            title="result5", indexer_name="newznab1", categories=[
                5000
            ])  #TV: Is kept because its category (tv) is never ignored
        pcResult = mockbuilder.buildNewznabItem(
            title="result6", indexer_name="newznab1", categories=[4000]
        )  # PC: Is kept because its category (tv) is only ignored for internal searches (but this is API)

        expectedItems = self.prepareSearchMocks(m,
                                                1,
                                                newznabItems=[[
                                                    movieSdResult, xxxResult,
                                                    movieResult, movieHdResult,
                                                    tvResult, pcResult
                                                ]])
        expectedItems = expectedItems[2:]  #First two results will be dismissed

        with web.app.test_request_context('/api?t=search&q=query&apikey=%s' %
                                          config.settings.main.apikey):
            response = web.api()
            entries, _, _ = newznab.NewzNab(
                Bunch.fromDict({
                    "name": "forTest",
                    "score": 0,
                    "host": "host"
                })).parseXml(response.data)
            self.assertSearchResults(entries, expectedItems)
            calledUrls = sorted([x.url for x in m.request_history])
            self.assertTrue(
                compare(
                    'http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query',
                    calledUrls[0]), calledUrls[0])
    def testSimpleQuerySearch(self, m):
        web.app.template_folder = "../templates"

        # Query only
        expectedItems = self.prepareSearchMocks(m, 1, 1)
        with web.app.test_request_context('/api?t=search&q=query&apikey=%s' %
                                          config.settings.main.apikey):
            response = web.api()
            entries, _, _ = newznab.NewzNab(
                Bunch.fromDict({
                    "name": "forTest",
                    "score": 0,
                    "host": "host"
                })).parseXml(response.data)
            self.assertSearchResults(entries, expectedItems)
            calledUrls = sorted([x.url for x in m.request_history])
            self.assertTrue(
                compare(
                    'http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query',
                    calledUrls[0]))

        # Query with category
        expectedItems = self.prepareSearchMocks(m, 1, 1)
        with web.app.test_request_context(
                '/api?t=search&q=query&apikey=%s&cat=2000' %
                config.settings.main.apikey):
            response = web.api()
            entries, _, _ = newznab.NewzNab(
                Bunch.fromDict({
                    "name": "forTest",
                    "score": 0,
                    "host": "host"
                })).parseXml(response.data)
            self.assertSearchResults(entries, expectedItems)
            calledUrls = sorted([x.url for x in m.request_history])
            self.assertTrue(
                compare(
                    'http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query&cat=2000',
                    calledUrls[0]))
    def testRequiredAndForbiddenWords(self, m):
        web.app.template_folder = "../templates"

        config.settings.searching.forbiddenWords = "newznab1result1"  # Will be removed from parsed results
        config.settings.categories.categories[
            "movies"].forbiddenWords = "newznab1result2"  # Will be removed from parsed results
        config.settings.searching.forbiddenWords = "newznab1result3"  # Will be excluded in query
        config.settings.categories.categories[
            "movies"].forbiddenWords = "newznab1result4"  # Will be excluded in query
        config.settings.categories.categories[
            "movies"].requiredWords = "newznab1result6"  # Will be left
        config.settings.searching.requiredWords = "newznab1result7"  # Will be left

        config.settings.categories.categories[
            "movies"].applyRestrictions = "external"

        expectedItems = self.prepareSearchMocks(m,
                                                1,
                                                7,
                                                categories=[2000],
                                                skip=[(
                                                    1,
                                                    3,
                                                ), (
                                                    1,
                                                    4,
                                                )])
        expectedItems.pop(0)  # The result with globally forbidden word
        expectedItems.pop(0)  # The result with category forbidden word
        expectedItems.pop(
            0
        )  # The result with neither globally nor category required word (newznab1result5)
        with web.app.test_request_context(
                '/api?t=search&q=query&apikey=%s&cat=2000' %
                config.settings.main.apikey):
            response = web.api()
            entries, _, _ = newznab.NewzNab(
                Bunch.fromDict({
                    "name": "forTest",
                    "score": 0,
                    "host": "host"
                })).parseXml(response.data)
            self.assertSearchResults(entries, expectedItems)
            calledUrls = sorted([x.url for x in m.request_history])
            self.assertTrue(
                compare(
                    'http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query+!newznab1result3+!newznab1result4&cat=2000',
                    calledUrls[0]), calledUrls[0])
 def testRequiredAndForbiddenWords(self, m):
     web.app.template_folder = "../templates"
 
     config.settings.searching.forbiddenWords = "newznab1result1.title"  # Will be removed from parsed results
     config.settings.categories.categories["movies"].forbiddenWords = "newznab1result2.title"  # Will be removed from parsed results
     config.settings.searching.forbiddenWords = "newznab1result3"  # Will be excluded in query
     config.settings.categories.categories["movies"].forbiddenWords = "newznab1result4"  # Will be excluded in query
     config.settings.categories.categories["movies"].requiredWords = "newznab1result6.title"  # Will be left
     config.settings.searching.requiredWords = "newznab1result7.title"  # Will be left
 
     config.settings.categories.categories["movies"].applyRestrictions = "external"
 
     expectedItems = self.prepareSearchMocks(m, 1, 7, categories=[2000], skip=[(1, 3,), (1, 4,)])
     expectedItems.pop(0)  # The result with globally forbidden word
     expectedItems.pop(0)  # The result with category forbidden word 
     expectedItems.pop(0)  # The result with neither globally nor category required word (newznab1result5)
     with web.app.test_request_context('/api?t=search&q=query&apikey=%s&cat=2000' % config.settings.main.apikey):
         response = web.api()
         entries, _, _ = newznab.NewzNab(Bunch.fromDict({"name": "forTest", "score": 0, "host": "host"})).parseXml(response.data)
         self.assertSearchResults(entries, expectedItems)
         calledUrls = sorted([x.url for x in m.request_history])
         self.assertTrue(compare('http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query+!newznab1result3+!newznab1result4&cat=2000', calledUrls[0]), calledUrls[0])
 def testIgnoreByCategories(self, m):
     web.app.template_folder = "../templates"
 
     config.settings.categories.categories["moviessd"].ignoreResults = "always"
     config.settings.categories.categories["xxx"].ignoreResults = "always"
     config.settings.categories.categories["pc"].ignoreResults = "internal"
     
     movieSdResult = mockbuilder.buildNewznabItem(title="result1", indexer_name="newznab1", categories=[2030]) #MoviesSD: Is dismissed because its specific category is ignored
     xxxResult = mockbuilder.buildNewznabItem(title="result2", indexer_name="newznab1", categories=[6000]) #XXX: Is dismissed because its category is ignored (no more or less specific category exists)
     movieResult = mockbuilder.buildNewznabItem(title="result3", indexer_name="newznab1", categories=[2000]) #Is kept because more specific category Movies SD is ignored but not movies in general
     movieHdResult = mockbuilder.buildNewznabItem(title="result4", indexer_name="newznab1", categories=[2040]) #MoviesHD: Is kept because the other specific category is ignored but not this one
     tvResult = mockbuilder.buildNewznabItem(title="result5", indexer_name="newznab1", categories=[5000]) #TV: Is kept because its category (tv) is never ignored 
     pcResult = mockbuilder.buildNewznabItem(title="result6", indexer_name="newznab1", categories=[4000])  # PC: Is kept because its category (tv) is only ignored for internal searches (but this is API)
 
     expectedItems = self.prepareSearchMocks(m, 1, newznabItems=[[movieSdResult, xxxResult, movieResult, movieHdResult, tvResult, pcResult]])
     expectedItems = expectedItems[2:] #First two results will be dismissed 
     
     with web.app.test_request_context('/api?t=search&q=query&apikey=%s' % config.settings.main.apikey):
         response = web.api()
         entries, _, _ = newznab.NewzNab(Bunch.fromDict({"name": "forTest", "score": 0, "host": "host"})).parseXml(response.data)
         self.assertSearchResults(entries, expectedItems)
         calledUrls = sorted([x.url for x in m.request_history])
         self.assertTrue(compare('http://www.newznab1.com/api?apikey=apikeyindexer.com&t=search&extended=1&offset=0&limit=100&q=query', calledUrls[0]), calledUrls[0])