Пример #1
0
def test_get_sitemap_section_pages():
    addon_factory()
    addon_factory()
    addon_factory()

    sitemaps = get_sitemaps()
    pages = get_sitemap_section_pages(sitemaps)
    assert pages == [
        ('amo', None, 1),
        ('addons', 'firefox', 1),
        ('addons', 'android', 1),
        ('categories', 'firefox', 1),
        ('collections', 'firefox', 1),
        ('collections', 'android', 1),
        ('users', 'firefox', 1),
        ('users', 'android', 1),
    ]
    with mock.patch.object(AddonSitemap, 'limit', 40):
        # 3 pages per addon * 3 addons * 10 locales = 90 urls for addons; 3 pages @ 40pp
        pages = get_sitemap_section_pages(sitemaps)
        assert pages == [
            ('amo', None, 1),
            ('addons', 'firefox', 1),
            ('addons', 'firefox', 2),
            ('addons', 'firefox', 3),
            ('addons', 'android', 1),
            ('addons', 'android', 2),
            ('addons', 'android', 3),
            ('categories', 'firefox', 1),
            ('collections', 'firefox', 1),
            ('collections', 'android', 1),
            ('users', 'firefox', 1),
            ('users', 'android', 1),
        ]

    # test the default pagination limit

    def items_mock(self):
        return [
            AccountSitemap.item_tuple(datetime.now(), user_id, 7, 8)
            for user_id in range(0, 201)
        ]

    with mock.patch.object(AccountSitemap, 'items', items_mock):
        # 201 mock user pages * 10 locales = 2010 urls for addons; 3 pages @ 1000pp
        pages = get_sitemap_section_pages(sitemaps)
        assert pages == [
            ('amo', None, 1),
            ('addons', 'firefox', 1),
            ('addons', 'android', 1),
            ('categories', 'firefox', 1),
            ('collections', 'firefox', 1),
            ('collections', 'android', 1),
            ('users', 'firefox', 1),
            ('users', 'firefox', 2),
            ('users', 'firefox', 3),
            ('users', 'android', 1),
            ('users', 'android', 2),
            ('users', 'android', 3),
        ]
Пример #2
0
    def test_basic(self):
        sitemaps_dir = settings.SITEMAP_STORAGE_PATH
        write_sitemaps()
        sitemaps = get_sitemaps()
        # Root should contain all sections dirs + index.
        assert (len(
            os.listdir(sitemaps_dir)) == len(set(item[0]
                                                 for item in sitemaps)) + 1)

        with open(os.path.join(sitemaps_dir, 'sitemap.xml')) as sitemap:
            contents = sitemap.read()
            entry = (
                '<sitemap><loc>http://testserver/sitemap.xml?{params}</loc></sitemap>'
            )
            for (section, app), sitemap in sitemaps.items():
                if not app:
                    assert entry.format(
                        params=f'section={section}') in contents
                else:
                    assert (entry.format(
                        params=f'section={section}&amp;app_name={app.short}')
                            in contents)
            assert (
                '<sitemap><loc>http://testserver/blog/sitemap.xml</loc></sitemap>'
                in contents)

        with open(os.path.join(sitemaps_dir, 'amo/sitemap.xml')) as sitemap:
            contents = sitemap.read()
            assert '<url><loc>http://testserver/en-US/about</loc>' in contents

        with open(os.path.join(sitemaps_dir,
                               'addons/firefox/1/01/1.xml')) as sitemap:
            contents = sitemap.read()
            assert '<url><loc>http://testserver/en-US/firefox/' in contents

        with open(os.path.join(sitemaps_dir,
                               'addons/android/1/01/1.xml')) as sitemap:
            contents = sitemap.read()
            assert '<url><loc>http://testserver/en-US/android/' in contents

        xml_path = os.path.join(sitemaps_dir, 'collections/firefox/1/01/1.xml')
        with open(xml_path) as sitemap:
            contents = sitemap.read()
            assert (
                '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
                'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n\n</urlset>'
                in contents)
