示例#1
0
 def test_match_keywords(self):
     tests = [
         # tr.keywords, description, expected
         ([], u'', True),
         ([], u'my dog has fleas', True),
         ([u'my'], u'', False),
         ([u'my'], u'my dog has fleas', True),
         ([u'my'], u'MY DOG HAS FLEAS', True),
         ([u'your', u'dog'], u'my dog has fleas', True),
         ([u'my dog'], u'my dog has fleas', True),
         ([u'my dog'], u'your dog has fleas', False),
         ([u'my dog', u'cat'], u'my dog has fleas', True),
         ([u'my cat'], u'my dog has fleas', False),
         ([u'my cat'], u'my  cat  hat  fleas', True),
         ([u'my  cat'], u'my  cat  hat  fleas', True),
         ([u'\xca'], u'abcd \xca abcd', True),
         ([u'my-cat'], u'my-cat has fleas', True),
         ([u'(my cat)'], u'(my cat) has fleas', True),
         ([u'cat'], u'i love my cat!', True),
         ([u'cat'], u'(cat)', True),
     ]
     for tr_keywords, description, expected in tests:
         tr = TriggerRuleFactory(keywords=tr_keywords)
         trm = tr.get_matcher()
         assert trm.match_description(description) == expected
示例#2
0
    def test_match(self):
        # Note: This isn't an exhaustive test. Just a rough cursory check.

        # TriggerRule that matches everything matches everything.
        tr = TriggerRuleFactory(versions=[], locales=[], keywords=[], products=[])
        trm = tr.get_matcher()

        resp = ResponseFactory()
        assert trm.match(resp) is True

        tr = TriggerRuleFactory(versions=[u"38*"], locales=[u"en-US", u"fr"], keywords=[u"rc4"], url_exists=True)
        prod = ProductFactory()
        tr.products.add(prod)
        trm = tr.get_matcher()

        resp = ResponseFactory(
            version=u"38.0.5",
            locale=u"en-US",
            product=prod.db_name,
            description=u"rc4 is awesome",
            url=u"https://example.com/",
        )
        assert trm.match(resp) is True
        resp.locale = "es"
        assert trm.match(resp) is False
示例#3
0
 def test_match_keywords(self):
     tests = [
         # tr.keywords, description, expected
         ([], u"", True),
         ([], u"my dog has fleas", True),
         ([u"my"], u"", False),
         ([u"my"], u"my dog has fleas", True),
         ([u"my"], u"MY DOG HAS FLEAS", True),
         ([u"your", u"dog"], u"my dog has fleas", True),
         ([u"my dog"], u"my dog has fleas", True),
         ([u"my dog"], u"your dog has fleas", False),
         ([u"my dog", u"cat"], u"my dog has fleas", True),
         ([u"my cat"], u"my dog has fleas", False),
         ([u"my cat"], u"my  cat  hat  fleas", True),
         ([u"my  cat"], u"my  cat  hat  fleas", True),
         ([u"\xca"], u"abcd \xca abcd", True),
         ([u"my-cat"], u"my-cat has fleas", True),
         ([u"(my cat)"], u"(my cat) has fleas", True),
         ([u"cat"], u"i love my cat!", True),
         ([u"cat"], u"(cat)", True),
     ]
     for tr_keywords, description, expected in tests:
         tr = TriggerRuleFactory(keywords=tr_keywords)
         trm = tr.get_matcher()
         assert trm.match_description(description) == expected
示例#4
0
    def test_match(self):
        # Note: This isn't an exhaustive test. Just a rough cursory check.

        # TriggerRule that matches everything matches everything.
        tr = TriggerRuleFactory(
            versions=[],
            locales=[],
            keywords=[],
            products=[],
        )

        resp = ResponseFactory()
        assert tr.match(resp) is True

        tr = TriggerRuleFactory(
            versions=[u'38*'],
            locales=[u'en-US', u'fr'],
            keywords=[u'rc4'],
            url_exists=True
        )
        prod = ProductFactory()
        tr.products.add(prod)
        resp = ResponseFactory(
            version=u'38.0.5',
            locale=u'en-US',
            product=prod.db_name,
            description=u'rc4 is awesome',
            url=u'https://example.com/'
        )
        assert tr.match(resp) is True
        resp.locale = 'es'
        assert tr.match(resp) is False
