<commit_before># coding=utf-8
from __future__ import absolute_import, print_function, unicode_literals

from abilian.web import url_for

from abilian.sbe.testing import BaseTestCase


class TestViews(BaseTestCase):
    no_login = True

    def test_all_registered_urls(self):

        SKIP = frozenset(
            [
                "admin.audit_search_users",
                "search.search_main",
                "search.live",
                "notifications.debug_social",
                "social.groups_json",
                "social.groups_new",
                "social.users_json",
                "social.users_dt_json",
                "communities.community_default_image",
                "images.user_default",
            ]
        )

        rules = sorted(self.app.url_map.iter_rules(), key=lambda x: x.endpoint)
        for rule in rules:

            if "GET" not in rule.methods:
                continue

            if rule.arguments:
                continue

            # FIXME. Skip several exceptions.
            if rule.endpoint in SKIP:
                continue

            # These endpoints expect a parameter ('q') that we don't provide
            if rule.endpoint.endswith("list_json2"):
                continue

            url = url_for(rule.endpoint)

            if "/ajax/" in url:
                continue

            print(rule.endpoint, url)

            try:
                response = self.client.get(url)
                err_msg = "Bad link: {} (status={})".format(url, response.status_code)
                assert response.status_code in (200, 302), err_msg
            except BaseException:
                print("Problem with url: {}".format(url))
                raise<commit_msg>Use pytest<commit_after>from __future__ import absolute_import, print_function, unicode_literals

import re
import traceback
import warnings

from abilian.testing.fixtures import admin_user, login_admin
from abilian.web import url_for
from pytest import mark

ENDPOINTS_TO_SKIP = frozenset(
    [
        "admin.audit_search_users",
        "search.search_main",
        "search.live",
        "notifications.debug_social",
        "social.groups_json",
        "social.groups_new",
        "social.users_json",
        "social.users_dt_json",
        "communities.community_default_image",
        "images.user_default",
    ]
)

PATTERNS_TO_SKIP = [r"^.*\.list_json2$", r"^setup\..*$"]


def get_all_rules(app):
    rules = sorted(app.url_map.iter_rules(), key=lambda x: x.endpoint)

    for rule in rules:
        if "GET" not in rule.methods:
            continue

        if rule.arguments:
            continue

        endpoint = rule.endpoint
        print(endpoint)

        # Skip several exceptions.
        if endpoint in ENDPOINTS_TO_SKIP:
            continue

        if any(re.match(p, endpoint) for p in PATTERNS_TO_SKIP):
            continue

        url = url_for(endpoint)

        if "/ajax/" in url:
            continue

        yield rule


@mark.usefixtures("req_ctx")
def test_all_registered_urls(app, db, client):
    warnings.filterwarnings("ignore")

    rules = get_all_rules(app)
    user = admin_user(db)

    for rule in rules:
        login_admin(user, client)

        endpoint = rule.endpoint
        url = url_for(endpoint)
        try:
            response = client.get(url)
            assert response.status_code in (
                200,
                302,
            ), "Bad link: {} (status={})".format(url, response.status_code)
        except BaseException:
            msg = "Problem with endpoint: {} / url: {}".format(endpoint, url)
            print(msg)
            traceback.print_exc()
            raise
{'fraction_non_alphanumeric': 0.09354663774403471, 'fraction_numerical': 0.004067245119305857, 'mean_word_length': 1.8376923076923077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3607315', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1019, 'n_words': 236}