Пример #3
0
def test_write_sitemaps():
    addon_factory()
    TestCase.make_addon_promoted(
        addon_factory(version_kw={'application': amo.ANDROID.id}),
        RECOMMENDED,
        approve_version=True,
    )
    sitemaps_dir = settings.SITEMAP_STORAGE_PATH
    assert len(os.listdir(sitemaps_dir)) == 0
    write_sitemaps()
    sitemaps = get_sitemaps()
    assert len(os.listdir(sitemaps_dir)) == len(sitemaps) + 1  # 1 is the index

    with open(os.path.join(sitemaps_dir, 'sitemap.xml')) as sitemap:
        contents = sitemap.read()
        entry = '<sitemap><loc>http://testserver/sitemap.xml?{params}</loc></sitemap>'
        for (section, app), sitemap in sitemaps.items():
            if not app:
                assert entry.format(params=f'section={section}') in contents
            else:
                assert (entry.format(
                    params=f'section={section}&amp;app_name={app.short}')
                        in contents)

    with open(os.path.join(sitemaps_dir, 'sitemap-amo.xml')) as sitemap:
        contents = sitemap.read()
        assert '<url><loc>http://testserver/en-US/about</loc>' in contents

    with open(os.path.join(sitemaps_dir,
                           'sitemap-addons-firefox.xml')) as sitemap:
        contents = sitemap.read()
        assert '<url><loc>http://testserver/en-US/firefox/' in contents

    with open(os.path.join(sitemaps_dir,
                           'sitemap-addons-android.xml')) as sitemap:
        contents = sitemap.read()
        assert '<url><loc>http://testserver/en-US/android/' in contents

    with open(os.path.join(sitemaps_dir,
                           'sitemap-collections-firefox.xml')) as sitemap:
        contents = sitemap.read()
        assert ('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
                'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n\n</urlset>'
                in contents)
Пример #4
0
def test_get_sitemap_section_pages():
    addon_factory()
    addon_factory()
    addon_factory()

    sitemaps = get_sitemaps()
    pages = get_sitemap_section_pages(sitemaps)
    assert pages == [
        ('amo', None, 1),
        ('addons', 'firefox', 1),
        ('addons', 'android', 1),
        ('categories', 'firefox', 1),
        ('collections', 'firefox', 1),
        ('users', 'firefox', 1),
        ('users', 'android', 1),
        ('tags', 'firefox', 1),
        ('tags', 'android', 1),
    ]
    with mock.patch.object(AddonSitemap, 'limit', 25):
        pages = get_sitemap_section_pages(sitemaps)
        # 2 pages per addon * 3 addons * 10 locales = 60 urls for addons; 3 pages @ 25pp
        assert len(sitemaps.get(('addons', amo.FIREFOX))._items()) == 60
        assert pages == [
            ('amo', None, 1),
            ('addons', 'firefox', 1),
            ('addons', 'firefox', 2),
            ('addons', 'firefox', 3),
            ('addons', 'android', 1),
            ('categories', 'firefox', 1),
            ('collections', 'firefox', 1),
            ('users', 'firefox', 1),
            ('users', 'android', 1),
            ('tags', 'firefox', 1),
            ('tags', 'android', 1),
        ]

    # test the default pagination limit

    def items_mock(self):
        return [
            AccountSitemap.item_tuple(datetime.now(), user_id, 7, 8)
            for user_id in range(0, 401)
        ]

    with mock.patch.object(AccountSitemap, 'items', items_mock):
        # 401 mock user pages * 10 locales = 4010 urls for addons; 3 pages @ 2000pp
        pages = get_sitemap_section_pages(sitemaps)
        assert pages == [
            ('amo', None, 1),
            ('addons', 'firefox', 1),
            ('addons', 'android', 1),
            ('categories', 'firefox', 1),
            ('collections', 'firefox', 1),
            ('users', 'firefox', 1),
            ('users', 'firefox', 2),
            ('users', 'firefox', 3),
            ('users', 'android', 1),
            ('users', 'android', 2),
            ('users', 'android', 3),
            ('tags', 'firefox', 1),
            ('tags', 'android', 1),
        ]