示例#5
0
    def test_match_versions(self):
        tests = [
            # tr versions, feedback version, expected
            ([], '', True),
            ([], '38', True),
            ([], '38.0.5', True),

            (['38'], '', False),
            (['38'], '38', True),
            (['38'], '38.0.5', False),

            (['38*'], '', False),
            (['38*'], '38', True),
            (['38*'], '38.0.5', True),

            (['38*', '37.1'], '', False),
            (['38*', '37.1'], '38', True),
            (['38*', '37.1'], '38.0.5', True),
            (['38*', '37.1'], '37', False),
            (['38*', '37.1'], '37.1', True)
        ]
        for tr_versions, feedback_version, expected in tests:
            tr = TriggerRuleFactory(versions=tr_versions)
            trm = tr.get_matcher()
            assert trm.match_version(feedback_version) == expected
示例#6
0
    def test_sort_order(self):
        # sortorder--not db row order--sets the order of the
        # suggestions.
        rules = [
            TriggerRuleFactory(
                sortorder=10, locales=['en-US'], slug='a', is_enabled=True
            ),
            TriggerRuleFactory(
                sortorder=1, locales=['en-US'], slug='b', is_enabled=True
            ),
        ]
        resp = self.post_feedback(
            locale='en-US',
            data={'happy': 0, 'description': u'rc4 is awesome'}
        )

        links = resp.jinja_context['suggestions']

        # The disabled one won't match, but the other two will match in the
        # sort order.
        assert len(links) == 2

        assert (
            links[0].url == build_redirect_url(format_redirect(rules[1].slug))
        )
        assert (
            links[1].url == build_redirect_url(format_redirect(rules[0].slug))
        )
示例#7
0
    def test_keyword_regexes_are_cached(self):
        for text in [u"cat", u"dog", u"mouse"]:
            tr = TriggerRuleFactory(keywords=[text])
            trm = tr.get_matcher()
            trm.match_description(text)

            tr = TriggerRuleFactory(keywords=[text])
            trm = tr.get_matcher()
            trm.match_description(text)

        cache_info = _generate_keywords_regex.cache_info()
        assert cache_info.hits >= 3
示例#8
0
 def test_match_url_exists(self):
     tests = [
         # tr.url_exists, url, expected
         (None, '', True),
         (None, 'https://example.com/', True),
         (True, '', False),
         (True, 'https://example.com/', True),
         (False, '', True),
         (False, 'https://example.com/', False)
     ]
     for tr_url_exists, url, expected in tests:
         tr = TriggerRuleFactory(url_exists=tr_url_exists)
         assert tr.match_url_exists(url) == expected
示例#9
0
 def test_match_url_exists(self):
     tests = [
         # tr.url_exists, url, expected
         (None, '', True),
         (None, 'https://example.com/', True),
         (True, '', False),
         (True, 'https://example.com/', True),
         (False, '', True),
         (False, 'https://example.com/', False)
     ]
     for tr_url_exists, url, expected in tests:
         tr = TriggerRuleFactory(url_exists=tr_url_exists)
         trm = tr.get_matcher()
         assert trm.match_url_exists(url) == expected
示例#10
0
 def test_match_url_exists(self):
     tests = [
         # tr.url_exists, url, expected
         (None, "", True),
         (None, "https://example.com/", True),
         (True, "", False),
         (True, "https://example.com/", True),
         (False, "", True),
         (False, "https://example.com/", False),
     ]
     for tr_url_exists, url, expected in tests:
         tr = TriggerRuleFactory(url_exists=tr_url_exists)
         trm = tr.get_matcher()
         assert trm.match_url_exists(url) == expected
示例#11
0
    def test_match_product(self):
        # Test match all products
        tr = TriggerRuleFactory()
        trm = tr.get_matcher()

        assert trm.match_product_name("") is True
        assert trm.match_product_name("sprocket") is True

        # Test match specific product
        prod = ProductFactory()
        tr = TriggerRuleFactory()
        tr.products.add(prod)
        trm = tr.get_matcher()

        assert trm.match_product_name("") is False
        assert trm.match_product_name("sprocket") is False
        assert trm.match_product_name(prod.db_name) is True
示例#12
0
 def test_match_locale(self):
     tests = [
         # tr locales, feedback locale, expected
         ([], "", True),
         ([], "en-US", True),
         ([], "fr", True),
         (["en-US"], "", False),
         (["en-US"], "en-US", True),
         (["en-US"], "fr", False),
         (["en-US", "fr"], "", False),
         (["en-US", "fr"], "en-US", True),
         (["en-US", "fr"], "fr", True),
         (["en-US", "fr"], "es", False),
     ]
     for tr_locales, feedback_locale, expected in tests:
         tr = TriggerRuleFactory(locales=tr_locales)
         trm = tr.get_matcher()
         assert trm.match_locale(feedback_locale) == expected
示例#13
0
    def test_disabled_rule_wont_match(self):
        TriggerRuleFactory(locales=['en-US'], is_enabled=False)
        resp = self.post_feedback(
            locale='en-US',
            data={'happy': 0, 'description': u'rc4 is awesome'}
        )

        links = resp.jinja_context['suggestions']

        assert len(links) == 0
示例#14
0
    def test_match_locale(self):
        tests = [
            # tr locales, feedback locale, expected
            ([], '', True),
            ([], 'en-US', True),
            ([], 'fr', True),

            (['en-US'], '', False),
            (['en-US'], 'en-US', True),
            (['en-US'], 'fr', False),

            (['en-US', 'fr'], '', False),
            (['en-US', 'fr'], 'en-US', True),
            (['en-US', 'fr'], 'fr', True),
            (['en-US', 'fr'], 'es', False)
        ]
        for tr_locales, feedback_locale, expected in tests:
            tr = TriggerRuleFactory(locales=tr_locales)
            assert tr.match_locale(feedback_locale) == expected
示例#15
0
    def test_match_locale(self):
        tests = [
            # tr locales, feedback locale, expected
            ([], '', True),
            ([], 'en-US', True),
            ([], 'fr', True),

            (['en-US'], '', False),
            (['en-US'], 'en-US', True),
            (['en-US'], 'fr', False),

            (['en-US', 'fr'], '', False),
            (['en-US', 'fr'], 'en-US', True),
            (['en-US', 'fr'], 'fr', True),
            (['en-US', 'fr'], 'es', False)
        ]
        for tr_locales, feedback_locale, expected in tests:
            tr = TriggerRuleFactory(locales=tr_locales)
            trm = tr.get_matcher()
            assert trm.match_locale(feedback_locale) == expected
示例#16
0
    def test_match(self):
        # Note: This isn't an exhaustive test. Just a rough cursory check.

        # TriggerRule that matches everything matches everything.
        tr = TriggerRuleFactory(
            versions=[],
            locales=[],
            keywords=[],
            products=[],
        )
        trm = tr.get_matcher()

        resp = ResponseFactory()
        assert trm.match(resp) is True

        tr = TriggerRuleFactory(
            versions=[u'38*'],
            locales=[u'en-US', u'fr'],
            keywords=[u'rc4'],
            url_exists=True
        )
        prod = ProductFactory()
        tr.products.add(prod)
        trm = tr.get_matcher()

        resp = ResponseFactory(
            version=u'38.0.5',
            locale=u'en-US',
            product=prod.db_name,
            description=u'rc4 is awesome',
            url=u'https://example.com/'
        )
        assert trm.match(resp) is True
        resp.locale = 'es'
        assert trm.match(resp) is False
示例#17
0
 def test_match_versions(self):
     tests = [
         # tr versions, feedback version, expected
         ([], "", True),
         ([], "38", True),
         ([], "38.0.5", True),
         (["38"], "", False),
         (["38"], "38", True),
         (["38"], "38.0.5", False),
         (["38*"], "", False),
         (["38*"], "38", True),
         (["38*"], "38.0.5", True),
         (["38*", "37.1"], "", False),
         (["38*", "37.1"], "38", True),
         (["38*", "37.1"], "38.0.5", True),
         (["38*", "37.1"], "37", False),
         (["38*", "37.1"], "37.1", True),
     ]
     for tr_versions, feedback_version, expected in tests:
         tr = TriggerRuleFactory(versions=tr_versions)
         trm = tr.get_matcher()
         assert trm.match_version(feedback_version) == expected
示例#18
0
 def test_match_keywords(self):
     tests = [
         # tr.keywords, description, expected
         ([], u'', True),
         ([], u'my dog has fleas', True),
         ([u'my'], u'', False),
         ([u'my'], u'my dog has fleas', True),
         ([u'my'], u'MY DOG HAS FLEAS', True),
         ([u'your', u'dog'], u'my dog has fleas', True),
         ([u'my dog'], u'my dog has fleas', True),
         ([u'my dog'], u'your dog has fleas', False),
         ([u'my dog', u'cat'], u'my dog has fleas', True),
         ([u'my cat'], u'my dog has fleas', False),
         ([u'my cat'], u'my  cat  hat  fleas', True),
         ([u'my  cat'], u'my  cat  hat  fleas', True),
         ([u'\xca'], u'abcd \xca abcd', True),
         ([u'my-cat'], u'my-cat has fleas', True),
         ([u'(my cat)'], u'(my cat) has fleas', True),
         ([u'cat'], u'i love my cat!', True),
         ([u'cat'], u'(cat)', True),
     ]
     for tr_keywords, description, expected in tests:
         tr = TriggerRuleFactory(keywords=tr_keywords)
         assert tr.match_description(description) == expected
示例#19
0
    def test_match_versions(self):
        tests = [
            # tr versions, feedback version, expected
            ([], '', True),
            ([], '38', True),
            ([], '38.0.5', True),

            (['38'], '', False),
            (['38'], '38', True),
            (['38'], '38.0.5', False),

            (['38*'], '', False),
            (['38*'], '38', True),
            (['38*'], '38.0.5', True),

            (['38*', '37.1'], '', False),
            (['38*', '37.1'], '38', True),
            (['38*', '37.1'], '38.0.5', True),
            (['38*', '37.1'], '37', False),
            (['38*', '37.1'], '37.1', True)
        ]
        for tr_versions, feedback_version, expected in tests:
            tr = TriggerRuleFactory(versions=tr_versions)
            assert tr.match_version(feedback_version) == expected
示例#20
0
    def test_redirector(self):
        tr = TriggerRuleFactory(url=u'http://example.com/', is_enabled=True)
        resp = self.post_feedback(
            locale='en-US',
            data={'happy': 0, 'description': u'rc4 is awesome'}
        )

        links = resp.jinja_context['suggestions']

        assert len(links) == 1
        assert links[0].url == build_redirect_url(format_redirect(tr.slug))

        resp = self.client.get(links[0].url)
        assert resp.status_code == 302
        assert resp.url == 'http://example.com/'
示例#21
0
    def test_enabled_rule_matches_and_returns_correct_data(self):
        tr = TriggerRuleFactory(locales=['en-US'], is_enabled=True)
        resp = self.post_feedback(
            locale='en-US',
            data={'happy': 0, 'description': u'rc4 is awesome'}
        )

        links = resp.jinja_context['suggestions']

        assert len(links) == 1
        assert links[0].provider == PROVIDER
        assert links[0].provider_version == PROVIDER_VERSION
        assert links[0].summary == tr.title
        assert links[0].description == tr.description
        assert links[0].url == build_redirect_url(format_redirect(tr.slug))
示例#22
0
    def test_keyword_regexes_are_cached(self):
        for text in [u'cat', u'dog', u'mouse']:
            tr = TriggerRuleFactory(keywords=[text])
            tr.match_description(text)

            tr = TriggerRuleFactory(keywords=[text])
            tr.match_description(text)

        cache_info = _generate_keywords_regex.cache_info()
        assert cache_info.hits >= 3
示例#23
0
    def test_match_product(self):
        # Test match all products
        tr = TriggerRuleFactory()

        assert tr.match_product('') is True
        assert tr.match_product('sprocket') is True

        # Test match specific product
        prod = ProductFactory()
        tr = TriggerRuleFactory()
        tr.products.add(prod)

        assert tr.match_product('') is False
        assert tr.match_product('sprocket') is False
        assert tr.match_product(prod.db_name) is True
示例#24
0
    def test_keyword_regexes_are_cached(self):
        for text in [u'cat', u'dog', u'mouse']:
            tr = TriggerRuleFactory(keywords=[text])
            trm = tr.get_matcher()
            trm.match_description(text)

            tr = TriggerRuleFactory(keywords=[text])
            trm = tr.get_matcher()
            trm.match_description(text)

        cache_info = _generate_keywords_regex.cache_info()
        assert cache_info.hits >= 3
示例#25
0
    def test_match_product(self):
        # Test match all products
        tr = TriggerRuleFactory()
        trm = tr.get_matcher()

        assert trm.match_product_name('') is True
        assert trm.match_product_name('sprocket') is True

        # Test match specific product
        prod = ProductFactory()
        tr = TriggerRuleFactory()
        tr.products.add(prod)
        trm = tr.get_matcher()

        assert trm.match_product_name('') is False
        assert trm.match_product_name('sprocket') is False
        assert trm.match_product_name(prod.db_name) is True